Mehmet Can Vuran, Instructor University of Nebraska-Lincoln Acknowledgement: Overheads adapted from those provided by the authors of the textbook.

Slides:



Advertisements
Similar presentations
The University of Adelaide, School of Computer Science
Advertisements

COMP3221 lec16-function-II.1 Saeid Nooshabadi COMP 3221 Microprocessors and Embedded Systems Lectures 16 : Functions in C/ Assembly - II
Chapter 2 Instruction Set Architecture
1 Lecture 4: Procedure Calls Today’s topics:  Procedure calls  Large constants  The compilation process Reminder: Assignment 1 is due on Thursday.
Procedures in more detail. CMPE12cGabriel Hugh Elkaim 2 Why use procedures? –Code reuse –More readable code –Less code Microprocessors (and assembly languages)
Procedure Calls Prof. Sirer CS 316 Cornell University.
Computer Architecture CSCE 350
CPS3340 COMPUTER ARCHITECTURE Fall Semester, /17/2013 Lecture 12: Procedures Instructor: Ashraf Yaseen DEPARTMENT OF MATH & COMPUTER SCIENCE CENTRAL.
Ch. 8 Functions.
Apr. 12, 2000Systems Architecture I1 Systems Architecture I (CS ) Lecture 6: Branching and Procedures in MIPS* Jeremy R. Johnson Wed. Apr. 12, 2000.
The University of Adelaide, School of Computer Science
ECE 353 Introduction to Microprocessor Systems Michael G. Morrow, P.E. Week 6.
Mehmet Can Vuran, Instructor University of Nebraska-Lincoln Acknowledgement: Overheads adapted from those provided by the authors of the textbook.
Informationsteknologi Saturday, September 29, 2007 Computer Architecture I - Class 41 Today’s class More assembly language programming.
Stacks and HeapsCS-3013 A-term A Short Digression on Stacks and Heaps CS-3013 Operating Systems A-term 2008 (Slides include materials from Modern.
1 Storage Registers vs. memory Access to registers is much faster than access to memory Goal: store as much data as possible in registers Limitations/considerations:
Procedures in more detail. CMPE12cCyrus Bazeghi 2 Procedures Why use procedures? Reuse of code More readable Less code Microprocessors (and assembly languages)
Digression on Stack and Heaps CS-502 (EMC) Fall A Short Digression on Stacks and Heaps CS-502, Operating Systems Fall 2009 (EMC) (Slides include.
1 Chapter 7: Runtime Environments. int * larger (int a, int b) { if (a > b) return &a; //wrong else return &b; //wrong } int * larger (int *a, int *b)
CS 536 Spring Code generation I Lecture 20.
Run time vs. Compile time
Stacks and HeapsCS-502 Fall A Short Digression Stacks and Heaps CS-502, Operating Systems Fall 2007 (Slides include materials from Operating System.
COMPUTER ARCHITECTURE & OPERATIONS I Instructor: Hao Ji.
Runtime Environments What is in the memory? Runtime Environment2 Outline Memory organization during program execution Static runtime environments.
13/02/2009CA&O Lecture 04 by Engr. Umbreen Sabir Computer Architecture & Organization Instructions: Language of Computer Engr. Umbreen Sabir Computer Engineering.
Runtime Environments Compiler Construction Chapter 7.
Programming Language Principles Lecture 24 Prepared by Manuel E. Bermúdez, Ph.D. Associate Professor University of Florida Subroutines.
Slides revised 3/25/2014 by Patrick Kelley. 2 Procedures Higher Level languages have adopted a standard Referred to as C-style calling Uses the stack.
CPSC 388 – Compiler Design and Construction Runtime Environments.
Lecture 18: 11/5/2002CS170 Fall CS170 Computer Organization and Architecture I Ayman Abdel-Hamid Department of Computer Science Old Dominion University.
1 Control Abstraction (Section ) CSCI 431 Programming Languages Fall 2003 A compilation of material developed by Felix Hernandez-Campos and Michael.
Procedure Calls and the Stack (Lectures #18) ECE 445 – Computer Organization The slides included herein were taken from the materials accompanying Computer.
Procedures. Why use procedures? ? Microprocessors (and assembly languages) provide only minimal support for procedures Must build a standard form for.
Adapted from Computer Organization and Design, Patterson & Hennessy, UCB ECE232: Hardware Organization and Design Part 7: MIPS Instructions III
Lecture 19: 11/7/2002CS170 Fall CS170 Computer Organization and Architecture I Ayman Abdel-Hamid Department of Computer Science Old Dominion University.
Procedure Basics Computer Organization I 1 October 2009 © McQuain, Feng & Ribbens Procedure Support From previous study of high-level languages,
Chapter 2-4 Subroutines and Stacks Subroutines Parameter Passing Stack Frame Additional Instructions Next Lecture   Chapter 3: ARM Instruction Set Architecture.
Computer Architecture CSE 3322 Lecture 4 crystal.uta.edu/~jpatters/cse3322 Assignments due 9/15: 3.7, 3.9, 3.11.
ITCS 3181 Logic and Computer Systems 2015 B. Wilkinson Slides4-2.ppt Modification date: March 23, Procedures Essential ingredient of high level.
CSC 8505 Compiler Construction Runtime Environments.
ITEC 352 Lecture 19 Functions in Assembly. Functions + Assembly Review Questions? Project due on Friday Stacks Function activation / deactivation.
Intermediate Representation II Storage Allocation and Management EECS 483 – Lecture 18 University of Michigan Wednesday, November 8, 2006.
Computer Architecture CSE 3322 Lecture 4 Assignment: 2.4.1, 2.4.4, 2.6.1, , Due 2/10/09
RUNTIME ENVIRONMENT AND VARIABLE BINDINGS How to manage local variables.
CPS 4150 Computer Organization Chapter 2-2 Fall 2006 Ching-Song Don Wei.
Function Calling. Mips Assembly Call and Return Steps for procedure calling –Save the return address –Jump to the procedure (function) –Execute the procedure.
LECTURE 19 Subroutines and Parameter Passing. ABSTRACTION Recall: Abstraction is the process by which we can hide larger or more complex code fragments.
Function Calls in Assembly MIPS R3000 Language (extensive use of stack) Updated 7/11/2013.
Computer Architecture & Operations I
Rocky K. C. Chang Version 0.1, 25 September 2017
Computer Science 210 Computer Organization
Computer structure: Procedure Calls
CSCI206 - Computer Organization & Programming
Procedures (Functions)
Chapter 9 :: Subroutines and Control Abstraction
CSCI206 - Computer Organization & Programming
Chap. 8 :: Subroutines and Control Abstraction
Chap. 8 :: Subroutines and Control Abstraction
MIPS Instructions.
The University of Adelaide, School of Computer Science
MIPS Functions.
Program and memory layout
Procedures and Calling Conventions
Systems Architecture I
Program and memory layout
Runtime Environments What is in the memory?.
Computer Architecture
Program and memory layout
Where is all the knowledge we lost with information? T. S. Eliot
Program and memory layout
Presentation transcript:

Mehmet Can Vuran, Instructor University of Nebraska-Lincoln Acknowledgement: Overheads adapted from those provided by the authors of the textbook

 A stack is a list of data elements where elements are added/removed at top end only  Also known as pushdown stack or last-in-first-out (LIFO) stack  We push a new element on the stack top or pop the top element from the stack  Programmer can create a stack in the memory  There is often a special processor stack as well 2

 Processor has stack pointer (SP) register that points to top of the processor stack  Push operation involves two instructions: SubtractSP, SP, #4 StoreRj, (SP)  Pop operation also involves two instructions: LoadRj, (SP) AddSP, SP, #4  Maintenance of stack requires checking when the stack is empty or overflows – can be done by checking the SP against lower and upper bounds.  Among other things, the processor stack is useful in subroutine calls. 3

4 Stack pointer (SP) register (should) points to top of the processor stack

5 SP = 100 Initial Stack [something]

6 SP = 92 N #A Add couple input parameters [something]

7 #A N SP = 72 R1 R2 After register saves R3 R4 R [something]

8 #A Max Value SP = 72 R1 R2 Update stack with an output parameter R3 R4 R [something]

9 SP=92 After return from a function #A Max Value [something]

10 SP=100 After stack restore by calling program [something]

 You do not talk about…  Leave the stack as you found it *  * some exceptions apply 11

 Subroutine calls  Parameter passing 12

 During execution of Call instruction, PC upated to point to instruction after Call  Save this address for Return instruction to use  Simplest method: place address in link register (return address register)  Call instruction performs two operations: store updated PC contents in link register, then branch to target (subroutine) address  Return just branches to address in link register

14

16 SUB1SUB2SUB First Instr Call SUB Next Instr.. Return. 200 Call SUB2 204Next Instr First Instr.. Return

17 SUB1SUB2SUB First Instr Call SUB Next Instr.. Return. 200 Call SUB2 204Next Instr First Instr.. Return PC LINK

18 SUB1SUB2SUB First Instr Call SUB Next Instr.. Return. 200 Call SUB2 204Next Instr First Instr.. Return PC LINK

19 SUB1SUB2SUB First Instr Call SUB Next Instr.. Return. 200 Call SUB2 204Next Instr First Instr.. Return PC LINK

20 SUB1SUB2SUB First Instr Call SUB Next Instr.. Return. 200 Call SUB2 204Next Instr First Instr.. Return PC LINK

21 SUB1SUB2SUB First Instr Call SUB Next Instr.. Return. 200 Call SUB2 204Next Instr First Instr.. Return PC LINK

22 SUB1SUB2SUB First Instr Call SUB Next Instr.. Return. 200 Call SUB2 204Next Instr First Instr.. Return #Return PC LINK

23 SUB1SUB2SUB First Instr Call SUB Next Instr.. Return. 200 Call SUB2 204Next Instr First Instr.. Return 1604 PC LINK

24 SUB1SUB2SUB First Instr Call SUB Next Instr.. Return. 200 Call SUB2 204Next Instr First Instr.. Return 1604 PC LINK

25 SUB1SUB2SUB First Instr Call SUB Next Instr.. Return. 200 Call SUB2 204Next Instr First Instr.. Return 1604 PC LINK

26 SUB1SUB2SUB First Instr Call SUB Next Instr.. Return. 200 Call SUB2 204Next Instr First Instr.. Return 1604 PC LINK ERROR!

 When nested calls are made, the last call made is first one to be returned, i.e. the call- return protocol is Last-In First-Out (LIFO).  The nested calls can be arbitrarily deep, e.g. for recursive routines.  Hence, subroutines use the processor stack to store the Link register values during nested calls. 27

 Mechanism hidden in HLL but must be explicit in assembly language.  A program may call a subroutine many times with different data to obtain different results  Information exchange to/from a subroutine is called parameter passing  Pass input parameters before the subroutine Call receive output parameters after the call  Parameters may be passed & received in registers  Simple, but limited to available registers  Alternative: use stack for parameter passing, and also for local variables & saving registers 28

 Convert the example program of finding the max of N numbers to a subroutine:  Calling program provides N (number of elements) and the address of the first number (i.e., A[0])  Subroutine MAX(N, #A) returns the max value of N numbers stored starting at location #A.  Hence, there are two input parameters and one output parameter.  First, consider passing& receiving parameters in registers (we use the register assignments in the code we developed earlier to keep it simple)the code we developed earlier ▪ N in reg. R3 ▪ Address #A of A[0] in reg. R5 ▪ Max value returned in R1  Assume the values of N and A[0] are at location N and A. 29

 Calling Program  Load parameters N and A in the designated registers (R3 & R5)  Call MAX  Return value should be available in the designated register (R1)  Subroutine (MAX)  Push registers (other than parameter registers) used by the subroutine on the stack (i.e. push R2 and R4)  Find the value of the max element and place it in R1 (code identical to what we already saw except for the last two lines, since the value is to be returned in register R1)already saw  Pop registers from the stack (& update stack)  Return  Note: Since MAX does not call another subroutine (it is a leaf subroutine), it does not need to save the Link register on the stack. 30

 Calling Program  Load parameters in designated registers MoveR3, #N # Location of N LoadR3, (R3)# Value of N Load R5, #A # Address of first element  Call MAX Call MAX  Max value will be available in R1 after return 31

 Subroutine (MAX)  Push registers R2 and R4 used by the subroutine on the stack Subtract SP, SP, #8# Create room for two items Store R2, 4(SP)# Push R2 Store R4, (SP)# Push R4  Find the value of the max element before  Pop registers from the stack (& update stack) LoadR2, 4(SP)# Restore R2 LoadR4, (SP)# Restore R4 AddSP, SP, #8 # Update stack pointer  Return Return 32

33 Variable:Maxi#N = (Addr. N) A[i]#A= Addr. A[0] RegisterR1R2R3R4R5 MoveR3, #N LoadR3, (R3) MoveR5, #A Load R1, (R5) MoveR2, #1 Loop:AddR5, R5, #4 LoadR4, (R5) Branch_if_(R1>=R4) Skip MoveR1, R4 Skip:AddR2, R2, #1 Branch_if_(R3>R2) Loop MoveR2, #Max StoreR1, (R2) Assembly Code

 Break the Code into three pieces: 1. Becomes part of calling program for passing parameters 2. Becomes part of the subroutine; precede it with register saves; follow it with register restores and return 3. Becomes part of calling program to save returned value 34 Variable:Maxi#N = (Addr. N) A[i]#A= Addr. A[0] RegisterR1R2R3R4R5 MoveR3, #N LoadR3, (R3) MoveR5, #A Load R1, (R5) MoveR2, #1 Loop:AddR5, R5, #4 LoadR4, (R5) Branch_if_(R1>=R4) Skip MoveR1, R4 Skip:AddR2, R2, #1 Branch_if_(R3>R2) Loop MoveR2, #Max StoreR1, (R2) Assembly Code 1 2 3

35 Variable:Maxi#N = (Addr. N) A[i]#A= Addr. A[0] RegisterR1R2R3R4R5 MoveR3, #N LoadR3, (R3) MoveR5, #A MoveR2, #Max StoreR1, (R2) Calling Program Load R1, (R5) MoveR2, #1 Loop:AddR5, R5, #4 LoadR4, (R5) Branch_if_(R1>=R4) Skip MoveR1, R4 Skip:AddR2, R2, #1 Branch_if_(R3>R2) Loop MAX Subroutine

36 Variable:Maxi#N = (Addr. N) A[i]#A= Addr. A[0] RegisterR1R2R3R4R5 MoveR3, #N LoadR3, (R3) MoveR5, #A MoveR2, #Max StoreR1, (R2) Calling Program Load R1, (R5) MoveR2, #1 Loop:AddR5, R5, #4 LoadR4, (R5) Branch_if_(R1>=R4) Skip MoveR1, R4 Skip:AddR2, R2, #1 Branch_if_(R3>R2) Loop MAX Subroutine CallMAX MAX: Return

37 Variable:Maxi#N = (Addr. N) A[i]#A= Addr. A[0] RegisterR1R2R3R4R5 MoveR3, #N LoadR3, (R3) MoveR5, #A MoveR2, #Max StoreR1, (R2) Calling Program Load R1, (R5) MoveR2, #1 Loop:AddR5, R5, #4 LoadR4, (R5) Branch_if_(R1>=R4) Skip MoveR1, R4 Skip:AddR2, R2, #1 Branch_if_(R3>R2) Loop MAX Subroutine MAX:Subtract SP, SP, #8 Store R2, 4(SP) Store R4, (SP) LoadR2, 4(SP) LoadR4, (SP) AddSP, SP, #8 Return CallMAX

 Convert the code for the calling program and MAX to Nios II and verify it works as expected. 38

 Assume A calls B with input parameters i 1, …,i n and B returns resulting values o 1, …, o m. Assume m <= n (common case).  General Scheme:  A pushes i 1, …,i n on the stack and calls B.  B pushes any (saving) registers it would need for its computation on the stack; also the return-address register if it is not a leaf procedure.  B performs its computation and writes the resulting values o 1, …, o m on the stack (reusing the space used by input parameters).  B pops the saving registers from the stack and returns. 39

 Calling Program  Push input parameters onto stack (grows high to low), SubtractSP, SP, #8# Create room for 2 items LoadR2, #N# Address of N in R2 LoadR2, (R2)# Value of N in R2 StoreR2, 4(SP)# Push it on the stack LoadR2, #A# Get the second parameter in R2 StoreR2, (SP)# push it on the stack  Call subroutine CallMAX  Get max value from the stack and restores stack (pop input parameters) LoadR1,4(SP) # Result pushed on stack by MAX # Result overwrites value of N on stack AddSP, SP, #8 #Effectively pops stack 40

 Subroutine MAX  Save all regs. it will use (R1 – R5) on stack Subtract SP, SP, #20Create room for five items Store R1, 16(SP)Push R1 Store R2, 12(SP)Push R2 … Store R5, (SP)Push R5  Load input parameters from stack and compute sum LoadR3, 24(SP)Get N into R3 LoadR5, 20(SP)Get #A into R5  Body of the Subroutine:  Restore saved registers and pop stack, return LoadR1, 16(SP)Restore R1 LoadR2, 12(SP)R2… LoadR5, (SP)R5 AddSP, SP, #20Pop stack ReturnReturn to calling program 41

 Required Changes 1. Push R1–R5 on stack 2. R3 and R5 must be initialized from the stack 3. Last two lines must be replaced with pushing the result in R1 onto stack where input parameter N was passed. 4. Restore R1-R5 and pop stack 5. Return 42 MoveR3, #N LoadR3, (R3) MoveR5, #A Load R1, (R5) MoveR2, #1 Loop:AddR5, R5, #4 LoadR4, (R5) Branch_if_(R1>=R4) Skip MoveR1, R4 Skip:AddR2, R2, #1 Branch_if_(R3>R2) Loop MoveR2, #Max StoreR1, (R2) Earlier Assembly Code

43 SP = 100 Initial Stack [something]

44 SP = 92 N #A Add couple parameters [something]

45 #A N SP = 72 R1 R2 After register saves R3 R4 R [something]

46 #A Max Value SP = 72 R1 R2 Update stack with output parameter R3 R4 R [something]

47 SP=92 After return from a function #A Max Value [something]

48 SP=100 After stack restore by calling program [something]

 Required Changes 1. Push R1–R5 on stack 49 MAX:Subtract SP, SP, #20 Store R1, 16(SP)Push R1 Store R2, 12(SP)Push R2… Store R5, (SP)Push R5 MoveR3, #N LoadR3, (R3) MoveR5, #A Load R1, (R5) MoveR2, #1 Loop:AddR5, R5, #4 LoadR4, (R5) Branch_if_(R1>=R4) Skip MoveR1, R4 Skip:AddR2, R2, #1 Branch_if_(R3>R2) Loop MoveR2, #Max StoreR1, (R2)

 Required Changes 1. Push R1–R5 on stack 2. R3 and R5 must be initialized from the stack 50 MAX:Subtract SP, SP, #20 Store R1, 16(SP)Push R1 Store R2, 12(SP)Push R2… Store R5, (SP)Push R5 LoadR3, 24(SP) LoadR5, 20(SP) MoveR2, #1 Loop:AddR5, R5, #4 LoadR4, (R5) Branch_if_(R1>=R4) Skip MoveR1, R4 Skip:AddR2, R2, #1 Branch_if_(R3>R2) Loop MoveR2, #Max StoreR1, (R2)

 Required Changes 1. Push R1–R5 on stack 2. R3 and R5 must be initialized from the stack 3. Last two lines must be replaced with pushing the result in R1 onto stack where input parameter N was passed. 51 MAX:Subtract SP, SP, #20 Store R1, 16(SP)Push R1 Store R2, 12(SP)Push R2… StoreR5, (SP)Push R5 LoadR3, 24(SP) LoadR5, 20(SP) MoveR2, #1 Loop:AddR5, R5, #4 LoadR4, (R5) Branch_if_(R1>=R4) Skip MoveR1, R4 Skip:AddR2, R2, #1 Branch_if_(R3>R2) Loop StoreR1, 24(SP)

 Required Changes 1. Push R1–R5 on stack 2. R3 and R5 must be initialized from the stack 3. Last two lined must be replaced with pushing the result in R1 onto stack where input parameter N was passed. 4. Restore R1-R5 and pop stack 52 MAX:Subtract SP, SP, #20 Store R1, 16(SP)Push R1 Store R2, 12(SP)Push R2… StoreR5, (SP)Push R5 LoadR3, 24(SP) LoadR5, 20(SP) MoveR2, #1 Loop:AddR5, R5, #4 LoadR4, (R5) Branch_if_(R1>=R4) Skip MoveR1, R4 Skip:AddR2, R2, #1 Branch_if_(R3>R2) Loop StoreR1, 24(SP) Load R1, 16(SP)Restore R1 Load R2, 12(SP)Restore R2… LoadR5, (SP)Restore R5 AddSP, SP, #20

 Required Changes 1. Push R1–R5 on stack 2. R3 and R5 must be initialized from the stack 3. Last two lined must be replaced with pushing the result in R1 onto stack where input parameter N was passed. 4. Restore R1-R5 and pop stack 5. Return 53 MAX:Subtract SP, SP, #20 Store R1, 16(SP)Push R1 Store R2, 12(SP)Push R2… StoreR5, (SP)Push R5 LoadR3, 24(SP) LoadR5, 20(SP) MoveR2, #1 Loop:AddR5, R5, #4 LoadR4, (R5) Branch_if_(R1>=R4) Skip MoveR1, R4 Skip:AddR2, R2, #1 Branch_if_(R3>R2) Loop StoreR1, 24(SP) Load R1, 16(SP)Restore R1 Load R2, 12(SP)Restore R2… LoadR5, (SP)Restore R5 Return

 Convert the code for the calling program and Max to Nios II and verify it works as expected. 54

 Locations at the top of the processor stack are used as a private work space by subroutines  A stack frame is allocated on subroutine entry and deallocated on subroutine exit  A frame pointer (FP) register enables access to private work space for current subroutine  With subroutine nesting, the stack frame also saves return address and FP of each caller

1. Return Address Register needs to be saved only if the procedure corresponding to this stack frame is non-leaf. 2. A local variable must be saved for a non-leaf procedure only if its value needs to be preserved across a call to another procedure. 56 I/O Parameters Saved FP Local Variables 2 Saving Registers Return Address Register 1 FP

 As we saw earlier, the stack frame for the call to MAX(A,N) is shown on the right.  This one is without the frame pointer  Note that the space occupied by the input parameters is part of the stack frame of the calling program 57 #A N R1 R2 R3 R4 R5 Stack Frame for call to MAX(A,N)

58 #A N R1 R2 R3 R4 R5 Saved [FP] FP

 Specific to each procedure call  Builds up on the stack for each nested call and builds down for each return  If during a procedure call, stack is used for computation, the stack-frame size may change dynamically  The order of items on a stack frame is by convention only and can change for a different convention. 59

The next three slides show step-by-step development of assembly code for another example of a non-recursive procedure. You should study this carefully and then do the following exercise to convert the code to a subroutine, to understand better the process we used to convert the max-finding program

itoah(n, S) /* Convert positive integer n to hexadecimal string S in ASCII - non recursive */ int n; char S[]; { int i, j i = 0; do { j = n % 16; /* modulo-16 operator */ if(j<10) S[i++] = j + '0'; else S[i++] = j-10+'A’ /* i++ is C syntax */ } while ((n = n/16) > 0); /* while condition checked at the end of the loop */ } 1.Hand-simulate the program for the call itoah(95, S). 2.Assume, input parameters n & S, and the local variables i & j are mapped to registers R2, R3, R4, and R5 respectively. Further, assume that the program requires no other registers to compute the body of the loop. Show the stack state: (1) before the call to itoah (2) after the call just before the first instruction in the body is executed, and (3) just before the procedure is ready to return. 61

62 { int i, j i = 0; do { j = n % 16; if(j<10) S[i++] = j + '0'; else S[i++] = j-10+'A’ } while ((n = n/16) > 0) } { int i, j i = 0; do { j = n % 16; if(j<10) S[i++] = j + '0'; else S[i++] = j-10+'A’ } while ((n = n/16) > 0) }

63 nS[i]ij R2R3R4R5 Register Assignments: i = 0 LOOP: j = n and 0xF if(j<10) toto L1 S[i] = j – 10 + ‘A’ goto L2 L1:S[i] = j + ‘0’ L2:i = i+1 n = n/16 if(n>0) goto LOOP MoveR4, R0 LOOP: And R5, R2, #0xF Branch_if_(R5<#10) L1 SubtractR3, R5, #10 AddR3, R3, #41 Branch L2 L1:AddR3, R5, #30 L2:AddR4, R4, #1 Right_ShiftR2, R2, 4 Branch_if_(R2>0) LOOP Intermediate Code Assembly Code

 Convert the itoah program to two programs: main that calls the subroutine itoah. Assume parameters n and #S are passed in registers.  Note that string S is both an input and the output parameter. 64

itoah(n, S, i) /* Convert positive integer n to hexadecimal string S in ASCII, i is index - recursive program*/ int n, i; char S[]; { int j; if ((j = n/16) != 0)/* integer division */ itoah(j, S, i+1); j = n % 16; if(j<10) S[i] = j + '0'; else S[i] = j-10+'A' } 1.Hand-simulate the program for the call itoah(95, S, 0). 2.Assume, input parameters n, S, and i and the local variable j are mapped to registers R2, R3, R4, and R5 respectively. Further, assume that the program requires no other registers to compute the body of the loop. Show the stack state: (1) before each call (2) after each call just before the first instruction in the body is executed, and (3) just before the program is ready to return. 65

66

67 Variable:Maxi#N = (Addr. N) A[i]#A= Addr. A[0] RegisterR1R2R3R4R5 Intermediate Code Max = A[0] i = 1 Loop: if(!(A[i])>Max)) goto Skip Max = A[i] Skip:i = i+1 if(N>i) goto Loop MoveR3, #N LoadR3, (R3) MoveR5, #A Load R1, (R5) MoveR2, #1 Loop:AddR5, R5, #4 LoadR4, (R5) Branch_if_(R1>=R4) Skip MoveR1, R4 Skip:AddR2, R2, #1 Branch_if_(R3>R2) Loop MoveR2, #Max StoreR1, (R2) Assembly Code Max = A[0] for i=1, i<N { if(A[i]>Max) Max = A[i] } Note: R2 is reused after the for loop to store the address #Max