私は開発中の 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 の後は常に折り返されます。
なぜこれを行っているのか、誰にもアイデアはありますか?