【问题标题】:LC-3 Program is returning something that cannot be printedLC-3 程序正在返回无法打印的内容
【发布时间】:2015-04-04 20:33:07
【问题描述】:

我为 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 输入为零,程序结束。

【问题讨论】:

    标签: lc3


    【解决方案1】:

    调试您的程序后,我在您的代码中发现了两个逻辑错误。第一个是当您尝试在整数值和 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
    

    【讨论】:

      猜你喜欢
      • 2020-08-05
      • 1970-01-01
      • 2012-07-02
      • 2019-01-26
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多