How to add 2 numbers in lc3 to get a sum of 4 digits? -


so far have made code add 2 numbers 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 1  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 2  puts    ld r0, plus48  add r0, r3, r0  out    halt  plus48 .fill 48  minus48 .fill -48    string1 .stringz "\nplease enter number: "  string2 .stringz "\nsum is: "  .end

and works fine i've been trying make number input store more 1 digit , i've done:

.orig x3000    lea r0, string1		;input 1  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 2  puts    ld r0, plus48  add r0, r3, r0  out    halt  plus48 .fill 48  minus48 .fill -48    string1 .stringz "\nplease enter number: "  string2 .stringz "\nsum is: "  .end

i have tried use loop can input more single digits , sum can calculate 9999. loop outputs weird characters doesn't run want it, lc3 pretty confusing took me forever addition of single digits, appreciated.

i didn't on of code in detail i'm little confused first loop.

loop getc out add r1, r0, 0 brz outside 

you're taking ascii char , adding 0 check if our ascii char null, can't null char user.

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

these next few lines need modified. when these 9 lines run taking char keyboard converting ascii value integer, adding int ascii value. that's why you're getting never ending loop of random char.

i recommend having several variables each base 10 value.

example:

; stored values num1_1    .fill x0000    ; stores last number entered user num1_10   .fill x0000    ; stores 10's value num1_100  .fill x0000    ; stores 100's value num1_1000 .fill x0000    ; stores 1,000's 

so if given number 5,382 user store 5 num1_1000, 3 num1_100, ect... , add digit of 2 numbers separately.

or might easier have table helps add base 10 values 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 can use number given user index value in array want.


Comments