CENG 446 - PowerPoint PPT Presentation

About This Presentation
Title:

CENG 446

Description:

MIPS Procedure Calling ... Regular program flow is suspended and control is transferred to the procedure. ... place where the procedure can access them ... – PowerPoint PPT presentation

Number of Views:23
Avg rating:3.0/5.0
Slides: 17
Provided by: bhem9
Category:
Tags: ceng | procedure

less

Transcript and Presenter's Notes

Title: CENG 446


1
CENG 446 Advanced Computer Architectures
  • Dr. Brian T. Hemmelman
  • Chapter 2 Slides (Part 3)

2
MIPS Procedure Calling
  • Procedures or functions are very useful in
    programming allowing efficiency and code reuse.
  • Regular program flow is suspended and control is
    transferred to the procedure.
  • Once the procedure has completed its task,
    control must be returned to the proper location
    in the calling program.
  • This process often includes passing information
    into and returning information from the procedure
    to the calling program.

3
Procedure Calling Process
  • Put parameters in a place where the procedure can
    access them (registers).
  • Transfer control to the procedure.
  • Acquire the storage resources needed for the
    procedure.
  • Perform the desired task.
  • Put the result in a place where the calling
    program can access it (registers).
  • Return control to the point of origin, since a
    procedure can be called from several points in in
    a program.

4
Special Resources for Procedures
  • Because procedures are used so frequently there
    are specific instructions and hardware resources
    that support their execution.
  • a0 - a3 four argument registers in which to
    pass parameters.
  • v0 - v1 two value registers in which to return
    values.
  • ra one return address register to return to the
    point of origin.
  • jal jump-and-link instruction (transfers program
    control to procedure but also saves proper return
    address in ra register).
  • jr jump register instruction (transfers program
    control back to address located in a register,
    e.g. register ra).

5
Procedures More Details
  • a0 - a3 Often we need to pass data into a
    procedure that it will use in completing its task
    (the argument list in your function call). These
    registers are where the caller can put the data
    and the callee can look for it.
  • v0 - v1 By the same token the procedure may
    have answers or data that need to be returned to
    the calling program. These registers are where
    the callee can place the return values and the
    caller can look for them.

6
Procedures More Details
  • To transfer program execution to the procedure,
    the caller must perform a jump.
  • However the difference in this case (as compared
    to a branch for instance) is that once the
    procedure has completed its task we need to
    return back to the same point in the calling
    program.
  • This return process is set up by using the
    jump-and-link instructionjal ProcedureAddress
  • The link is formed by having the return location
    address stored into the ra register at the same
    time the jump to the procedure instructions
    occurs. Thus, the procedure has a formal process
    by which to return control back to the calling
    program. This is accomplished by calling the
    jump register instruction once the procedure is
    completedjr ra

7
Procedures More Details
  • The Program Counter (PC) tells the hardware (and
    software) where the next program instruction to
    be executed is located.
  • Given that our instructions are 32-bit quantities
    (4-byte words) the PC normally updates its
    contents as PC PC 4 to point to the next
    sequential instruction.
  • When branches or jumps are executed the PC is
    instead loaded with the address pointed to by the
    branch or jump instruction.
  • When calling a procedure then, instead of going
    to the next instruction at PC 4 we jump to the
    address of the first procedure instruction.

8
Procedures More Details
  • Clearly, when done with the procedure, we wish to
    return control to the next instruction after the
    procedure callwhich is located at PC 4.
  • Thus, PC 4 is the address which should be
    stored in register ra when a jal instruction
    is performed.
  • The corresponding jr ra instruction that is
    executed at the end of the procedure will return
    program flow to the instruction at PC 4.

9
Speaking of Registers
  • s0 - s7 are saved registers. That is, they
    likely hold the values of variables being
    operated on in the calling program.
  • The procedure therefore should not overwrite or
    destroy the values contained in these registers.
  • To maintain the integrity of the data in s0-s7
    the procedure should either not use them, or, if
    it uses them, should save and then restore the
    contents for the calling program.

10
Speaking of Registers
  • t0 - t9 are temporary registers intended to
    hold intermediate results so they are available
    for the procedure to also use as a work space.
    Overwriting their contents will not affect the
    calling program.
  • Some procedures may need to utilize more data or
    arguments than can be stored in a0 - a3.
  • In this case we can place lots of information on
    the stack, which is a dedicated area of main
    memory that can be used as a scratchpad or work
    area for program data.

11
Stack Pointer Register
  • MIPS includes register sp to hold the memory
    address of the stack pointer (the location of the
    most recent word added to the scratchpad).
  • Putting additional data onto the stack is called
    a push.
  • Pulling data off of the stack is called a pop.
  • In MIPS, the stack pointer starts near the high
    end of memory addresses. Data that is pushed
    onto the stack actually goes into lower memory
    addresses, with the stack pointer getting
    decreased. When data is popped off of the stack,
    the stack pointer value will increase to higher
    memory addresses.

12
Books example, pages 114-115
  • C source codeint leaf_example (int g, int h,
    int i, int j) int f f (g h) (i
    j) return f
  • Assume the values of g, h, i, and j have already
    been moved into a0 - a3 before we jump to the
    procedure.
  • Variable f will be located at s0 during the
    procedure so we need to save the current contents
    of s0 on to the stack.
  • The intermediate results of the calculations need
    to use t0 and t1. While these are temporary
    registers and shouldnt need to be saved on the
    stack if being properly used, the example in the
    book does so to help illustrate the stack
    operation.

13
Books example, pages 114-115
  • MIPS assembly codeleaf_example addi sp,
    sp, -12 adjust the stack pointer to make
    room for s0, t0, t1 on stack sw t1, 8(sp)
    push t1 onto stack sw t0, 4(sp) push t0
    onto stack sw s0, 0(sp) push s0 onto
    stack add t0, a0, a1 compute g
    h add t1, a2, a3 compute i j sub s0,
    t0, t1 f t0 - t1 add v0, s0, zero
    place f in v0 for return lw s0, 0(sp) pop
    s0 off of stack lw t0, 4(sp) pop t0 off of
    stack lw t1, 8(sp) pop t1 off of
    stack addi sp, sp, 12 move sp back up 3
    words jr ra return to calling program

14
Books example, pages 114-115
  • Stack contents before during and after the
    procedure.

15
Books example, pages 114-115
  • More efficient MIPS code leaf_example addi s
    p, sp, -4 adjust the stack pointer to
    make room for s0 on stack sw s0,
    0(sp) push s0 onto stack add t0, a0, a1
    compute g h add t1, a2, a3 compute i
    j sub s0, t0, t1 f t0 - t1 add v0,
    s0, zero place f in v0 for return lw s0,
    0(sp) pop s0 off of stack addi sp, sp, 4
    move sp back up 3 words jr ra return to
    calling program

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