我一直在用汇编语言开发这个计算器,但我不知道我做错了什么。它输出的是第一个字符串,但我认为此后它不会得到我的输入。 我将附上代码,以便您查看。
.ORIG x3000
; Strings used in the program
num_temp2 .FILL #0
num_temp .FILL #0
; Program starts here
inputFirstNum .STRINGZ "Input the first number you would like to use: "
ContProgram
JSR ClearReg
LEA R0, inputFirstNum
PUTS
JSR GetInput
LD R1, num_temp
LEA R0, inputOpp
PUTS
GETC
ST R0, num_temp
LD R0, num_temp
JSR DecodeOperation
LEA R0, inputSecNum
PUTS
JSR GetInput
LD R2, num_temp
; Definitions for operation codes
codesArray .FILL #-43 ; '+'
.FILL #-45 ; '-'
.FILL #-42 ; '*'
.FILL #-47 ; '/'
GetInput
AND R3, R3, #0 ; Clear R3 for digit count
AND R4, R4, #0 ; Clear R4 for accumulated number
InputLoop
GETC ; Read character into R0
OUT ; Echo character
ADD R5, R0, R6 ; Subtract 48 by adding R6 (which contains -48)
BRn InputDone ; Break loop if input is not a digit
; Perform R4 = R4 * 10 + R5 using repeated addition
AND R6, R6, #0 ; Clear R6 to use as temporary register for sum
LD R7, ten ; Load the constant 10 into R7
ADD R2, R4, #0 ; Copy R4 to R2 for repeated addition
MultiplyByTen
ADD R6, R6, R2 ; Accumulate R4 into R6, 10 times
ADD R7, R7, #-1 ; Decrement the counter
BRp MultiplyByTen ; Repeat until counter is zero
ADD R4, R6, R5 ; Add the new digit to the accumulated result in R6
ADD R3, R3, #1 ; Increment digit count
BR InputLoop
InputDone
ST R4, num_temp2 ; Store the number
RET
ADDITION
ADD R4, R1, R2
BR OPERATION_FINISHED
SUBTRACTION
NOT R2, R2
ADD R2, R2, #1
ADD R4, R1, R2
BR OPERATION_FINISHED
MULTIPLY
AND R3, R3, #0
AND R4, R2 #0
MULTIPLY_LOOP
BRz DONE_MULTIPLY
ADD R3, R3, R1
ADD R4, R4, #-1
BRnp MULTIPLY_LOOP
DONE_MULTIPLY
BRnzp OPERATION_FINISHED
DIVIDE
AND R3, R3, #0
AND R4, R1, #0
DIVIDE_LOOP
NOT R5, R2
ADD R5, R5, #1
ADD R6, R4, R5
BRn DIVIDE_DONE
ADD R4, R4, R5
ADD R3, R3, #1
BRnzp DIVIDE_LOOP
DIVIDE_DONE
BRnzp OPERATION_FINISHED
DecodeOperation
LD R3, codesArray ; Load base address of codesArray
ADD R4, R0, #0 ; Copy operation character to R4
DecodeLoop
LDR R5, R3, #0 ; Load operation code
NOT R6, R5 ; Invert code for comparison
ADD R6, R6, R4 ; Add inverted code to character
BRz ExecuteOperation ; Branch if zero (match)
ADD R3, R3, #1 ; Move to next code
BRnzp DecodeLoop ; Continue if not out of codes
RET
ExecuteOperation
AND R3, R3, #0 ; Clear R3 for safe operation execution
ADD R3, R5, #0 ; Copy operation code to R3 for checking
BRz ADDITION ; Branch if R3 is 0 (addition)
BRp MULTIPLY_CHECK ; Check further if it's multiplication or division
BRn SUBTRACTION ; Branch if R3 is negative (subtraction)
MULTIPLY_CHECK
ADD R3, R3, #-2 ; Adjust R3 to check for multiplication or division
BRz MULTIPLY
BRp DIVIDE
STOP_PROGRAM
TRAP x25
resultMsg .STRINGZ "The answer is: "
OPERATION_FINISHED
LEA R0, resultMsg
PUTS
ADD R0, R4, #0 ; Assuming R4 holds the result
OUT
LEA R0, continue
PUTS
GETC
LD R6, NEG_ONE_TEN ; Load -110 into R6 before comparison
ADD R0, R0, R6 ; Subtract 110 from user input to check against 'n'
BRz STOP_PROGRAM ; If 'n', halt the program
BRnzp ContProgram ; Otherwise, jump back to start if not 'n'
ClearReg
AND R1, R1, #0
AND R2, R2, #0
AND R3, R3, #0
AND R4, R4, #0
AND R5, R5, #0
AND R6, R6, #0
RET
ten .FILL #10
NEG_FORTYEIGHT .FILL #-48
NEG_ONE_TEN .FILL #-110
inputSecNum .STRINGZ "Input the second number you would like to use: "
debugMsg1 .STRINGZ "\nDebug: First number loaded\n"
debugMsg2 .STRINGZ "\nDebug: Operation decoded\n"
inputOpp .STRINGZ "Input operation you would like to use '+, -, *, /': "
continue .STRINGZ "Would you like to continue? (y/n): "
.END
我尝试了很多不同的方法来解决这个问题,但到目前为止没有任何效果。
首先出现的两个错误:
InputLoop
有 ADD R5, R0, R6 ; Subtract 48 by adding R6 (which contains -48)
GETC
和 OUT
陷阱会覆盖 R7 中的返回地址;你必须从某个地方保存并恢复它。