首页 文章

在intel8086上打印十六进制分数

提问于
浏览
0

我希望能够将十六进制数字打印为分数 . 让我们说.D

;This macro registers AH,AL
READ MACRO       
MOV AH,8     
INT 21H
ENDM

;This macro registers AH,DL
PRINT MACRO CHAR
PUSH AX
PUSH DX
MOV DL,CHAR
MOV AH,2
INT 21H
POP DX
POP AX         
ENDM
.main:
READ ;This is Actually .D
xor ah,ah ; clear it
; some code to translate ascii to hex    

; Needed code goes here

你可以看到我读了十六进制字符,我想把它作为一个分数打印出来 . 例如.D打印为.8125

我不明白如何转换它并打印它 . 我只使用intel 8086程序集 . 我知道这实际上是浮点数在c中所做的,但我不知道如何实现它 .

1 回答

  • 0

    当我理解定点到字符串转换时,答案非常简单 . 基本上你已经将零以下的每一位都转化为它们的值 . 由于我只有4位,算法很简单:

    //cl has the bits lets say 0000 1111 = 0F
    mov ax,0
    rol cl,5     // I only care for the 4 bit. 
                 // So i shift 4 times => 1111 0000 and then a 5th
    jnc next1    // If carry has not set then skip this bit
    add ax,5000  // 1st bit worths 5000 or 1/2 * 10000 to make it an integer
    next1:
    rol cl,1     // Rotate for the next bit
    jnc next2
    add ax,2500  // 2st bit worths 2500 or 1/4 * 10000 to make it an integer
    next2:
    rol cl,1     // Rotate for the next bit
    jnc next3
    add ax,1250  // 3st bit worths 1250 or 1/8 * 10000 to make it an integer
    next3:
    rol cl,1     // Rotate for the next bit
    jnc next4
    add ax,625  // 4th bit worths 625 or 1/16 * 10000 to make it an integer
    next4:
    

    现在al寄存器有结果 . 如果我们有更多的位低于零,那么我们将继续该过程 . 这个想法是每一点都值得前一个的一半 . 这段代码并不是最优的,但它确实可以解决这个问题 .

相关问题