I am going through a problem I found on a website of making a calculator in LC-3. I am going about it in steps and saw something about doing what I am currently doing using stacks. I was wondering if someone could help me with what I have so far to convert it to using stacks, so far I have just been using registers! The code I have below is obviously incomplete, this is just what i have when i stopped and started researching on how to do some things. Here is my LC-3 Code for what I have so far:
;get first string from user
START LEA R0, PROMPT1 ; Display the prompt
PUTS
LEA R0, BUFFER
JSR READLINE
;convert string to number
LEA R0, BUFFER
JSR ATOI
;save the number
ST R0, FIRST_ARG
;get second string from user
LEA R0, PROMPT2 ; Display the prompt
PUTS
LEA R0, BUFFER
JSR READLINE
;convert string to number
LEA R0, BUFFER
JSR ATOI
;save the number
ST R0, SECOND_ARG
;load the arguments into R0 and R1
LD R0, FIRST_ARG
LD R1, SECOND_ARG
JSR SUM
PRINT_OUTPUT
;convert R0 into a string
LEA R1, BUFFER
JSR ITOA ;not made yet, also will need division and subtraction subroutines
;print the sum of the two digits entered
LEA R0, ANSWER ; Display the prompt
PUTS
LEA R0, BUFFER
PUTS
;print a new line character.
LD R0, ENTER
OUT
STOP HALT ;
;subroutine SUM : calculates the sum of two numbers
;input: R0,R1
;output: R0 = R0 + R1
SUM
ADD R0, R0, R1
RET
;subroutine PROD : calculates the product of two numbers
;input: R0,R1
;output: R0 = R0 * R1
MUL
ST R2, MUL_SAVE_R2
AND R2, R2, #0
AND R1, R1, R1
MUL_START
BRz MUL_END
ADD R2, R2, R0
ADD R1, R1, #-1
BR MUL_START
MUL_END
ADD R0, R2, #0
LD R2, MUL_SAVE_R2
RET
MUL_SAVE_R2 .FILL x0000
;subroutine READLINE : Reads a line of input from keyboard.
;input: R0. contains the address of the memory location where the
; string must be placed.
READLINE
ST R7, RL_RETURN
AND R1, R0, R0
RL_START
;get a character and echo it
GETC
OUT
;compare the character with ENTER which has ascii value 10
ADD R2, R0, #-10
BRz RL_END ;the user typed ENTER, stop the loop
STR R0, R1, #0 ;store whatever the user typed
ADD R1, R1, #1 ;increment the pointer
BR RL_START
RL_END
AND R0, R0, #0
STR R0, R1, #0 ;write the null character
LD R7, RL_RETURN
RET
RL_RETURN .FILL x0000
;subroutine ATOI : Converts an ASCII string to an integer
;input: R0, contains the address of the string
;output: R0, should contain the integer value
ATOI
ST R7, ATOI_RETURN
AND R2, R0, R0 ;R2 <- R0;
AND R0, R0, #0 ;R0 <- 0
ADD R1, R0, #10 ;R1 <- 10
LD R4, ASCIIZERO
NOT R4, R4
ADD R4, R4, #1 ;This is to convert ascii character to integer
ATOI_START
LDR R5, R2, #0
BRz ATOI_END ;we've reached the end of the string
AND R1, R1, #0
ADD R1, R1, #10 ;R1 <- 10
JSR MUL ;multiply current number by 10
ADD R5, R5, R4 ;subtract ASCIIZERO from R5
BRn INVALID_INPUT ;user typed something less than '0'
ADD R6, R5, #-9
BRp INVALID_INPUT ;user typed something more than '9'
ADD R0, R0, R5
ADD R2, R2, #1 ;next character
BR ATOI_START
INVALID_INPUT
AND R0, R0, #0 ;make R0 <- -1
ADD R0, R0, #-1
ATOI_END
LD R7, ATOI_RETURN
RET
ATOI_RETURN .FILL x0000
;allocate memory for the input
FIRST_ARG .FILL x0000
SECOND_ARG .FILL x0000
BUFFER .BLKW #15 ; allocating memory for storing user input.
POP
LDR R0, R6, #0
ADD R6, R6, #1
RET
PUSH
ADD R6, R6, #-1
STR R0, R6, #0
RET
;constants
MINUS .FILL x002D; '-'
ENTER .FILL x000A; newline character
PROMPT1 .STRINGZ "Please input the first digit > "
PROMPT2 .STRINGZ "Please input the second digit > "
ANSWER .STRINGZ "Sum of the two digits entered = "
ASCIIZERO .FILL x0030; '0'
.END