user56521
user56521

Reputation: 31

Simple LC3 calculator

I am writing a program to accept user input that is one of the following characters, 0-9, +, -, *, /, and ^. It only takes two operands at a time and does so in reverse polish notation, such as, 5 6 + or 2 8 /. I have everything figured out except for the user input part. I know to use GETC and OUT but I do not understand how these two functions work and therefore am having quite the trouble implementing them at the start of the program. Here is what I have. It currently compiles without errors, but it does not seem to work correctly.

;INTRO PARAGRAPH
; I will start by defining my register use. R0 holds the last character that was input by the user. R1, R2, R3, and R4 ; are all just temporary registers that hold values which are only important for an immediate task. R6 is my stack pointer.
; R5 is what I use to take information out of the stack. I start by initializing all registers to zero. From there I take
; the last character input by the user and filter it through conditions to find out what character it is and what subroutine
; to send it to. If it is a number 0-9, it is pushed right into the stack. If it is an invalid character it jumps to the
; INVALID_CHARACTER loop which outputs a message then terminates the program. If it is an operator it is sent to DECODE where
; it has its hex value inverted and is then sent through several conditions to find out which operator it is and which loop
; to send it of to. Register use is stated above the operator loops if it could be confusing as to what each register is
; being used for. Finally, the program will terminate when it sees a new line character. Here I set xA as the new line
; character.

                    .ORIG x3000

    ;initializes all registers to zero
                    AND R0, R0, #0
                    AND R1, R1, #0
                    AND R2, R2, #0
                    AND R3, R3, #0
                    AND R4, R4, #0
                    AND R5, R5, #0
                    AND R6, R6, #0
                    AND R7, R7, #0

    ;this block will ask for user input then display it to the monitor

    NEXTCHARACTER           GETC
                            OUT

    ;reads input and determines whether or not it is a valid character

    ;Space?
                    LD R1, OPPOSPACE 
                    ADD R1, R0, R1
                    BRz NEXTCHARACTER

   ;New line?
                    LD R1, OPPOLINE
                    ADD R1, R0, R1
                    BRz DONE

   ;Multiplication?
                    LD R1, OPPOMULTI
                    ADD R1, R0, R1
                    BRz DECODE
                    Brn INVALID_CHARACTER

    ;Exponent?
                    LD R1, OPPOEXPO
                    ADD R1, R0, R1
                    BRz DECODE
                    Brp INVALID_CHARACTER

    ;Is at most 9? 
                    LD R1, OPPOCOLON
                    ADD R1, R0, R1
                    Brzp INVALID_CHARACTER

    ;Is it an apostrophe?
                    LD R1, OPPOAPOST
                    ADD R1, R0, R1
                    BRz INVALID_CHARACTER

    ;Is it a decimal point?
                    LD R1, OPPODECI
                    ADD R1, R1, R0
                    BRz INVALID_CHARACTER

    ;Operand or operator?
                    LD R1, OPPOZERO
                    ADD R1, R1, R0
                    BRzp PUSH
                    BRn DECODE

    INVALID_CHARACTER       .STRINGZ "Error invalid input"
                    BRnzp DONE

    ERROR                   .STRINGZ "operation jumps are incorrect"
                    BRnzp DONE

    ;stores negation of operators in stack
    DECODE                  NOT R0, R0
                    ADD R0, R0, #1          ;stores the opposite hex value 

    ;figure out what operation to jump to
                    LD R1, ADDING
                    ADD R1, R1, R0
                    BRz ADDITION            ;jumps to addition sequence 

                    LD R1, SUBTRACT
                    ADD R1, R1, R0
                    BRz SUBTRACTION         ;jumps to subtraction sequence

                    LD R1, MULTI
                    ADD R1, R1, R0
                    BRz MULTIPLICATION      ;jumps to multiplication seq

                    LD R1, DIVIDE
                    ADD R1, R1, R0
                    Brz DIVISION            ;jumps to division sequence

                    LD R1, EXPONENT
                    ADD R1, R1, R0
                    BRz EXPONENTIAL         ;jumps to exponent sequence
                    Brnp ERROR

    ;pushes and pops characters into and out of the stack
    PUSH            ADD R6, R6, #-1         ;moves stack pointer up 
                    STR R0, R6, #0          ;stores R0 at current spot
                    BRnzp NEXTCHARACTER

    POP             LDR R5, R6, #0          ;loads  R6 into R5
                    ADD R6, R6, #1          ;moves stack pointer 

    ;here are the operations
    ;R2 and R3 are operands R2 holds answer                     
    ADDITION        ADD R2, R5, #0          ;loads first number  into R2

                    LDR R5, R6, #0          ;loads  R6 into R5
                    ADD R6, R6, #1          ;moves stack pointer 

                    ADD R3, R5, #0          ;loads second number  into R2
                    ADD R2, R2, R3          ;stores answer in R2

                    ADD R6, R6, #-1         ;moves stack pointer up 
                    STR R2, R6, #0          ;stores R2 in empty spot 
                    BRnzp NEXTCHARACTER     ;goes back to user input to read 

    SUBTRACTION     ADD R3, R5, #0          ;loads number into R3

                    LDR R5, R6, #0
                    ADD R6, R6, #1          ;moves pointer 

                    ADD R2, R5, #0          ;loads digit to be added into R2
                    NOT R3, R3  
                    ADD R3, R3, #1          ;2's complement of R3
                    ADD R2, R2, R3          ;subtraction of the two numbers

                    ADD R6, R6, #-1         ;moves stack pointer up 
                    STR R2, R6, #0          ;stores R2 in empty spot 

                    BRnzp NEXTCHARACTER     ;goes back to user input to read 

    MULTIPLICATION  ADD R2, R5, #0          ;loads number to be multiplied

                    LDR R5, R6, #0
                    ADD R6, R6, #1          ;moves pointer 

                    ADD R3, R5, #0          ;loads number to be multiplied 

    MULTIPLICATIONLOOP      ADD R2, R3, R2
                    ADD R3, R3, #-1
                    BRp MULTIPLICATIONLOOP

                    ADD R6, R6, #-1         ;moves stack pointer up 
                    STR R2, R6, #0          ;stores R2 in empty spot 

                    BRnzp NEXTCHARACTER     ;goes back to user input to read 

    ;R1 will be a place holder here
    ;R4 will hold quotient
    ;R5 will hold remainder
    ;R2 is dividend
    ;R3 is divisor
    DIVISION        ADD R3, R5, #0          ;loads divisor

                    LDR R5, R6, #0
                    ADD R6, R6, #1          ;moves pointer after loading R5

                    ADD R2, R5, #0          ;loads dividend

                    NOT R1, R3          
                    ADD R1, R1, #1
    DIVLOOP         ADD R4, R4, #1
                    AND R2, R2, R0
                    BRn NEGATIVE
                    BRp DIVLOOP
                    BRz ZERO
    NEGATIVE        ADD R4, R4, #-1
                    ADD R5, R2, R3

                    ADD R6, R6, #-1         ;moves stack pointer up   
                    STR R4, R6, #0          ;stores R4 in empty spot 

    ZERO            BRnzp NEXTCHARACTER     ;goes back to user input to read 

    ;R2 is exponent
    ;R3 is base and answer
    ;R4 is base copy
    ;R1 is base copy
    EXPONENTIAL             ADD R2, R5, #0          ;loads exponent into R2

                    LDR R5, R5, #0
                    ADD R6, R6, #1          ;moves pointer after loading R5

                    ADD R3, R5, #0          ;loads base into R3
                    ADD R1, R1, R3          ;loads base into R1
    OUTEREXPOLOOP   ADD R4, R1, #0          ;loads base into R4 for counter
    INNEREXPOLOOP   ADD R3, R3, R3          ;starts multiplication sequence
                    ADD R4, R4, #-1         ;decrements base counter
                    BRp INNEREXPOLOOP
                    ADD R2, R2, #-1         ;determines if needs to re-enter 
                    BRp OUTEREXPOLOOP   

                    ADD R6, R6, #-1         ;moves stack pointer up 
                    STR R3, R6, #0          ;stores R3 in empty spot 

                    BRnzp NEXTCHARACTER     ;goes back to user input to read 


    DONE                    HALT


    MULTI           .FILL x002A
   EXPONENT         .FILL x005E
   ADDING           .FILL x002B
    SUBTRACT        .FILL x002D
    DIVIDE          .FILL x002F
    OPPOSPACE       .FILL xFFDF
    OPPOLINE        .FILL xFFF5
    OPPOMULTI       .FILL xFFD5
    OPPOEXPO        .FILL xFFA1
    OPPOCOLON       .FILL xFFC5
    OPPOAPOST       .FILL xFFD3
    OPPODECI        .FILL xFFD1
    OPPOZERO        .FILL xFFCF
    STACK_START     .FILL x5000
    STACK_POINTER   .FILL x5000
    STACK_TOP       .FILL x5050
    OPPOSTART       .FILL xAFFF

    .END

Upvotes: 0

Views: 7384

Answers (1)

Reagankm
Reagankm

Reputation: 5429

You would typically use GETC with a prompt to get input from the user. GETC waits for a character input from the user and saves it in R0. At the bottom of the program where you have your .FILL things, you can create a prompt like this:

PROMPT    .STRINGZ    "Enter a number or operation: "

To display it in your program and get the input, you would do something like this:

LEA    R0, PROMPT    ;Save the location of the prompt
PUTS                 ;Print the prompt to the screen
GETC                 ;Store the character into R0

OUT is sort of the reverse of GETC. It takes a single character stored in R0 and prints it to the screen. So, to echo back the character the user typed (so they can see what they typed), you just include an OUT line underneath your GETC.

OUT                 ;Echo character to the screen

Upvotes: 1

Related Questions