LC3处理两位数的总和

时间:2015-11-16 23:15:18

标签: assembly lc3

用汇编语言(类似于我给出的,LC3可读)。 我只是弄不清楚如何......

修改以下程序,以便它可以在值0和9之间添加两个数字。当前程序只处理一个数字作为总和。我的程序必须处理最多9 + 9作为输入和输出,最多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

1 个答案:

答案 0 :(得分:0)

正如杰斯特所指出的那样,你想从两个数字的总和中减去10来看看总和是大于还是小于10.如果它小于,则打印出数值。如果总和大于10,那么我们要打印一个' 1'到控制台,然后打印出值=(num1 + num2) - 10.例如(8 + 5) - 10 = 3,我们打印出一个' 1' char然后打印出' 3'获得' 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