[英]m68k Hex to Decimal not working right
我正在为正在开发的M68k计算机编写小型操作系统,但遇到了一个小问题。 我需要能够以十进制(31)向用户显示一个十六进制值(例如$ 1F)。我已经编写了以下代码来执行此操作,但是它存在一些问题:
ConvertHexByteToDecimal:
move sr, -(sp) ; Back up status register to stack.
move #$2700, sr ; Disable interrupts.
move.b d2, -(sp) ; Back up d2 to the stack.
and.b #$0F, d2 ; Get rid of the high nybble
cmp.b #$9, d2 ; Is the low nybble in the range of 0-9?
bgt.s @convertHex ; If not, branch.
move.b (sp)+, d3 ; Restore the 10's place from the stack
and.b #$F0, d3 ; Get rid of the low nybble
add.b d3, d2 ; Add the 10's place.
bra.s @done ; If so, branch.
@convertHex:
sub.b #$A, d2 ; Subtract $A from the hexadecimal meeper.
move.b (sp)+, d3 ; Restore the 10's place from the stack
and.b #$F0, d3 ; Get rid of the low nybble
add.b #$10, d3 ; Add 1 to the 10's place.
add.b d3, d2 ; Add the 10's place to the number.
@done:
move.b d2, d1 ; Copy to output register.
move (sp)+, sr ; Restore status register.
rts ; Return to sub.
该代码可以很好地处理高达$ F的值。 例如,如果我输入$ B,它将输出11。但是,一旦数字超过$ F,它将开始被破坏。 如果我在其中输入$ 10,则会输出10,依此类推。 它总是在$ xF之后回绕。
是否有人对为什么这样做有任何想法?
如果您尝试将数字输出为十进制数,则一次只能处理一个半字节就无法做到这一点。 除10 0 == 2 0 == 1
之外,2的幂和10的幂不啮合。
10端的与所有其他非负功率0
两个端,同时非负功率与2
, 4
, 6
或8
(从未0
)。
为了解决这个问题,我们的想法是使用十次幂除法来获得所需的结果。 类似于汇编的伪代码,例如:
// Desired value is in num
push num // example $1f/31
if num < 100 goto tens // no hundreds, so skip
val = num / 100 + '0'
output val
num = num % 100
tens:
if num < 10 goto ones // is >= 10 so do this bit
val = num / 10 + '0' // gives us '3'
output val
num = num % 10 // remainder is 1
ones:
val = num + '0' // gives us '1'
output val
pop num
请注意,我们正在执行与您的代码相同的操作,但是实际上是在执行以16为基数的除法和模数,而不是以10为基数。
您必须自己将伪代码转换为68k,距离我削减该芯片的代码大约有二十年的时间。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.