MIPS R2000 Assembly Language (Procedure Call, Interrupt, IO, Syscall) CS 230 이준원 1 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 2 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 $sp lw$t0, ($sp) add $sp, $sp, 4 old … … … high addr new low addr 3 MIPS Procedure Calls caller callee … … … $ra jal loc … … … … loc: … … … … … … jr $ra … What if the callee calls another procedure? 4 MIPS Procedure Call (2) caller … $ra callee & caller jal loc … … … … loc: sub $sp, 4 sw $ra, ($sp) … jal loc2 $ra … … lw $ra, ($sp) add $sp, 4 jr $ra another callee loc2: … … … … … … … jr $ra … 5 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 6 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 7 Stack Frame argument 5 old $fp argument 6 …. SF of procedue A SF of procedue B SF of procedue C saved registers offset from $fp local variables new $fp stack stack grows and shrinks during expression evaluation $sp 8 Stack Frames for Procedure Calls stack main program starts SF of main main calls procedure A SF of procedure A Proc A calls procedure B SF of procedure B Proc B calls procedure C SF of procedure C 9 Stack Frames for Procedure Calls stack main program starts SF of main main calls procedure Procedure A finishesA SF of procedure A Procedure B finishesB Proc A calls procedure SF of procedure B Procedure C finishes SF of procedure C 10 Procedure Call Actions - review • • caller $fp – 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 $sp – 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 argument 5 argument 6 …. saved registers local variables 11 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 12 A Simple Procedure Call Example - factorial ! .text .globl main .text fact: main: subu sw sw addu $sp, $sp, 32 $ra, 20($sp) $fp, 16($sp) $fp, $sp, 32 li jal $a0, 10 fact la $a0, $LC move $a1, $v0 jal printf lw lw addu jr subu sw sw addu sw $sp, $sp, 32 $ra, 20($sp) $fp, 16($sp) $fp, $sp, 32 $a0, 0($fp) # save arg, n lw bgtz li j $v0, 0($fp) $v0, $L2 $v0, 1 $L1 # load n lw subu move jal lw mul $v1, 0($fp) $v0, $v1, 1 $a0, $v0 fact $v1, 0($fp) $v0, $v0, $v1 # load n # compute n-1 # move arg to $a0 lw lw addu jr $ra, 20($sp) $fp, 16($sp) $sp, $sp, 32 $ra # return 1 # to return code $L2: $ra, 20($sp) $fp, 16($sp) $sp, $sp, 32 $ra # load n # return val in $v0 $L1: .rdata $LC: .ascii “The answer is %d\n\000” 13 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 14 A Real Procedure Call - non leaf float nonleaf(I, j) int I, *j { double atof(); int temp; .globl nonleaf .ent nonleaf # for debugger nonleaf: subu sw .mask .frame temp = i - *j; if (I < *j) temp = -temp; return atof(temp); } $L1: lw subu $sp, 24 # create stack frame $ra, 20($sp) 0x80000000, -4 # only $31 is saved at $sp +24 -4 $sp, 24, $31 # for debugger: #frame size, return address $v0, 0($a1) # args are in $ao and $a1 $v1, $a0, $v0 # temp in #v1 bge negu $a0, $v0, $L1 $v1, $v1 move jal cvt.s.d lw addu jr .end $a0, $v1 atof $f0, $f0 $ra, 20($sp) $sp, 24 $ra nonleaf # temp = -temp; # call atof #restore ra # for debugger 15 A Real Procedure Call - leaf w/o stack int leaf(p1, p2) int p1, p2; { return (p1 > p2) p1: p2; } .globl leaf .ent nonleaf # for debugger nonleaf .frame $sp, 0, $31 $L1: $L2: ble move b $a0, $a1, $L1 $v0, $a0 $L2 move $v0, $a1 jr .end $ra leaf # for debugger: #frame size, return address # args are in $ao and $a1 16 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 s exp significand 17 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 ! 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 ×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 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) FPR 0 FPR 2 least FGR 0 most FGR 1 least FGR 2 most FGR 3 FGR 4 FGR 27 FPR 28 FPR 30 least FGR 28 most FGR 29 least FGR 30 most FGR 31 21 MIPS FP Instructions OP Description l.d $f4, addr load d-word to $f4 s.s $f4, addr store s-word from $f4 mov.d $f4, $f6 move word ctc1 $7, rd cfc1 $6, rd move control word to FPA move from cvt.s.fmt cvt.d.fmt cvt.w.fmt convert to single FP convert to double FP convert to fixed point exmaple: fmt: Format s: single precision d: double precision w: binary fixed point (integer) cvt.s.w FRdest, FRsrc cvt.d.w FRdest, FRsrc cvt.d.s FRdest, FRsrc 22 MIPS FP Instructions (2) OP add.fmt sub.fmt mul.fmt div.fmt abs.fmt mov.fmt neg.fmt Description 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 compare and branch c.cond.format bc1t bc1f FR1, FR2 label 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 exmaple: c.eq.s FRsrc1, FRsrc2 bc1t xxx 23 Exception user program Exception: System Exception Handler 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 24 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 25 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 26 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 I/O device request External Invoke OS from user program Arithmetic overflow Using an undefined instruction Hardware malfunctions From where? Interrupt Internal Internal Internal Either MIPS terminology Exception Exception Exception Exception or Interrupt 27 Addressing the Exception Handler • Traditional Approach: Interupt Vector – PC <- MEM[ IV_base + cause || 00] – 370, 68000, Vax, 80x86, . . . iv_base • MIPS Approach: fixed entry – PC <- EXC_addr – Actually very small table • RESET entry • TLB • other cause handler code handler entry code iv_base cause 28 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 29 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) 30 Cause Register Status • • 15 10 5 Pending Code 2 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 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 mfc0 $k1 $14 sgt bgtz # move cause register to $k0 # move EPC to $k1 # k reg need not be saved (users don’t use them) $v0 $k0 0x44 # is this correct? $v0 done # if it is an interrupt, ignore mov mov jal $a0, $k0 $a1, $k1 print_excp done: lw $a0 save0 lw $a1 save1 addiu $k1 $k1 4 rfe jr $k1 .kdata save0: .word 0 save1: .word 0 # will return to saved PC + 4 # restore interrupted state 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. Unused 1 1 Receiver control (0xffff0000) Interrupt enable Unused Ready 8 Receiver data (0xffff0004) Received byte 34 SPIM IO (cont’d) Unused 1 1 Transmitter control (0xffff0008) Interrupt enable Unused Ready 8 Transmitter data (0xffff000c) Transmitted byte • 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 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 36 SPIM syscall service call code print integer print float print double print string read integer read float read double read string sbrk exit 1 2 3 4 5 6 7 8 9 10 arguments int in $a0 float in $a0 double in $a0 addr of string in $a0 move li $a0, $t2 $v0, 1 syscall results int in $v0 float in $v0 double in $v0 $a0=buffer, $a1=length $a0 = amount addr in $v0 37