我如何在lc3中显示两位数字

时间:2017-04-26 18:19:39

标签: assembly lc3

我正在创建一个代码来计算矩形的面积。我已完成乘法,但它只显示数字0-9。我的教授说,为了显示2位数字,我需要在循环中减去10并计算循环发生的次数。我试了一下它并没有工作,有人可以帮助我。

.ORIG x3000
AND R3, R3, #0 ;r3 stores the sum, set r3 to zero
AND R4, R4, #0 ;r4 is the counter
LD R5, INVERSE_ASCII_OFFSET ;inverse ascii offset
LD R6, DECIMAL_OFFSET ;decimal offset
;---------------------

;storing first input digits
LEA R0, display1 ;load the address of the 'display1' message string
PUTS ;Prints the message string
GETC ;get the first number
OUT ;print the first number
ADD R1, R0, #0 ;store input value(ascii) to r1
ADD R1, R1, R5 ;get real value of r1

;storing second input digits
LEA R0, display2 ;load the address of the 'display2' message string
PUTS ;Prints the message string
GETC ;get the first number
OUT ;print the first number
ADD R2, R0, #0 ;store input value(ascii) to r2
ADD R2, R2, R5 ;get real value of r2
;----------------------

ADD R4, R2, #0 ;fill counter with multiplier
MULTIPLICATION:
ADD R3, R3, R1 ;add to sum
ADD R4, R4, #-1 ;decrease counter by one
BRp MULTIPLICATION ;continue loop until multiplier is 0
;----------------------

;Product in R3
AND R1, R1, #0 ;r1 will hold -10
AND R2, R2, #0 ;r2 = 10's counter
AND R4, R4, #0 ;r4 = 1's counter
AND R5, R5, #0 ;r5 will hold the answer
LD R1, NEG_TEN ;r1 = -10
ADD R5, R3, #0 ;copy answer to r5

Loop1
ADD R5, R5, R1 ;Product - 10
BRn EndDec
ADD R2, R2, #1 ;increment 10's counter by 1
BRnzp Loop1
;----------------------
LEA R0, stringResult
PUTS
ADD R0, R3, R6 ;move result to r0
OUT ;print result
HALT
display1 .STRINGZ "\nenter the length: "
display2 .STRINGZ "\nenter the width: "
stringResult .STRINGZ "\nArea: "
INVERSE_ASCII_OFFSET .fill xFFD0 ; Negative of x0030.
DECIMAL_OFFSET .fill #48
NEG_TEN .fill #-10 ; -10

1 个答案:

答案 0 :(得分:1)

让我们看看它如何适用于单个数字: 如果R0 = 4,那么我们添加' 0' 0它。 4 +' 0' =' 4',这是我们可以打印的东西。

现在假设R0 = 14。当我们做14 +' 0'我们得到'>' (ascii 62)。不是我们想要的!我们需要一次考虑每个角色。这意味着我们需要生成一种首先打印“1”的算法。然后打印' 4'。从以前开始,我们知道如何做1->' 1'现在问题变成"我们怎样才能把14变成1& 4' 4"

考虑位置数系统,我们知道14 = 1x10 ^ 1 + 4x10 ^ 0。移动到三位数字,我们看到142 = 1x10 ^ 2 + 4x10 ^ 1 + 2x10 ^ 0。看看这些事实,我们可以看到142/100 = 1,142 / 10 = 14和142/1 = 142.这是第一步,我们可以看到如何使用它来提取我们的第一个数字。

但这并不能帮助我们掌握更多数字。但是,记住该分区有一个伙伴,模数,我们可以看到我们可以执行142/100 = 1和142%100 = 42。更进一步,我们注意到42/10 = 4和42%10 = 2。所以现在我们已经把142变成了1 4 2,我们已经知道如何变成1&2;' 1' ' 4' ' 2'

当然,LC3并没有除法和模数函数,但这是一个可以解决的问题,我会留给读者。