-1

I'm not sure how to input a two digit number and actually be able to work with it (add, subtract, AND, etc.). So far I can enter my start and end numbers as strings and use LEA to output the strings, but I need them in registers so I can convert them from ASCII to integers and compute the sum. I think my logic for the sum computation is fine, but I could be wrong. So far I have this:

    .ORIG x3000

    AND R0, R0, #0                                                  ; clear R0

    LEA R2, blankspace                                              ; string space R2
    LD R1, pooploop                                                 ; loop condition 
    LEA R0, ENTER   
    PUTS                                                            ; print start number prompt                                                         

startloop       
    GETC
    OUT
STR R0, R2, #0
ADD R2, R2, #1
ADD R1, R1, #-1
    BRp startloop



    LEA R3, deathnote                                               ; string space R3
    LD R1, pooploop                                                 ; loop condition
    LEA R0, END
    PUTS                                                            ; print end number prompt

endloop     
    GETC
    OUT
STR R0, R3, #0
ADD R3, R3, #1
ADD R1, R1, #-1                                                     
    BRp endloop

ADD R2, R2, #-12                                                    ; convert
ADD R2, R2, #-12
ADD R2, R2, #-12
ADD R2, R2, #-12

ADD R3, R3, #-12                                                    ; convert
ADD R3, R3, #-12
ADD R3, R3, #-12
ADD R3, R3, #-12

NOT R2, R2                                                          ; negate R2
ADD R4, R3, R2                                                      ; R3 - R2                                           
    BRn NEG
NOT R2, R2                                                          ; if end number is greater, negate R2 again

loop    
ADD R2, R2, #1                                                      ; increment start number by 1
NOT R2, R2                                                          ; negate R2
ADD R5, R3, R2                                                      ; R3 - (R2+1)
    BRz exit                                                        ; exit when start number is equal to end number
NOT R2, R2                                                          ; negate R2 if start number is less than end number
AND R5, R2, #1                                                      ; 
    BRp sum
    BRnzp loop

sum
ADD R6, R2, #0
    BRnzp loop

exit


NEG LEA R0, ERROR
    PUTS

    HALT


deathnote   .blkw 50
blankspace  .blkw 100
pooploop .FILL x02
ENTER   .STRINGZ "\nEnter Start Number > "
END     .STRINGZ "\nEnter End Number > "
ERROR   .STRINGZ "\nERROR! Invalid Entry!"
  • 1
    I am not familiar with the hardware, but what are the current results, did you try it? Where is the problem exactly? You can also do some operations without converting strings to numbers (ex: addition, substraction). – Doncho Gunchev Oct 28 '17 at 21:13
  • basically whats happening right now is when it gets to the part of the code where it checks to see if the end number is greater than the start number, it branches to the ERROR string, even if I input an end number that IS greater than the start number. I believe it is because the ASCII values aren't actually in the registers R2 and R3, but are just the addresses – prisonmike Oct 29 '17 at 07:03
  • Can you try in debugger by single stepping instructions? Usually the difference between ASCII digit (values 48-57) and memory address (whatever else and usually quite large) is obvious. Then maybe check some examples/tutorials of LC-3 to better understand how to work with values vs addresses (I don't know LC-3, so I didn't read your code in detail, just the `LEA R3, deathnote` sounds like "load address", so it's plausible explanation ... although you probably overwrite R3 later with some input? not sure, hard to tell without learning LC-3). – Ped7g Oct 29 '17 at 10:40

1 Answers1

2

Quick scan of your code.

GETC
OUT
STR R0, R2, #0
[...]
GETC
OUT
STR R0, R3, #0

Remember that LD/LDR/LDI/ST/STR/STI instructions will go out into memory. If you simply want to move one register's value to another then you should not be using any of those instructions.

Moving one register to another can simply be done with the ADD or AND instruction. Simply ADD 0 to the register and store into the destination register, or AND with itself and store into the destination register.

Brandon
  • 438
  • 5
  • 4
  • Thanks for your comment. I realized what I was doing and what I needed to do were two different things. I can post my full code later for anyone that is making the same error that I did – prisonmike Oct 31 '17 at 19:56