CIT 593: Intro to Computer Systems Homework #4: Assembly Language Due October 18, 2012, 4:30pm Instructions You may print this document and write your answers here, or submit solutions on a separate piece of paper. You should show your work, of course, but make sure your final answer is clearly indicated. Name Important note! Because we will discuss the solutions to this assignment during the exam review on October 18, this assignment may not be submitted late. Please be sure to submit it on time! Question #1 (5 points) You are writing an LC-3 program and, for the instruction at address x2024, you want to load the data held at address x6ab4 into register R1. However, you were sleeping in class when we talked about the load indirect (LDI) instruction so you only know about the other modes (LD, LDR, and LEA). So it seems to you that you'll need some combination of at least two instructions to accomplish this, since x6ab4 is too far from x2024 to use PC-relative mode. What sequence of instructions could you use instead, without using LDI? Question #2 (5 points) Consider the following LC-3 program written in assembly language. First, complete the table by writing the corresponding assembly language instructions. Address Encoding Instruction 0x3002 0001 0100 1010 0001 0x3003 0001 0100 1000 0010 0x3004 0001 0010 0111 1111 0x3005 0000 0011 1111 1101 In the program above, assume that R1 holds some positive integer n, and R2 holds the value 0. What value is held in R2 (in terms of n) when this program terminates? Assume the values manipulated by this program are small enough that overflow does not occur.
Question #3 (10 points) Consider the following LC-3 assembly language program:.orig x3000 LEA LEA LD R0, PHRASE R1, DATA R2, VALUE LOOP LDR R3, R0, #0 STR R3, R1, #0 ADD R0, R0, #1 ADD R1, R1, #1 ADD R2, R2, #-1 BRp LOOP END HALT PHRASE.STRINGZ "This is not a test!" DATA.BLKW x10 VALUE.FILL x10.end Create the symbol table entries generated by the assembler when translating the above program into machine code: Label LOOP END PHRASE DATA VALUE Offset What values are stored at the following addresses when this program is loaded into memory? Express the values in hexadecimal. Address x3001 Value x3008 X300A
Question #4 (10 points) Your friend is trying to write an assembly language program to read in two single-digit numbers from the input and print the sum to the output. However, his program is not working correctly: when he enters '5' and then '2', he expects '7' but the output is the letter d. He knows that you are an expert at assembly language programming and asks for your help. What is wrong with the program below? Hint: there are at least two errors in the code; you only need to identify and explain how to correct two of them. You don't have to actually write the correct code, just explain how to fix it. 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26.ORIG x3000 JSR GET ; call subroutine JSR GET ; call subroutine again ADD R2, R0, R0 ; put the sum in R2 PRINT LDI R1, DSR ; write out to screen BRzp PRINT STI R2, DDR HALT ;; subroutine to read from the keyboard GET LDI R1, KBSR BRzp GET LDI R0, KBDR RET KBSR.FILL xfe00 KBDR.FILL xfe02 DSR.FILL xfe04 DDR.FILL xfe06.end
Question #5 (20 40 points) In this part, you will write an LC-3 assembly language program that allows the user to draw lines of color on the screen. Do not submit your code on paper! You will submit it using the turnin program, as in Homework #3; instructions will be posted on Piazza. Be sure that your program runs correctly in the LC-3 simulator before submitting it, of course! The program works as follows: first, the user enters either the 'r', 'g', or 'b' key, indicating that they want a red, green, or blue line to be drawn then, the user enters a number from 1 to 9, indicating the number of lines to draw the program draws the appropriate number of lines in the appropriate color, and then waits for the next input. The lines should be drawn starting at the top of the screen, and subsequent key presses should result in lines being drawn lower down the screen. For instance, if the user presses 'r', then '2', then 'g', then '4', the screen should show two red lines on top, with four green lines underneath. Other things to consider: When you re testing your program, be sure to click the white box below the black video output screen in the simulator; that box is for keyboard input and needs to have focus in the simulator. If the user presses the letter 'q' for the color, the program should terminate; you should ignore any other keys for the color, and instead continue to wait for a legal color input. The program only needs to handle lower-case letters for the color, i.e. you don't have to worry about R, G, etc. You can assume that the user will correctly enter a number from 1 to 9 when you're waiting for the number of lines to enter; you don't have to worry about error handling there. At some point, you will run out of lines on the screen. That's okay, you don't have to explicitly handle that condition. You may find it necessary (hint, hint) to hardcode some of the useful values and put them into memory locations in your program. Here are the ASCII values for the letters you need to check for: letter ASCII value r g b x72 x67 x62 q x71 Note that the ASCII value for the '1' key is not 1!! It's x31. The '2' key is x32, and so on. Here are the color codes that you need to use: color code red green blue x7c00 x03e0 x001f
Remember, to draw on the screen in the simulator, write the color code to the appropriate memory address, starting at xc000 (which is the upper lefthand corner). Each row on the screen has 128 pixels and thus is represented by 128 contiguous memory addresses. Go through Lab #3 again if you need a refresher. Advice on how to approach this assignment This is a challenging assignment so give yourself lots of time and think of it in small pieces. First, you need to somehow figure out which key the user entered. Start with the keyboard echo sample program in the textbook on page 207 (section 8.3.4) to make sure that you can read keyboard entry and that the program knows which keys were pressed. Next, modify the program so that you are able to determine whether the first key pressed was the letter 'r'. For testing purposes, just use 'r' for now, and don't worry about reading the second key press (in which the user enters the number of lines to draw) or any other characters just yet. Once your program has determined that the letter 'r' was pressed, use your solution from Lab #3 Exercise #1 (there is a solution on the course website, too) to draw a single red line across the screen. Be careful about which registers are holding which values! Then, after you've drawn a single red line across the street, branch back up to the start of the program and wait for the next keyboard input. Okay, now you're getting somewhere! Now you can draw a single red line each time the letter 'r' is pressed! From here, change the program so that it checks if the key is 'r', 'b', or 'g', and chooses a different color to draw depending on the letter that was entered. At this point, the program should be able to draw a red, blue, or green line each time one of those is pressed. Now you just have three more modifications to make: the program should quit if the user enters 'q' the program should continue to wait for legal input if the letter is not 'r', 'b', 'g', or 'q' after getting the color from the user, the program should read in the number of lines to draw, and then actually draw that number of lines You do not have to use subroutines in this assignment, but you can if it helps you organize everything. The hardest part of this assignment is that you will have lots of loops and need to keep track of which counters are in which registers. So use comments!