Assembly LC-3 Print stars from user input 0-49

问题描述 投票:0回答:0

此 LC-3 程序旨在要求用户输入 0-49 之间的数字。然后程序打印出与输入的数字相关的星星。其他详细信息包括,当用户输入“q”时,程序退出并打印出一条再见消息。此外,如果用户输入 0-49 以外的数字,它会打印“输入无效,请重试消息”。最后,程序会忽略除数字和字母 q 之外的所有内容。例如:用户输入 3fds5 程序只读取 35.

话虽如此,该程序的主要问题是,当用户在控制台窗口中输入数字时,它会回显一个未知符号。当输入无效输入或输入“q”时,它也不会识别。

这是代码

.ORIG x3000

; constants

LD R5, m48
LD R6, m50

START      LEA R0, newline
           OUT
           OUT

LOOP       ; prompt user for input
           LEA R0, prompt
           PUTS

           ; read user input
           GETC ; read first character
           ADD R1, R0, #-10 ; check if first character is a number
           BRn INVALID_INPUT
           ADD R0, R0, R5 ; convert ASCII code to binary
           ADD R2, R0, #0
           LEA R0, star
           OUT ; echo first digit to console

           GETC ; read second character
           ADD R1, R0, #-10 ; check if second character is a number
           BRn INPUT_COMPLETE
           ADD R0, R0, R5 ; convert ASCII code to binary (R5 set to -48)
           ADD R2, R2, R0
           LEA R0, star
           OUT ; echo second digit to console

           GETC ; read third character (linefeed)
           BRnzp LOOP ; ignore any additional input and loop back to prompt

INPUT_COMPLETE
           ADD R0, R2, #0
           BRn INVALID_INPUT ; check if input is negative

           ; check if input is within range
           ADD R1, R0, R6 ; R1 = R0 - 50 (R6 set to -50)
           BRn PRINT_STARS ; branch if R0 <= 49
           BR INVALID_INPUT ; otherwise, input is invalid

PRINT_STARS
           ; print stars
           LD R0, num
           ADD R1, R0, #0
PRINT_LOOP LEA R0, star
           OUT
           ADD R1, R1, #-1
           BRnzp PRINT_LOOP

           ; print "All Done"
           LEA R0, newline
           OUT
           LEA R0, done
           PUTS
           LEA R0, newline
           OUT
           BRnzp LOOP

INVALID_INPUT
           ; print "Invalid input, try again!"
           LEA R0, newline
           OUT
           LEA R0, invalid
           PUTS
           BR LOOP

; handle 'q' input
QUIT       LEA R0, goodbye
           PUTS
           LEA R0, newline
           OUT
           HALT
; variables
num        .FILL x0000 ; user-entered number
count      .FILL x0000 ; number of stars to print
m48        .FILL #-48
m50        .FILL #-50
;Strings
newline    .FILL x0A   ; ASCII code for linefeed character
star       .FILL x2A   ; ASCII code for '*' character
prompt     .STRINGZ "Enter a number (0-49): "
invalid    .STRINGZ "Invalid input, try again!"
done       .STRINGZ "All Done"
goodbye    .STRINGZ "\n\nGoodbye!!!\n\n"
.END

assembly binary ascii lc3
© www.soinside.com 2019 - 2024. All rights reserved.