mips r2000 assembly language (procedure call, interrupt, io, … · 2009-03-25 · – simply...

37
1 MIPS R2000 Assembly Language (Procedure Call, Interrupt, IO, Syscall) CS 230 이준원

Upload: others

Post on 31-Jul-2020

0 views

Category:

Documents


0 download

TRANSCRIPT

Page 1: MIPS R2000 Assembly Language (Procedure Call, Interrupt, IO, … · 2009-03-25 · – simply suspend and resume user program • Traps – caused by internal events • exceptional

1

MIPS R2000 Assembly Language(Procedure Call, Interrupt, IO, Syscall)

CS 230

이준원

Page 2: MIPS R2000 Assembly Language (Procedure Call, Interrupt, IO, … · 2009-03-25 · – simply suspend and resume user program • Traps – caused by internal events • exceptional

2

Procedure Call

• Why do we need it?

– structured programming

– reuse frequently used routine

• Who takes care of it?

– compiler for HLL

– programmer for assembly programming

• What should be done?

1. save registers whose values will be needed after the call

• who will do this job? caller or callee

2. save return address

3. pass arguments to the callee

• Anything else?

– space to store local variables of the callee

– data structure to handle nested calls

• stack

Page 3: MIPS R2000 Assembly Language (Procedure Call, Interrupt, IO, … · 2009-03-25 · – simply suspend and resume user program • Traps – caused by internal events • exceptional

3

Stack

• last in first out data structure

– push: puts an item into the stack

– pop: gets the item at the top of the stack

• stack pointer (SP) in most architecture

– why? because stack is used so frequently

• MIPS stack

– push

sub $sp, $sp, 4

sw $t0, ($sp)

– pop

lw$t0, ($sp)

add $sp, $sp, 4

………new$sp

highaddr

lowaddr

old

Page 4: MIPS R2000 Assembly Language (Procedure Call, Interrupt, IO, … · 2009-03-25 · – simply suspend and resume user program • Traps – caused by internal events • exceptional

4

MIPS Procedure Calls

………

jal loc…………

………………

jr $ra…

loc:$ra

caller

callee

What if the callee calls another procedure?

Page 5: MIPS R2000 Assembly Language (Procedure Call, Interrupt, IO, … · 2009-03-25 · – simply suspend and resume user program • Traps – caused by internal events • exceptional

5

MIPS Procedure Call (2)

…………

caller

callee & caller

sw $ra, ($sp)sub $sp, 4

another calleeloc:

jal loc$ra

……………

……jal loc2

loc2:

$ra

lw $ra, ($sp)

jr $raadd $sp, 4

…jr $ra

……

Page 6: MIPS R2000 Assembly Language (Procedure Call, Interrupt, IO, … · 2009-03-25 · – simply suspend and resume user program • Traps – caused by internal events • exceptional

6

Saving Private Registers

• use registers as many as possible

– they are much faster than memory (more than 10 times)

– there are only a limited number of registers (32 in MIPS)

• callee save vs caller save

– whichever that needs to save less registers

• MIPS conventions

– $s0..$s7 are callee saves

– $t0..$t9 are caller saves

• problems

– registers are not enough

– a procedure calles another procedure

– solution:

• you save/restore variables on the stack carefully

• stack frame – it is for a compiler

Page 7: MIPS R2000 Assembly Language (Procedure Call, Interrupt, IO, … · 2009-03-25 · – simply suspend and resume user program • Traps – caused by internal events • exceptional

7

Stack Frame

• purpose

– store data used by a procedure in a single frame

– data can be accessed using a single pointer ($fp) within a procedure

– the stack may be used for other purpose – expression evaluation

• accessing data using $sp can be tricky – stack is used for other purposes

• Is it really necessary?

– yes, for recursive calls

– yes, for complex chained procedure calls

– no, for simple programs

– compilers use it !

• contents in a stack frame

– arguments other than stored in a0..a3

– saved register values

– variables local to the procedure

Page 8: MIPS R2000 Assembly Language (Procedure Call, Interrupt, IO, … · 2009-03-25 · – simply suspend and resume user program • Traps – caused by internal events • exceptional

8

Stack Frame

stack

old $fpargument 5

argument 6

saved registers

localvariables

$spstack growsand shrinks

duringexpression evaluation

….

SF ofprocedue A

SF ofprocedue B

SF ofprocedue C

new $fp

offsetfrom $fp

Page 9: MIPS R2000 Assembly Language (Procedure Call, Interrupt, IO, … · 2009-03-25 · – simply suspend and resume user program • Traps – caused by internal events • exceptional

9

Stack Frames for Procedure Calls

stack

SF of mainmain program starts

SF of procedure A

main calls procedure A

SF ofprocedure B

Proc A calls procedure B

SF ofprocedure C

Proc B calls procedure C

Page 10: MIPS R2000 Assembly Language (Procedure Call, Interrupt, IO, … · 2009-03-25 · – simply suspend and resume user program • Traps – caused by internal events • exceptional

10

SF of procedure A

SF ofprocedure B

SF ofprocedure C

Stack Frames for Procedure Calls

main program starts

main calls procedure A

Proc A calls procedure B

Procedure C finishes

stack

SF of main

Procedure B finishes

Procedure A finishes

Page 11: MIPS R2000 Assembly Language (Procedure Call, Interrupt, IO, … · 2009-03-25 · – simply suspend and resume user program • Traps – caused by internal events • exceptional

11

Procedure Call Actions - review

• caller

– put arguments

• first four in $a0 ~ $a3 and remaining ones on stack

– save any registers that will be used later

• $t0..$t9

• callee will save $fp, $ra, and $s0 ~ $s7 if necessary

– jump to the procedure using jal instruction

• jump to the given address and

• saves the return address in $ra ($31)

• callee

– calculate the size of its frame and allocate it on the stack

– save $fp

– save $ra on the stack if the callee itself will call another procedure

– save $s0 ~ $s7 if the callee will modify them

– establish $fp

– return values in $v0, $v1

$fp argument 5

saved registers

localvariables

$sp

argument 6….

Page 12: MIPS R2000 Assembly Language (Procedure Call, Interrupt, IO, … · 2009-03-25 · – simply suspend and resume user program • Traps – caused by internal events • exceptional

12

Procedure Call Actions (cont’d)

• callee returns

– if it is to return a value, place it on $v0, $v1

– restore all callee-saved registers

– pop the frame from the stack

– jump to the address stored in $ra

Page 13: MIPS R2000 Assembly Language (Procedure Call, Interrupt, IO, … · 2009-03-25 · – simply suspend and resume user program • Traps – caused by internal events • exceptional

13

.text

.globl mainmain:

subu $sp, $sp, 32sw $ra, 20($sp)sw $fp, 16($sp)addu $fp, $sp, 32

li $a0, 10jal fact

la $a0, $LCmove $a1, $v0jal printf

lw $ra, 20($sp)lw $fp, 16($sp)addu $sp, $sp, 32jr $ra

.rdata$LC:

.ascii “The answer is %d\n\000”

.textfact:

subu $sp, $sp, 32sw $ra, 20($sp)sw $fp, 16($sp)addu $fp, $sp, 32sw $a0, 0($fp) # save arg, n

lw $v0, 0($fp) # load nbgtz $v0, $L2li $v0, 1 # return 1j $L1 # to return code

$L2:lw $v1, 0($fp) # load nsubu $v0, $v1, 1 # compute n-1move $a0, $v0 # move arg to $a0jal factlw $v1, 0($fp) # load nmul $v0, $v0, $v1 # return val in $v0

$L1:lw $ra, 20($sp)lw $fp, 16($sp)addu $sp, $sp, 32jr $ra

A Simple Procedure Call Example - factorial !

Page 14: MIPS R2000 Assembly Language (Procedure Call, Interrupt, IO, … · 2009-03-25 · – simply suspend and resume user program • Traps – caused by internal events • exceptional

14

Optimizing Procedure Call by Compiler

• Procedure Call Taxonomy

– non-leaf

• routines that call other routines

• previous example

– leaf

• routines that do not execute any procedure calls

• more classification– one that needs stack storage for local variables

» no need for saving return address

– one that do not have local variables

» no need for stack frame

Page 15: MIPS R2000 Assembly Language (Procedure Call, Interrupt, IO, … · 2009-03-25 · – simply suspend and resume user program • Traps – caused by internal events • exceptional

15

A Real Procedure Call - non leaf

.globl nonleaf

.ent nonleaf # for debuggernonleaf:

subu $sp, 24 # create stack framesw $ra, 20($sp) .mask 0x80000000, -4 # only $31 is saved at $sp +24 -4.frame $sp, 24, $31 # for debugger:

#frame size, return addresslw $v0, 0($a1) # args are in $ao and $a1subu $v1, $a0, $v0 # temp in #v1

bge $a0, $v0, $L1negu $v1, $v1 # temp = -temp;

$L1: move $a0, $v1jal atof # call atofcvt.s.d $f0, $f0lw $ra, 20($sp) #restore raaddu $sp, 24jr $ra.end nonleaf # for debugger

floatnonleaf(I, j) int I, *j { double atof(); int temp;

temp = i - *j; if (I < *j) temp = -temp; return atof(temp); }

Page 16: MIPS R2000 Assembly Language (Procedure Call, Interrupt, IO, … · 2009-03-25 · – simply suspend and resume user program • Traps – caused by internal events • exceptional

16

A Real Procedure Call - leaf w/o stack

.globl leaf

.ent nonleaf # for debuggernonleaf

.frame $sp, 0, $31 # for debugger: #frame size, return address

ble $a0, $a1, $L1 # args are in $ao and $a1move $v0, $a0b $L2

$L1: move $v0, $a1$L2:

jr $ra.end leaf

intleaf(p1, p2) int p1, p2; { return (p1 > p2) p1: p2; }

Page 17: MIPS R2000 Assembly Language (Procedure Call, Interrupt, IO, … · 2009-03-25 · – simply suspend and resume user program • Traps – caused by internal events • exceptional

17

Floating Point

• We need a way to represent

– numbers with fractions, e.g., 3.1416

– very small numbers, e.g., .000000001

– very large numbers, e.g., 3.15576×109

• Representation:

– sign, exponent, significand: (–1)sign ×significand ×2exponent

– more bits for significand gives more accuracy

– more bits for exponent increases range

• IEEE 754 floating point standard:

– single precision: 8 bit exponent, 23 bit significand

– double precision: 11 bit exponent, 52 bit significand

exp significands

Page 18: MIPS R2000 Assembly Language (Procedure Call, Interrupt, IO, … · 2009-03-25 · – simply suspend and resume user program • Traps – caused by internal events • exceptional

18

Normalized Numbers

• you can always make the leading bit of significant “1”

– 0.001 × 26 = 0.100 × 24

• why?

– think about addition/subtraction !

• the computer prefers that all the floating numbers be normalized

– simplify addition/subtraction operations

• floating point operations are expensive

• but, normalized numbers waste the bits !

Page 19: MIPS R2000 Assembly Language (Procedure Call, Interrupt, IO, … · 2009-03-25 · – simply suspend and resume user program • Traps – caused by internal events • exceptional

19

IEEE 754 floating-point standard

• Leading “1” bit of significand is implicit

• Exponent is “biased” to make sorting easier

– all 0s is smallest exponent all 1s is largest

– bias of 127 for single precision and 1023 for double precision

– summary: (–1)sign ×significand) ×2exponent – bias

• Example:

– decimal: -.75 = -3/4 = -3/22

– binary: -.11 = -1.1 x 2-1

– floating point: exponent = 126 = 01111110

– IEEE single precision: 10111111010000000000000000000000

Page 20: MIPS R2000 Assembly Language (Procedure Call, Interrupt, IO, … · 2009-03-25 · – simply suspend and resume user program • Traps – caused by internal events • exceptional

20

Number Definition

• Normalized Numbers

– leading digit is not a zero

– make hardware simple

• Denormalized Numbers

– to represent very small numbers

• Infinity

– result of division by zero

– two infinities: +/-

• Zero

– two zeroes

• NaN

– not a number

– zero/zero

exponent fraction

1 ~ 254normalized

fraction

0denormalized

fraction

255 0

0 0

255 non-zero

Page 21: MIPS R2000 Assembly Language (Procedure Call, Interrupt, IO, … · 2009-03-25 · – simply suspend and resume user program • Traps – caused by internal events • exceptional

21

MIPS Floating Point Registers

FGR 0

Floating Point General Purpose Registers

(FGR)

FGR 1

FGR 2

FGR 3

FGR 4

FGR 27

FGR 28

FGR 29

FGR 30

FGR 31

Floating Point Registers (FPR)

least

mostFPR 0

least

mostFPR 2

least

mostFPR 30

least

mostFPR 28

Page 22: MIPS R2000 Assembly Language (Procedure Call, Interrupt, IO, … · 2009-03-25 · – simply suspend and resume user program • Traps – caused by internal events • exceptional

22

MIPS FP Instructions

OP Description

l.d $f4, addrs.s $f4, addrmov.d $f4, $f6

ctc1 $7, rdcfc1 $6, rd

cvt.s.fmtcvt.d.fmtcvt.w.fmt

load d-word to $f4store s-word from $f4move word

move control word to FPAmove from

convert to single FPconvert to double FPconvert to fixed point

fmt: Formats: single precisiond: double precisionw: binary fixed point (integer)

exmaple: cvt.s.w FRdest, FRsrccvt.d.w FRdest, FRsrccvt.d.s FRdest, FRsrc

Page 23: MIPS R2000 Assembly Language (Procedure Call, Interrupt, IO, … · 2009-03-25 · – simply suspend and resume user program • Traps – caused by internal events • exceptional

23

MIPS FP Instructions (2)

OP Description

add.fmtsub.fmtmul.fmtdiv.fmtabs.fmtmov.fmtneg.fmt

addsubstractmultiplydivideabsolute valuemove fp to fpnegate

compare and branch

c.cond.format FR1, FR2bc1t labelbc1f label

cond: Condition•eq, le, lt, t, f, or, nlt, gt, …..•result sets a flag in FPA•branch instruction on this flag

BC1T label # branch if trueBC1F label # branch if false

exmaple: add.s Frdest, FRsrc1, FRsrc2 add.d Frdest, FRsrc1, FRsrc2 sub.s Frdest, FRsrc1, DRsrc2

exmaple: c.eq.s FRsrc1, FRsrc2 bc1t xxx

Page 24: MIPS R2000 Assembly Language (Procedure Call, Interrupt, IO, … · 2009-03-25 · – simply suspend and resume user program • Traps – caused by internal events • exceptional

24

Exception

• Exception = unprogrammed control transfer

– system takes action to handle the exception

• must record the address of the offending instruction

– returns control to user

– must save & restore user state

user program

normal control flow: sequential, jumps, branches, calls, returns

SystemExceptionHandlerException:

return fromexception

Page 25: MIPS R2000 Assembly Language (Procedure Call, Interrupt, IO, … · 2009-03-25 · – simply suspend and resume user program • Traps – caused by internal events • exceptional

25

User/System Modes

• By providing two modes of execution (user/system) it is possible for the computer to manage itself

– operating system is a special program that runs in the privileged mode and has access to all of the resources of the computer

– presents virtual resources to each user that are more convenient than the physical resources

• files vs. disk sectors

• virtual memory vs physical memory

– protects each user program from others

• Exceptions allow the system to take action in response to events that occur while user program is executing

– O/S begins at the handler

Page 26: MIPS R2000 Assembly Language (Procedure Call, Interrupt, IO, … · 2009-03-25 · – simply suspend and resume user program • Traps – caused by internal events • exceptional

26

Two Types of Exceptions

• Interrupts

– caused by external events

– asynchronous to program execution

– may be handled between instructions

– simply suspend and resume user program

• Traps

– caused by internal events

• exceptional conditions (overflow)

• errors (parity)

• faults (non-resident page)

– synchronous to program execution

– condition must be remedied by the handler

– instruction may be retried or simulated and program continued or program may be aborted

Page 27: MIPS R2000 Assembly Language (Procedure Call, Interrupt, IO, … · 2009-03-25 · – simply suspend and resume user program • Traps – caused by internal events • exceptional

27

MIPS Convention

• exception means any unexpected change in control flow, without distinguishing internal or external; use the term interrupt only when the event is externally caused.

Type of event From where? MIPS terminology

I/O device request External Interrupt

Invoke OS from user program Internal Exception

Arithmetic overflow Internal Exception

Using an undefined instruction Internal Exception

Hardware malfunctions Either Exception or Interrupt

Page 28: MIPS R2000 Assembly Language (Procedure Call, Interrupt, IO, … · 2009-03-25 · – simply suspend and resume user program • Traps – caused by internal events • exceptional

28

Addressing the Exception Handler

• Traditional Approach: Interupt Vector

– PC <- MEM[ IV_base + cause || 00]

– 370, 68000, Vax, 80x86, . . .

• MIPS Approach: fixed entry

– PC <- EXC_addr

– Actually very small table

• RESET entry

• TLB

• other

iv_basecause

handlercode

iv_basecause

handler entry code

Page 29: MIPS R2000 Assembly Language (Procedure Call, Interrupt, IO, … · 2009-03-25 · – simply suspend and resume user program • Traps – caused by internal events • exceptional

29

Saving State

• Push it onto the stack

– Vax, 68k, 80x86

• Save it in special registers

– MIPS EPC, BadVaddr, Status, Cause

• Shadow Registers

– M88k

– Save state in a shadow of the internal pipeline registers

Page 30: MIPS R2000 Assembly Language (Procedure Call, Interrupt, IO, … · 2009-03-25 · – simply suspend and resume user program • Traps – caused by internal events • exceptional

30

MIPS Registers for Exceptions

• EPC-

– 32-bit register used to hold the address of the affected instruction (register 14 of coprocessor 0).

• Cause

– register used to record the cause of the exception. In the MIPS architecture this register is 32 bits, though some bits are currently unused. Assume that bits 5 to 2 of this register encodes the two possible exception sources mentioned above: undefined instruction=0 and arithmetic overflow=1 (register 13 of coprocessor 0).

• BadVAddr

– register contained memory address at which memory reference occurred (register 8 of coprocessor 0)

• Status

– interrupt mask and enable bits (register 12 of coprocessor 0)

Page 31: MIPS R2000 Assembly Language (Procedure Call, Interrupt, IO, … · 2009-03-25 · – simply suspend and resume user program • Traps – caused by internal events • exceptional

31

Cause Register

• Pending interrupt 5 hardware levels: bit set if interrupt occurs but not yet serviced– handles cases when more than one interrupt occurs at same time, or while records

interrupt requests when interrupts disabled• Exception Code encodes reasons for interrupt

– 0 (INT) => external interrupt– 1~3 => TLB related– 4 (ADDRL) => address error exception (load or instr fetch)– 5 (ADDRS) => address error exception (store)– 6 (IBUS) => bus error on instruction fetch– 7 (DBUS) => bus error on data fetch– 8 (Syscall) => Syscall exception– 9 (BKPT) => Breakpoint exception– 10 (RI) => Reserved Instruction exception– 12 (OVF) => Arithmetic overflow exception

Status15 10

Pending

5 2

Code

Page 32: MIPS R2000 Assembly Language (Procedure Call, Interrupt, IO, … · 2009-03-25 · – simply suspend and resume user program • Traps – caused by internal events • exceptional

32

Exception Handler Example

.ktext 0x80000080 # MIPS jumps to here on an exceptionsw $a0 save0 # save registers that will be used bysw $a1 save1 # this routine on memory, NOT on stack

# this code is not re-entrant

mfc0 $k0 $13 # move cause register to $k0mfc0 $k1 $14 # move EPC to $k1

# k reg need not be saved (users don’t use them)sgt $v0 $k0 0x44 # is this correct?bgtz $v0 done # if it is an interrupt, ignore

mov $a0, $k0mov $a1, $k1jal print_excp

done:lw $a0 save0lw $a1 save1addiu $k1 $k1 4 # will return to saved PC + 4

rfe # restore interrupted statejr $k1.kdata

save0: .word 0save1: .word 0

Page 33: MIPS R2000 Assembly Language (Procedure Call, Interrupt, IO, … · 2009-03-25 · – simply suspend and resume user program • Traps – caused by internal events • exceptional

33

Input and Output

• IO Devices

– keyboard, screen, disk, network, CD, ….

• Commanding IO devices

– special IO instruction

– memory mapped IO

• special locations of address space are mapped to IO devices

• need to access registers inside IO controllers

• Communicating with the processor

– polling

• CPU keeps checking status of IO device (status register)

• may waste CPU power (when events are rare)

– interrupt

• IO devices raises interrupt whenever necessary

• overhead to process interrupt

Page 34: MIPS R2000 Assembly Language (Procedure Call, Interrupt, IO, … · 2009-03-25 · – simply suspend and resume user program • Traps – caused by internal events • exceptional

34

SPIM IO

• memory mapped IO

– 4 registers are mapped to memory locations

• to read data from keyboard– Ready bit means that the “Received data” register has a data.

– When a data arrives, interrupt is raised if it is enabled.1

Interruptenable

Ready

1Unused

Receiver control(0xffff0000)

8

Received byte

Unused

Receiver data(0xffff0004)

Page 35: MIPS R2000 Assembly Language (Procedure Call, Interrupt, IO, … · 2009-03-25 · – simply suspend and resume user program • Traps – caused by internal events • exceptional

35

SPIM IO (cont’d)

• to write a data on screen

– Ready bit indicates if the device is ready to accept a data

– An interrupt is raised whenever it is ready if it was enabled

1

Interruptenable

Ready

1Unused

Transmitter control(0xffff0008)

Transmitter data(0xffff000c)

8

Transmitted byte

Unused

Page 36: MIPS R2000 Assembly Language (Procedure Call, Interrupt, IO, … · 2009-03-25 · – simply suspend and resume user program • Traps – caused by internal events • exceptional

36

System Call

• Syscall a.k.a. syscall call

– an interface through which user programs interacts with OS

• OS defines this interface

– protects OS from buggy(or malicious) user programs

– procedure

1. store parameters in registers

2. specify the syscall type (usually in a register)

3. syscall

4. something happens to invoke OS (later in OS course)

5. syscall routine of the OS is invoked

6. return to the user program• how to return to the calling place?

• how to reinstate the state of the user program?

– expensive operation

Page 37: MIPS R2000 Assembly Language (Procedure Call, Interrupt, IO, … · 2009-03-25 · – simply suspend and resume user program • Traps – caused by internal events • exceptional

37

SPIM syscall move $a0, $t2li $v0, 1

syscall

service call code arguments resultsprint integer 1 int in $a0 print float 2 float in $a0

print double 3 double in $a0 print string 4 addr of string in $a0 read integer 5 int in $v0read float 6 float in $v0

read double 7 double in $v0read string 8 $a0=buffer, $a1=length

sbrk 9 $a0 = amount addr in $v0exit 10