ECE 206-001
Name:___________________________________________
NC State University
Exam 2 (A) – Nov. 4, 2003
3
1.
(21 points)
You’re going to write an LC-3 subroutine that shifts the value in R0 to the right.
The
value in R1 indicates the number of bits to shift.
(We’ll assume that R1 holds a value between 1 and
15.)
For each box below, write LC-3 assembly language to perform the task indicated in the
lefthand column.
Some of the boxes are already filled in.
Start subroutine at location x3500
.ORIG x3500
Save registers R1, R2, R3, and R4.
ST R1, SaveR1
ST R2, SaveR2
ST R3, SaveR3
ST R4, SaveR4
Set R2 = 1. Set R3 = 1.
Set R4 = R0.
Set R0 = 0.
AND R2, R2, #0
ADD R2, R2, #1
ADD R3, R2, #0
ADD R4, R0, #0
AND R0, R0, #0
Shift R2 one bit to the left.
L1
ADD R2, R2, R2
Decrement R1.
If the result is > 0,
go back to the previous box.
ADD R1, R1, #-1
BRp L1
AND R4 with R2.
If the result
is non-zero, add R3 to R0.
L2
AND R1, R2, R4
BRz L3
ADD R0, R0, R3
Shift R3 one bit to the left.
Shift R2 one bit to the left.
If R2 is non-zero,
go back to the previous box.
ADD R3, R3, R3
ADD R2, R2, R2
BRnp L2
Restore R1, R2, R3, and R4.
LD R1, SaveR1
LD R2, SaveR2
LD R3, SaveR3
LD R4, SaveR4
Return to caller.
RET
; or JMP R7
Allocate space to
save registers
SaveR1
.BLKW 1
SaveR2
.BLKW 1
SaveR3
.BLKW 1
SaveR4
.BLKW 1
End of program.
.END