【问题标题】:LC3 Assembly LanguageLC3 汇编语言
【发布时间】:2012-05-10 04:31:00
【问题描述】:

我正在尝试编写一个 LC3 汇编语言程序,它接受两个输入数字并打印出 x * y = z

我可以让它为数字 0-9 工作,但是任何高于它的数字我都会得到奇怪的字母或符号。

另外,我怎样才能使它不仅每个GETC 只需要 1 个输入,而且可以使用两个数字,例如。 10 * 12= 120? 任何帮助,将不胜感激! :)

这是我到目前为止所做的事情

    .ORIG x3000
AND R3, R3, #0 ;r3 stores the sum, set r3 to zero
AND R4, R4, #0 ;r4 is the counter
LD R5, INVERSE_ASCII_OFFSET ;inverse ascii offset
LD R6, DECIMAL_OFFSET ;decimal offset
;---------------------
;storing first input digits
LEA R0, display1 ;load the address of the 'display1' message string
PUTS ;Prints the message string
GETC ;get the first number
OUT ;print the first number
ADD R1, R0, #0 ;store input value(ascii) to r1
ADD R1, R1, R5 ;get real value of r1
;storing second input digits
LEA R0, display2 ;load the address of the 'display2' message string
PUTS ;Prints the message string
GETC ;get the first number
OUT ;print the first number
ADD R2, R0, #0 ;store input value(ascii) to r2
ADD R2, R2, R5 ;get real value of r2
;----------------------
ADD R4, R2, #0 ;fill counter with multiplier
MULTIPLICATION:
ADD R3, R3, R1 ;add to sum
ADD R4, R4, #-1 ;decrease counter by one
BRp MULTIPLICATION ;continue loop until multiplier is 0
LEA R0, stringResult
PUTS
ADD R0, R3, R6 ;move result to r0
OUT ;print result
HALT
display1 .STRINGZ "\nenter the 1st no.: "
display2 .STRINGZ "\nenter the 2nd no.: "
stringResult .STRINGZ "\nResult: "
INVERSE_ASCII_OFFSET .fill xFFD0 ; Negative of x0030.
DECIMAL_OFFSET .fill #48
.END

【问题讨论】:

    标签: lc3


    【解决方案1】:

    您的显示功能通过将一个数字添加到'0' 的基本 ascii 值来工作。这是有效的,因为 ascii 表的排列方式很方便。例如,'0' + 1 = '1',相当于0x30 + 1 = 0x31。但是,如果您可能发现 '0' + 12 = '<'。这是因为'0' = 0x30,所以0x30 + 12 (0xC) = 0x3C。查看 ascii 图表,我们看到 0x3C = '<'。也就是说,这是一种只打印单个数字的有效方法。

    这两个问题的答案在于编写一个迭代处理数字并用它们形成数字的例程。换句话说,您将需要一个循环来确定下一个要打印的字符并打印出来。

    【讨论】:

    猜你喜欢
    • 1970-01-01
    • 2021-12-02
    • 2014-05-06
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2010-12-24
    相关资源
    最近更新 更多