繁体   English   中英

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

[英]how do i display double digit numbers in 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

让我们看一下它如何处理一个数字:如果R0 = 4,则将其添加为“ 0”。 4 +'0'='4',这是我们可以打印的内容。

现在想象R0 = 14。 当我们做14 +'0'时,我们得到'>'(ASCII 62)。 根本不是我们想要的! 我们需要一次考虑每个字符。 这意味着我们需要生成一种算法,该算法首先打印“ 1”,然后打印“ 4”。 从以前开始,我们知道了如何做1->'1'和4->'4',所以现在的问题变成了“如何将14变成1和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''4''2'。

当然,LC3不具有除法和模数功能,但这是一个可解决的问题,我留给读者自己解决。

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM