0

私は開発中の M68k コンピューター用の小さな OS を書いていますが、ちょっとした問題に遭遇しました。ユーザーに 16 進数値 ($1F など) を 10 進数 (31) で表示できるようにする必要があります。それを行うために次のコードを作成しましたが、いくつかの問題があります。

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 の後は常に折り返されます。

なぜこれを行っているのか、誰にもアイデアはありますか?

4

1 に答える 1

3

数値を 10 進数として出力しようとしている場合、一度に 1 つのニブルを処理するだけでは出力できません。2 の累乗と 10 の累乗は、 以外はかみ合いません。100 == 20 == 1

その他すべての 10 の負でないベキ乗は a 0while で終わります。24680

これを解決するためのアイデアは、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

コードと同じ種類の操作を行っていますが、基数 10 ではなく基数 16 の除算とモジュラスを効果的に実行していることに注意してください。

その疑似コードを自分で 68k に変換する必要があります。そのチップのコードをカットしてから約 20 年になります。

于 2011-12-21T04:23:57.480 に答える