Download Translation of C functions to LC-3 assembly and analysis of stack frames and more Exams Introduction to Aerospace Engineering in PDF only on Docsity!
ECE190 Exam 2, Fall 2006
Monday 30 October
Name:
• Be sure that your exam booklet has 13 pages.
• The exam is meant to be taken apart!
• Write your name at the top of each page.
• This is a closed book exam.
• You may not use a calculator.
• You are allowed TWO 8. 5 × 11 " sheets of notes.
• Absolutely no interaction between students is allowed.
• Show all of your work.
• More challenging questions are marked with ***.
• Don’t panic, and good luck!
“Adaptation of the a priori to the real world has no more originated from ‘experience’
than adaptation of the fin of the fish to the properties of water.”
—K. Lorenz, as quoted by N. Chomsky in Language and Mind , as quoted by O. Sacks in Seeing Voices
Problem 1 20 points
Problem 2 20 points
Problem 3 20 points
Problem 4 20 points
Problem 5 20 points
Total 100 points
Problem 1 (20 points): Short Answers
Please answer concisely. If you find yourself writing more than a few words or a simple drawing, your answer is
probably wrong.
Part A (5 points): Consider the following C function:
void /* returns nothing */ func (int x) { switch ((5 < x) - (3 > x)) { case -1: printf ("Too cold\n"); break; case 1: printf ("Too hot\n"); break; case 0: printf ("Just right\n"); break; default: printf ("Weird weather!\n"); break; } }
Fill in the blanks below to re-implement the function using if statements.
void /* returns nothing */ func (int x) { if ( ) { printf ("Too cold\n");
} else if ( ) { printf ("Too hot\n");
} else if ( ) { printf ("Just right\n"); } else { printf ("Weird weather!\n"); } }
Part B (5 points): Describe one advantage and one disadvantage of making a variable global rather than local to
a certain function in a C program. Hint: the disadvantages outweigh the advantages in practice, particularly for large
programs.
Problem 2 (20 points): Systematic Decomposition to LC-3 Assembly
1 if printer is online 1 if printer is ready
15 14 13 0 PSR xFE80 unused
PDR xFE
15 0 unused
8 7 char to print
wait for printer
send one character
update variables
initialize variables
save registers
START
restore registers
DONE
eight chars done?
R0 = temporary values R1 = count of characters sent already R3 = pointer to next character to send
register variables for trap routine
loop over characters
Y
N
Prof. Lumetta needs your help: a new printer device has been added to the LC-3, but he has not been able to write one
of the trap routines, and the next ECE190 assignment requires that trap routine! The trap routine in question sends
a sequence of eight characters stored in memory starting at R3 (an input value) to the printer. Before sending each
character to the printer, the trap routine must wait until both the online and ready bits of the PSR are equal to 1. The
character can then be written to PDR.
The figure above shows three things: on the left, a partial systematic decomposition for the trap routine (partial
because it requires more than one LC-3 instruction for each box); in the upper right, the addresses and pictures of the
new Printer Status Register (PSR) and Printer Data Register (PDR); in the lower right, the mapping from registers to
data values that you’ll need to use in the trap routine.
Part A (5 points): First protect the registers. The trap should preserve all register values. Fill in the code and
allocate storage as necessary below to accomplish this goal. Two data values have been provided for Parts B and C.
; save registers (FILL IN)
; initialize variables and loop over characters (Part C) ; restore registers (FILL IN)
RET ; DONE
; data needed for trap routine (FILL IN)
TRAP PSR .FILL xFE TRAP PDR .FILL xFE
Problem 2, continued:
Part B (5 points): The next step is to decompose the “wait for printer” box to the level of individual LC-
instructions. Before sending a character to the printer, the trap routine must wait until both the online and ready bits
of the PSR are equal to 1. Draw your answer as a flow chart with RTL or assembly inside each statement or test.
For example, you might label a test with “BR” and write N, Z, and P on the appropriate output arcs. Use the register
mapping shown in the figure (replicated below). Use data values from Part A (you should not need any others).
1 if printer is online 1 if printer is ready
15 14 13 0 PSR xFE80 unused
PDR xFE
15 0 unused
8 7 char to print
wait for printer
send one character
update variables
initialize variables
save registers
START
restore registers
DONE
eight chars done?
R0 = temporary values R1 = count of characters sent already R3 = pointer to next character to send
register variables for trap routine
loop over characters
Y
N
Problem 3 (20 points): The LC-3 Assembler
Consider the LC-3 program shown below. The numbers to the left are to help you answer the questions and are not
part of the program. Hint: what the program does is not important to the problem!
01 .ORIG x 02 03 INIT 04 LEA R0, START STR 05 JSR PRINT STR 06 LD R0, TEN 07 LEA R1, DATA B 08 09 STORE LOOP 10 STR R0, R1, # 11 ADD R1, R1, # 12 ADD R0, R0, #- 13 BRp ST LOOP 14 15 LD R0, TEN 16 ADD R1, R1, #- 17 AND R2, R2, # 18 19 ADD LOOP 20 LDR R3, R1, # 21 ADD R2, R3, R 22 ADD R1, R1, #- 23 ADD R0, R0, #- 24 BRp ADD LOOP 25 26 STORE SUM 27 ST R2, RESULT 28 TRAP # 29 30 PRINT STR 31 ST R7, SAVE R 32 PUTS 33 LD R7, SAVE R 34 RET 35 36 TEN .FILL # 37 SAVE R7 .BLKW # 38 DATA B .BLKW # 39 START STR .STRINGZ "Starting..." 40 RESULT .FILL # 41 .END
Label Address
INIT
STORE LOOP
ADD LOOP
STORE SUM
PRINT STR
TEN
SAVE R
DATA B
START STR
RESULT
Part A (10 points): Fill in the addresses for the symbol table above as they would be generated by the assembler.
Part B (4 points): Write out the binary word that would be generated by the assembler for line 15 of the program.
Part C (6 points): Assuming that both passes of the assembler were to execute, indicate which line numbers result
in errors reported by the assembler, specify in which pass each error occurs, and briefly explain why each is an error.
Name: ____________________________________________ 8
Problem 4 (20 points): From C to LC-3 and Back Again
Part A (10 points): translate the C function below to LC-3 assembly instructions. The diagram of the
stack frame for the function call has been provided for you.
Translate the while and return statements
from the function body independently, with no
register values shared between sections. The
stack frame management and register save/store
has been done for you (not shown in figures).
char* find_char
(char* str, char a)
{
char* strcpy = str;
while(*strcpy != a){ strcpy++; } return strcpy;
}
; create stack frame and save registers … ; char strcpy = str; ; DO NOT WRITE IN THIS BOX*
; translation for ; return i;
; translation for while loop ;while(strcpy != a) { ; strcpy++; ; }*
; restore registers and tear down stack frame … ; DO NOT WRITE IN THIS BOX
strcpy
prev. frame pointer
return address
return value
str
a
stack frame for find_char
R6,R
Problem 5 (20 points): C and Stack Frames
This question focuses on the program below, and particularly on the stack frames (also called activation records) that
are used by each function in the program.
#include <stdio.h>
/* function declarations / int bar (int a, int b); int foo (int p);
int bar (int a, int b) { int x = a + b;
if (0 < a) { printf ("%d\n", a * b); } return x; }
int foo (int* p) { *p = bar (-4, 11); return 6; }
int main () { int x = 0; int y;
y = foo (&x); bar (x, y); return 0; }
Part A (3 points): When someone runs the program, what is the order of subroutine calls for the program, starting
from main? In other words, what is the sequence of JSR target over the whole program execution? Give a comma-
separated list, including only the main, foo, and bar functions.
main,
Part B (3 points): What, if anything, is printed by the program?
Problem 5, continued:
Part C (14 points): The stack frame for the main function is shown below. During execution of main, the stack
pointer R6=xBFEF, and the frame pointer R5=xBFF0.
Use the figure to draw the stack just after completion of the return statement in the bar function when it is called
from main, i.e. , just before bar’s stack frame is torn down and the subroutine returns to main.
Draw arrows to indicate the values of R6 and R5 at the point of program execution just described. For each memory
location included in the stack ( i.e. , between the stack pointer and the bottom of the figure), label the location with the
type of information and the value stored there. If a memory location’s value cannot be known, put a question mark
by the description, e.g. , “x=?”.
Do not mark or label any locations above the stack pointer, even if you know the values in those locations!
The address of the JSR bar instruction in main is x3040.
return value = _______________
return address =
prev. frame ptr =
local var x = ________________
local var y = ________________
xBFF
xBFF
xBFF
xBFF
xBFEF
xBFEE
linkagemain’s stack frame
(no parameters)
xBFED
xBFEC
xBFEB
xBFEA
xBFE
xBFE
xBFE
xBFE
xBFE
xBFF
x
R
R
LD LDI LDR LEA NOT ST STI STR
ADD ADD AND AND BR JMP JSR TRAP
DR
SR
SR
SR
imm
SR
SR
SR
imm
p
z
n^
PCoffset
BaseR
DR DR DR
PCoffset
DR
SR1 + SR2, Setcc
ADD DR, SR1, SR
DR
SR1 + SEXT(imm5), Setcc
ADD DR, SR1,
imm
DR
SR1 AND SR2, Setcc
DR
SR1 AND SEXT(imm5), Setcc
AND DR, SR1, SR2AND DR, SR1,
imm
BR{nzp}
PCoffset
((n AND N) OR (z AND Z) OR (p AND P)):
PC
PC + SEXT(PCoffset9)
JMP BaseR
PC
BaseR
R
PC, PC
PC + SEXT(PCoffset11)
JSR
PCoffset
R
PC, PC
M[ZEXT(trapvect8)]
TRAP
trapvect
PCoffset
DR
PCoffset
DR
offset
PCoffset
DR DR
BaseR
DR
SR
PCoffset
SR
PCoffset
SR
offset
SR
BaseR
LD DR,
PCoffset
LDI DR,
PCoffset
LDR DR, BaseR,
offset
LEA DR,
PCoffset
NOT DR, SRST SR,
PCoffset
DR
M[PC + SEXT(PCoffset9)], Setcc
DR
M[M[PC + SEXT(PCoffset9)]], Setcc
DR
M[BaseR + SEXT(offset6)], Setcc
DR
PC + SEXT(PCoffset9), Setcc
DR
NOT SR, Setcc
M[PC + SEXT(PCoffset9)]
SR
M[M[PC + SEXT(PCoffset9)]]
SR
M[BaseR + SEXT(offset6)]
SR
STI SR,
PCoffset
STR SR, BaseR,
offset
NOTES: RTL corresponds to execution (after fetch!); JSRR not shown
trapvect