MIPS architecture
load
and store
instructions to use values in memory
$sp
, $a0
, and $t1
(a temporary register)
x86 architecture
%esp
, %eax
, and %ecx
(a temporary register)
Need some code-generation invariants
$a0
(or x86 register %eax
)
$sp
. The top of the stack is at address $sp+4
%esp
. The next location of the stack is at address %esp-4
.
sp
" stands for stack-pointer.
MIPS opcodes (relevant only)
lw reg1, offset(reg2)
reg2+offset
into reg1
add reg1, reg2, reg3
reg1 <-- reg2+reg3
sw reg1, offset(reg2)
reg1
to address reg2+offset
addiu reg1, reg2, imm
reg1 <-- reg2+imm
u
" means overflow is not checked (overflow means different things for signed and unsigned interpretation of the registers)
li reg, imm
reg <-- imm
x86 opcodes (relevant only)
movl %reg1/(memaddr1)/$imm, %reg2/(memaddr2)
reg1
(or address memaddr1
or the immediate value itself) into reg2
or to memory address memaddr2
add %reg1/(memaddr1)/$imm, %reg2/(memaddr2)
%reg2/(memaddr2) <-- reg1/(memaddr1)/imm + %reg2/(memaddr2)
push %reg/(memaddr)/$imm
(%esp-4) <-- reg/(memaddr)/imm; %esp <-- %esp-4
pop %reg/(memaddr)
reg/(memaddr) <-- (%esp); %esp <-- %esp+4
The one-register stack-machine code for 7+5
in MIPS:
acc <-- 7 : li $a0, 7 push acc : sw $a0, 0($sp) addiu $sp, $sp, -4 acc <-- 5 : li $a0, 5 acc < acc + top_of_stack : lw $t1, 4($sp) : add $a0, $a0, $t1 pop : addiu $sp, $sp, 4
The stack-machine code for 7+5
in x86:
acc <-- 7 : movl $7,%eax push acc : pushl %eax acc <-- 5 : movl $5, %eax acc < acc + top_of_stack : addl (%esp),%eax pop : popl %ecx #just pop the register to unused register ecx
A more optimized version was possible in x86:
acc <-- 7 : push $7 push acc acc <-- 5 : mov $5, %eax acc < acc + top_of_stack : add (%esp), %eax pop : pop %ecx #just pop the register to unused register ecx