Assembly 如何在MC68k中将ascii数转换为二进制
我必须编写一个程序,要求用户输入20个0-100之间的数字,以找到数字的平均值,并将其归类为失败或通过,但它将输入保存为ascii在内存中,我必须将其从ascii更改为二进制。我知道ascii数字在十六进制中是30-39,但我不确定如何在MC68K中实现它,就像如果我输入93作为一个数字,那么它将被保存为3933,但如何将其转换为二进制?以下是我的做法:Assembly 如何在MC68k中将ascii数转换为二进制,assembly,easy68k,Assembly,Easy68k,我必须编写一个程序,要求用户输入20个0-100之间的数字,以找到数字的平均值,并将其归类为失败或通过,但它将输入保存为ascii在内存中,我必须将其从ascii更改为二进制。我知道ascii数字在十六进制中是30-39,但我不确定如何在MC68K中实现它,就像如果我输入93作为一个数字,那么它将被保存为3933,但如何将其转换为二进制?以下是我的做法: clear number loop: get highest digit character not dealt with yet
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
注意:以上是一个我没有接触过的处理器的未测试汇编代码。。。好一阵子了。希望它至少能鼓舞人心。在当时,当然没有人敢在这里使用骡子,但这是有指导意义的。请原谅这个双关语。减去“0”?正如您已经知道的,十六进制中的0x30不起作用,因为十六进制中的3933比93大得多。所以我必须把它转换成93,但是每一个数字都是用ASCII码输入的。所以你也必须从每个数字中减去0x30。您将得到一系列“纯”二进制数字,然后需要将它们组合成一个二进制数。恐怕是基本的算术。是的,我知道3933分成3933,每个可以减去30得到93,但这不是我想要的。假设我输入93,它被保存为3933,但我想让它给我5DUm的十六进制值,你把我弄丢了。如果你有一个正确的二进制值列表,你可以用通常的方式将它们组合在一起,得到总共'93',这是你的'5D'。