我必须编写一个程序,该程序需要 20 个用户输入的 0-100 数字来查找数字的平均值并将它们归类为失败或通过,但它将输入作为 ascii 保存在内存中,我必须将其从 ascii 更改为二进制。我知道 ascii 数字在十六进制中是 30-39,但我不确定如何在 MC68K 中实现它,比如如果我输入 93 作为数字,那么它将保存为 3933,但如何将其转换为二进制?
问问题
2469 次
2 回答
0
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:
...
于 2014-04-07T21:48:39.793 回答
0
这是我的做法:
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
在这里使用,但它具有指导意义。原谅双关语。
于 2014-04-09T11:09:53.363 回答