In: Computer Science
PLEASE SOLVE I WILL RATE AND THUMBS UP
Assembly code question
Write an LC3 program to compute the XOR (exclusive OR) operation. The program computes the XOR of two numbers stored in registers R0 and R1 and returns the result in register R3. To test your program, before computing the XOR load the two values from memory locations x4000 (into R0) and x4001 (into R1).
LC3 has very minimal instruction set. In fact only 15. XOR instruction is not provided so unlike other processors like 8086 we can't straight away use xor instruction.
So we have to implement xor in terms of NOT and AND operation provided by the architecture.
Lets first understand XOR operation it is basically if one of the two input is 1 and other is 0 then the result is 1 else 0
It can be expressed as
(A AND NOT(B)) OR (NOT(A) AND B)
Since one of the input is one and other 0 we find two terms by
anding one input with not of other. If one input is true and other
0 then this will give us 1. Since it can be two ways i.e. A is 1
and B 0 or A is 0 and B 1 so we calculate two terms. If any one of
this is true it indicates that one inuput was 1 and other was 0.
This is achieved by oring of the two arrived terms. This gives us
equivalent of XOR of two input.
SO A XOR B can be expressed as
(A AND NOT(B) ) OR (NOT(A) AND B) ------------------------ 1
But we still have an issue, there is no OR instruction provided by architecture. We use De Morgan Theorm here. One of the two theorems state that NOR can be expressed as and of there complements. i.e.
NOT ( X OR Y ) = NOT (X) AND NOT(Y)
Taking NOT of Terms on both sides we have
=> X OR Y = NOT ( NOT (X) AND NOT (Y) ) -------------------- 2
So we now have OR expressed as NOT and AND operation. We use this to solve what we got in 1. i.e
(A AND NOT(B) ) OR (NOT(A) AND B)
expessing or as combination of NOT and AND as in 2 we get
NOT ( NOT (A AND NOT(B) ) AND NOT (NOT(A) AND B) ) ------------ 3
So expressing the above terms from inside out we have.
X = NOT (B)
P = A AND X --- A AND NOT(B)
I = NOT ( P ) --- NOT ( A AND NOT (B) )
Y = NOT (A)
Q = Y AND B --- NOT(A) AND B
J = NOT ( Q) --- NOT ( NOT(A) AND B )
S = I AND J --- NOT ( A AND NOT (B) ) AND NOT ( NOT(A)
AND B )
T = NOT (S) --- NOT ( NOT ( A AND NOT (B) ) AND NOT ( NOT(A) AND B
) )
Last operation gives us what we had in equation ------ 3.
So based on above steps here is the program using LC3 pnemonics
Let R0 has first input A and R1 has second input B
NOT R1,R1 ; NOT (B)
AND R3,R0,R1 ; R3 = A AND NOT(B)
NOT R1,R1 ; Reverting R2 to original value of B
NOT R0,R0 ; NOT(A)
AND R4,R0,R1 ; NOT(A) AND B
NOT R0,R0 ; Not really required but only to ensure R1 retains
original A value.
NOT R3,R3 ; NOT ( A AND NOT(B) )
NOT R4,R4 ; NOT ( NOT (A) AND B)
AND R3,R3,R4 ; NOT ( A AND NOT (B) ) AND NOT ( NOT(A) AND B )
NOT R3,R3 ; NOT ( NOT ( A AND NOT (B) ) AND NOT ( NOT(A) AND B )
)
;R3 has the R0 XOR R1 i.e. A XOR B
Now to test this xor operation on value stored in memory location 0x4000 Here is the program with description.
There is just one thing to note. In question it is given the
location to be x4000 and x4001 , i think it should be x4000 and
x4002 as every regiseter and memory load is 16 bits i.e. 2 bytes.
Let me know if this was not the case.
So below program should load from 0x4000 and 0x4002 and xor them
and produce the result in regiser R3
; just chosing usual 3000 as start address
.ORIG x3000
LDI R3, A ; Load content of A location in R3 i.e. x4000
LDR R0, R3, #0 ; load content of memory whose address is in
R3
LDR R1, R3, #2 ; Load content of memory whose address is in R3 + 2
i.e. 0x4002
JSR CALCXOR ; Calls subroutine CALCXOR which performs xor on
content of R1 and R2
HALT
CALCXOR NOT R1,R1
AND R3,R0,R1
NOT R1,R1
NOT R0,R0
AND R4,R0,R1
NOT R0,R0
NOT R3,R3
NOT R4,R4
AND R3,R3,R4
NOT R3,R3
RET
A .FILL x4000
.END