除法后如何输出从零开始的浮点数?
How do I output floating point starting with zero after division?
目前我的做法如下
mov bx,divisor
div bx
mov quotient,ax
mov ax,dx
mul hundred ;--r * 100 to show 2 floating point, r * 1000 to show 3...
div bx
mov float,ax
mov floatr,dx
然后我可能会根据 floatr 的值增加 float。然后,我将输出商,'.',浮点数。这适用于大多数数字或带有以非零数字开头的浮点数的数字。例如,1/100 = 0.01,但是,我的程序将输出 0.1,它不会输出额外的零。
编写整数输出函数以产生固定数量的输出数字,当重复除以 10 产生零时不停止。
这将为您提供将此整数用作小数部分的小数部分所需的前导零。
顺便说一句,这是定点数而不是浮点数。您正在扩展以能够处理小数点后的固定位数。浮点表示将涉及存储指数和 significand/mantissa,而不是单独的整数和小数部分。
原理和我回答的一样。这是我对 16 位 MASM 的实现:
.MODEL small, C
.386
.STACK 100h
.CODE
main PROC
mov ax, 1 ; Dividend
mov bx, 100 ; Divisor
xor dx, dx ; Clear high 16 bit of dividend
div bx ; Result: AX, remainder in DX
call WriteDec
mov al, '.' ; Decimal point
call WriteChar
mov cx, 10 ; Maximal 10 digits
L1:
imul ax, dx, 10 ; AX = DX * 10 i.e. New dividend = Old remainder * 10
xor dx, dx ; Clear the high part of dividend
div bx ; AX rem. DX = DX:AX / BX
call WriteDec
dec cx
jz SHORT E
test dx, dx ; Is there a remainder?
jnz L1 ; Yes - once more
E:
mov ax, 4C00h
int 21h
main ENDP
WriteDec PROC USES AX BX CX DX DI DS
LOCAL decimal[6]:byte
mov dx, ss
mov ds, dx
lea di, decimal ; LOCAL target string decimal
mov bx, 10 ; divisor
xor cx, cx ; CX=0 (number of digits)
First_Loop:
xor dx, dx ; Attention: DIV applies also DX!
div bx ; DX:AX / BX = AX remainder: DX
push dx ; LIFO
inc cl ; increment number of digits
test ax, ax ; AX = 0?
jnz First_Loop ; no: once more
Second_Loop:
pop ax ; get back pushed digit
or al, 00110000b ; AL to ASCII
mov byte ptr [di], al ; save AL
inc di ; DI points to next character in string DECIMAL
loop Second_Loop ; until there are no digits left
mov byte ptr [di], '$' ; End-of-string delimiter for INT 21 / FN 09h
lea dx, decimal
mov ah, 09h
int 21h
ret
WriteDec ENDP
WriteChar PROC USES AX DX
mov dl, al
mov ah, 02h
int 21h
ret
WriteChar ENDP
END main
顺便说一句:新年快乐
目前我的做法如下
mov bx,divisor
div bx
mov quotient,ax
mov ax,dx
mul hundred ;--r * 100 to show 2 floating point, r * 1000 to show 3...
div bx
mov float,ax
mov floatr,dx
然后我可能会根据 floatr 的值增加 float。然后,我将输出商,'.',浮点数。这适用于大多数数字或带有以非零数字开头的浮点数的数字。例如,1/100 = 0.01,但是,我的程序将输出 0.1,它不会输出额外的零。
编写整数输出函数以产生固定数量的输出数字,当重复除以 10 产生零时不停止。
这将为您提供将此整数用作小数部分的小数部分所需的前导零。
顺便说一句,这是定点数而不是浮点数。您正在扩展以能够处理小数点后的固定位数。浮点表示将涉及存储指数和 significand/mantissa,而不是单独的整数和小数部分。
原理和我回答的一样
.MODEL small, C
.386
.STACK 100h
.CODE
main PROC
mov ax, 1 ; Dividend
mov bx, 100 ; Divisor
xor dx, dx ; Clear high 16 bit of dividend
div bx ; Result: AX, remainder in DX
call WriteDec
mov al, '.' ; Decimal point
call WriteChar
mov cx, 10 ; Maximal 10 digits
L1:
imul ax, dx, 10 ; AX = DX * 10 i.e. New dividend = Old remainder * 10
xor dx, dx ; Clear the high part of dividend
div bx ; AX rem. DX = DX:AX / BX
call WriteDec
dec cx
jz SHORT E
test dx, dx ; Is there a remainder?
jnz L1 ; Yes - once more
E:
mov ax, 4C00h
int 21h
main ENDP
WriteDec PROC USES AX BX CX DX DI DS
LOCAL decimal[6]:byte
mov dx, ss
mov ds, dx
lea di, decimal ; LOCAL target string decimal
mov bx, 10 ; divisor
xor cx, cx ; CX=0 (number of digits)
First_Loop:
xor dx, dx ; Attention: DIV applies also DX!
div bx ; DX:AX / BX = AX remainder: DX
push dx ; LIFO
inc cl ; increment number of digits
test ax, ax ; AX = 0?
jnz First_Loop ; no: once more
Second_Loop:
pop ax ; get back pushed digit
or al, 00110000b ; AL to ASCII
mov byte ptr [di], al ; save AL
inc di ; DI points to next character in string DECIMAL
loop Second_Loop ; until there are no digits left
mov byte ptr [di], '$' ; End-of-string delimiter for INT 21 / FN 09h
lea dx, decimal
mov ah, 09h
int 21h
ret
WriteDec ENDP
WriteChar PROC USES AX DX
mov dl, al
mov ah, 02h
int 21h
ret
WriteChar ENDP
END main
顺便说一句:新年快乐