在 intel8086 上打印十六进制小数

Printing Hexadecimal fraction number on intel8086

我希望能够将十六进制数打印为分数。让我们说.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 中的作用,但我不知道如何实现它。

当我理解了定点到字符串的转换时,答案真的很简单。基本上你已经将零以下的每一位转换为它们的值。因为我只有 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寄存器有结果了。如果我们有更多的位低于零,那么我们将继续这个过程。这个想法是每一位的价值是前一位的一半。这段代码不是最佳的,但它对我有用。