Apr 6 - Illinois Institute of Technology

Transcription

Apr 6 - Illinois Institute of Technology
Illinois Institute of Technology
Lab 6
LC-3 Programming
CS 350: Computer Organization & Assembler Language Programming
Lab 6, due Fri Apr 3 Tue Apr 7 *
A. Why?
• The LC-3 and similar architectures are representative of simple instruction set
architectures.
• Writing low-level programs illustrates what low-level hardware does and how
compilers and interpreters work.
B. Outcomes
After this lecture lab, you should be able to:
• Translate pseudocode into programs in LC-3 assembler.
• Write short assembler programs to do calculations and searches.
C. Programming Problems [100 points total]
Note: Take your *.asm files, zip them together, and submit the zipped file through
Blackboard. (Don’t include the *.obj, etc. files or runs of the programs.) Be sure
to put your name inside each asm file.
For each program, up to roughly 30% of the credit is for good commenting. At
the top of each program file, say what the program does. Also, say what registers
are holding which values and what the relationships are between those values. In
the program itself, include line comments to say what you’re doing (in higher-level
terms if you can: “; count++” is better than just “; R0++”).
Problem 1 [40 pts]
Write an LC-3 program that left-shifts a value from one word to another: We save
the bits that are shifted out of the first word and store them into a second word.
For example, if we shift X = 1111 1111 1111 1111 left five places, then we have
*
No extension to Apr 8
CS 350: Comp Org & Asm Pgm’g
–1 –
© James Sasaki, 2015
Illinois Institute of Technology
Lab 6
two words of results: R = 1111 1111 1110 0000 (the bits remaining after
shifting) and L = 0000 0000 0001 1111 (the bits that were shifted out).
Your program should have labels X, L, R, and N. Running the program should
left-shift the value in X by N positions and save the results into locations L and R.
The labels don't have to be declared in the order shown below, and you can declare
extra labels and variables if you like. As an example, say we start with
X!
N!
R!
L!
.FILL! xFFFF
.FILL! 5
.BLKW! 1
.BLKW! 1
Then at the end, L and R should contain x001F and xFFE0
don't read in X and N; they're hard-coded into the program.
on different X and N, you'll have to modify their declarations
rerun the program. Also, you don't have to check for 0 ≤ N ≤
respectively. Note you
To test your program
and re-assemble and
16.
Here’s some pseudocode. (Use registers to hold L, R, and I as you manipulate
them; store L and R in their labeled memory locations when you're done.)
L ← 0
R ← X
I ← N (I = the number of shifts yet to do)
while I > 1
Left-shift L one bit with zero fill
Inspect the leftmost bit of R
if the leftmost bit of R is 1
Set the rightmost bit of L to 1
Left-shift R one bit with zero fill
--I
end while
Store L into memory
Store R into memory
Hints:
• Each line of pseudocode above corresponds to one LC-3 instruction.
• To left-shift a value one bit with zero fill, multiply it by 2.
• The leftmost bit of a value is 1 iff the value is negative.
CS 350: Comp Org & Asm Pgm’g
–2 –
© James Sasaki, 2015
Illinois Institute of Technology
Lab 6
Grading scheme: Each line of pseudocode above is worth roughly 2 to 4 points
depending on its difficulty. Commenting / documentation is worth 12 points.
Problem 2 [60 points]
Write an LC-3 program that reads a character and a string and counts the number
of occurrences of the character in the string. If the count is 9 or less, print out the
count; if the count is 10 or more. For example, the LC-3 console might look like
this: (user input is underlined; “↵” means carriage return). Note the string can
contain spaces.
Char to search for: e
String to search through: alphabee tz!? #QZ&↵
The count was 5
Here’s some pseudocode for the program:
Print a prompt for a character s to search for
Read it in and echo it back out
Initialize count = 0
Print a prompt for a string
Read a character c and echo it
while c ≠ line feed
! if c = s then ++count
! read next value of c and echo it
end loop
if count ≤ 9
! Convert the count to an ASCII character
! Print the count to the monitor
else
! Print "There were more than 9 occurrences"
halt
Technical note: Why ≤ 9 occurrences? Because otherwise you’d have to convert an
arbitrary integer into a string of digits, which is painful.
Grading scheme: Each line of pseudocode above is worth roughly 2 to 4 points
depending on its difficulty. Commenting / documentation is worth 12 points.
CS 350: Comp Org & Asm Pgm’g
–3 –
© James Sasaki, 2015