2011-12-21 128 views
0

我正在爲我正在開發的M68k計算機編寫一個小操作系統,並且遇到了一個小問題。我需要能夠向用戶顯示十進制(31)十六進制值(比如$ 1F)我已經寫了下面的代碼這樣做,但它有幾個問題:m68k十六進制到十進制不能正常工作

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後迴繞。

有沒有人有任何想法,爲什麼它這樣做?

+1

代碼不會嘗試實際轉換爲十進制,如果原始值在10..15模式16範圍內,它只會添加6.它從不輸出任何內容,只是將修改後的值返回到寄存器中。你爲什麼期望這實際上有用? – 2011-12-21 04:29:21

回答

3

如果你想輸出一個十進制的數字,你將無法一次處理一個nybble。除了100 == 20 == 1之外,兩個權力和十個權力都不會網格化。

具有兩個端與2468(從未0)一個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 

需要注意的是,我們正在做同樣的排序操作爲您的代碼,但你切實做好基地-16除法和模量,而不是基地10。

你必須自己將這個僞代碼轉換成68k,這是我爲該芯片切割代碼後的大約二十年。