1 / 20

6.1 Simple Procedure Calls

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).

magee
Download Presentation

6.1 Simple Procedure Calls

An Image/Link below is provided (as is) to download presentation Download Policy: Content on the Website is provided to you AS IS for your information and personal use and may not be sold / licensed / shared on other websites without getting consent from its author. Content is provided to you AS IS for your information and personal use only. Download presentation by click this link. While downloading, if for some reason you are not able to download a presentation, the publisher may have deleted the file from their server. During download, if you can't get a presentation, the file might be deleted by the publisher.

E N D

Presentation Transcript


  1. 1

  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.

  3. Put argument in places known to the procedue (register $a0 - $a3). Transfer control to the procedure, saving the return address (jal). Acquire storage space, if required, for use by the procedure. Perform the desired task. Put results in place known to the calling program (register $v0 - $v1) Return control to calling point (jr) Applicable to procedure call that invokes maximal 4 words of arguments and 2 words of results. procedure call

  4. 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. Procedure convention

  5. 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 Figure 6.2 Example of nested procedure calls.

  6. 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 Figure 6.3 Overview of the memory address space in MiniMIPS.

  7. Push: addi $sp, $sp,-4 sw $t4, 0($sp) Pop: lw $t5, 0($sp) addi $sp, $sp, 4 Figure 6.4 Effects of push and pop operations on a stack.

  8. 6.3 Parameters and Results • How can we pass more than four input parameters to a procedure or receive more than two results? • Where does a procedure save its own parameters and intermediate results when calling another procedure (nested calls)? Figure 6.5 Use of the stack by a procedure.

  9. 6.4 Data Types MiniMIPS has the following data types:

  10. Singed integer: 2’s-complement

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

  12. 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]; Figure 6.6 Load and store instructions for byte-size data elements.

  13. 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. 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.

  14. 6.5 Arrays and Pointers • 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. • Pointer: use a register that points to the list element being examined and update it in each step to point to the next element. Figure 6.8 Using the indexing method and the pointer updating method to step through the elements of an array.

  15. Example 6.4 Selection sort using a max-finding procedure Figure 6.9 One iteration of selection sort.

  16. 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) Figure 6.10 The multiply (mult) and divide (div) instructions of MiniMIPS.

  17. mfhi $t0 # set $t0 to (Hi) mflo $t0, $s1 # set $t0 to (Lo) Figure 6.11 MiniMIPS instructions for copying the contents of Hi and Lo registers into general registers.

  18. Shift instructions 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) Figure 6.12 The four logical shift instructions of MiniMIPS.

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

More Related