2014-03-27 433 views
0

如何將用戶的任何字符輸入轉換爲相應的十進制值?我剛開始有麻煩。LC3彙編語言程序

程序必須實現以下的事情:

  1. 該程序從鍵盤接受的字符。

  2. 如果字符是數字('0'到'9'): a)將字符轉換爲相應的十進制值。換句話說,'0'變爲零,'1' 變爲1,...'9'變成9.讓我們調用該值R(對於「運行長度」)。 b)等待另一個字符(使用GETC)。 c)將該角色的R副本打印到控制檯。 ) d)返回步驟1

  3. 否則,如果字符是輸入/返回(ASCII#10):打印一個換行符(ASCII#10)到控制檯,並 回到步驟1。

  4. 否則,如果角色是其他任何東西,請暫停程序。

回答

0

如上所述,問題有點不清楚:輸入已經是十進制值了。

如果你真的指的是如何將輸入轉換爲二進制表示,你可以進行數學運算,那麼基本上你可以從每個字符值中減去48(30h)來將每個數字轉換爲數字值。

asciitable.com

如果有多個輸入數字,你遍歷他們和10除了最後一個每次迭代乘法累加器。

+0

我編輯了原來的問題。讓我知道現在是否更清楚 – user3469170

0

你顯然在這裏問同樣的事情:program for LC3 Assembly language

這將有助於在未來,如果你發佈你的代碼。

我沒有按照需要轉換這種類型的問題,而是做了一些檢查,看看數字是什麼,並打印出一個等效的字符串。希望我的計劃給你一些想法:

.ORIG x3000 ; start at x3000 above system memory 
LEA R0, CLASS ; load address of string CLASS in R0 
PUTS  ; use PUTS to output string to console 
LEA R0, MYNAME ; repeat steps for class template 
PUTS 
LEA R0, PRNUM 
PUTS 
LEA R0, NWLINE 
PUTS 

; now get a character from the user and echo it back 
; using the prompts INPROM and OUTPROM 
ASKINP LEA R0, INPPROM 
    PUTS 
    GETC  ; get a character from the keyboard 
    ADD R2,R0,#0 ; move the value in R0 to R2 for later comparisons 

; PROCESS THE INPUT 
; first figure out if they entered a non-printing character with ASCII code 
; <32. If so, suppress the printing of the character and give the user the 
; standard error that they have not entered a number. We do this by subtracting 
; 33 from the input and if the CC is negative, it's a non-printing character. 
; For purposes of this program a space is considered a non-printing character. 
    ADD R2, R2, #-15 
    ADD R2, R2, #-15 
    ADD R2, R2, #-3 ; subtracted 33, see if it's negative 
    BRn XERR ; if so branch to error message 

; if we're still here, then it's a printable character and we continue here... 
    OUT  ; echo the character back on the input line 
    LEA R0, SPACE ; need a space after the input 
    PUTS 
    AND R0, R0, #0 ; set the condition to zero again 

; figure out what character the user entered, and we have already subtracted 33 from 
; the entry. See if it's a ! and the user wants to end. If so, the CC 
; will be Zero and we break to XDONE. 
; if that's not it, subtract another 15 (for a total subtracted of 48) and 
; successively test to see if it's a number match. If there is no match, 
; then print out that the user has not made a correct entry 

; now we subtracted 33 so if they entered ! we now have 0 in R2, subtract 0 
; and test the CC to see if it's Zero. 
    ADD R1, R2, #0 
    BRz XDONE 
; if we have not just branched, subtract another 15 and keep testing 
    ADD R2, R2, #-15 
    ADD R1, R2, #-9 
    BRz XNINE 
    ADD R1, R2, #-8 
    BRz XEIGHT 
    ADD R1, R2, #-7 
    BRz XSEVEN 
    ADD R1, R2, #-6 
    BRz XSIX 
    ADD R1, R2, #-5 
    BRz XFIVE 
    ADD R1, R2, #-4 
    BRz XFOUR 
    ADD R1, R2, #-3 
    BRz XTHREE 
    ADD R1, R2, #-2 
    BRz XTWO 
    ADD R1, R2, #-1 
    BRz XONE 
    ADD R1, R2, #0 
    BRz XZERO 
; not a quit signal or number, need to print out an error and start again 
    BRnzp XERR 

; here is the section that prints out the string related to the 
; number that was entered, by loading the string into R0 and 
; using PUTS to print it out, then BReak back to the beginning 
; of the input sequence and ask again. Pre-condition is that the 
; user typed in a number for 0-9, if not they will get an error 
; elsewhere in the program 
XZERO LEA R0, ZERO 
    PUTS 
    LEA R0, NWLINE 
    PUTS 
    BRnzp ASKINP 
XONE LEA R0, ONE 
    PUTS 
    LEA R0, NWLINE 
    PUTS 
    BRnzp ASKINP 
XTWO LEA R0, TWO 
    PUTS 
    LEA R0, NWLINE 
    PUTS 
    BRnzp ASKINP 
XTHREE LEA R0, THREE 
    PUTS 
    LEA R0, NWLINE 
    PUTS 
    BRnzp ASKINP 
XFOUR LEA R0, FOUR 
    PUTS 
    LEA R0, NWLINE 
    PUTS 
    BRnzp ASKINP 
XFIVE LEA R0, FIVE 
    PUTS 
    LEA R0, NWLINE 
    PUTS 
    BRnzp ASKINP 
XSIX LEA R0, SIX 
    PUTS 
    LEA R0, NWLINE 
    PUTS 
    BRnzp ASKINP 
XSEVEN LEA R0, SEVEN 
    PUTS 
    LEA R0, NWLINE 
    PUTS  
    BRnzp ASKINP 
XEIGHT LEA R0, EIGHT 
    PUTS 
    LEA R0, NWLINE 
    PUTS 
    BRnzp ASKINP 
XNINE LEA R0, NINE 
    PUTS 
    LEA R0, NWLINE 
    PUTS 
    BRnzp ASKINP 
XERR LEA R0, INPERR 
    PUTS 
    LEA R0, NWLINE 
    PUTS 
    BRnzp ASKINP 
XDONE LEA R0, DONE 
    PUTS 
    LEA R0, NWLINE 
    PUTS 
    HALT 

; store the following strings with these labels 
NWLINE .STRINGZ "\n" 
SPACE .STRINGZ " " 
CLASS .STRINGZ "CS2810\n" 
MYNAME .STRINGZ "James Lohse\n" 
PRNUM .STRINGZ "Project 3\n" 
BYEBYE .STRINGZ "Program execution terminated!\n" 
INPPROM .STRINGZ "Input a number 0-9: " 
INPERR .STRINGZ "Error! You did not input a number." 
DONE .STRINGZ "Done!" 
ZERO .STRINGZ "zero" 
ONE .STRINGZ "one" 
TWO .STRINGZ "two" 
THREE .STRINGZ "three" 
FOUR .STRINGZ "four" 
FIVE .STRINGZ "five" 
SIX .STRINGZ "six" 
SEVEN .STRINGZ "seven" 
EIGHT .STRINGZ "eight" 
NINE .STRINGZ "nine " 
    .END 
+0

而且我相信我可以用一個循環檢查值而不是原始語句來做到這一點更優雅 – JimLohse

+0

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