LC-3 If/Else 语句

发布于 2024-12-21 13:16:40 字数 888 浏览 0 评论 0原文

我的 LC-3 程序有问题,无法从 if/else 语句中显示字符串。我不知道我是否执行了错误的语句,或者是否显示了错误的字符串。目标是让它在用户输入 0 时显示 IF,并在输入 时显示 else(停止程序) 1..

    .ORIG   x3000


START:
; clear registers
    AND R0, R0, 0
    AND R1, R0, 0
    AND R2, R0, 0
    AND R3, R0, 0
    AND R4, R0, 0

; print greeting
    LEA R0, GREETING
    PUTS

; get user-input
; echo it back
    GETC
    PUTC

; store entered string 
    ST  R0, USERINPUT

;FIRST IF STATEMENT
OUTPUT  LD R2, USERINPUT
    BRz ENDIF
    LEA R3, GREETING
;ELSE
    ENDIF
    LD R2, USERINPUT
    HALT
    DONE

; stop the processor
    HALT

    GREETING:   .STRINGZ    "\nWelcome to the game.\nDo you want to play?\n0:Yes   1:No\n: "
    GREETINGTWO:    .STRINGZ    "\nTest if statement: "

    ; variables
    USERINPUT:  .FILL   0
    ; end of code
    .END

I have a problem with this LC-3 program, I can't get the string to display from the if/else statement. I don't know if I'm doing the statement wrong, or if I am displaying the string wrong. The goal is to have it display the IF when the user enters 0 and the else (halt the program) when they enter a 1.

    .ORIG   x3000


START:
; clear registers
    AND R0, R0, 0
    AND R1, R0, 0
    AND R2, R0, 0
    AND R3, R0, 0
    AND R4, R0, 0

; print greeting
    LEA R0, GREETING
    PUTS

; get user-input
; echo it back
    GETC
    PUTC

; store entered string 
    ST  R0, USERINPUT

;FIRST IF STATEMENT
OUTPUT  LD R2, USERINPUT
    BRz ENDIF
    LEA R3, GREETING
;ELSE
    ENDIF
    LD R2, USERINPUT
    HALT
    DONE

; stop the processor
    HALT

    GREETING:   .STRINGZ    "\nWelcome to the game.\nDo you want to play?\n0:Yes   1:No\n: "
    GREETINGTWO:    .STRINGZ    "\nTest if statement: "

    ; variables
    USERINPUT:  .FILL   0
    ; end of code
    .END

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

冷月断魂刀 2024-12-28 13:16:40

您显示的字符串不正确。

LEA 仅加载标签/内存偏移的有效地址,不会将其打印出来。如果要打印出字符串,则必须调用 TRAP x22(宏为 PUTS),如上面代码片段的第 14 行所示。

You're displaying the string incorrectly.

LEA only loads the effective address of a label/memory offset, it does not print it out. If you want to print out a string, you must call TRAP x22 (macroed to PUTS), as in the 14th line of your code snippet above.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文