M2 – Instruction Set Architecture · Module Outline Addressing modes. Instruction classes. MIPS-I...

51
M2 – Instruction Set Architecture

Transcript of M2 – Instruction Set Architecture · Module Outline Addressing modes. Instruction classes. MIPS-I...

Page 1: M2 – Instruction Set Architecture · Module Outline Addressing modes. Instruction classes. MIPS-I ISA. Translating and starting a program. High level languages, Assembly languages

M2 – Instruction Set Architecture

Page 2: M2 – Instruction Set Architecture · Module Outline Addressing modes. Instruction classes. MIPS-I ISA. Translating and starting a program. High level languages, Assembly languages

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.

Page 3: M2 – Instruction Set Architecture · Module Outline Addressing modes. Instruction classes. MIPS-I ISA. Translating and starting a program. High level languages, Assembly languages

Steps in gcc

hello.c hello.i hello.s

hello.oa.out

cpp cc1

as

ld

Libraries (eg. Math)

Page 4: M2 – Instruction Set Architecture · Module Outline Addressing modes. Instruction classes. MIPS-I ISA. Translating and starting a program. High level languages, Assembly languages

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

Page 5: M2 – Instruction Set Architecture · Module Outline Addressing modes. Instruction classes. MIPS-I ISA. Translating and starting a program. High level languages, Assembly languages

Example

Page 6: M2 – Instruction Set Architecture · Module Outline Addressing modes. Instruction classes. MIPS-I ISA. Translating and starting a program. High level languages, Assembly languages

Object File Format (hello.o)

Page 7: M2 – Instruction Set Architecture · Module Outline Addressing modes. Instruction classes. MIPS-I ISA. Translating and starting a program. High level languages, Assembly languages

Object File Format (hello.o)

Page 8: M2 – Instruction Set Architecture · Module Outline Addressing modes. Instruction classes. MIPS-I ISA. Translating and starting a program. High level languages, Assembly languages

Object File Format (hello.o)

Page 9: M2 – Instruction Set Architecture · Module Outline Addressing modes. Instruction classes. MIPS-I ISA. Translating and starting a program. High level languages, Assembly languages

Linking Multiple Modules

Page 10: M2 – Instruction Set Architecture · Module Outline Addressing modes. Instruction classes. MIPS-I ISA. Translating and starting a program. High level languages, Assembly languages

Linking Multiple Modules

Page 11: M2 – Instruction Set Architecture · Module Outline Addressing modes. Instruction classes. MIPS-I ISA. Translating and starting a program. High level languages, Assembly languages

Linking Example – File 1

Page 12: M2 – Instruction Set Architecture · Module Outline Addressing modes. Instruction classes. MIPS-I ISA. Translating and starting a program. High level languages, Assembly languages

Linking Example – File 2

Page 13: M2 – Instruction Set Architecture · Module Outline Addressing modes. Instruction classes. MIPS-I ISA. Translating and starting a program. High level languages, Assembly languages

Linking Example – a.out

Page 14: M2 – Instruction Set Architecture · Module Outline Addressing modes. Instruction classes. MIPS-I ISA. Translating and starting a program. High level languages, Assembly languages

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

Page 15: M2 – Instruction Set Architecture · Module Outline Addressing modes. Instruction classes. MIPS-I ISA. Translating and starting a program. High level languages, Assembly languages

Subroutine Calls

Page 16: M2 – Instruction Set Architecture · Module Outline Addressing modes. Instruction classes. MIPS-I ISA. Translating and starting a program. High level languages, Assembly languages

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

Page 17: M2 – Instruction Set Architecture · Module Outline Addressing modes. Instruction classes. MIPS-I ISA. Translating and starting a program. High level languages, Assembly languages

Subroutines in MIPS

# main program........jal func1............

func1:........jr $ra

CallerCaller

CalleeCallee

0x1040x104R31

0x100

0x104

0x0FC

0x200

0x240

0x1040x104PC

Page 18: M2 – Instruction Set Architecture · Module Outline Addressing modes. Instruction classes. MIPS-I ISA. Translating and starting a program. High level languages, Assembly languages

