【问题标题】:how do i display double digit numbers in lc3如何在lc3中显示两位数
【发布时间】:2017-04-26 18:19:38
【问题描述】:

我正在创建一个代码来计算矩形的面积。我已经完成了乘法,但它只显示数字 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

【问题讨论】:

    标签: assembly lc3


    【解决方案1】:

    让我们看看它是如何处理单个数字的: 如果 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 没有除法和取模功能,但这是一个可以解决的问题,我将留给读者。

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2023-04-02
      • 1970-01-01
      • 1970-01-01
      • 2013-12-05
      • 1970-01-01
      相关资源
      最近更新 更多