MIPS R2000 Assembly Language Procedure Call, Interrupt, IO, Syscall - PowerPoint PPT Presentation

1 / 36
About This Presentation
Title:

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

Description:

MIPS R2000 Assembly Language Procedure Call, Interrupt, IO, Syscall – PowerPoint PPT presentation

Number of Views:360
Avg rating:3.0/5.0
Slides: 37
Provided by: joon
Category:

less

Transcript and Presenter's Notes

Title: MIPS R2000 Assembly Language Procedure Call, Interrupt, IO, Syscall


1
MIPS R2000 Assembly Language(Procedure Call,
Interrupt, IO, Syscall)
  • CS 230
  • ???

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?
  • save registers whose values will be needed after
    the call
  • who will do this job? caller
  • 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

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

high addr



sp

low addr
4
MIPS Procedure Calls
caller
callee




jal loc

ra

loc







jr ra

What if the callee calls another procedure?
5
MIPS Procedure Call (2)
caller
callee caller

another callee











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

7
Stack Frame
  • purpose
  • put 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

8
Stack Frame
fp
argument 5
stack
argument 6
.
saved registers
stack frame of a procedure
local variables
Dynamic data
Static data
stack grows
sp
text
stack grows and shrinks during expression
evaluation
total address space
9
Stack Frames for Procedure Calls
stack
10
Stack 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
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
argument 6
.
saved registers
local variables
sp
12
Procedure 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

13
A 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
14
Optimizing Procedure Call
  • 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

15
A 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)
16
A 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
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.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

18
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 (1significand) 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

19
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 254
normalized fraction
0
denormalized fraction
255
0
0
0
255
non-zero
20
MIPS 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
21
MIPS FP Instructions
OP
Description
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
add.fmt sub.fmt mul.fmt div.fmt abs.fmt mov.fmt ne
g.fmt c.cond.fmt
add substract multiply divide absolute value move
fp to fp negate compare
fmt Format s single precision d double
precision w binary fixed point (integer)
convert f4 (double) into single and store in
f0 cvt.s.d f0, f4 use even numbered reg
  • 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

22
Exception
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

23
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 that the physical resources
  • files vs. disk sectors
  • virtual memory vs physical memory
  • protects each user program from others
  • Exceptions allow the system to taken action in
    response to events that occur while user program
    is executing
  • O/S begins at the handler

24
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

25
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 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

26
Addressing 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
27
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

28
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 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)

29
Status Register
15
8
Status
Mask
old
prev
current
  • Mask 1 bit for each of 5 hardware and 3
    software interrupt levels
  • 1 gt enables interrupts
  • 0 gt disables interrupts
  • k kernel/user
  • 0 gt was in the kernel when interrupt occurred
  • 1 gt was running user mode
  • e interrupt enable
  • 0 gt interrupts were disabled
  • 1 gt interrupts were enabled
  • When interrupt occurs, 6 LSB shifted left 2 bits,
    setting 2 LSB to 0
  • run in kernel mode with interrupts disabled

30
Cause 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

31
Exception 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
32
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

33
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
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
34
SPIM 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

35
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
  • 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

36
SPIM 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

Write a Comment
User Comments (0)
About PowerShow.com