2012-05-10 231 views
1

我想寫一個LC3彙編語言程序,需要兩個輸入數字並打印出x * y = zLC3彙編語言

我可以讓它工作的號碼0-9然而任何數字上面,我得到奇怪的字母或符號。

另外我怎樣才能使它不僅可以只採取每個GETC 1個輸入,但兩個數字,例如。 10 * 12= 120? 任何幫助,將不勝感激! :)

這是我迄今所做

.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 
LEA R0, stringResult 
PUTS 
ADD R0, R3, R6 ;move result to r0 
OUT ;print result 
HALT 
display1 .STRINGZ "\nenter the 1st no.: " 
display2 .STRINGZ "\nenter the 2nd no.: " 
stringResult .STRINGZ "\nResult: " 
INVERSE_ASCII_OFFSET .fill xFFD0 ; Negative of x0030. 
DECIMAL_OFFSET .fill #48 
.END 

回答

1

你的顯示功能的工作原理是將一個數字的'0'基地ASCII值。這是有效的,因爲ascii表的排列方式很方便。例如,'0' + 1 = '1',相當於0x30 + 1 = 0x31。但是,如果您可能發現'0' + 12 = '<'。這是因爲'0' = 0x30,所以0x30 + 12 (0xC) = 0x3C。看看ascii圖表我們看到0x3C = '<'。也就是說,這是一種僅打印出單個數字的有效方法。

這兩個問題的答案都在於編寫一個迭代處理數字並與它們形成數字的例程。換句話說,您將需要一個循環來確定下一個打印出哪個字符並將其打印出來。

+0

你可以看看這個嗎? http://superuser.com/questions/904324/which-of-the-following-instructions-can-reference-a-memory-location-that-is-100 – committedandroider