【问题标题】:How to add 2 numbers in lc3 to get a sum of 4 digits?如何在lc3中添加2个数字以获得4位数字的总和?
【发布时间】:2015-05-08 04:30:39
【问题描述】:

到目前为止,我已经制作了一个添加 2 个数字但它们是单个数字的代码。

.orig x3000

lea r0, string1
puts
getc
out
add r1, r0, 0

ld r0, minus48
add r1, r1, r0


lea r0, string1		;input one
puts


LOOP
getc
out
add r2, r0, 0
ld r0, minus48
add r2, r2, r0

add r3, r1, r2
out


OUTSIDE

lea r0, string2		;input two
puts

ld r0, plus48
add r0, r3, r0
out

HALT
plus48 .FILL 48
minus48 .FILL -48

string1 .stringz "\nPlease enter a number: "
string2 .stringz "\nSum is: "
.end

这很好用,但是我一直在尝试让数字输入存储多于 1 位,这就是我所做的:

.orig x3000

lea r0, string1		;input one
puts

LOOP
getc
out
add r1, r0, 0
brz OUTSIDE

ld r0, minus48
add r1, r1, r0
out 
brnzp LOOP 

lea r0, string1		
puts


getc
out
add r2, r0, 0
ld r0, minus48
add r2, r2, r0

add r3, r1, r2
out
OUTSIDE


lea r0, string2		;input two
puts

ld r0, plus48
add r0, r3, r0
out

HALT
plus48 .FILL 48
minus48 .FILL -48

string1 .stringz "\nPlease enter a number: "
string2 .stringz "\nSum is: "
.end

我尝试使用循环,这样我可以输入更多的数字,并且总和可以计算到 9999。但是我的循环输出了奇怪的字符,但它并没有像我想要的那样运行,LC3 就像它一样令人困惑花了我很长时间才得到个位数的加法,所以非常感谢您的帮助。

【问题讨论】:

    标签: lc3


    【解决方案1】:

    我没有详细查看您的所有代码,但我对第一个循环有点困惑。

    LOOP
    getc
    out
    add r1, r0, 0
    brz OUTSIDE
    

    您正在获取 ASCII 字符并添加 0 来检查我们的 ASCII 字符是否为空,但您无法从用户那里获得空字符。

    ld r0, minus48
    add r1, r1, r0
    out 
    brnzp LOOP 
    

    接下来的几行也需要修改。基本上,当这 9 行运行时,您从键盘获取一个字符,将该 ASCII 值转换为一个整数,然后将该 int 添加到它的 ASCII 值中。这就是为什么你会得到一个永无止境的随机字符循环。

    我建议为每个以 10 为底的值设置多个变量。

    例子:

    ; Stored values
    NUM1_1    .FILL x0000    ; stores the last number entered by the user
    NUM1_10   .FILL x0000    ; stores the 10's value
    NUM1_100  .FILL x0000    ; stores the 100's value
    NUM1_1000 .FILL x0000    ; stores the 1,000's
    

    因此,如果用户给定数字 5,382,您会将 5 存储到 NUM1_1000 中,将 3 存储到 NUM1_100 中,等等......然后分别添加这两个数字的数字。

    或者,有一个查找表可能更容易,它可以帮助您在用户输入时添加基数为 10 的值。

    例子:

    LookUp10       .FILL  #0
                   .FILL  #10
                   .FILL  #20
                   .FILL  #30
                   .FILL  #40
                   .FILL  #50
                   .FILL  #60
                   .FILL  #70
                   .FILL  #80
                   .FILL  #90
    
    
    LookUp100      .FILL  #0
                   .FILL  #100
                   .FILL  #200
                   .FILL  #300
                   .FILL  #400
                   .FILL  #500
                   .FILL  #600
                   .FILL  #700
                   .FILL  #800
                   .FILL  #900
    

    然后你可以使用用户给你的数字作为你想要的数组中的值的索引。

    【讨论】:

    • 谢谢。另外我到底在哪里进行修改?我实际上不知道如何有效地使用循环......我的代码是否需要一个循环才能让用户输入多个数字?
    • 我认为循环是获取用户输入的好方法,因为您永远不知道用户是否只想将 3 位数字和 2 位数字加在一起。您可以继续循环用户输入,直到他们按下 ENTER 键。另外,如果我的第一条评论有帮助,如果您将其标记为已回答,我将不胜感激。
    • 读取多位数字的常用算法是total = 10*total + new_digit。 LC3 应该能够通过一些指令乘以 10,例如如reg*8 + reg*2 与班次,或(reg*4 + reg) * 2 仅使用add 指令加倍或添加两个东西。这可能比查找表更容易。
    • Algorithm to convert integer between bases using LC-3 Assembly Language 使用 t*10 + digit 算法具有多位 base-10 输入。只需使用 4x add 指令进行乘以 10,在计算 reg*8 时节省 reg*2
    猜你喜欢
    • 2019-06-07
    • 2019-01-09
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2017-07-10
    • 1970-01-01
    • 2022-06-11
    相关资源
    最近更新 更多