user9016902
user9016902

Reputation:

Does the sequence of la and li matter

So today I started learning the MIPS assembly language and like in every other programming language you start with a hello world. I look in some tutorials and noticed that a few people write li before la and a few write la before li. Does the sequence matter?

This works:

.data
message: .asciiz "Hello World"
.text
li $v0, 4
la $a0, message
syscall

And this also works:

.data
message: .asciiz "Hello World"
.text
la $a0, message
li $v0, 4
syscall

So which one should I write first?

Upvotes: 0

Views: 326

Answers (1)

Peter Cordes
Peter Cordes

Reputation: 364483

It doesn't matter which order you write to registers. The two instructions have totally independent effects on the architectural state of the machine (i.e. register values), so you can reorder them freely, bounded only by other instructions that write or read $v0 or $a0.

Remember that in assembly language, you're programming steps for a machine to carry out. There's no magic where a combination of lines means something special; each source line is an instruction on its own which only cares about the state of the machine when it runs, not how it got there. It can't look into the past and tell that one register was written before another. (Even out-of-order / pipelined CPUs give the illusion of executing instructions one at a time in program order.)

Any performance effects of initializing a register more than 1 instruction before use are negligible (or non-existent on an in-order pipeline. la and li are pseudo-ops for ALU instructions, not loads from memory constants, so the registers will be ready for use by the next instruction).

Upvotes: 1

Related Questions