2017-04-26 64 views
0

我正在創建一個代碼來計算矩形的面積。我已經完成了乘法,但它只顯示數字0-9。我的教授說,爲了顯示2位數字,我需要在一個循環中減去10,並計算循環發生的次數。我試了一下,它不起作用有人可以幫助我。我如何在lc3中顯示雙位數字

.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 

回答

0

讓我們來看看它是如何工作的一個數字: 如果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沒有分割和模量的功能,但這是一個可以解決的問題,我會留給讀者看。