Naming labels

In assembly, you must name pieces of your functions. Fortunately, if you have the “Settings > Function-local labels” setting turned on in MARS, you can start control flow labels with an underscore _ to make it “local” to that function. That way, you don’t have to uniquely name every label in your program!

Because of Function-local Labels, this is how you will name things:

  1. Functions are named without a leading underscore, using snake_case. e.g.
    • main
    • check_input
    • update_objects
  2. Control flow labels start with _ and might say what kind of control flow they’re part of e.g.
    • _loop
    • _loop_end
    • _endif
    • _check_L
    • _check_R

Here are some bad label names:


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
    _loop:
    println_str "loop"
    add t0, t0, 1
    blt t0, 10, _loop # where?
main:
    println_str "hi!"
    li t0, 0
_loop: # oh! more obvious
    println_str "loop"
    add t0, t0, 1
    blt t0, 10, _loop

Commenting and spacing your lines out

A single line of HLL code can be the equivalent of 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, _not_10
    println_str "it's 10!"
_not_10:
    li t0, 0
_loop_top:
    bge t0, 5, _loop_end
    rem t1, t0, 2
    bne t1, 0, _odd
    println_str "even"
    j _odd_endif
_odd:
    println_str "odd"
_odd_endif:
    add t0, t0, 1
    j _loop_top
_loop_end:
    li v0, 10
    syscall
another_function:
    jr ra
third_function:
    jr ra













main:
    # if(t0 == 10)
    bne t0, 10, _not_10
        println_str "it's 10!"
    _not_10:

    # there are a few ways of indenting this
    # loop that would be equally okay.
    # for(i = 0 to 5)
    li t0, 0
_loop_top:
    bge t0, 5, _loop_end
        # if((i % 2) == 0)
        rem t1, t0, 2
        bne t1, 0, _odd
            println_str "even"
        j _odd_endif
        # else
        _odd:
            println_str "odd"
        _odd_endif:
    add t0, t0, 1
    j _loop_top
_loop_end:

    # exit()
    li v0, 10
    syscall

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

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