Title: MIPS R2000 Assembly Language Procedure Call, Interrupt, IO, Syscall
1MIPS R2000 Assembly Language(Procedure Call,
Interrupt, IO, Syscall)
2Procedure 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?
- save registers whose values will be needed after
the call - who will do this job? caller or callee
- save return address
- pass arguments to the callee
- Anything else?
- space to store local variables of the callee
- data structure to handle nested calls
- stack
3Stack
- 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
old
high addr
sp
new
low addr
4MIPS Procedure Calls
caller
callee
jal loc
ra
loc
jr ra
What if the callee calls another procedure?
5MIPS Procedure Call (2)
caller
callee caller
another callee
6Saving 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
7Stack 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
8Stack Frame
old fp
argument 5
argument 6
.
saved registers
SF of procedue A
offset from fp
SF of procedue B
local variables
SF of procedue C
new fp
stack grows and shrinks during expression
evaluation
sp
stack
9Stack Frames for Procedure Calls
stack
10Stack Frames for Procedure Calls
stack
main program starts
SF of main
SF of procedure A
main calls procedure A
Procedure A finishes
SF of procedure B
Proc A calls procedure B
Procedure B finishes
SF of procedure C
Procedure C finishes
11Procedure 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
argument 6
.
saved registers
local variables
sp
12Procedure Call Actions (contd)
- 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
13A Simple Procedure Call Example - factorial !
.text .globl main main subu sp, sp,
32 sw ra, 20(sp) sw fp, 16(sp) addu fp,
sp, 32 li a0, 10 jal fact la a0,
LC move a1, v0 jal printf lw ra,
20(sp) lw fp, 16(sp) addu sp, sp,
32 jr ra .rdata LC .ascii The answer is
d\n\000
.text fact subu sp, sp, 32 sw ra,
20(sp) sw fp, 16(sp) addu fp, sp,
32 sw a0, 0(fp) save arg, n lw v0,
0(fp) load n bgtz v0, L2 li v0, 1
return 1 j L1 to return code L2 lw v1,
0(fp) load n subu v0, v1, 1 compute
n-1 move a0, v0 move arg to
a0 jal fact lw v1, 0(fp) load n mul v0,
v0, v1 return val in v0 L1 lw ra,
20(sp) lw fp, 16(sp) addu sp, sp,
32 jr ra
14Optimizing 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
15A Real Procedure Call - non leaf
.globl nonleaf .ent nonleaf for
debugger nonleaf subu sp, 24 create stack
frame sw ra, 20(sp) .mask 0x80000000, -4
only 31 is saved at sp 24 -4 .frame sp, 24,
31 for debugger frame size, return
address lw v0, 0(a1) args are in ao and
a1 subu v1, a0, v0 temp in v1 bge a0,
v0, L1 negu v1, v1 temp
-temp L1 move a0, v1 jal atof call
atof cvt.s.d f0, f0 lw ra, 20(sp) restore
ra addu sp, 24 jr ra .end nonleaf for
debugger
float nonleaf(I, j) int I, j
double atof() int temp
temp i - j if (I lt j) temp
-temp return atof(temp)
16A Real Procedure Call - leaf w/o stack
int leaf(p1, p2) int p1, p2
return (p1 gt p2) p1 p2
.globl leaf .ent nonleaf for
debugger nonleaf .frame sp, 0, 31 for
debugger frame size, return
address ble a0, a1, L1 args are in ao and
a1 move v0, a0 b L2 L1 move v0,
a1 L2 jr ra .end leaf
17Floating 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.15576109
- 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
significand
s
18Normalized 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 !
19IEEE 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 (1.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 10111111010000000000000000
000000
20Number 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 254
normalized fraction
0
denormalized fraction
255
0
0
0
255
non-zero
21MIPS Floating Point Registers
Floating Point General Purpose Registers (FGR)
Floating Point Registers (FPR)
FGR 0
FGR 1
FGR 2
FGR 3
FGR 4
FGR 27
FGR 28
FGR 29
FGR 30
FGR 31
22MIPS FP Instructions
OP
Description
l.d f4, addr s.s f4, addr mov.d f4, f6 ctc1
7, rd cfc1 6, rd cvt.s.fmt cvt.d.fmt cvt.w.fmt
load d-word to f4 store s-word from f4 move
word move control word to FPA move
from convert to single FP convert to double
FP convert to fixed point
fmt Format s single precision d double
precision w binary fixed point (integer)
exmaple cvt.s.w FRdest, FRsrc cvt.d.w FRdest,
FRsrc cvt.d.s FRdest, FRsrc
23MIPS FP Instructions (2)
OP
Description
- compare and branch
- c.cond.format FR1, FR2
- bc1t label
- bc1f 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 true
- BC1F label branch if false
add.fmt sub.fmt mul.fmt div.fmt abs.fmt mov.fmt ne
g.fmt
add substract multiply divide absolute value move
fp to fp negate
exmaple add.s Frdest, FRsrc1, FRsrc2
add.d Frdest, FRsrc1, FRsrc2 sub.s Frdest,
FRsrc1, DRsrc2
exmaple c.eq.s FRsrc1, FRsrc2 bc1t xxx
24Exception
System Exception Handler
user program
Exception
return from exception
normal control flow sequential, jumps,
branches, calls, returns
- 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
25User/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
26Two 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
27MIPS 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 terminologyI/O
device request External InterruptInvoke OS
from user program Internal ExceptionArithmetic
overflow Internal ExceptionUsing an undefined
instruction Internal ExceptionHardware
malfunctions Either Exception or Interrupt
28Addressing the Exception Handler
- Traditional Approach Interupt Vector
- PC lt- MEM IV_base cause 00
- 370, 68000, Vax, 80x86, . . .
- MIPS Approach fixed entry
- PC lt- EXC_addr
- Actually very small table
- RESET entry
- TLB
- other
handler entry code
iv_base
cause
29Saving 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
30MIPS 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 instruction0 and
arithmetic overflow1 (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)
31Cause Register
15
10
5
2
Status
Pending
Code
- 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) gt external interrupt
- 13 gt TLB related
- 4 (ADDRL) gt address error exception (load or
instr fetch) - 5 (ADDRS) gt address error exception (store)
- 6 (IBUS) gt bus error on instruction fetch
- 7 (DBUS) gt bus error on data fetch
- 8 (Syscall) gt Syscall exception
- 9 (BKPT) gt Breakpoint exception
- 10 (RI) gt Reserved Instruction exception
- 12 (OVF) gt Arithmetic overflow exception
32Exception Handler Example
.ktext 0x80000080 MIPS jumps to here on an
exception sw a0 save0 save registers that
will be used by sw a1 save1 this routine on
memory, NOT on stack this code is not
re-entrant mfc0 k0 13 move cause register
to k0 mfc0 k1 14 move EPC to k1 k
reg need not be saved (users dont use
them) sgt v0 k0 0x44 is this
correct? bgtz v0 done if it is an interrupt,
ignore mov a0, k0 mov a1,
k1 jal print_excp done lw a0
save0 lw a1 save1 addiu k1 k1 4 will
return to saved PC 4 rfe restore
interrupted state jr k1 .kdata save0 .word
0 save1 .word 0
33Input 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
34SPIM 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
1
U
n
u
s
e
d
R
e
c
e
i
v
e
r
c
o
n
t
r
o
l
(
0
x
f
f
f
f
0
0
0
0
)
I
n
t
e
r
r
u
p
t
R
e
a
d
y
e
n
a
b
l
e
8
U
n
u
s
e
d
R
e
c
e
i
v
e
r
d
a
t
a
(
0
x
f
f
f
f
0
0
0
4
)
R
e
c
e
i
v
e
d
b
y
t
e
35SPIM IO (contd)
1
1
U
n
u
s
e
d
T
r
a
n
s
m
i
t
t
e
r
c
o
n
t
r
o
l
(
0
x
f
f
f
f
0
0
0
8
)
I
n
t
e
r
r
u
p
t
R
e
a
d
y
e
n
a
b
l
e
8
U
n
u
s
e
d
T
r
a
n
s
m
i
t
t
e
r
d
a
t
a
(
0
x
f
f
f
f
0
0
0
c
)
T
r
a
n
s
m
i
t
t
e
d
b
y
t
e
- 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
36System 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
- store parameters in registers
- specify the syscall type (usually in a register)
- syscall
- something happens to invoke OS (later in OS
course) - syscall routine of the OS is invoked
- return to the user program
- how to return to the calling place?
- how to reinstate the state of the user program?
- expensive operation
37SPIM syscall
move a0, t2 li v0, 1 syscall
service
call code
arguments
results
print 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 v0
read float
6
float in v0
read double
7
double in v0
read string
8
a0buffer, a1length
sbrk
9
a0 amount
addr in v0
exit
10