LC-3 程序正在返回无法打印的内容
LC-3 Program is returning something that cannot be printed
我给LC-3写了一个程序,把10以内的数取进去直到0,然后输出输入的最大数。
似乎一切正常,但我总是得到不正确或不存在的结果。例如,如果我输入 1、2、3,然后输入 0,它应该说:
Zero entered, ending program. The largest integer entered was 3
但我一无所获。
我尝试输出最大整数的方法是用补码的方式求2中较大的那个数,不输入0就循环回去,但是我觉得我的逻辑有很大的问题问题。
抱歉,格式可能有点不稳定,如果您需要更多信息或者我做错了什么(就 post 而言 - 我知道我的代码有误)请让我知道。
提前致谢! (另外,我在代码底部有一个示例输出。)
.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
输入零,结束程序。
调试您的程序后,我在您的代码中发现了两个逻辑错误。第一个是当您尝试在整数值和 ascii 值之间进行转换时。第二种是当您比较两个数字以查找哪个更大时。我已经重新创建了您程序的工作版本并对其进行了非常详尽的评论,因此应该很容易理解。
第一个问题是您的 ascii 整数转换代码。您试图使用等于 48 的 POS48 将 ascii 数字转换为整数值;但是,为了从 ascii 转换为整数,您必须减去 48,或者在汇编的情况下添加 -48。然后要从整数值转换回 ascii 值,您需要添加正数 48。
例如:
NEG48 .FILL xFFD0 ;create constant NEG48 which = xFFD0 = -48
LD R6, NEG48 ;load NEG48 into register 6
GETC ;get user input
ADD R0, R0, R6 ;add -48 to the user input to get integer value
第二个问题是代码比较两个整数值以找出哪个更大。在您的代码中,您得到了两个值的补码,得到了第二个值的负数,然后将它加到第一个值上。这是正确的,只是您需要使用 BRn 和 BRp 来评估结果是阳性还是阴性。如果该值为正数,则表示第一个数较大,如果为负数,则表示第二个数较大。 BRn为负则分支,BRp为正则分支
例如,以下代码会将用户先前在程序中输入的新数字与用户输入的当前最大值进行比较,然后将较大的值保存到 CURRENTMAX 变量中:
LD R2, CURRENTMAX ;load CURRENTMAX
LD R3, NEWNUM ;load NEWNUM
NOT R4, R3 ;two's complement of NEWNUM
ADD R4, R4, #1 ;getting negative of NEWNUM
ADD R1, R4, R2 ;adding the negative of NEWNUM to CURRENTMAX
BRn LrgR3 ;if the result in R1 is negative NEWNUM is larger so branch to LrgR3
BRp LrgR2 ;if the result in R1 is positive CURRENTMAX is larger so branch to LrgR2
;**************** LrgR3 ****************
LrgR3 ;begin LrgR3
ST R3, CURRENTMAX ;store the larger result in CURRENTMAX
BR LOOP ;branch to LOOP
;**************** LrgR2 ****************
LrgR2 ;begin LrgR2
ST R2, CURRENTMAX ;store the larger result in CURRENTMAX
BR LOOP ;branch to LOOP
最后是我为了帮助你更好地解决这个问题而重新编写的工作版本。
.ORIG x3000
;**************** POLL INITIAL USER INPUT ****************
LD R5, POS48 ;load num to char conversion value
LD R6, NEG48 ;load char to num conversion value
LEA R0, MSG1 ;load MSG1
PUTS ;display MSG1
GETC ;get user input
OUT ;display users input
ADD R0, R0, R6 ;convert input char into a number value
ST R0, CURRENTMAX ;store numer value into MAX since the only number entered is obviously the MAX value entered
BRz ZERO ;if zero branch to ZERO
LD R0, NEWLINE ;load newline
OUT ;display newline
;**************** LOOP ****************
LOOP ;begin LOOP
LEA R0, MSG1 ;load MSG1
PUTS ;display MSG1
GETC ;get under input
OUT ;display user input
ADD R0, R0, R6 ;convert user input char into a number value
ST R0, NEWNUM ;store number value into NEWNUM
BRz ZERO ;if zero branch to ZERO
LD R0, NEWLINE ;load newline
OUT ;display newline
LD R2, CURRENTMAX ;load CURRENTMAX
LD R3, NEWNUM ;load NEWNUM
NOT R4, R3 ;two's complement of NEWNUM
ADD R4, R4, #1 ;getting negative of NEWNUM
ADD R1, R4, R2 ;adding the negative of NEWNUM to CURRENTMAX
BRn LrgR3 ;if the result in R1 is negative NEWNUM is larger so branch to LrgR3
BRp LrgR2 ;if the result in R1 is positive CURRENTMAX is larger so branch to LrgR2
;**************** LrgR3 ****************
LrgR3 ;begin LrgR3
ST R3, CURRENTMAX ;store the larger result in CURRENTMAX
BR LOOP ;branch to LOOP
;**************** LrgR2 ****************
LrgR2 ;begin LrgR2
ST R2, CURRENTMAX ;store the larger result in CURRENTMAX
BR LOOP ;branch to LOOP
;**************** ZERO ****************
ZERO ;begin ZERO
LD R0, NEWLINE ;load newline
OUT ;display newline
LEA R0, MSG2 ;load MSG2
PUTS ;display MSG2
LD R0, NEWLINE ;load newline
OUT ;display newline
LEA R0, MSG3 ;load MSG3
PUTS ;display MSG3
LD R0, CURRENTMAX ;load CURRENTMAXvalue
ADD R0, R0, R5 ;convert CURRENTMAX num into char
OUT ;display CURRENTMAX's char value
HALT ;end program
;**************** Variables and Constants ****************
MSG1 .STRINGZ "Enter a single-digit integer: "
MSG2 .STRINGZ "Zero entered, ending program."
MSG3 .STRINGZ "The largest integer is: "
POS48 .FILL x30
NEG48 .FILL xFFD0
NEWLINE .FILL #10
NEWNUM .BLKW 1
CURRENTMAX .BLKW 1
.END
我给LC-3写了一个程序,把10以内的数取进去直到0,然后输出输入的最大数。
似乎一切正常,但我总是得到不正确或不存在的结果。例如,如果我输入 1、2、3,然后输入 0,它应该说:
Zero entered, ending program. The largest integer entered was 3
但我一无所获。
我尝试输出最大整数的方法是用补码的方式求2中较大的那个数,不输入0就循环回去,但是我觉得我的逻辑有很大的问题问题。
抱歉,格式可能有点不稳定,如果您需要更多信息或者我做错了什么(就 post 而言 - 我知道我的代码有误)请让我知道。
提前致谢! (另外,我在代码底部有一个示例输出。)
.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 输入零,结束程序。
调试您的程序后,我在您的代码中发现了两个逻辑错误。第一个是当您尝试在整数值和 ascii 值之间进行转换时。第二种是当您比较两个数字以查找哪个更大时。我已经重新创建了您程序的工作版本并对其进行了非常详尽的评论,因此应该很容易理解。
第一个问题是您的 ascii 整数转换代码。您试图使用等于 48 的 POS48 将 ascii 数字转换为整数值;但是,为了从 ascii 转换为整数,您必须减去 48,或者在汇编的情况下添加 -48。然后要从整数值转换回 ascii 值,您需要添加正数 48。
例如:
NEG48 .FILL xFFD0 ;create constant NEG48 which = xFFD0 = -48
LD R6, NEG48 ;load NEG48 into register 6
GETC ;get user input
ADD R0, R0, R6 ;add -48 to the user input to get integer value
第二个问题是代码比较两个整数值以找出哪个更大。在您的代码中,您得到了两个值的补码,得到了第二个值的负数,然后将它加到第一个值上。这是正确的,只是您需要使用 BRn 和 BRp 来评估结果是阳性还是阴性。如果该值为正数,则表示第一个数较大,如果为负数,则表示第二个数较大。 BRn为负则分支,BRp为正则分支
例如,以下代码会将用户先前在程序中输入的新数字与用户输入的当前最大值进行比较,然后将较大的值保存到 CURRENTMAX 变量中:
LD R2, CURRENTMAX ;load CURRENTMAX
LD R3, NEWNUM ;load NEWNUM
NOT R4, R3 ;two's complement of NEWNUM
ADD R4, R4, #1 ;getting negative of NEWNUM
ADD R1, R4, R2 ;adding the negative of NEWNUM to CURRENTMAX
BRn LrgR3 ;if the result in R1 is negative NEWNUM is larger so branch to LrgR3
BRp LrgR2 ;if the result in R1 is positive CURRENTMAX is larger so branch to LrgR2
;**************** LrgR3 ****************
LrgR3 ;begin LrgR3
ST R3, CURRENTMAX ;store the larger result in CURRENTMAX
BR LOOP ;branch to LOOP
;**************** LrgR2 ****************
LrgR2 ;begin LrgR2
ST R2, CURRENTMAX ;store the larger result in CURRENTMAX
BR LOOP ;branch to LOOP
最后是我为了帮助你更好地解决这个问题而重新编写的工作版本。
.ORIG x3000
;**************** POLL INITIAL USER INPUT ****************
LD R5, POS48 ;load num to char conversion value
LD R6, NEG48 ;load char to num conversion value
LEA R0, MSG1 ;load MSG1
PUTS ;display MSG1
GETC ;get user input
OUT ;display users input
ADD R0, R0, R6 ;convert input char into a number value
ST R0, CURRENTMAX ;store numer value into MAX since the only number entered is obviously the MAX value entered
BRz ZERO ;if zero branch to ZERO
LD R0, NEWLINE ;load newline
OUT ;display newline
;**************** LOOP ****************
LOOP ;begin LOOP
LEA R0, MSG1 ;load MSG1
PUTS ;display MSG1
GETC ;get under input
OUT ;display user input
ADD R0, R0, R6 ;convert user input char into a number value
ST R0, NEWNUM ;store number value into NEWNUM
BRz ZERO ;if zero branch to ZERO
LD R0, NEWLINE ;load newline
OUT ;display newline
LD R2, CURRENTMAX ;load CURRENTMAX
LD R3, NEWNUM ;load NEWNUM
NOT R4, R3 ;two's complement of NEWNUM
ADD R4, R4, #1 ;getting negative of NEWNUM
ADD R1, R4, R2 ;adding the negative of NEWNUM to CURRENTMAX
BRn LrgR3 ;if the result in R1 is negative NEWNUM is larger so branch to LrgR3
BRp LrgR2 ;if the result in R1 is positive CURRENTMAX is larger so branch to LrgR2
;**************** LrgR3 ****************
LrgR3 ;begin LrgR3
ST R3, CURRENTMAX ;store the larger result in CURRENTMAX
BR LOOP ;branch to LOOP
;**************** LrgR2 ****************
LrgR2 ;begin LrgR2
ST R2, CURRENTMAX ;store the larger result in CURRENTMAX
BR LOOP ;branch to LOOP
;**************** ZERO ****************
ZERO ;begin ZERO
LD R0, NEWLINE ;load newline
OUT ;display newline
LEA R0, MSG2 ;load MSG2
PUTS ;display MSG2
LD R0, NEWLINE ;load newline
OUT ;display newline
LEA R0, MSG3 ;load MSG3
PUTS ;display MSG3
LD R0, CURRENTMAX ;load CURRENTMAXvalue
ADD R0, R0, R5 ;convert CURRENTMAX num into char
OUT ;display CURRENTMAX's char value
HALT ;end program
;**************** Variables and Constants ****************
MSG1 .STRINGZ "Enter a single-digit integer: "
MSG2 .STRINGZ "Zero entered, ending program."
MSG3 .STRINGZ "The largest integer is: "
POS48 .FILL x30
NEG48 .FILL xFFD0
NEWLINE .FILL #10
NEWNUM .BLKW 1
CURRENTMAX .BLKW 1
.END