Academic Integrity: tutoring, explanations, and feedback — we don’t complete graded work or submit on a student’s behalf.

The following LC-3 program checks if the integer stored at memory location “x500

ID: 3808415 • 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) (4 Points) 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) (2 points) 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) (2 points) 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)

i) LDI R0, Store_x5000

Store_x5000 .FILL x5000

ii) AND R1,R1,#0

ADD R1,R1,#3

iii) NOT R1,R1

LOOP ADD R0,R0,R1

iv) BRp LOOP

v) BRn HALT

vi) AND R2,R2,#0

ADD R2,R2,#1

vii) STI R2, Store_x5001

Store_x5001 .FILL x5001

2. It will not work for negative integers.

If we subtract 3 , it will go on decreasing and will program will be eventually stuck in endless loop.

3. We can initially check if the number is positive or negative. Based on the sign of the number, we will run two different routines,one for positive numbers and one for negative numbers.