The following LC-3 program checks if the integer stored at memory location “x500
ID: 3808527 • Letter: T
Question
The following LC-3 program checks if the integer stored at memory location “x5000" is divisible by 3. If yes, it stores the value 1 at memory location “x5001”, else it does nothing. Assume all the registers and the memory location “x5001” have an initial value of 0.
a)Fill in the missing instructions of the code.
Suggestion: Verify your solution by running it in PennSim.
0011 0000 0000 0000 ; Program starts at x3000
(i) ____ ____ ____ ____ ; Load value at x5000 into R0
(ii) ____ ____ ____ ____ ; Initialize R1 to 3
(iii) ____ ____ ____ ____ ; LOOP: calculate R0 - R1
(iv) ____ ____ ____ ____ ; Branch if positive to LOOP
(v) ____ ____ ____ ____ ; Branch if negative to HALT
(vi) ____ ____ ____ ____ ; Initialize R2 to 1
(vii) ____ ____ ____ ____; Store R2 to x5001
1111 0000 0010 0101 ; HALT
0101 0000 0000 0000 ; DATA1: x5000
(viii)____ ____ ____ ____; DATA2: x5001
b) After the above program finishes execution, a value of 0 would mean that the integer at x5000 is not a multiple of 3.
Does this program correctly identify all integers which are multiples of 3? If not, specify the integer(s) for which the program doesn’t work, and why it does not work for those value(s).
c) Briefly explain how you can fix the bug(s) identified in part b) ? Mention the names of the LC3 instructions, you would use to fix the bug.
Explanation / Answer
a)
LD R0 x5000;
LD R1 3;
LOOP ADD R0 R0 -R1
BR LOOP
b)
No. it doesn't identify all multiples of 3, it doesn't work on negetive integers
c)
We could use BR to branch if its positive or negetive, if the number is negetive we can initialize out loop variable R0 as negetive.