Homework 4

EECS 284
Posted: February 5, 2000
Due: February 10, 2000
 

Learning objectives

 Homework guidelines

 
1
16 points
Create the symbol table entries generated by the LC-2 assembler when translating the following routine into machine code: 
          .orig x3000 
          ST R3, SAVE3 
          ST R1, SAVE1 
          LEA R0, HELLO 
          PUTS 
TEST      IN 
          BRz TEST 
          LD  R3, ZERO 
          ADD R1, R0, R3 
          BRn FINISH 
          LD  R3, NINE 
          ADD R1, R0, R3 
          BRp FINISH 
          LEA R0, DIGIT 
          PUTS 
FINISH    HALT 
SAVE3     .FILL x0000 
SAVE1     .FILL x0000 
HELLO     .STRINGZ "Hello\n" 
ZERO      .FILL #-48 
NINE      .FILL #-57 
DIGIT     .STRINGZ "It's a digit\n\n" 
          .end

2
10 points

For each of the items below, indicate whether the calling function or the called function performs the action. 
 
a)
Writing the arguments into the activation record.
b)
Writing the return value into the activation record.
c)
Writing the dynamic link.
d)
Placing the return address in the activation record.
e)
Modifying the value in R6 to contain the address of the called function's activation record.
 

3
34 points


You will use the LC-2 editor and simulator for this problem.  This sentence contains a web link for an LC-2 Assembly Language program in a file named hw4p3.asm.  Save this file in the folder containing your LC-2 software.  What you'll find in the file is a translation of a C program named hw4p3.c (the C code is provided just to give you an overview of the program).  Assemble the program and load it in the simulator.  Do not alter the program before answering the following questions. 
a)
Notice that the first GETC mnemonic in the subroutine GETS becomes a TRAP instruction when assembled (at x3061).  The trap vector is x20. When this TRAP instruction is executed, the operating system computes a memory address in the System Control Block where it looks up the starting address of the GETC routine. What is the memory address in the System Control Block containing the address of the first instruction in the GETC routine? 
b)
Use the simulator's "Jump display to" feature in the "Display" menu to find the place where that address is stored.  What is the starting address of the GETC routine?
c)
Place a breakpoint at instruction x0455 (make sure this is the only breakpoint set) and run the program until it stops at the breakpoint.  What instruction from hw4p3.asm is executing when the breakpoint is reached the first time?  (Give the memory address and the Assembly Language representation of the instruction).  What is the hex value in R1 at that time?
d)
What is the size of main's activation record?  (Use the procedure outlined in the Activation Record Worksheet to compute the size). 
e)
What is the size of the activation record for GETS?
f)
Before invoking the subroutine GETS at x300B, the main function must pass values for two parameters, s and n. Explain how the offsets from R6 are computed to store the parameters in the correct locations in the activation record for GETS.  (A brief explanation is preferred--three or four sentences). 
 

4
40 points


Consider the following C program fragment: 
void main() 
{ 
   int i, j; 
   i = 8; 
   j = fnc1(i); 
} 

int fnc1( int k ) 
{ 
   return 2 * fnc2(k);  
} 

int fnc2( int n ) 
{ 
   return n + 4; 
}

In a partial translation of the program into Assembly Language using the runtime stack, the following subroutines have been written for fnc1 and fnc2
SR1    LDR R2, R6, #3       ; load k into R2 
       STR R2, R6, #9       ; store k to space reserved for n  
                            ;     in SR2 
       STR R6, R6, #8       ; store stack pointer as dynamic 
                            ;     link 
       ADD R6, R6, #6       ; compute new stack pointer 
       JSR SR2              ; call SR2 
       LDR R6, R6, #2       ; restore stack pointer 
       LDR R3, R6, #4       ; load return value 
       ADD R3, R3, R3       ; multiply return value by 2 
       STR R3, R6, #0       ; store result as return value 
       RET 

SR2    LDR R2, R6, #3       ; load n into R2 
       ADD R3, R2, #4       ; add 4 to n 
       STR R3, R6, #0       ; store result as return value 
       RET

 
a)
If the value 8 is placed at offset 3 in SR1's activation record, will SR1 return the correct value to main?  If so, what will that value be?  If not, explain why not and what could be done to fix the problem (in three sentences or less).
b)
List in order the items (other than general-purpose registers R0 through R5) for which space must be reserved in the activation record for main when it is translated into Assembly Language.  Indicate what the offset will be for each item in the activation record.
c)
Write a series of Assembly Language instructions to translate the following line of C code in main: 
j = fnc1(i); 
(Be sure to include code to pass the value of i to SR1, invoke SR1, and store the return value in the space reserved for j in the activation record).