Introduction to Code Generation

MIPS architecture

x86 architecture

Need some code-generation invariants

MIPS opcodes (relevant only)

x86 opcodes (relevant only)

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