Saving and Restoring Registers - PowerPoint PPT Presentation

1 / 15
About This Presentation
Title:

Saving and Restoring Registers

Description:

Saving and Restoring Registers – PowerPoint PPT presentation

Number of Views:17
Avg rating:3.0/5.0
Slides: 16
Provided by: vishwani1
Category:

less

Transcript and Presenter's Notes

Title: Saving and Restoring Registers


1
Saving and Restoring Registers
  • Vishwani D. Agrawal
  • James J. Danaher Professor
  • Department of Electrical and Computer Engineering
  • Auburn University
  • http//www.eng.auburn.edu/vagrawal
  • vagrawal_at_eng.auburn.edu

2
Memory and Registers
Memory
0 4 8 12 . 4n . . . 4n4k .
byte addr.
Word 0
Register 0
zero
Word 1
Register 1
Word 2
Register 2
Register 3
v0 (Word n)
Register 4
4n
v1 (Word n1)
Register 5
k
vk (Word nk)
Register 31
jump addr.
vk1 (Word nk1)
3
Data Placement in Memory
s0-s7 saved in Stack during procedure
sp
Stack
Dynamic data
gp
Static data
Machine code (text)
pc
Reserved
0
4
Register Convention
The following convention is understood and used
by all calling (caller) and called (callee)
programs.
Preserved Not preserved Saved reg. s0 -
s7 Temp. reg. t0 - t9 Stack pointer reg.
sp Argument reg. a0 - a3 Return addr. Reg.
ra Return value reg. v0 - v1 Stack above the
stack pointer Stack below the stack pointer
5
Caller Actions
  • Put parameters values in argument registers, a0
    - a3
  • Save registers in memory
  • Jump and link to calee
  • Put return address (PC4) in ra
  • jal calees address
  • On return, restore saved registers from memory
  • Find returned parameters in value registers, v0
    - v3

6
Caller Saving and Restoring
  • Before the call
  • Push the stack pointer
  • Save those temporary registers in the memory,
    whose contents will be required later calee may
    change these.
  • After the call (on return)
  • Restore all saved registers
  • Pop the stack pointer

7
Calee Actions
  • Save any registers among s0 - s7 that might be
    changed caller expects them to be unchanged.
  • Use arguments in registers a0 - a3
  • Place results in value registers v0 - v3
  • Restore saved registers from memory
  • Leave stack pointer in the same state as when the
    call was initiated.
  • Jump to address in register ra ( jr ra )

8
Calee Example
  • Procedure proc uses register s0
  • Assembly code
  • proc
  • addi sp, sp, -4 push stack down
  • sw s0, 0(sp) save s0
  • .
  • Assembly code of proc
  • .
  • lw s0, 0(sp) restore s0
  • addi sp, sp, 4 pop 1 word off stack
  • jr ra return

9
When Calee becomes a Caller
  • Saving and restoring of saved and temporary
    registers is done same as described before.
  • May reuse argument registers (a0 - a3) they
    are saved and restored as necessary.
  • Must reuse ra its content is saved in memory
    and restored on return.

10
Example Program?Callee A?Calee B
Main program Procedure A(arg1) Procedure B(arg1)
. . addi sp, sp,
-8 sw ra, 4(sp) sw a0, 0(sp) addi a0,
zero, 7 jal B lw a0, 0(sp) lw ra,
4(sp) addi sp, sp, 8 .
. jr ra
. . addi a0,
zero, 3 jal A . .
. . . jr ra
11
Example A Recursive Procedure
Int fact (int n) if (n lt 1) return (1) else
return (n fact (n-1))
This procedure returns factorial of integer n,
i.e., n! n (n-1) (n-2) . . . 2 1 n
(n-1)! Boundary case, 0! 1
12
What to Save?
  • Recursive procedure will call itself. So,
  • Must reuse return address register ra
  • Must change argument register a0
  • These two registers must be saved.

13
Assembly Code for fact
fact addi sp, sp, -8 adjust stack for two
items sw ra, 4(sp) save return address of
caller sw a0, 0(sp) save caller supplied
argument n slti t0, a0, 1 t0 1, if n
0 beq t0, zero, L1 go to L1, if n
1 addi v0, zero, 1 return 1 addi sp,
sp, 8 no need to restore registers,
since none was changed, but
must restore stack pointer jr ra return
to caller instruction after jal
14
Assembly Code Continued
L1 addi a0, a0, -1 set a0 to n-1 jal
fact call fact with argument n-1 lw a0,
0(sp) on return from fact, restore n lw
ra, 4(sp) restore return address addi sp,
sp, 8 adjust stack pointer mul v0, a0,
v0 n! n (n-1)! jr ra return to
caller
15
Execution of fact for n 1
Call sequence Caller a0 ra v0 1 Program n
PC4 n(n-1)! 2 fact n-1 L18 (n-1)(n-2)!
3 fact n-2 L18 (n-2)(n-3)! fact . fact
. fact . n-2 fact 3 L18 32
6 n-1 fact 2 L18 21 2 n fact 1 L18 1
1 1 n1 fact 0 L18 1
Write a Comment
User Comments (0)
About PowerShow.com