Procedure Call - PowerPoint PPT Presentation

1 / 15
About This Presentation
Title:

Procedure Call

Description:

... on the stack and as each one returns, they must remove the values they piled on. ... To get data from the stack, you pull a plate from the pile ... – PowerPoint PPT presentation

Number of Views:46
Avg rating:3.0/5.0
Slides: 16
Provided by: ahmadrh
Category:
Tags: call | piled | procedure

less

Transcript and Presenter's Notes

Title: Procedure Call


1
Procedure Call
2
  • Consider a very long main program as shown in the
    following figure
  • Assume that when the processor reaches point A,
    it must calculate the sine of angle which is
    stored in register a0
  • Also assume that a program
  • exists for calculating the sine
  • of a number
  • As shown in the figure (Sec b)
  • the sine program can simply
  • be embedded in the main
  • program starting at point A.
  • This may be the most
  • economical way, in terms of
  • program speed, to do it if
  • the sine only needs to be
  • calculated once

Main Program
Main Program
Main Program
A
A
A
B
Sine Program
BRA
B
B
a) Main Program
b) Embedded sine program
c) Sine subprogram
3
  • Now, however, suppose the sine must also be
    calculated when the processor reaches point B. We
    could again embed the sine program at this point
    but then it would be duplicating code that was
    already written.
  • This is not very efficient in terms of required
    memory space for the program code
  • Alternatively, we could make the sine program a
    subprogram, as shown in figure (part c) .
  • When the processor reached point A, in the main
    program, it could unconditionally jump to the
    sine program and then when it is finished, jump
    back to main
  • Again, when it reached point B, in the main
    program it could jump to the sine program
    however, the problem occur when it is finished.
    The jump instruction, that is the last
    instruction in the sine subprogram would take the
    processor back to the instruction following point
    A

4
  • The problem posed in the last two pages can be
    solved by making the sine program a subroutine
  • A subroutine is a subprogram that has the feature
    of being able to call any number times and of
    being able to return the processor to the correct
    location when execution of the subroutine is
    finished

5
  • Subroutine Instructions
  • A subroutine can be created by using jal (jump
    and link) instruction
  • jal label
  • Instruction jal, does two things
  • First, saves address of the next instruction into
    register ra (register 31),which is the return
    address register
  • Second, it makes the processor jump to the given
    address by label
  • Once this instruction is used, the subroutine is
    executed
  • When the subroutine ends, the processor can be
    returned to the proper location by using the jr
    instruction

6
  • The syntax for jr instruction is
  • jr ra
  • jr ra instruction, in particular, jumps back to
    the return address that was saved in ra.

jr ra
7
This is an example of simple subroutine. It
only shows how a routine is created and how
the control flows from the main program to a
routine and goes back to the main
program .data str1 .asciiz "\n Before going to
subroutine" str2 .asciiz "\n In
subroutine" str3 .asciiz "\n Coming out of
subroutine" .text main li v0, 4 la a0,
str1 syscall jal sub1 jumping to sub1 and
automatically saving the address of next
instruction to ra li v0, 4 la a0,
str3 syscall j exit sub1 li v0,
4 la a0, str2 syscall jr ra after
finishing sub1 this instruction causes the
control jump back to the main program by
jumping to the address stored in
ra exit li v0, 10 syscall
8
  • Conventions for Register Usage for Subroutine
  • In the execution of a procedure, the program must
    follow these six steps
  • Place parameters in a place where the procedure
    can access them
  • Transfer the control to the procedure
  • Obtain the storage resources needed for the
    procedure (stack)
  • Perform the desired task
  • Place the result value in a place where the
    calling program can access it
  • Return control to one instruction after the point
    of origin where the subroutine was called

9
  • In order to follow these six steps, the MIP
    software allocates the following of 32 registers
    for procedure calling.
  • a0 .. a3 argument registers in which to pass
    parameters
  • v0 and v1 are two value registers in which to
    return values from subroutine to caller program
  • t0 .. t9 are the temporary registers that may
    be used freely by a routine. Their values may not
    be saved when the control goes to the subroutine
    or when the subroutine ends and the control is
    back to the caller program
  • s0 .. s7 are the registers that must be
    preserved on a procedure call. When the control
    jumps to the subroutine, their value should be
    stored and before ending the subroutine their
    value should be restored
  • sp (29), is the stack pointer, which points to
    the last location on the stack.

10
  • Where do we save the values of registers s0 ..
    s7 upon jumping to a subroutine?
  • There is a special part of memory called stack.
  • Stack is a an area of RAM that is used for
    special functions such as storing register
    values, and storing return addresses of
    subroutines
  • Stack is known as a Last-In-First-Out (LIFO)
    data structure
  • This is ideal for routines as typically they need
    to save some registers on the way in and retrieve
    the values on the way out of subroutines
  • As functions call each other, they pile up more
    items on the stack and as each one returns, they
    must remove the values they piled on.

11
(No Transcript)
12
  • This is such a useful method, that all operating
    system setup space as stack in RAM for programs
    and initialize a stack pointer pointing to stack
    area.
  • By design, computer stacks grow downward from
    higher to lower addresses. The stack pointer
    always points to the top word on the stack.
  • Some people think of the stack as a pile of
    plates. Each plate representing a byte of data
  • To put data into the stack you push the plate
    onto the pile. To get data from the stack, you
    pull a plate from the pile
  • These operations are called push, and pop
  • Push To place a new item on the stack, the stack
    pointer is first decremented , and then the item
    is stored at the new location
  • Pop To remove an item from the stack, the value
    pointed to by the stack pointer is copied
    (usually into a register), and then the stack
    pointer is incremented , exactly reversing the
    push operation

13
  • This figure shows the operations on a stack. In
    any 32-bit system (like MIPS), a word is always
    pushed or popped, so sp is always kept a
    multiple of 4.

14
  • To push a certain register, for example register
    s0 to the stack
  • First make room in the stack for storage of the
    register
  • Second store the register on the stack.
  • sub sp,sp,4
  • sw s0,0(sp)
  • Pop Reverses this order,
  • First retrieving the value previously pushed ,
  • Second increment the stack pointer.
  • lw s0,0(sp)
  • addi sp,sp,4

15
  • As you can see in this figure ,stack is a
    Last-In-First-Out (LIFO) data structure.
  • This means you need to pop items in the reverse
    order they were pushed.
  • It is generally important to pop the items that
    were pushed previously

Stack is LIFO
Pop B
Push B
Push A
Pop A
B
As before
sp
A
A
A
PreviousContent
Write a Comment
User Comments (0)
About PowerShow.com