MOV vs LEA with pointers
I randomly remembered I specifically used LA (which I'm guessing is the MIPS equivalent of LEA) when trying to accessing variables on the stack in a prior MIPS assembly course. However, in x86 32 intel , I realized I primarily use mov to access variables in the stack. I understand that LEA loads an address, and stores the address in a register, and then by dereferenced to change to obtain the value that was at that address.
I see that eax is will be equal to esp+4 dereferenced in both scenarios. What are the benefits of using one method over the other, in this scenario ? I wrote this code to obtain the arguments of the function by the way I'm curious bc my professor in my MIPS course told us to avoid using mov I don't remember at all why he would do so.
mov $eax [$esp+4]
vs
lea $eax [$esp+4]
mov $eax [$eax]
MIPS la
is the same as x86 mov reg, imm32
: It's a pseudo-instruction for putting address constants into registers. It only works for actual constants, unlike LEA which can use any addressing mode.
MIPS doesn't have a real move
instruction; move $t1, $t0
is a pseudo-op for
or $t1, $zero, $t0
, ie it does $t1 = 0 | $t0 = $t0
$t1 = 0 | $t0 = $t0
. If you need to copy a register, you might as well write it as move
instead of addu
or or
with the zero register. (But usually in MIPS you can avoid copying registers, because instructions have a separate destination, unlike x86 where the add
instruction does dst += src
instead of dst = src1 + src2
.)
x86 addressing modes can use 32-bit constants directly, and there are far fewer registers available. It often makes sense to use an address directly instead of putting it into a register first. eg add eax, [my_array + ecx*4]
. See Referencing the contents of a memory location. (x86 addressing modes).
MIPS only has one addressing mode: register + 16-bit immediate. For small addresses (in the low 16 bits of address space), you can use them directly, eg ls $t0, my_static_variable($zero)
or lw $t0, my_array($t1)
. (But of course you don't have scaled indexes, so you have to addui $t1, $t1, 4
to index the next element).
上一篇: 可以使用LEA指令来加载值
下一篇: MOV与带指针的LEA