在英特尔 8086 上打印十六进制分数



我希望能够将十六进制数打印为分数。比方说.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

我不明白如何转换和打印它。我只使用英特尔 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 寄存器有了结果。如果我们有更多的位低于零,那么我们将继续这个过程。这个想法是,每一点的价值都是前一个的一半。这段代码不是最佳的,但它可以帮我工作。

最新更新