Computer Sciences Dept.

CS/ECE 252 Introduction to Computer Engineering

Fall 2007 Section 3
Instructor Mark D. Hill
TAs Sanghamitra Roy, Eric Hill, Samuel Javner, Natalie Enright Jerger, & Guoliang Jin

URL: http://www.cs.wisc.edu/~markhill/cs252/Fall2007/

Homework 6 Solutions

First contact for questions is TA Sam Javner at: javner@wisc.edu

Problem 1

Problem 5.23 on page 149 of ItCS.

x30FF    1110 0010 0000 0001  ; R1 <- PC' + 1      ; R1 <- x3101
x3100    0110 0100 0100 0010  ; R2 <- mem[R1 + 2]  ; R2 <- mem[x3103] = x1482
x3101    1111 0000 0010 0101  ; TRAP x25 = HALT
x3102    0001 0100 0100 0001  ; x1441
x3103    0001 0100 1000 0010  ; x1482

R2 = x1482 when execution halts

Problem 2

Problem 5.31 on page 151 of ItCS.

We are given that the first 4 bits of the instruction are 0001, so it must be an ADD instruction. We know that the destination register must be R5, so there are two possibilites we can consider:

  • R5 <- R? + R?
  • R5 <- R? + SEXT(imm5)

    We can rule out the first option, as there are no two registers which added together equal xFFF8.

    Note that R0 + xFFF8 = xFFF8 = -8 (decimal).
    Also, note that -8 as a 5-bit immediate number = 11000, and that if we sign extend this value we get 1111111111111000 = xFFF8. So our ADD instruction should be: R5 <- R0 + -8

    Thus, our instruction looks like:

    0001 101 000 1 11000

    Problem 3

    Write an LC-3 program that finds the two's complement of a number stored in memory and stores the result back in memory. The number is stored in memory location x301F. Store the result in x301E. For the screenshot, make the value in memory location x301F be x4321

    Problem 4

    Write an LC-3 program that that compares two numbers stored in memory at locations x301E and x301F. If the numbers are equal, then R1 is set equal to 1, else R1 is set equal to 0. For the screenshot, make the value in both memory locations (x301E and x301F) be x1234.

    Problem 5

    Write an LC-3 program that searches for odd numbers in memory locations ranging from x3100 to x3110 (inclusive). Set R0 to the number of odd numbers that are found. Your solution should use a looping construct like the examples in lecture (i.e. if you were asked to examine locations ranging from x3100 to x3120, the number of instructions in your program would not change).

    Use the following file as your data file for this problem: hw6p5_data_f07.obj. In the simulator, you will need to load this file under "File -> Load Program" in addition to your program file. You do not need to capture the data from this file in your screenshot.

  •  
    Computer Sciences | UW Home