Introduction to Code Generation
- Code generation would be based on stack machine with accumulator or 1-register stack machine.
- SPIM is used as simulator, code generated by MIPS can be used on any kind of hardware.
- Accumulator would be kept in register $a0, and stack in memory.
- To access stack there is this stack pointer denoted by $sp, this is actually the next unallocated space and as the stack grows towards the low address the MIPS then the address of top is $sp+4, 4 because MIPS works on 4 byte words.
- MIPS architecture:
- Its a RISC machine
- for operands and results, registers are used.
- use load and store to access memory
- 32 bit general purpose registers(32-bit each), we use three of them $a0, $sp and for temporary values $t1
- The register operation done in a MIPS program are:
lw reg1 offset(reg2) //load word into reg1 the contents of offset+reg2 add reg1 reg2 reg3 //performs add operation reg1 ← reg2 + reg3 sw reg1 offset(reg2) //stores the contents of reg1 in reg2 + offset addiu reg1 reg2 imm //adds immediate unsigned: imm is a constant value; reg1 ← reg2 + imm li reg1 imm //load immediate; load imm in reg1
lets take the example of 7 + 5
load 7 li $a0 7 push 7 sw $a0 0($sp) add $sp $sp -4 load 5 li $a0 5 acc ← acc + top_of_stack lw $t1 4($sp) add $a0 $a0 $t1 pop addiu $sp $sp 4