Question

In: Computer Science

PLEASE SOLVE I WILL RATE AND THUMBS UP Assembly code question Write an LC3 program to...

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).

Solutions

Expert Solution

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


Related Solutions

PLEASE SOLVE I WILL THUMBS UP AND RATE YOUR ANSWER WELL For the following two C...
PLEASE SOLVE I WILL THUMBS UP AND RATE YOUR ANSWER WELL For the following two C functions: int q1(int x) { int m8 = 0x55; int m16 = m8 | m8 << 8; int m32 = m16 | m16 <<16; int z = x | m32; return !(~z); } int q2(int x) { int m8 = 0x55; int m16 = m8 | m8 << 8; int m32 = m16 | m16 <<16; int z = x & m32; return !!z;...
PLEASE ANSWER I WILL RATE YOUR ANSWER AND THUMBS UP For the following C functions: int...
PLEASE ANSWER I WILL RATE YOUR ANSWER AND THUMBS UP For the following C functions: int q7(int x) {     return x & (~x+1); } int q8(int x, int m, int n) {     int a = ~m+1;     int b = ~x +1;     a = x + a;     b = b + n;     return !((a|b) >> 31); } int q9(int x, int n) {    /* assume x and n are not a negative integer */...
Please! I want the instructions of how to solve it, not the answer. Write a program...
Please! I want the instructions of how to solve it, not the answer. Write a program that does the following in order: 1. Asks the user to enter a name 2. Asks the user to enter a number “x” 3. Asks the user to enter a number “y” 4. Calculates the sum of “x” and “y” 5. Prints out the number for “x”, “y” and “sum” An example of the program input and output is shown below: Enter your name:...
Write MIPS assembly code for the following C code. for (i = 10; i < 30;...
Write MIPS assembly code for the following C code. for (i = 10; i < 30; i ++) { if ((ar[i] > b) || (ar[i] <= c)) ar[i] = 0; else ar[i] = a; }
write a program for the microcontroller-msp430fr6989 using code composer studio not assembly language. write a code...
write a program for the microcontroller-msp430fr6989 using code composer studio not assembly language. write a code that transmits a single character and lights the red LED upon receiving that character. The board will "talk" to itself. The green LED should turn on whenever a message is sent and the LCD will display the message being received.
Please answer this question correctly and quickly for a thumbs up. In the Molly Anderson article...
Please answer this question correctly and quickly for a thumbs up. In the Molly Anderson article assigned for this class, Professor Anderson describes her food systems vision. Thinking about your own food systems vision, what are three things you would like to change or affect in the food system? Please respond by listing one thing you would like to do individually, one thing you would like society to do collectively, and one thing you think the UW could do to...
Assembly code Write an assembly program that converts all uppercase letters to their corresponding lower cases....
Assembly code Write an assembly program that converts all uppercase letters to their corresponding lower cases. meanwhile all characters that are not upper case letters should remain unchanged. Hint: ASCII: a = 97, z = 122, A = 65, Z = 90 String : "Riders On The Storm - The Doors"
write a assembly language program to convert GRAY to BCD code in 8051
write a assembly language program to convert GRAY to BCD code in 8051
please leave alot of quality information and i will provide a thumbs up in return thank...
please leave alot of quality information and i will provide a thumbs up in return thank you 1. Joan is an Accountant who opposes the introduction of a new financial control system. For 15 years she has worked with a manual system. Now the firm is introducing a new computer-based system. How would you, as her manager, attempt to change Joan's attitude towards the new system? 2. Some people believe that perception is a more important explanation of behavior than...
Write a RARS assembly language program to solve the following: For a set of integers stored...
Write a RARS assembly language program to solve the following: For a set of integers stored in an array, calculate the sum of the positive numbers and the sum of the negative numbers, storing the results into memory. In the data segment of the program, define an array consisting of 25 integers (words) that are a mix of positive and negative values. You can select any values you wish but try to create a balance of positive and negative numbers....
ADVERTISEMENT
ADVERTISEMENT
ADVERTISEMENT