1 / 12

Subroutine Call

Callers and Callees Many calls but one return Subroutine Linkage Simple Linkage convention Stack-based Linkage Convention Central Connecticut State University, MIPS Tutorial. Chapters 26, 27. Subroutine Call. What is the subroutine ?.

gfarrell
Download Presentation

Subroutine Call

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. Callers and Callees Many calls but one return Subroutine Linkage Simple Linkage convention Stack-based Linkage Convention Central Connecticut State University, MIPS Tutorial. Chapters 26, 27. Subroutine Call

  2. What is the subroutine ? • All high level languages have the concept of a subroutine(sometimes called procedure, function, or method). • A subroutine is a logical division of the code that may be regarded as a self-contained operation - a module that is independent of the rest of the code.. • A subroutine might be executed several times with different data as the program executes.

  3. Do we need subroutines ? Read Integer Subroutine “Enter Integer“ 1234 Read this integer using trap handler • Below is the example of simple case to use subroutines • The program reads three integers from the user and computes the sum. • The outline of the program is: • read first integer • read second integer • read third integer • compute the sum • write out the result   • Reading an integer could be a subroutineas it includes repeated operations and it’s not effective to use those operations several times in the main program: • Print prompt “Enter Integer” • Read Integer using Trap Handler

  4. How to organize subroutine in assembly ? J ? Caller Callee Can we use “J” instruction ? If the main routine needs to start up ("call") a subroutine “sub”it can jump to it with a jinstruction. At the end of the subroutine, control can be returned with another “j”instruction. The subroutine returns to a statement in main labeled “ret”. The subroutine is called at just one point in main and it returns to an address a few instructions after that point. The subroutine is only used oncein the main program because it always returns to the same location.

  5. Many Calls but One Return • We need to jump but have a linkto jump back. The main routine is written to call a useful subroutine sub at several locationsin the code. But sub is written to return to only one location. In the past, before the concept was completely understood, hardware support for subroutines was missing. Various trickswere used to implement the idea. What is needed is a method that sends the return addressto the subroutine. When the subroutine finishes, it passes control to that return address.

  6. The jal Instruction ( jump and link ) • jalsub • $ra <― PC+4 $ra <― address 8 bytes away from the jal • PC <― sub load the PC with the subroutine entry point The register that is used for linkage is register $31, which is called $raby the extended assembler. It holds the return addressfor a subroutine. The instruction that puts the return address into $ra is (usually) the jal instruction.

  7. Jal example Fetch: When the jal is fetched the PC has 0x00400014. Increment: The PC is incremented to 0x00400018. Execute: $ra <― 0x0040018+4 PC <― 0x00400100 The jal is at address 0x00400014. The return address is 0x0040001C which is the address of the jal plus eight.

  8. Jumping and returning problem is resolved • Are there remain other problems ? • Limited amount of registers - Calling Convention ? The diagram shows the subroutine sub being called from three different points in the main routine. Each time the subroutine is called it returns to the appropriate return address.

  9. Calling convention A calling convention is an agreement about how subroutines are called and how control is returned to the caller. how data is passed into and out of the subroutine How the subroutine is made independent from the rest of the code Mostly this is an agreement about how software will work. Different languages and different operating systems for the same processor usually have different calling conventions.

  10. Simple subroutine linkage convention • $t0 - $t9 — The subroutine is free to change these registers. • $s0 - $s7 — The subroutine must not change these registers. • $a0 - $a3 — These registers contain arguments for the subroutine. The subroutine can change them. • $v0 - $v1 — These registers contain values returned from the subroutine. Since a subroutine may not call another subroutine, programs will consist of a main routine that calls any number of subroutines. • A subroutine is called using jal. • A subroutine will NOT call another subroutine. • The subroutine returns to its caller using jr $ra.

  11. Subroutine call example .text .globlpread pread: la $a0,prompt # print string li $v0,4 # service 4 syscall li $v0,5 # read int into $v0 syscall # service 5 jr $ra# return nop # .data prompt: .asciiz "Enter an integer: " jalpread # read first integer nop # move $s0,$v0 # save it in $s0  jalpread # read second integer nop # move $s1,$v0 # save it in $s1 jalpread # read third integer nop # move $s2,$v0 # save it in $s2 addu$s0,$s0,$s1 # compute the sum addu$a0,$s0,$s2 li $v0,1 # print the sum syscall

  12. Simple linkage problems • No recursive or nested call of subroutines • $s0 - $s7 — The subroutine must not change these registers so only the main program can use them. Recursive subroutines cannot use them. • $ra - register will be overwritten after the first call of the next nested subroutine and we’ll lose the return address for the previous subroutine. • $t0 - $t7 and other registers all the time will be erased by nested subroutines.

More Related