Upload
others
View
3
Download
0
Embed Size (px)
Citation preview
M2 – Instruction Set Architecture
Module Outline● Addressing modes. Instruction classes.● MIPS-I ISA.● Translating and starting a program.● High level languages, Assembly languages
and object code.● Subroutine and subroutine call. Use of stack
for handling subroutine call and return.
Steps in gcc
hello.c hello.i hello.s
hello.oa.out
cpp cc1
as
ld
Libraries (eg. Math)
Steps in gcc
Step Output FileType Remarks
C Preprocessor hello.i C source text #include, #define, ...
C Compiler hello.s Assembler source text Individual modules. Labels. Undefined global references.Assembler hello.o Object code
Linkage Editor a.out Executable Global references resolved
Example
Object File Format (hello.o)
Object File Format (hello.o)
Object File Format (hello.o)
Linking Multiple Modules
Linking Multiple Modules
Linking Example – File 1
Linking Example – File 2
Linking Example – a.out
The a.out executable● What does the a.out file contain?
– Program “code” (machine instructions)
– Data values (values, size of arrays)
● Other information that is needed for– execution
– debugging● Debugging: The stage in program development where
mistakes (“bugs”) in the program are identified
Subroutine Calls
Subroutines in MIPS
● Subroutine Call – jal subname– Saves return address in R31 ($ra) and jumps to
subroutine entry label subname
● Subroutine Return – jr $31– Loads PC with return address in $31
Subroutines in MIPS
# main program........jal func1............
func1:........jr $ra
CallerCaller
CalleeCallee
0x1040x104R31
0x100
0x104
0x0FC
0x200
0x240
0x1040x104PC
Subroutines in MIPS
# main program........jal func1............
func1:........jr $ra
CallerCaller
CalleeCallee
0x1040x104R31
0x100
0x104
0x0FC
0x200
0x240
0x2000x200PC
Subroutines in MIPS
# main program........jal func1............
func1:........jr $ra
CallerCaller
CalleeCallee
0x1040x104R31
0x100
0x104
0x0FC
0x200
0x240
0x2440x244PC
Subroutines in MIPS
# main program........jal func1............
func1:........jr $ra
CallerCaller
CalleeCallee
0x1040x104R31
0x100
0x104
0x0FC
0x200
0x240
0x1040x104PC
Parameter Passing● No parameters were passed from caller to
func1● Recall:
Subroutines – Parameter Passing# main program........add R4, R0, R16add R5, R0, R17jal func1............
func1:........jr $ra
0x200
0x240
Subroutines – Parameter Passing# main program........add $a0, $zero, $s0add $a1, $zero, $s1jal accArrayprint $v0............
accArray:add $v0, $zero, $zeroloop:beq $a0, $zero, donelw $a1, $t0add $v0, $v0, $t0addiu $a1, $a1, 4addi $a0, $a0, -1j loopdone:jr $ra
Subroutines – Parameter Passing● Caller saves parameters in $a0 - $a3● Callee stores results in $v0, $v1.● How does the caller pass more than 4
parameters to the callee?● Program stack
The MIPS StackSTACK
0xFC
0xF8
0x7FFF FFF40x7FFF FFF4
94
71
10
...
...
...
...
...
...
...
...
...
$sp (R29)
0xF40xF00xEC
$sp
● Push the value in $t0 on the stack
The MIPS StackSTACK
0xFC
0xF8
0x7FFF FFF00x7FFF FFF0
94
71
10
...
...
...
...
...
...
...
...
...
$sp (R29)
addi $sp, $sp, -4sw $t0, 0($sp)
0xF40xF00xEC
$spPushPush
9999
● Push the value in $t0 on the stack
The MIPS StackSTACK
0xFC
0xF8
0x7FFF FFF40x7FFF FFF4
94
71
10
...
...
...
...
...
...
...
...
...
$sp (R29)
0xF40xF00xEC
$sp9999
lw $t1, 0($sp)addi $sp, $sp, +4
PopPop
● Pop into $t1
Subroutines – Parameter Passing
# main program# 6 parameters to func1........# 4 args are in $a0 - $a3...# push 2 on stackaddi $sp, $sp, -8sw $t0, 0($sp)sw $t1, -4($sp)jal func1............
...
$sp
......
...
...
...
Before parameters pushedBefore parameters pushed
Subroutines – Parameter Passing
# main program# 6 parameters to func1........# 4 args are in $a0 - $a3...# push 2 on stackaddi $sp, $sp, -8sw $t0, 0($sp)sw $t1, -4($sp)jal func1............
...$t0$sp
$t1
......
...
...
...
Stack after parameters are pushedStack after parameters are pushed
Subroutines – Parameter Passing
# main program# 6 parameters to func1........# 4 args are in $a0 - $a3...# push 2 on stackaddi $sp, $sp, -8sw $t0, 0($sp)sw $t1, -4($sp)jal func1............
...$t0$sp
$t1
......
...
...
...
func1:....lw $t4, 0($sp)lw $t5, -4($sp)........
Stack after parameters are pushedStack after parameters are pushed
Nested Subroutines
# main program........jal func1............
func1:....jal func2....jr $ra
func2:........jr $ra
Stores return address in $raStores return address in $ra
Stores return address in $raStores return address in $ra
Nested Subroutines● func1 overwrites return address in $ra (R31)● Store the current return address in the program
stack
Nested Subroutines
...$t0$sp
$t1
......
...
...
...
Stack before func1 is calledStack before func1 is calledfunc1:addi $sp, $sp, -4sw $ra, 0($sp)............
Nested Subroutines
$t0
$sp
$t1
...
...
...
...
After pushing $ra on stackAfter pushing $ra on stackfunc1:addi $sp, $sp, -4sw $ra, 0($sp)............
$ra
What does the stack look like after func1 passes contents of register $t2 as a parameter to func2 and calls func2?Show the code changes in func1 and func2.
What does the stack look like after func1 passes contents of register $t2 as a parameter to func2 and calls func2?Show the code changes in func1 and func2.
Nested Subroutines
$t0
$t1
...
...
...
...
After pushing $ra on stackAfter pushing $ra on stackfunc1:addi $sp, $sp, -4sw $ra, 0($sp)....addi $sp, $sp, -4sw $t2, 0($sp)jal func2........ $ra
func2:addi $sp, $sp, -4sw $ra, 0($sp)....
$t0
$ra
$t2
$sp $ra
Stack Frame● Stack Frame: Private
space for a subroutine allocated on entry and deallocated on exit
● Identified by a Frame Pointer ($fp (R30))
$t0
$ra
$sp
func1Frame
func2Frame
$fp
Stack Frame
$t0
$ra
$sp
func1Frame
func2Frame
$fp
$sp
$fp
LocalVariables
Return Addr
SavedRegisters
Old FP
Stack FrameStack Frame
Frame Pointer
$t0
$t1
...
...
$t0$sp
...
$fp● After entry into a
subroutine:– Save return address
– Save frame pointer of the caller function
– Point the frame pointer to the first location of stack frame of the current subroutine
Before the callBefore the call
Frame Pointer
$t0
$t1
...
...
func1:addi $sp, $sp, -8sw $ra, 4($sp)sw $fp, 0($sp)add $fp, $sp, $zero............
$ra
$t0
$sp
...
$fp
After the callAfter the call
$fp
Parameters can beaccessed in thecallee function:8($fp), 12($fp)
Parameters can beaccessed in thecallee function:8($fp), 12($fp)
Frame Pointer
$t0
$t1
...
...
$ra
$t0
$sp
...
$fp $fp
● At the exit of the subroutine:– Pop the frame pointer
of the caller function
– Point the frame pointer to the first location of stack frame of the caller subroutine
– Pop the return address
– Jump to the return address location
Frame Pointer
$t0
$t1
...
...
func1:addi $sp, $sp, -8sw $ra, 4($sp)sw $fp, 0($sp)add $fp, $sp, $zero............lw $fp, 0($sp)lw $ra, 4($sp)addi $sp, $sp, 8jr $ra
$ra
$t0
$sp
...
$fp $fp
$sp
$fp
Stack Frame – Recall
$t0
$ra
$sp
func1Frame
func2Frame
$fp
$sp
$fp
LocalVariables
Return Addr
SavedRegisters
Old FP
Stack FrameStack Frame
Saved Registers● Registers 16 – 23 are saved across function
calls
Saved Registers● Registers 16 – 23 are saved across function
calls● Save registers $s0 - $s7 if used by the callee● Example: $s0, $s1 are saved
Stack Frame
$t0
$t1
...
...
func1:addi $sp, $sp, -16sw $ra, 12($sp)sw $fp, 8($sp)sw $s0, 4($sp)sw $s1, 0($sp)add $fp, $sp, 8........lw $s0, 4($sp)lw $s1, 0($sp)lw $fp, 8($sp)lw $ra, 12($sp)addi $sp, $sp, 16jr $ra
$ra
$t0
...
$fp
$s0
$s1$sp
$fp
Stack Frame
$t0
$t1
...
...
$ra
$t0
...
$fp
$s0
$s1
$fp
func1_X
func1_Y$sp
● Local variables are allocated on the stack after the saved registers
Stack Frame
Module Outline● Addressing modes. Instruction classes.● MIPS-I ISA.● Translating and starting a program.● High level languages, Assembly languages
and object code.● Subroutine and subroutine call. Use of stack
for handling subroutine call and return.
Backup
Assembler● Translates assembly language source
program into object code● Assembly language uses mnemonics to
represent OP codes– Mnemonics: ADD, ADDI, ADDU, ...
– Syntax rules, addressing modes for data operands
● Assembler generates the binary encoding for the OP code and other instruction fields
Symbol Table● Assembler directives
– .data, .text, .globl, ...
● Symbol table keeps track of names and their corresponding values