LC-3 Assembly Calculator

60 views Asked by At

What am I getting this output:

ENTER FIRST OPERAND : 4

ENTER SECOND OPERAND: 2

ADDITION: 6

SUBTRACTION: 2

MULTIPLICATION:

DIVISION:

----- Halting the processor -----

From this code:

    .ORIG x3000
;
; Program to add two numbers. Prompt user to enter the numbers.
; The numbers are ASCII characters. Convert them to integers by
; subtracting x30 from the ASCII code. Store the operands in
; registers R1 and R2.
; Add the two integers. Store the sum in R0. Convert the sum to
; an ASCII character by adding x30 to it. Otherwise, you will not
; be able to print it.
;

    ld R3, MINUSx30 ; R3 <- x-30 (ASCII 0)
    ld R6, PLUSx30

; GET THE FIRST OPERAND
    lea R0, getop1 ;Prompt user
    puts
    getc ; Read the character
    out ; Echo the character
    add R1, R0, R3 ; first operand is in R1

; GET THE SECOND OPERAND
    lea R0, getop2
    puts
    getc ; Read the next character
    out ; Echo the character
    add R2, R0, R3 ; second operand in R2

; OUTPUT A NEXT-LINE CHARACTER (10)
    and R0, R0, #0
    add R0, R0, #10 ;#10 is Line feed in ASCI
    out

; Output a Message
        LEA R0, ADD_RESULT
        PUTS

; ADD the two numbers, Convert result to ASCII & Display
    add R0, R1, R2 ; R0 <- R1 + R2
    ld R3, PLUSx30 ; R3 <- x30
    add R0, R0, R3 ; R0 <- R0 +x30
    out ; Output result

;;;;;;;;;;;;;;;;;;;;;; ADDITION WORKS NO TOUCH

;Get twos compliment of second operand
        NOT R2, R2
    ADD R2, R2, #1
; Output a Message
        LEA R0, SUB_RESULT
        PUTS

; Perform subtraction by addition
    ADD R0,R1,R2    ; Perform operation
    BRZP POS        ; Result is positive 
    ADD R4, R0, #0  ; Result is negative. Save R0
    
; Result is negative, display ‘-‘ sign
        LEA R0, MINUS   ; Result negative. Display '-'
        PUTS
        ADD  R0, R4, #0 ; R0 <- R4
        NOT R0, R0      ; Take twos complement of result
        ADD R0, R0, #1
        POS LD R3, PLUS30
        ADD R0, R3, R0  ;Converts to ASCII digit
        OUT         ;Display Result

;;;;;;;;;;;;;;;;;;;;;; SUBTRACTION WORKS NO TOUCH
LEA R0, MUL_RESULT  ; Display multiplication result message
        PUTS
        AND R3, R3, #0      ; Clear R3
        LD R4, #0          ; Initialize R4 to 0 (accumulator)
MUL_LOOP ADD R3, R3, R1     ; Multiply R1 by R2
        ADD R4, R4, #1      ; Increment R4 (counter)
        ADD R2, R2, #-1     ; Decrement R2
        BRzp MUL_LOOP       ; Repeat until R2 is 0 or negative
        ADD R3, R3, R6  ; Convert result to ASCII
        OUT

        ; Division
        LEA R0, DIV_RESULT  ; Display division result message
        PUTS
        AND R3, R3, #0      ; Clear R3
        LD R4, #0          ; Initialize R4 to 0 (quotient)
DIV_LOOP ADD R3, R3, R2     ; Subtract R2 from R1
        BRN DIV_DONE        ; If result is negative, division is done
        ADD R4, R4, #1      ; Increment R4 (quotient)
        BRnzp DIV_LOOP      ; Repeat until result is negative
DIV_DONE ADD R4, R4, R6  ; Convert quotient to ASCII
        OUT
    HALT ; Done
; DATA DECLARATIONS

getop1      .STRINGZ "\nENTER FIRST OPERAND : "
getop2      .STRINGZ "\nENTER SECOND OPERAND: "
ADD_RESULT  .STRINGZ "\nADDITION: "
SUB_RESULT  .STRINGZ "\nSUBTRACTION: "
MUL_RESULT  .STRINGZ "\nMULTIPLICATION: "
DIV_RESULT  .STRINGZ "\nDIVISION: "

MINUSx30 .FILL x-30
PLUSx30 .FILL x30
PLUS30  .FILL x30
MINUS30     .FILL x-30 
MINUS       .FILL X2D 
.end ; END OF CODE

The addition and subtraction works fine together, but the the multiplication and division I get SQUARE ? marks as the answer. So please tell me how I can fix this.

0

There are 0 answers