Exercise 3: Adding 200 to a Value in Memory — Possible Solution ==================================================================== THE CORRECT SEQUENCE ------------------------------ LD R1, COUNT ; load COUNT's current value into R1 LD R2, TWOHUNDRED ; load the constant 200 into R2 ADD R1, R1, R2 ; R1 <- R1 + R2 (register mode addition) ST R1, COUNT ; store the result back to COUNT ... COUNT .FILL #0 TWOHUNDRED .FILL #200 WHY A SINGLE IMMEDIATE ADD CANNOT DO THIS ------------------------------ Per this chapter's own warn-box, LC-3's immediate field for ADD/AND is only 5 bits wide. A signed 5-bit value can only represent -16 through 15 -- nowhere close to 200. Because 200 can't fit in that 5-bit field at all, ADD R1, R1, #200 is not just impractical, it is literally not expressible in LC-3's immediate-mode encoding -- the assembler would reject it outright. WHY THE CONSTANT NEEDS ITS OWN MEMORY LOCATION ------------------------------ Since 200 can't be embedded as an immediate operand, it has to be stored in memory instead (using .FILL, as previewed back in assembly1-1's own pipeline example) and loaded into a register with LD, exactly the way COUNT's own current value has to be loaded before ADD can touch it -- per assembly1-4's load/store architecture rule, ADD can only ever operate on values already sitting in registers, so both operands (COUNT's value and the constant 200) have to be loaded into registers before the register-mode ADD can add them together. WHY THIS WORKS AS AN ANSWER ------------------------------ It gives a complete, correctly-ordered instruction sequence using only valid LC-3 instructions, and explains the immediate-mode constraint concretely (5 bits caps the range at -16 to 15, and 200 simply doesn't fit) rather than vaguely describing it as "too big."