如何在lc3中加2个数字以获得4个数字的总和? [英] How to add 2 numbers in lc3 to get a sum of 4 digits?

查看:168
本文介绍了如何在lc3中加2个数字以获得4个数字的总和?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

到目前为止,我已经编写了一个代码,将两个数字相加,但是它们是个位数.

So far i have made a code that add 2 numbers but they are single digits.

.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位,这就是我所做的:

and this works fine however I've been trying to make the number input store more then 1 digit and this is what I've done:

.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像它一样令人困惑我花了我一整天的时间才能获得一位数字的加法,因此将不胜感激.

I have tried to use a loop so I can input more then single digits and the sum can calculate up to 9999. But my loop outputs weird characters but it doesn't run like I want it, LC3 is pretty confusing like it took me forever to get the addition of single digits, so help will be very much appreciated.

推荐答案

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

I didn't look over all of your code in detail but I'm a little confused about this first loop.

LOOP
getc
out
add r1, r0, 0
brz OUTSIDE

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

You're taking the ASCII char and adding 0 to check if our ASCII char is null, but you can't get a null char from the user.

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

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

These next few lines will also need to be modified. Basically when these 9 lines are run you are taking a char from the keyboard converting that ASCII value to an integer, then adding that int to its ASCII value. That's why you're getting a never ending loop of random char.

我建议每个基数10的值都有几个变量.

I would recommend having several variables for each base 10 value.

示例:

; 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中,以此类推,然后分别添加两个数字的位数.

So if you where given the number 5,382 by the user you would store 5 into NUM1_1000, 3 into NUM1_100, ect... and then add digit of the two numbers separately.

或者有一个查找表可能会更容易,该表可以帮助您在用户输入基数10时添加基数.

Or it might be easier to have a look up table that helps you add the base 10 values as the user types them in.

示例:

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

然后,您可以仅使用用户给您的数字作为所需数组中值的索引.

Then you can just use the number given to you by the user as an index for the value in the array you want.

这篇关于如何在lc3中加2个数字以获得4个数字的总和?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