簡體   English   中英

m68k十六進制到十進制無法正常工作

[英]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兩個端,同時非負功率與2468 (從未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.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM