Presentation is loading. Please wait.

Presentation is loading. Please wait.

Compiling examples in MIPS

Similar presentations


Presentation on theme: "Compiling examples in MIPS"— Presentation transcript:

1 Compiling examples in MIPS
Compiling of one module The compiler’s task is to create the machine instructions in correspondence to the existing assembly language instructions.

2 Compiler prepares & “maps” – doesn’t run
.text main: ori $3,$0,1 sltiu $8,$2,56 beq $8,$0,out nop sltiu $8,$2,30 beq $8,$0,cont out: ori $3,$0,0 cont: nop [0x ] ?? ?? ?? ?? [0x ] [0x ] [0x c] [0x ] [0x ] [0x ] [0x c] [0x ] . . . Compiler’s task is not to put the machine instructions to the memory addresses but to prepare the machine instructions content supposing that later those instructions will be placed in the memory by OS starting from the address 0x and then should run. Compiler creates the map of memory. We have declared “.text” segment so our program will start from the default address 0x and the first machine instruction will be placed in that address when the program will run. Declaration of “.text 0x ” supposes that the program will be placed on that address to run

3 Some instructions are translated immediately some of them - not
[0x ] 0x ori $3, $0, 1 [0x ] [0x ] [0x c] [0x ] [0x ] [0x ] [0x c] [0x ] .text main: ori $3,$0,1 sltiu $8,$2,56 beq $8,$0,out nop sltiu $8,$2,30 beq $8,$0,cont out: ori $3,$0,0 cont: nop ori $3,$0,1 could be translated to the machine instruction immediately MIPS has 3 types of machine instructions format and 5 types of addressing modes Some assembly instruction types and some addressing modes immediately could be translated to the machine instructions.

4 What to do with the main ? Symbol table of this module
[0x ] 0x ori $3, $0, 1 [0x ] [0x ] [0x c] [0x ] [0x ] [0x ] [0x c] [0x ] .text main: ori $3,$0,1 sltiu $8,$2,56 beq $8,$0,out nop sltiu $8,$2,30 beq $8,$0,cont out: ori $3,$0,0 cont: nop Symbol table of this module Label Type Address main global 0x . . . All labels (instructions and data) are collected in “Symbol table” to be used later by compiler or linker to resolve the addresses of that labels. Symbol table belongs to this object module and is kept together with the object module to be used later

5 The next instruction So far so good sltiu $8,$2,56
[0x ] 0x ori $3, $0, 1 [0x ] 0x2c sltiu $8, $2, 56 [0x ] [0x c] [0x ] [0x ] [0x ] [0x c] [0x ] .text main: ori $3,$0,1 sltiu $8,$2,56 beq $8,$0,out nop sltiu $8,$2,30 beq $8,$0,cont out: ori $3,$0,0 cont: nop So far so good sltiu $8,$2,56 translated to the machine instruction without dependencies and takes the next word

6 Branch – unresolved address
[0x ] 0x ori $3, $0, 1 [0x ] 0x2c sltiu $8, $2, 56 [0x ] 0x1100xxxx beq $8, $0, out ??? [0x c] [0x ] [0x ] [0x ] [0x c] [0x ] Symbol table of this module Label Type Address main global 0x . . . .text main: ori $3,$0,1 sltiu $8,$2,56 beq $8,$0,out nop sltiu $8,$2,30 beq $8,$0,cont out: ori $3,$0,0 cont: nop Temporary unresolved Symbol Table out – 0x beq $8,$0,out Only the first half of the instruction is known. We don’t know the address of “out” to fill the 2nd half (look at symbol table). We remember this gap in temporary symbol table (???).

7 No dependency is always easy to compile
[0x ] 0x [0x ] 0x2c480038 [0x ] 0x1100xxxx beq $8, $0, out ??? [0x c] 0x nop [0x ] 0x2c48001e sltiu $8, $2, 30 [0x ] [0x ] [0x c] [0x ] .text main: ori $3,$0,1 sltiu $8,$2,56 beq $8,$0,out nop sltiu $8,$2,30 beq $8,$0,cont out: ori $3,$0,0 cont: nop

8 Again Branch – unresolved address
.text main: ori $3,$0,1 sltiu $8,$2,56 beq $8,$0,out nop sltiu $8,$2,30 beq $8,$0,cont out: ori $3,$0,0 cont: nop [0x ] 0x [0x ] 0x2c480038 [0x ] 0x1100xxxx [0x c] 0x [0x ] 0x2c48001e [0x ] 0x1100xxxx [0x ] [0x c] [0x ] Temporary unresolved Symbol Table out – 0x cont- 0x beq $8,$0,cont We don’t know the address of “cont” to fill the 2nd half (look at symbol table). We remember this gap also in temporary symbol table. Symbol table of this module Label Type Address main global 0x . . .

9 First pass is finished .text main: ori $3,$0,1 sltiu $8,$2,56 beq $8,$0,out nop sltiu $8,$2,30 beq $8,$0,cont out: ori $3,$0,0 cont: nop [0x ] 0x [0x ] 0x2c480038 [0x ] 0x1100xxxx [0x c] 0x [0x ] 0x2c48001e [0x ] 0x1100xxxx [0x ] 0x [0x c] 0x [0x ] 0x Now we know the addresses and can fill the gaps. Go to second pass. nop – 0s out – Should be added to the Symbol Table. cont – Should be added to the Symbol Table. Symbol table of this module Label Type Address main global 0x out local 0x c cont local 0x

10 Second pass, fill the gaps from symbol table.
.text main: ori $3,$0,1 sltiu $8,$2,56 beq $8,$0,out nop sltiu $8,$2,30 beq $8,$0,cont out: ori $3,$0,0 cont: nop [0x ] 0x [0x ] 0x2c480038 [0x ] 0x [0x c] 0x [0x ] 0x2c48001e [0x ] 0x [0x ] 0x [0x c] 0x [0x ] 0x +16 +8 Our program is ready to load to memory and run from 0x address Symbol table of this module (kept) Label Type Address main global 0x out local 0x c cont local 0x Temporary unresolved Symbol Table out – 0x cont- 0x Can this program work if it will be loaded to the memory another address? Temporary unresolved Symbol Table eliminated out – 0x cont- 0x Yes. The addresses are relative.


Download ppt "Compiling examples in MIPS"

Similar presentations


Ads by Google