user5569691
user5569691

Reputation: 1

LC3 handling two digit sum

In assembly language (similar to what i have given, readable by the LC3). I just can't figure out how to ......

Modify the following program so it can add two numbers between the values 0 and 9. The current program only handles one digit as the sum. My program must handle up to 9 + 9 as input and output up to 18.

.ORIG x3000 ; begin at x3000

; input two numbers

IN ;input an integer character (ascii) {TRAP 23}

LD R3, HEXN30 ;subtract x30 to get integer

ADD R0, R0, R3

ADD R1, R0, x0 ;move the first integer to register 1

IN ;input another integer {TRAP 23}

ADD R0, R0, R3 ;convert it to an integer

; add the numbers

ADD R2, R0, R1 ;add the two integers

; print the results

LEA R0, MESG ;load the address of the message string

PUTS ;"PUTS" outputs a string {TRAP 22}

  ADD R0, R2, x0 ;move the sum to R0, to be output

  LD R3, HEX30 ;add 30 to integer to get integer character

  ADD R0, R0, R3

  OUT ;display the sum {TRAP 21}

     ; stop

HALT ;{TRAP 25}

; data

 MESG .STRINGZ "The sum of those two numbers is: "

 HEXN30 .FILL xFFD0 ; -30 HEX

 HEX30 .FILL x0030 ; 30 HEX

.END

;  input two numbers

IN ;input an integer character (ascii) {TRAP 23}

LD R3, HEXN30 ;subtract x30 to get integer

 ADD R0, R0, R3

ADD R1, R0, x0 ;move the first integer to register 1

IN ;input another integer {TRAP 23}

 ADD R0, R0, R3 ;convert it to an integer

; add the numbers

ADD R2, R0, R1 ;add the two integers

; print the results

LEA R0, MESG ;load the address of the message string

PUTS ;"PUTS" outputs a string {TRAP 22}

ADD R0, R2, x0 ;move the sum to R0, to be output

LD R3, HEX30 ;add 30 to integer to get integer character

  ADD R0, R0, R3

   OUT ;display the sum {TRAP 21}

   ; stop

HALT ;{TRAP 25}

; data

MESG .STRINGZ "The sum of those two numbers is: "

HEXN30 .FILL xFFD0 ; -30 HEX

HEX30 .FILL x0030 ; 30 HEX

.END

Upvotes: 0

Views: 5999

Answers (1)

Chris M
Chris M

Reputation: 651

As Jester pointed out, you want to subtract 10 from the sum of the two numbers to see if the sum is greater or less than 10. If it's less than then print out the values. If the sum is greater than 10, then we want to print a '1' to the console and then print out the value = (num1 + num2) - 10. For example (8 + 5) - 10 = 3, we would print out a '1' char and then print out the '3' to get the visual output of '13'.

.

.ORIG x3000 ; begin at x3000

;  input two numbers
IN ;input an integer character (ascii) {TRAP 23}

LD R3, HEXN30 ;subtract x30 to get integer

ADD R0, R0, R3

ADD R1, R0, x0 ;move the first integer to register 1

IN ;input another integer {TRAP 23}

ADD R0, R0, R3 ;convert it to an integer

; add the numbers

ADD R2, R0, R1 ;add the two integers

; print the results

LEA R0, MESG ;load the address of the message string

PUTS ;"PUTS" outputs a string {TRAP 22}

ADD R0, R2, x0 ;move the sum to R0, to be output


LD R2, NEG_TEN  ; load -10 into R2
ADD R2, R2, R0  ; minus ten from our sum
BRn JUMP    ; skip this code if our value is less than 10
AND R4, R4, #0  ; clear R4
ADD R4, R4, R2  ; store R2 into R4
LD R0, ASCII_1  ; load the ascii char '1'
OUT     ; print '1' to the console
AND R0, R0, #0  ; clear R0
ADD R0, R0, R4  ; store R4 back into R1


JUMP

LD R3, HEX30 ;add 30 to integer to get integer character
ADD R0, R0, R3
OUT ;display the sum {TRAP 21}

; stop

HALT ;{TRAP 25}

; data

MESG .STRINGZ "The sum of those two numbers is: "

HEXN30 .FILL xFFD0 ; -30 HEX

HEX30 .FILL x0030 ; 30 HEX

NEG_TEN .FILL #-10

ASCII_1 .FILL x0031 ; ASCII char '1'

.END

Upvotes: 1

Related Questions