CS/ECE 252 Introduction to
Computer Engineering Spring
2009 All Sections |
Homework 7// Due at lecture Apr 17,
Mon
Primary contact for this homework:
Mengmeng Chen [mchen at cs dot wisc dot edu]
You may choose to do this homework
in a group of TWO or THREE students or all by yourself. If you do it in a
group, each of the group member should hand in ONE copy of the homework that
lists the common section number and names and UW ID numbers of all the other
students. Please staple multiple pages together.
Problem 1.
Consider the following two instructions in a LC-3 assembly language program:
ASCII LD R0, ASCII
L ST
R0, L
The symbol table entry for ASCII is x4F08.
(a) What will be contained in R0 immediately after the first instruction is executed.
R0
<- mem[ASCII] = x21FF, note that
x21FF is the binary code for
instruction LD R0, #-1
(b) What will be stored at memory location x4F09 after the second instruction is executed.
Mem[x4F09]
= R0 = x21FF. Essentially the first instruction will be copied to x4F09 and overwrite
the second instruction.
Problem 2.
Refer to Problem 7.15 on ItCS. Answer the following questions:
(a) What does the program do? (Explain it in less than 25 words.)
The program will scan through the sequence and multiply all the non-negative numbers by 2 and update the value in its memory location.
(b) Write out the symbol table of the program.
Symbol |
Address |
LOOP |
x3002 |
L1 |
x3007 |
NEXT |
x3009 |
DONE |
x300B |
NUMBERS |
x300C |
MASK |
x300D |
Problem 3.
You are given a string “introductiontocomputersystem” and you are going to write an assembly language program to find the letter that occurs at least three times in the string. Make sure you put your result in R0 and halt your program in the end.
Hint: Use .BLKW to allocate memory to keep track of each letters’
occurrences. Your program should work with arbitrary strings that contain only
lowercase letters.
(a) Draw a flowchart of your program.
(b) Submit a screenshot of your commented code and what is stored in R0 after you run your program on LC-3.
Problem 4.
The following program adds the values stored in the memory
location A, B, and C, and stores the result into memory. There are two errors
in the code. For each, describe the error and indicate whether it will be
detected at assembly time or at run time.
Line No.
1 |
|
.ORIG x3000 |
2 |
ONE |
LD R0, A |
3 |
|
ADD R1, R1, R0 |
4 |
TWO |
LD R0, B |
5 |
|
ADD R1, R1, R0 |
6 |
THREE |
LD R0, C |
7 |
|
ADD R1, R1, R0 |
8 |
|
ST R1M SUM |
9 |
|
TRAP x25 |
10 |
A |
.FILL x0001 |
11 |
B |
.FILL x0002 |
12 |
C |
.FILL x0003 |
13 |
D |
.FILL x0004 |
14 |
|
.END |
Bug1. (Line 3) R1 is not initialized before
being used. This bug can only be detected by debugging at run time.
Bug2. (Line 8)
Actually there are two bugs here. ‘R1M’ is not a valid register (It’s a typo).
Symbol ‘SUM’ is not defined. These two bugs will be detected at assembly time.
Problem 5.
You are given two pieces of program. They have been written by different programmers to store x0015 into memory location x4000.
a.
|
.ORIG x5000 |
|
AND R0, R0, #0 |
|
ADD R0, R0, #15 |
|
ADD R0, R0, #6 |
|
STI R0, PTR |
|
<rest
of the program…..> |
|
HALT |
PTR |
.FILL x4000 |
|
.END |
b.
|
.ORIG x4000 |
|
.FILL x0015 |
|
.END |
|
|
(1) Why there are two ‘ADD’ instructions in module (a)?
The
range of the immediate value in ADD instruction is only from +15 to -16. +15 is
the maximum number we can add at a time. Therefore the addition needs two
steps.
(2) Explain the fundamental differences in their approaches?
The
first approach uses instructions to dynamically initialize the value stored in
some memory location. The initialization is done at run time. The second
approach uses the pseudo-op provided by the assembler to statically initialize
the memory content. The initialization is done at assembly time.
(3) Give one example where module a ‘s approach is preferred.
The
advantage of the second approach is simplicity. However, it requires that the
programmer knows what the value should be before running the program.
Sometimes, the initial value can only be calculated by running part of the
program. Furthermore, sometimes the necessity of initializing some memory
location is also determined at run time.
In
both scenarios, you can use the first approach to come up with a subroutine and
reuse it when it is necessary. (you will have to modify it a little bit).