Subroutines in MIPS

# main program........jal func1............

func1:........jr $ra

CallerCaller

CalleeCallee

0x1040x104R31

0x100

0x104

0x0FC

0x200

0x240

0x2000x200PC

Page 19: M2 – Instruction Set Architecture · Module Outline Addressing modes. Instruction classes. MIPS-I ISA. Translating and starting a program. High level languages, Assembly languages

Subroutines in MIPS

# main program........jal func1............

func1:........jr $ra

CallerCaller

CalleeCallee

0x1040x104R31

0x100

0x104

0x0FC

0x200

0x240

0x2440x244PC

Page 20: M2 – Instruction Set Architecture · Module Outline Addressing modes. Instruction classes. MIPS-I ISA. Translating and starting a program. High level languages, Assembly languages

Subroutines in MIPS

# main program........jal func1............

func1:........jr $ra

CallerCaller

CalleeCallee

0x1040x104R31

0x100

0x104

0x0FC

0x200

0x240

0x1040x104PC

Page 21: M2 – Instruction Set Architecture · Module Outline Addressing modes. Instruction classes. MIPS-I ISA. Translating and starting a program. High level languages, Assembly languages

Parameter Passing● No parameters were passed from caller to

func1● Recall:

Page 22: M2 – Instruction Set Architecture · Module Outline Addressing modes. Instruction classes. MIPS-I ISA. Translating and starting a program. High level languages, Assembly languages

Subroutines – Parameter Passing# main program........add R4, R0, R16add R5, R0, R17jal func1............

func1:........jr $ra

0x200

0x240

Page 23: M2 – Instruction Set Architecture · Module Outline Addressing modes. Instruction classes. MIPS-I ISA. Translating and starting a program. High level languages, Assembly languages

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

Page 24: M2 – Instruction Set Architecture · Module Outline Addressing modes. Instruction classes. MIPS-I ISA. Translating and starting a program. High level languages, Assembly languages

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

Page 25: M2 – Instruction Set Architecture · Module Outline Addressing modes. Instruction classes. MIPS-I ISA. Translating and starting a program. High level languages, Assembly languages

The MIPS StackSTACK

0xFC

0xF8

0x7FFF FFF40x7FFF FFF4

94

71

10

...

...

...

...

...

...

...

...

...

$sp (R29)

0xF40xF00xEC

$sp

● Push the value in $t0 on the stack

Page 26: M2 – Instruction Set Architecture · Module Outline Addressing modes. Instruction classes. MIPS-I ISA. Translating and starting a program. High level languages, Assembly languages

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

Page 27: M2 – Instruction Set Architecture · Module Outline Addressing modes. Instruction classes. MIPS-I ISA. Translating and starting a program. High level languages, Assembly languages

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

Page 28: M2 – Instruction Set Architecture · Module Outline Addressing modes. Instruction classes. MIPS-I ISA. Translating and starting a program. High level languages, Assembly languages

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

Page 29: M2 – Instruction Set Architecture · Module Outline Addressing modes. Instruction classes. MIPS-I ISA. Translating and starting a program. High level languages, Assembly languages

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

Page 30: M2 – Instruction Set Architecture · Module Outline Addressing modes. Instruction classes. MIPS-I ISA. Translating and starting a program. High level languages, Assembly languages

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

Page 31: M2 – Instruction Set Architecture · Module Outline Addressing modes. Instruction classes. MIPS-I ISA. Translating and starting a program. High level languages, Assembly languages

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

Page 32: M2 – Instruction Set Architecture · Module Outline Addressing modes. Instruction classes. MIPS-I ISA. Translating and starting a program. High level languages, Assembly languages

Nested Subroutines● func1 overwrites return address in $ra (R31)● Store the current return address in the program

stack

Page 33: M2 – Instruction Set Architecture · Module Outline Addressing modes. Instruction classes. MIPS-I ISA. Translating and starting a program. High level languages, Assembly languages

Nested Subroutines

...$t0$sp

$t1

......

...

...

...

Stack before func1 is calledStack before func1 is calledfunc1:addi $sp, $sp, -4sw $ra, 0($sp)............

