LC-3 Assembly OR operation

40 views Asked by At

Displays a newline followed by "Enter First Number (4 binary digits): " as a prompt for the user to input a four-digit binary number.

Accepts user input for the first number, echoing each digit to the console. It checks for valid binary digits (0 or 1) and waits until it receives four valid digits.

Displays a newline followed by "Enter Second Number (4 binary digits): " as a prompt for the user to input the second four-digit binary number.

Accepts user input for the second number, echoing each digit to the console. It checks for valid binary digits (0 or 1) and waits until it receives four valid digits.

If the user types "q" at any time, the program displays a newline followed by "Thank you for playing!" and then halts.

Performs a logical OR operation on the first and second numbers entered by the user, converting them to binary for the operation. It then converts the result to a four-digit binary number and converts the digits to ASCII code.

Displays a newline followed by "The OR function of the two numbers is: zzzz" where zzzz is the four-digit binary number obtained from the OR operation. A newline is printed at the end of the string.

Im stuck at the OR operation.

This is my code:

.ORIG x3000
LEA R5 , SECOND_NUMBER
LEA R6, FIRST_NUMBER 
LEA R0, PROMPT1      ; Load address of prompt1 into R0.
PUTS                 ; Print prompt1.
ADD R1, R1, #0
LD R3, MAX_DIGITS   ; Load the maximum number of digits allowed.
LD R4, MAX_DIGITS2
LOOP GETC                 ; Read in a single character to R0.
OUT                   ; Echo the character.
ADD R1,R1, #0
LD R1, ASCII_q       ; Load ASCII value of 'q' into R1.
NOT R1, R1                   ; Invert the ASCII value of 'q'.
ADD R1, R1, #1               ; Add 1 to get two's complement of -113.
ADD R2, R0, R1               ; Subtract the input character from the two's complement of -113.
BRz THANKS1                  ; If zero, branch to THANKS1.
ADD R1, R1, #0
LD R1, ASCII_1               ; Load ASCII value of '1' into R1.
NOT R1, R1                   ; Invert the ASCII value of '1'.
ADD R1, R1, #1               ; Add 1 to get two's complement of -31.
ADD R2, R0, R1               ; Subtract the input character from the two's complement of -31.
BRz LOAD1                   ; If input is '1', branch to STORE1.
ADD R1, R1, #0
LD R1, ASCII_0               ; Load ASCII value of '0' into R1.
NOT R1, R1                   ; Invert the ASCII value of '0'.
ADD R1, R1, #1               ; Add 1 to get two's complement of -30.
ADD R2, R0, R1               ; Subtract the input character from the two's complement of -30.
BRz LOAD2                  ; If input is '0', branch to STORE0.
BRnp LOOP

LOAD1:
LD R0, ONE              ; Load the value ONE into R0
BRnzp STORE

LOAD2:
LD R0 , ZERO
BRnzp STORE

STORE:
STR R0, R6, 0
ADD R6, R6, 1
ADD R3, R3, #-1         ; Decrement count
BRz INPUT2                ; If count reaches zero, INPUT2
BRp LOOP

INPUT2:
LEA, R0, PROMPT2
PUTS
LOOP2 GETC                 ; Read in a single character to R0.
OUT                   ; Echo the character.
ADD R1,R1, #0
LD R1, ASCII_q       ; Load ASCII value of 'q' into R1.
NOT R1, R1                   ; Invert the ASCII value of 'q'.
ADD R1, R1, #1               ; Add 1 to get two's complement of -113.
ADD R2, R0, R1               ; Subtract the input character from the two's complement of -113.
BRz THANKS1                  ; If zero, branch to THANKS1.
ADD R1, R1, #0
LD R1, ASCII_1               ; Load ASCII value of '1' into R1.
NOT R1, R1                   ; Invert the ASCII value of '1'.
ADD R1, R1, #1               ; Add 1 to get two's complement of -31.
ADD R2, R0, R1               ; Subtract the input character from the two's complement of -31.
BRz LOAD3                   ; If input is '1', branch to STORE1.
ADD R1, R1, #0
LD R1, ASCII_0               ; Load ASCII value of '0' into R1.
NOT R1, R1                   ; Invert the ASCII value of '0'.
ADD R1, R1, #1               ; Add 1 to get two's complement of -30.
ADD R2, R0, R1               ; Subtract the input character from the two's complement of -30.
BRz LOAD4  

THANKS1:                     ; Label for thanks message
LEA R0, THANKS               ; Load the address of the thanks message into R0.
PUTS                         ; Display the thanks message.
HALT

LOAD3:
LD R0, ONE              ; Load the value ONE into R0
BRnzp STORE1

LOAD4:
LD R0 , ZERO
BRnzp STORE1

STORE1:
STR R0, R5, #0
ADD R5, R5, #1
ADD R4, R4, #-1         ; Decrement count
BRz ORFUNCTION                ; If count reaches zero, INPUT2
BRp LOOP2

ORFUNCTION:

LEA R1, FIRST_NUMBER    ; Load address of FIRST_NUMBER into R1
LEA R2, SECOND_NUMBER   ; Load address of SECOND_NUMBER into R2
LEA R3, RESULT          ; Load address of RESULT into R3

LD R5, FOUR             ; Counter for looping through each bit
LD R6, MASK             ; Load mask for isolating each bit

LOOP3:  
    LDR R4, R1, #0      ; Load a bit from FIRST_NUMBER
    LDR R4, R4, #0      ; Load a bit from SECOND_NUMBER
    AND R4, R4, R6      ; Apply the mask to isolate each bit
    STR R4, R3, #0      ; Store the result in RESULT
    
    ADD R1, R1, #1      ; Move to the next bit in FIRST_NUMBER
    ADD R2, R2, #1      ; Move to the next bit in SECOND_NUMBER
    ADD R3, R3, #1      ; Move to the next bit in RESULT
    
    ADD R5, R5, #-1     ; Decrement counter
    BRp LOOP3           ; Repeat the loop while counter is positive

    ; Display the result in the console
    LEA R0, RESULT_PROMPT ; Load address of the result string into R0
    PUTS                  ; Print the result string
    ADD R0, R3, #0
    OUT

    HALT




MASK .FILL x0001
FOUR .FILL #4
ONE .FILL x0001
ZERO .FILL x0000
RESULT .BLKW #4
MAX_DIGITS .FILL #4
MAX_DIGITS2 .FILL #4
FIRST_NUMBER    .BLKW 4   ; Array to store the first number digits
SECOND_NUMBER   .BLKW 4   ; Array to store the second number digits
THANKS: .STRINGZ "\n THANKS FOR PLAYING"
PROMPT1: .STRINGZ "\n ENTER FIRST NUMBER (4 binary digits):"
PROMPT2: .STRINGZ "\n ENTER SECOND NUMBER (4 binary digits):"
RESULT_PROMPT .STRINGZ " \n The OR function result is:  "
ASCII_0 .FILL x0030
ASCII_1 .FILL x0031
ASCII_q .FILL #113
NEG_Q .FILL #-113
.END

I'm expecting a result of 4 bits that came from my or operation

0

There are 0 answers