我为 LC-3 编写了一个程序,用于接收 10 以下的数字,直到达到 0,然后输出输入的最大数字。
一切似乎都有效,但我一直得到不正确或不存在的结果。例如,如果我输入 1、2、3,然后是 0,它应该说:
零进入,结束程序。输入的最大整数是 3
但我什么也得不到。
我试图用来输出最大整数的方法是使用二进制补码系统来确定较大的两个数,如果没有输入 0 则循环返回,但我认为我的逻辑有一个主要问题。
抱歉,格式可能有点不稳定,如果您需要更多信息或者我做错了什么(就帖子而言 - 我知道我的代码有误),请告诉我。
先感谢您!(我在代码底部还有一个示例输出。)
.ORIG x3000
AND R0,R0,#0 ;clear R0
AND R0,R0,#0 ;clear R1
LEA R0, MSG1 ;load address of message 1
PUTS ;display message
GETC ;read in character from keyboard
OUT ;echo input
ST R0, NUM1 ;store the number in num1
LD R2, NUM1
LD R1, POS48
ADD R2,R2, R1 ;adds 48 to make the character a number
BRz ZERO ;checks if the number is zero
LD R0, NEWLINE ;load newline
OUT ;execute newline
LOOP LEA R0, MSG1 ;load address of message 1
PUTS ;display message
GETC ;read in character from keyboard
OUT ;echo input
ST R0, NUM2 ;store character in num2
LD R2, POS48
LD R3, NUM2
ADD R0, R3, R2 ;adds 48 to make the character a number
BRz ZERO ;checks if the number is zero
LD R0, NEWLINE ;load newline
OUT ;execute newline
LD R1, NUM1 ;load the first number
LD R2, NUM2 ;load the second number
NOT R2, R2 ;two's complement of R2
ADD R2, R2, #1 ;getting negative of num 2
ADD R0, R1, R2 ;adding the two values
ST R0, MAX ;storing larger number in NUM5
BRnz LOOP ;Branch if R0 is positive
ZERO LEA R0, MSG2 ;load message if number entred is zero
PUTS ;display message
LD R0, NEWLINE ;load newline
OUT ;execute newline
LEA R0, MSG3 ;load largest int message
PUTS ;display
LD R2, MAX
LD R1, POS48
ADD R1, R2, R1 ;adds 48 to make the character a number
LD R0, MAX ;load largest int
OUT ;display largest int
HALT ;end program
;*** Data ***
MSG1 .STRINGZ "Enter a single-digit integer: "
MSG2 .STRINGZ "Zero entered, ending program."
MSG3 .STRINGZ "The largest integer is: "
POS48 .FILL #48
NEWLINE .FILL #10
NUM1 .BLKW 1
NUM2 .BLKW 1
MAX .BLKW 1
.END
示例输出 - 输入一位整数:1 输入一位整数:2 输入一位整数:3 输入一位整数:0 输入零,程序结束。