简体   繁体   English

m68k十六进制到十进制无法正常工作

[英]m68k Hex to Decimal not working right

I'm writing a small OS for a M68k computer that I'm developing, and I've ran into a little issue. 我正在为正在开发的M68k计算机编写小型操作系统,但遇到了一个小问题。 I need to be able to show the user a hexadecimal value (say $1F) in decimal (31.) I've written the following code for doing that, but it has a few issues: 我需要能够以十进制(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.

The code works nicely on values up to $F. 该代码可以很好地处理高达$ F的值。 For example, if I input $B, it outputs 11. However, once the numbers go past $F, it starts being broken. 例如,如果我输入$ B,它将输出11。但是,一旦数字超过$ F,它将开始被破坏。 If I input $10 into it, I get 10 outputted, and so on. 如果我在其中输入$ 10,则会输出10,依此类推。 It always wraps back after a $xF. 它总是在$ xF之后回绕。

Does anyone have any ideas as to why it's doing this? 是否有人对为什么这样做有任何想法?

If you're trying to output a number as decimal, you won't be able to do it by processing one nybble at a time. 如果您尝试将数字输出为十进制数,则一次只能处理一个半字节就无法做到这一点。 Powers of two and powers of ten do not mesh, other than 10 0 == 2 0 == 1 . 10 0 == 2 0 == 1之外,2的幂和10的幂不啮合。

All other non-negative powers of 10 end with a 0 while non-negative powers of two end with 2 , 4 , 6 or 8 (never 0 ). 10端的与所有其他非负功率0两个端,同时非负功率与2468 (从未0 )。

To solve this, the idea is to use division by powers of ten to get what you want. 为了解决这个问题,我们的想法是使用十次幂除法来获得所需的结果。 Assembly-like psuedo-code like: 类似于汇编的伪代码,例如:

    // 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

Note that we're doing the same sort of operations as your code but you're effectively doing base-16 division and modulus rather than base-10. 请注意,我们正在执行与您的代码相同的操作,但是实际上是在执行以16为基数的除法和模数,而不是以10为基数。

You'll have to convert that pseudo-code into 68k yourself, it's been about two decades since I cut code for that chip. 您必须自己将伪代码转换为68k,距离我削减该芯片的代码大约有二十年的时间。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM