Practice Questions

Question 0: Warming up with Fruit Loops

Write an LC-3 program that adds up the numbers from 1 to 10 (i.e., computes 1+2+3+4+5) and stores the result in R3.

a) Write C code

b) Write LC3 code

c) Draw a circuit for adding three 3bit unsigned numbers eg-1+2+3. For simple and clean looking circuit lets use 3bit unsigned representation for those numbers.

Question 1: Coding and Circuit Design

You are given an unsigned integer n. Return the number of 1 bits in its binary representation. You may assume n is a non-negative integer which fits within 16-bit.

eg- input:0000000000010101 gives output 3

eg- input 0000000000000000 gives output 0

a) Write C code?

b) Write that code in LC3? Feel free to hardcode the inputs into a label


Question 2: Coding and Circuit Design

You are given an unsigned 5 bit integer n. Pop and discard all the bits at even positions. Note: count the bit position as (1st bit, 2nd bit, 3rd bit, etc.) instead of starting the counting from 0th bit

eg- input 01110 will ouput 00010

eg- input 11111 will output 00111

eg- input 00000 will output 00000

eg- input 10100 will output 00110

a) Write LC3 code that does that?

b) Draw a circuit for that problem. Feel free to hardwire the input and output lines. (Hint: dont overthink)

d) What would be faster to run (LC3, circuit) and explain why?


Question 3: Floating-Point Precision

A developer is designing a system for a high-altitude drone. The altitude is stored as a 16-bit value.

a) The drone reaches an altitude of 32,968 meters. Explain why Format A fails to represent this number.

b) If the drone is at an altitude of exactly 1.5 meters, show the bit representation for Format B

c) The developer needs to perform a bitwise AND operation between the altitude and a mask. Which format allows for a predictable result using standard C bitwise operators? Justify your answer.


Question 4: Symbol Tables

Consider the following LC-3 Assembly code snippet starting at memory address

.ORIG x3000
        LD R1, VALUE
LOOP    BRz DONE
        ADD R1, R1, #-1
        STR R1, R2, #0
        ADD R2, R2, #1
        BRnzp LOOP
DONE    HALT
VALUE   .FILL x0005
        .END

a) Construct the Symbol Table including the Label and its corresponding Hex address.

b) When the assembler encounters the instruction BRnzp LOOP. How would it translate this specific instruction into its 16-bit binary machine code.


Question 5: Memory and Circuits

You are building a small memory unit that contains ​8 words​, where each word is 4 bits wide.

a) To select exactly one of these 8 words based on an address, what specific circuit must you use, and how many "Address Bits" are required for the input (for controlling what to output)?

b) You decide to implement a "Write Enable" feature using a Gated D-Latch. Explain why a raw S-R Latch is insufficient for a memory word that needs to hold its value while other words are being updated.

c) Describe the "Word Read" process: How does our circuit ensure that only the bits from the selected address appear on the output and not bits from the other 7 words? (Simply describe the circuit name and how its used in few lines)


Question 6: C Structs, Pointers, and Memory Mapping

You are given the following C code

struct Node {
    int data;
    struct Node* next;
};

void main() {
    struct Node a;
    struct Node b;
    a.data = 5;
    a.next = &b;
}

a) In memory, struct Node a starts at address x4000. If an integer and a pointer both take (16 bits), what are the exact memory addresses for a.data and a.next?

b) Write the LC-3 Assembly equivalent for the C statement a.next = &b;. Assume the address of b is stored in R0 and the base address of a is in R1.


Question 7 LC-3 Programming

The LC-3 memory starting at address x4000 contains an array of 8 signed integers (16-bit 2's complement). The array is terminated by a value x0000.

(a) Write an LC-3 assembly program that:

You may use R0–R5 freely. Mak sure to clearly label all instructions.

(b) Identify and explain the role of each addressing mode your solution uses. If your solution does not use at least two distinct modes, rewrite the relevant portion so it does.

(c) Rewrite only the loop termination condition check in C (just the if/while expression, not the full program) that is logically equivalent to your LC-3 sentinel check.


Question 8: LC3 Reading and Writing to Memory

An LC-3 program stores a student's score at memory address x4000.

(a) Write LC-3 instructions to:

(b) The value stored at x4000 is x001E (in hex). What is this in decimal? After adding 10, what is the new value in both decimal and hex?

(c) A student uses LD R0, R0, #0 to load from memory. What is wrong with this instruction? What should they use instead?


Question 9: LD, LDR, LEA, LDI: What's the Difference?

Here is a snapshot of LC-3 simulator memory:

Address   Value/instruction
x3050     LABE_AT_x3050 .fill x4100      			;a pointer (stores an address)
;some fishy but useless code goes here
x3000.    LD R1, LABE_AT_x4100
;some sharky but useless code on here
x4100     LABE_AT_x4100 .fill x0009      			;the actual value we want

Your program starts at x3000.

(a) Whats wrong in above code (apart from syntax errors). (Hint something's wrong with LD)

(b) How can we fix the above issue using different instruction (Hint think about other label)

(c) Match each C snippet to the LC-3 instruction that best describes what it does (LD, LDR, LEA, LDI). Give one reason for each.

// Snippet 1
int x = score;          // score is a nearby variable
// Snippet 2
int *p = &score;        // get the address of score, not its value
// Snippet 3
int x = array[i];       // i is already sitting in a register
// Snippet 4
int x = *ptr;           // ptr is stored in memory, not in a register

Part (d)

Robert from Robert movie says: "Why do we have four load instructions? Isn't one enough?"

In few sentences, explain why we need all four. Your answer must talk about following (or else Robert would get angry)