How to add 2 numbers in lc3 to get a sum of 4 digi

2019-09-11 00:20发布

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

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

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.

标签: lc3
1条回答
乱世女痞
2楼-- · 2019-09-11 01:05

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

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 

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.

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

Example:

; 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

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.

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.

Example:

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.

查看更多
登录 后发表回答