F.6
Chapter 6 Solutions
6.1 Yes, for example, an iterative block where the test condition remains true for each iteration.
This procedure will never end and is therefore not Fnite and not an algorithm. The following
is an example of a procedure that isn’t an algorithm:
x3000 0101 000 000 1 00000 ( LOOP AND
R0, R0, #0 )
x3001 0000 010 111111110
(
BRz LOOP )
This is not an algorithm because the branch instruction is always taken and the program loops
indeFnitely.
6.2 Problem Statement: Subtract the value in R1 from the value in R2 and store the result in R0.
The following is the systematic decomposition of the problem concluding with an LC-3 pro-
gram solving the problem.
(a)
Start -> Subtract R1 from R2; place result in R0
-> End
(b)
Start -> Compute the complement of the value in R1
-> Add this result to R2 and store the result in R0
-> End
(c)
Start -> Negate R1 -> Add 1 to R1 ->
Add R1 and R2; store result in R0 -> End
(d)
1001 001 001 111111 ( NOT R1, R1 )
0001 001 001 1 00001 ( ADD R1, R1, #1 )
0001 000 001 0 00 010 ( ADD R0, R1, R2 )
6.3 The following program uses DeMorgan’s Law to set the appropriate bits of the machine busy
register.
x3000 1010000000001110 (
LDI R0, S )
x3001 1010001000001110 (
LDI R1, I )
x3002 0101010010100000 (
AND R2, R2, #0 )
x3003 0001010010100001 (
ADD R2, R2, #1 )
x3004 0001001001111111 ( L ADD R1, R1, #-1 )
x3005 0000100000000010 (
BRn D )
x3006 0001010010000010 (
ADD R2, R2, R2 )
x3007 0000111111111100 (
BRnzp L )
x3008 0001001010100000 ( D ADD R1, R2, #0 )
x3009 1001000000111111 (
NOT R0, R0 )
x300a 1001001001111111 (
NOT R1, R1 )
x300b 0101000000000001 (
AND R0, R0, R1 )
x300c 1001000000111111 (
NOT R0, R0 )
x300d 1011000000000001 (
STI R0, S
)
1