[英]How to convert ascii number to binary in MC68k
我必須編寫一個程序,要求20個用戶輸入的0到100之間的數字來查找這些數字的平均值並將其分類為失敗或通過,但是它將輸入ascii保存在內存中,並且我必須將其從ascii更改為二進制。 我知道ascii的數字是十六進制的30-39,但是我不確定如何在MC68K中實現它,例如如果我輸入93作為數字,那么它將保存為3933,但是如何將其轉換為二進制?
clear number
loop:
get highest digit character not dealt with yet
compare digit character to '0' ; that's character 0, not value 0
blo done
compare digit character to '9'
bhi done
multiply number by 0x0a ; 10 in decimal
subtract 0x30 from the character
add to number
jmp loop
cone:
...
這是我的處理方式:
str_to_int:
; Converts a decimal signed 0-terminated string in a0 into an integer in d0.
; Stops on the first non-decimal character. Does not handle overflow.
; Trashes other registers with glee.
moveq #0, d3
.signed:
cmpi.b #'-',(a0) ; Check for leading '-'.
bne.s .convert
bchg #0,d3
addq.l #1,a0
bra.s .signed
.convert:
moveq.l #0,d0
moveq.l #0,d1
.digit:
move.v (a0)+,d1
beq.s .done
subi.b #'0',d1 ; Convert to integer.
bmi.s .done ; If < 0, digit wasn't valid.
cmpi.b #'9'-'0',d1
bgt.s .done ; If larger than 9, done.
muls.l #10,d0
add.l d1,d0
bra.s .digit
.done:
tst.b d3
bne.s .signed
rts
.signed:
neg.l d0
rts
注意:以上是未經測試的處理器的匯編代碼,已經有一段時間了。 希望它至少可以鼓舞人心。 過去,當然沒有人敢在這里使用muls
,但這是指導性的。 雙關語。
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.