Naming labels

You must name pieces of your functions. What’s worse, you can never use the same label name twice in your program!

It’s important to use a consistent naming convention. Here’s my convention. You can make your own.

  1. Functions are named as normal using snake_case. e.g.
    • main
    • check_input
    • update_objects
  2. Control flow labels start with _ and the name of the function they belong to. e.g.
    • _main_loop
    • _main_loop_end
    • _check_input_check_L
    • _check_input_check_R
    • _update_objects_loop

Yes, those control flow label names can get a bit long. But it ensures three important things:

Also, good code editors offer tab completion. For example, in Sublime, all I have to type is upobl, hit Tab, and it completes it to _update_objects_loop.


Indentation

asm has no control structures, but there are still rules about indentation. Well, there’s just 2.

  1. Labels go at the beginning of the line.
  2. Instructions are indented.

If you want to indent your control structures, feel free! If so, it’s okay to indent the labels, but still keep them “to the left” of the instructions they refer to.

Instead of...Do...
main:
li a0, 10 # ew
li v0, 1
syscall
main:
    li a0, 10 # yum
    li v0, 1
    syscall
main:
    println_str "hi!"
    li t0, 0
    _main_loop:
    println_str "loop"
    add t0, t0, 1
    blt t0, 10, _main_loop # where?
main:
    println_str "hi!"
    li t0, 0
_main_loop: # oh! more obvious
    println_str "loop"
    add t0, t0, 1
    blt t0, 10, _main_loop

Commenting and spacing your lines out

A single level of HLL code can be several asm instructions. If you write all your instructions one line after another without any spacing, it can get very difficult to read and change.

I recommend doing a few things:

Combining the “control flow indentation” from the previous section with these rules…

Instead of...Try...
main:
	bne t0, 10, _main_not_10
	println_str "it's 10!"
_main_not_10:
	li t0, 0
_main_loop_top:
	bge t0, 5, _main_loop_end
	rem t1, t0, 2
	bne t1, 0, _main_odd
	println_str "even"
	j _main_odd_endif
_main_odd:
	println_str "odd"
_main_odd_endif:
	add t0, t0, 1
	j _main_loop_top
_main_loop_end:
	li v0, 10
	syscall
another_function:
	jr ra
third_function:
	jr ra











main:
	# if(t0 == 10)
	bne t0, 10, _main_not_10
		println_str "it's 10!"
_main_not_10: # maybe indent this? idk

	# for(i = 0 to 5)
	li t0, 0
_main_loop_top:
	bge t0, 5, _main_loop_end
		# if((i % 2) == 0)
		rem t1, t0, 2
		bne t1, 0, _main_odd
			println_str "even"
			j _main_odd_endif
		# else
		_main_odd:
			println_str "odd"
		_main_odd_endif:
	add t0, t0, 1
	j _main_loop_top
_main_loop_end:

	# exit()
	li v0, 10
	syscall

# --------------------------------------
another_function:
	jr ra

# --------------------------------------
third_function:
	jr ra