【问题标题】:m68k Hex to Decimal not working rightm68k 十六进制到十进制无法正常工作
【发布时间】:2011-12-21 04:15:23
【问题描述】:

我正在为我正在开发的 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..15 mod 16 范围内,它只会添加 6。它从不输出任何内容,只是在 a 中返回修改后的值登记。为什么您希望这实际上会做任何有用的事情?

标签: assembly motorola 68000


【解决方案1】:

如果您尝试将数字输出为十进制,您将无法通过一次处理一个 nybble 来实现。除了 10<sup>0</sup> == 2<sup>0</sup> == 1 之外,2 的幂和 10 的幂不啮合。

10 的所有其他非负幂以 0 结尾,而两个非负幂以 2468 结尾(从不0)。

为了解决这个问题,我们的想法是使用 10 的幂来得到你想要的东西。类似汇编的伪代码,如:

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

请注意,我们正在执行与您的代码相同的操作,但您实际上是在执行 base-16 除法和模数,而不是 base-10。

你必须自己把那个伪代码转换成 68k,自从我为那个芯片剪代码已经过去了大约 20 年。

【讨论】:

    猜你喜欢
    • 2017-06-04
    • 2011-12-09
    • 2018-07-26
    • 2020-12-27
    • 2018-04-03
    • 2014-08-24
    • 2016-01-05
    • 2017-11-13
    • 2023-03-14
    相关资源
    最近更新 更多