1

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]
  • The first is equivalent to the MIPS `lw $t0, 4($sp)`. The second is equivalent to `addiu $t0, $sp, 4` followed by `lw $t0, 0($t0)`. – Raymond Chen Dec 11 '17 at 07:03
  • What syntax is this? It's not normal Intel syntax for any assembler I'm familiar with (NASM/YASM, FASM, MASM, GAS `.intel_syntax noprefix`), nor is it AT&T or Plan9/Go. It looks like you used MIPS `$reg` decorations and forgot the commas between operands. Intel syntax doesn't decorate register names; AT&T decorates with `%`. Some MIPS assemblers allow omitting commas, but x86 assemblers don't. – Peter Cordes Feb 23 '23 at 19:54

1 Answers1

0

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, i.e. it does $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. e.g. 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, e.g. 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).

Peter Cordes
  • 328,167
  • 45
  • 605
  • 847