Assume that you have the following table in your program:
MASKS .FILL x0001
.FILL x0002
.FILL x0004
.FILL x0008
.FILL x0010
.FILL x0020
.FILL x0040
.FILL x0080
.FILL x0100
.FILL x0200
.FILL x0400
.FILL x0800
.FILL x1000
.FILL x2000
.FILL x4000
.FILL x8000
Write a subroutine CLEAR in LC-3 assembly language that clears
a bit in R0 using the table above. The index of the bit
to clear is specified in R1. R0 and R1
are inputs to the subroutine.
Write a similar subroutine SET that sets the specified bit instead of clearing it.
Hint: You should remember to save and restore any registers your subroutine
uses (the "callee save" convention). Use the RET instruction as
the last instruction in your subroutine (R7 contains the address
of where in the caller to return to.)
Suppose we are writing an algorithm to multiply the elements of an array (unpacked, 16-bit 2's complement numbers), and we are told that a subroutine "mult_all" exists which multiplies four values, and returns the product. The mult_all subroutine assumes the source operands are in R1, R2, R3, R4, and returns the product in R0. For purposes of this assignment, let us assume that the individual values are small enough that the result will always fit in a 16-bit 2's complement register.
Your job: Using this subroutine, write a program to multiply the set of values contained in consecutive locations starting at location x6001. The number of such values is contained in x6000. Store your result at location x7000. Assume there is at least one value in the array(i.e., M[x6000] is greater than 0).
Hint: Feel free to include in your program
PTR .FILL x6001
CNT .FILL x6000
.ORIG x3000
JSR A
OUT ;TRAP x21
BRnzp DONE
A AND R0,R0,#0
ADD R0,R0,#5
JSR B
RET
DONE HALT
ASCII .FILL x0030
B LD R1,ASCII
ADD R0,R0,R1
RET
.END
PUSH V
PUSH W
PUSH X
PUSH Y
MUL
ADD
PUSH Z
SUB
DIV
POP U
Address Contents
x4000 x4016
x4001 x4003
x4002 x4008
x4003 x004D
x4004 x0061
x4005 x0072
x4006 x0063
x4007 x0000
x4008 x0039
x4009 x0030
x400A x0000
x400B x0000
x400C x4019
x400D x401E
x400E x004A
x400F x0061
x4010 x0063
x4011 x006B
x4012 x0000
x4013 x0031
x4014 x0038
x4015 x0000
x4016 x400B
x4017 x400E
X4018 x4013
x4019 x004D
x401A x0069
x401B x006B
x401C x0065
x401D x0000
x401E x0037
x401F x0036
x4020 x0000
.ORIG x3000
LEA R0, DATA
AND R1, R1, #0
ADD R1, R1, #9
LOOP1 ADD R2, R0, #0
ADD R3, R1, #0
LOOP2 JSR SUB1
ADD R4, R4, #0
BRzp LABEL
JSR SUB2
LABEL ADD R2, R2, #1
ADD R3, R3, #-1
BRp LOOP2
ADD R1, R1, #-1
BRp LOOP1
HALT
DATA .BLKW #10
SUB1 LDR R5, R2, #0
NOT R5, R5
ADD R5, R5, #1
LDR R6, R2, #1
ADD R4, R5, R6
RET
SUB2 LDR R4, R2, #0
LDR R5, R2, #1
STR R4, R2, #1
STR R5, R2, #0
RET
.END
.ORIG x3000
AND R0, R0, #0
LD R1, SIXTEEN
LD R2, WORD
A BRn B
________________
B ________________
BRz C
________________
BR A ; note: BR = BRnzp
C ST R0, RESULT
HALT
SIXTEEN .FILL #16
WORD .BLKW #1
RESULT .BLKW #1
.END
MUL _______________
ST R0, SAVER0
ST R1, SAVER1
ST R2, SAVER2
ST R5, SAVER5
AND R2, R2, #0
JSR POP
ADD R1, R0, #0
JSR POP
ADD R1, R1, #0
_______________
AGAIN ADD R2, R2, R0
_______________
BRp AGAIN
DONE ADD R0, R2, #0
JSR PUSH
_______________
LD R0, SAVER0
LD R1, SAVER1
LD R2, SAVER2
LD R5, SAVER5
RET
.ORIG x3000
AND R2, R2, #0
LD R3, NUM
BRz OUTPUT
NOT R3, R3
ADD R3, R3, #1
OUTLOOP ADD R2, R2, #1
_______________
AND R1, R1, #0
INLOOP ADD R1, R1, R2
ADD R0, R0, #-1
BRp INLOOP
_______________
BRn OUTLOOP
OUTPUT LD R0, ZERO
_______________
TRAP x21
HALT
NUM .BLKW 1
ZERO .FILL x30
.END

| R0 = x0000 PC = x3000 | KBSR = x8000 KBDR = x0061 DSR = x8000 DDR = x0031 | M[x3009] = xFE00 M[x300A] = xFE02 M[x300B] = xFE04 M[x300C] = xFE06 |
| PC | Instruction | Access | MAR | A | B | C | D | E[15:0] | F[1] | F[0] | MDR |
| x3000 | LD R0, x9 | 1 | x3000 | x2009 | |||||||
| 2 | |||||||||||
| 3 | |||||||||||
| x3001 | LDR R0, R0, #0 | 1 | |||||||||
| 2 | |||||||||||
| 3 |
.ORIG x3000
LD R0, INPUT
AND R3, R3, #0
LEA R6, MASKS
LD R1, COUNT
LOOP LDR R2, R6, #0
ADD R3, R3, R3
AND R5, R0, R2
BRz SKIP
ADD R3, R3, #1
ADD R0, R5, #0
SKIP ADD R6, R6, #1
ADD R1, R1, #-1
BRp LOOP
ST R3, RESULT
HALT
COUNT .FILL #4
MASKS .FILL 0xFF00
.FILL 0xF0F0
.FILL 0xCCCC
.FILL 0xAAAA
INPUT .BLKW 1
RESULT .BLKW 1
.END
.ORIG X3000 LEA R2, C LDR R1, R2, #0 LDI R6, C LDR R5, R1, #-3 ST R5, C LDR R5, R1, #-4 LDR R0, R2, #1 JSRR R5 AND R3, R3, #0 ADD R3, R3, #7 LEA R4, B A STR R4, R1, #0 ADD R4, R4, #2 ADD R1, R1, #1 ADD R3, R3, #-1 BRP A HALT B ADD R2, R2, #1 LDR R0, R2, #0 JSRR R5 TRAP X29 ADD R2, R2, #15 ADD R0, R2, #3 LD R5, C TRAP X2B ADD R2, R2, #5 LDR R0, R2, #0 JSRR R5 TRAP X27 JSRR R5 JSRR R6 C .FILL X25 .STRINGZ "EE306 and tests are awesome" .END