Page 34: M2 – Instruction Set Architecture · Module Outline Addressing modes. Instruction classes. MIPS-I ISA. Translating and starting a program. High level languages, Assembly languages

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.

Page 35: M2 – Instruction Set Architecture · Module Outline Addressing modes. Instruction classes. MIPS-I ISA. Translating and starting a program. High level languages, Assembly languages

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

Page 36: M2 – Instruction Set Architecture · Module Outline Addressing modes. Instruction classes. MIPS-I ISA. Translating and starting a program. High level languages, Assembly languages

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

Page 37: M2 – Instruction Set Architecture · Module Outline Addressing modes. Instruction classes. MIPS-I ISA. Translating and starting a program. High level languages, Assembly languages

Stack Frame

$t0

$ra

$sp

func1Frame

func2Frame

$fp

$sp

$fp

LocalVariables

Return Addr

SavedRegisters

Old FP

Stack FrameStack Frame

Page 38: M2 – Instruction Set Architecture · Module Outline Addressing modes. Instruction classes. MIPS-I ISA. Translating and starting a program. High level languages, Assembly languages

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

Page 39: M2 – Instruction Set Architecture · Module Outline Addressing modes. Instruction classes. MIPS-I ISA. Translating and starting a program. High level languages, Assembly languages

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)

Page 40: M2 – Instruction Set Architecture · Module Outline Addressing modes. Instruction classes. MIPS-I ISA. Translating and starting a program. High level languages, Assembly languages

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

Page 41: M2 – Instruction Set Architecture · Module Outline Addressing modes. Instruction classes. MIPS-I ISA. Translating and starting a program. High level languages, Assembly languages

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

Page 42: M2 – Instruction Set Architecture · Module Outline Addressing modes. Instruction classes. MIPS-I ISA. Translating and starting a program. High level languages, Assembly languages

Stack Frame – Recall

$t0

$ra

$sp

func1Frame

func2Frame

$fp

$sp

$fp

LocalVariables

Return Addr

SavedRegisters

Old FP

Stack FrameStack Frame

Page 43: M2 – Instruction Set Architecture · Module Outline Addressing modes. Instruction classes. MIPS-I ISA. Translating and starting a program. High level languages, Assembly languages

Saved Registers● Registers 16 – 23 are saved across function

calls

Page 44: M2 – Instruction Set Architecture · Module Outline Addressing modes. Instruction classes. MIPS-I ISA. Translating and starting a program. High level languages, Assembly languages

Saved Registers● Registers 16 – 23 are saved across function

calls● Save registers $s0 - $s7 if used by the callee● Example: $s0, $s1 are saved

Page 45: M2 – Instruction Set Architecture · Module Outline Addressing modes. Instruction classes. MIPS-I ISA. Translating and starting a program. High level languages, Assembly languages

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

Page 46: M2 – Instruction Set Architecture · Module Outline Addressing modes. Instruction classes. MIPS-I ISA. Translating and starting a program. High level languages, Assembly languages

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

Page 47: M2 – Instruction Set Architecture · Module Outline Addressing modes. Instruction classes. MIPS-I ISA. Translating and starting a program. High level languages, Assembly languages

Stack Frame

Page 48: M2 – Instruction Set Architecture · Module Outline Addressing modes. Instruction classes. MIPS-I ISA. Translating and starting a program. High level languages, Assembly languages

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.

Page 49: M2 – Instruction Set Architecture · Module Outline Addressing modes. Instruction classes. MIPS-I ISA. Translating and starting a program. High level languages, Assembly languages

Backup

Page 50: M2 – Instruction Set Architecture · Module Outline Addressing modes. Instruction classes. MIPS-I ISA. Translating and starting a program. High level languages, Assembly languages

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

Page 51: M2 – Instruction Set Architecture · Module Outline Addressing modes. Instruction classes. MIPS-I ISA. Translating and starting a program. High level languages, Assembly languages

Symbol Table● Assembler directives

– .data, .text, .globl, ...

● Symbol table keeps track of names and their corresponding values