0% found this document useful (0 votes)
35 views

Computer Architecture: MIPS Instruction Set Architecture

The document discusses the MIPS instruction set architecture and calling conventions in MIPS. It describes the MIPS register naming conventions and purposes. It explains the hybrid calling convention used in MIPS where some registers are caller saved and some are callee saved. It provides examples of compiling C code for leaf procedures, non-leaf procedures, and recursive procedures into MIPS assembly code.

Uploaded by

Elisée Ndjabu
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
35 views

Computer Architecture: MIPS Instruction Set Architecture

The document discusses the MIPS instruction set architecture and calling conventions in MIPS. It describes the MIPS register naming conventions and purposes. It explains the hybrid calling convention used in MIPS where some registers are caller saved and some are callee saved. It provides examples of compiling C code for leaf procedures, non-leaf procedures, and recursive procedures into MIPS assembly code.

Uploaded by

Elisée Ndjabu
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 14

Computer Architecture

MIPS Instruction Set Architecture


( Supporting Procedures )
MIPS Register Naming

0 $zero constant 0 16 $s0 permanent


(For variables in a high-level
1 $at reserved for assembler ··· language program)

2 $v0 return values 23 $s7


3 $v1 24 $t8 temporary

4 $a0 arguments 25 $t9

5 $a1 26 $k0 OS kernel (reserved)

6 $a2 27 $k1
7 $a3 28 $gp global pointer

8 $t0 temporary 29 $sp stack pointer


··· 30 $fp frame pointer
15 $t7 31 $ra return address

Computer Architecture & Network Lab 2


Calling Convention (Caller Save)

Caller Callee

• save (live) registers • allocate stack frame


• set up arguments
• save return address
and jump to the callee

• restore saved registers

• place return value


• free stack frame
• restore and jump to the
return address

Computer Architecture & Network Lab 3


Calling Convention (Callee Save)

Caller Callee

• set up arguments • allocate stack frame


• save return address • save registers(that may
and jump to the callee be modified in the callee)

• place return value


• restore saved registers
• free stack frame
• restore and jump to the
return address

Computer Architecture & Network Lab 4


MIPS Calling Convention (Hybrid)

Caller Callee

• save (live) caller-saved registers: • allocate stack frame:


$t0~$t9, $a0~$a3, $v0~$v1, $ra $sp ← $sp – frame size
• set up arguments : first four in • save callee-saved registers
$a0~$a3, rest on stack (that may be modified in the
• execute jal instruction callee) : $s0~$s7

• restore caller-saved registers

• place return value in $v0~$v1


• restore callee-saved registers
• free stack frame :
$sp ← $sp + frame size
• return by jr $ra

Computer Architecture & Network Lab 5


MIPS Register Convention

0 $zero constant 0 16 $s0 permanent (callee saves)


(For variables in a high-level
1 $at reserved for assembler ··· language program)

2 $v0 function return value 23 $s7


3 $v1 (caller saves) 24 $t8 temporary (caller saves)

4 $a0 arguments (caller saves) 25 $t9

5 $a1 26 $k0 OS kernel (reserved)

6 $a2 27 $k1
7 $a3 28 $gp global pointer (caller saves)

8 $t0 temporary (caller saves) 29 $sp stack pointer (preserved)


··· 30 $fp frame pointer ($s8 for cc)
(callee saves)
15 $t7 31 $ra return address(jal) (caller saves)

Computer Architecture & Network Lab 6


Compiling a Leaf Procedure

C Assembly
leaf_example :
sub $sp, $sp, 12
int leaf_example ( int g, sw $t1, 8($sp)
int h, int, i, int, j ) sw $t0, 4($sp)
{ sw $s0, 0($sp)
int f ; add $t0, $a0, $a1
add $t1, $a2, $a3
f = ( g + h ) – ( i + j ); sub $s0, $t0, $t1
return f; Actually add $v0, $s0, $zero
} Not needed lw $s0, 0($sp)
lw $t0, 4($sp)
lw $t1, 8($sp)
add $sp, $sp,12
jr $ra

Computer Architecture & Network Lab 7


Stack Allocation

FIGURE 3.12 Illustration of the stack allocation (a)before, (b)during, and (c)after the procedure call

Computer Architecture & Network Lab 8


Compiling a Non-Leaf Procedure

C Assembly
int nonleaf_example (int arg) nonleaf_example:
{ sub $sp, $sp, 12
int result; sw $s0, 8($sp)
result = arg + sw $ra, 4($sp)
leaf_example(5,4,3,2); sw $a0, 0($sp)
return result; add $a0, $zero, 5
} add $a1, $zero, 4
add $a2, $zero, 3
add $a3, $zero, 2
jal leaf_example
lw $a0, 0($sp)
lw $ra, 4($sp)
add $s0, $a0, $v0
add $v0, $zero, $s0
lw $s0, 8($sp)
add $sp, $sp, 12
jr $ra

Computer Architecture & Network Lab 9


Compiling a Recursive Procedure

C Assembly
fact :
int fact ( int n ) sub $sp, $sp, 8
{ sw $ra, 4($sp)
if ( n < 1 ) return (1) ; sw $a0, 0($sp)
else slt $t0, $a0, 1
return ( n * fact ( n – 1) ); beq $t0, $zero, L1
add $v0, $zero, 1
} add $sp, $sp, 8
jr $ra
L1 : sub $a0, $a0, 1
jal fact
lw $a0, 0($sp)
lw $ra, 4($sp)
add $sp, $sp, 8
mul $v0, $a0, $v0
jr $ra

Computer Architecture & Network Lab 10


Compiling and Starting a Program

Computer Architecture & Network Lab 11


Object File Example

Object file header Object file header


Name Procedure A Name Procedure B

Text size 100hex Text size 200hex

Data size 20hex Data size 30hex

Text segment Address Instruction Text segment Address Instruction

0 lw $a0, 0($gp) 0 sw $a1, 0($gp)

4 jal 0 4 jal 0

… … … …

Data segment 0 (X) Data segment 0 (Y)

… … … …

Relocation information Address Instruction type Dependency Relocation information Address Instruction type Dependency
0 lw X 0 sw Y
4 jal B 4 jal A
Symbol table Label Address Symbol table Label Address
X - Y -
B - A -

Computer Architecture & Network Lab 12


MIPS Memory Allocation

Computer Architecture & Network Lab 13


Executable File Example

Executable file header


Text size 300hex
Data size 50hex
Text segment Address Instruction
0040 0000hex lw $a0, 8000hex($gp)
0040 0004hex jal 40 0100hex
… …
0040 0100hex sw $a1, 8020hex($gp)
0000 0104hex jal 40 0000hex
… …
Data segment Address
1000 0000hex (X)
… …
1000 0020hex (Y)
… …

Computer Architecture & Network Lab 14

You might also like