6.1 simple procedure calls

20
1

Upload: magee

Post on 08-Jan-2016

36 views

Category:

Documents


0 download

DESCRIPTION

1. 6.1 Simple Procedure Calls. jal: performs the control transfer (unconditional jump) to the starting address of procedure, while also saving the return address in $ra. Put argument in places known to the procedue (register $a0 - $a3). - PowerPoint PPT Presentation

TRANSCRIPT

Page 1: 6.1 Simple Procedure Calls

1

Page 2: 6.1 Simple Procedure Calls

2

6.1 Simple Procedure Calls

jal: performs the control transfer (unconditional jump) to the starting address of procedure, while also saving the return address in $ra.

Page 3: 6.1 Simple Procedure Calls

3

procedure call1. Put argument in places known to the procedue (register $a0 -

$a3).

2. Transfer control to the procedure, saving the return address (jal).

3. Acquire storage space, if required, for use by the procedure.

4. Perform the desired task.

5. Put results in place known to the calling program (register $v0 - $v1)

6. Return control to calling point (jr)

Applicable to procedure call that invokes maximal 4 words of arguments and 2 words of results.

Page 4: 6.1 Simple Procedure Calls

4

Procedure convention

• A procedure can use registers $v0 - $v1 and $t0 - $t9 freely without having to save their original contents.

• Division of responsibility between the calling and called program– Caller-saved registers: $v0 - $v1, $t0 - $t9

A calling program should not expect any values placed in these 12 registers to remain unchanged.

– Callee-saved registers: $a0 - $a3, $s0 - $s7, $gp, $sp, $fp, $ra

A procedure (callee program) that modifies any of these must restore them to their original values before terminating.

Page 5: 6.1 Simple Procedure Calls

5Figure 6.2 Example of nested procedure calls.

Nested Procedure call

1. Procedure abc puts arguments in $a0 - $a3 and save any useful data in $v0 - $v1, $t0 - $t9

2. After return from xyz, procedure abc may transfer to

Page 6: 6.1 Simple Procedure Calls

6Figure 6.3 Overview of the memory address space in MiniMIPS.

6.2 Using Stack for Data Storage

Conventions for using memory address space in MiniMIPS:

Second half: memory-mapped I/O

First half:

•First 1M words: system use

•Next 63M words: text of the program being executed

•Beginning at 0x10000000: program’s data segment

•Beginning 0x7ffffffc growing backward: stack

Page 7: 6.1 Simple Procedure Calls

7

Figure 6.4 Effects of push and pop operations on a stack.

Push: addi $sp, $sp,-4

sw $t4, 0($sp)

Pop: lw $t5, 0($sp)

addi $sp, $sp, 4

Page 8: 6.1 Simple Procedure Calls

8Figure 6.5 Use of the stack by a procedure.

6.3 Parameters and Results

1. How can we pass more than four input parameters to a procedure or receive more than two results?

2. Where does a procedure save its own parameters and intermediate results when calling another procedure (nested calls)?

Page 9: 6.1 Simple Procedure Calls

9

6.4 Data Types

MiniMIPS has the following data types:

Page 10: 6.1 Simple Procedure Calls

10

Singed integer: 2’s-complement

Page 11: 6.1 Simple Procedure Calls

11

Table 6.1 ASCII (American Standard Code for Information Interchange)1, 2

Page 12: 6.1 Simple Procedure Calls

12Figure 6.6 Load and store instructions for byte-size data elements.

Load byte, load byte unsigned, store byte

lb $t0,8($s3) # load rt with byte mem[8+$s3];# signed-extended to fill the register;

lbu $t0,8($s3) # load rt with byte mem[8+$s3];# zero-extended to fill the register;

sb $t0,8($s3) # store byte 0 of rt to mem[8+$s3];

Page 13: 6.1 Simple Procedure Calls

13

Figure 6.7 A 32-bit word has no inherent meaning and can be interpreted in a number of equally valid ways in the absence of other cues (e.g., context) for the intended meaning.

A bit string, stored in memory or in a register, has no inherent meaning. It can mean different things depending on how it is interpreted.

Page 14: 6.1 Simple Procedure Calls

14

Figure 6.8 Using the indexing method and the pointer updating method to step through the elements of an array.

6.5 Arrays and Pointers1. Index: use a register that holds the index i and increment the register in

each step to effect moving from element i of the list to element i+1.

2. Pointer: use a register that points to the list element being examined and update it in each step to point to the next element.

Page 15: 6.1 Simple Procedure Calls

15

Figure 6.9 One iteration of selection sort.

Example 6.4 Selection sort using a max-finding procedure

Page 16: 6.1 Simple Procedure Calls

16

Figure 6.10 The multiply (mult) and divide (div) instructions of MiniMIPS.

6.6 Additional Instructions

mult $s0, $s1 # set Hi, Lo to ($s0)×($s0)

div $s0, $s1 # set Hi to ($s0) mod ($s0)

# set Lo to ($s0) / ($s0)

Page 17: 6.1 Simple Procedure Calls

17

Figure 6.11 MiniMIPS instructions for copying the contents of Hi and Lo registers into general registers.

mfhi $t0 # set $t0 to (Hi)

mflo $t0, $s1 # set $t0 to (Lo)

Page 18: 6.1 Simple Procedure Calls

18

Figure 6.12 The four logical shift instructions of MiniMIPS.

sll $t0, $s1, 2 # $t0 = ($s1) left-shifted by 2 bits

srl $t0, $s1, 2 # $t0 = ($s1) right-shifted by 2 bits

sllv $t0, $s1, $s0 # $t0 = ($s1) left-shifted by ($s0)

srlv $t0, $s1, $s0 # $t0 = ($s1) right-shifted by ($s0)

Shift instructions

Page 19: 6.1 Simple Procedure Calls

19

Table 6.2 The 40 MiniMIPS instructions covered in Chapters 5–7.*

Page 20: 6.1 Simple Procedure Calls

20