[英]Assembly - Converting Binary to Decimal in FASM
I'm doing a sum and sub in Assembly (FASM) trying to get my result in decimal.我在 Assembly (FASM) 中做一个 sum 和 sub 试图得到十进制的结果。 I write the values that I'll sum int decimal.我写下我将总结为 int 十进制的值。 When I run it, it really gives me an output but it is a binary output.当我运行它时,它确实给了我一个输出,但它是一个二进制输出。 I can translate to decimal by myself, but what I really want is that the output be already a decimal.我可以自己翻译成十进制,但我真正想要的是输出已经是十进制了。
name "add-sub"
org 100h
mov al, 10 ; bin: 00001010b
mov bl, 5 ; bin: 00000101b
add bl, al
sub bl, 1
mov cx, 8
print: mov ah, 2
mov dl, '0'
test bl, 10000000b
jz zero
mov dl, '1'
zero: int 21h
shl bl, 1
loop print
mov dl, 'b'
int 21h
mov ah, 0
int 16h
ret
Though you wrote the values decimal, the assembler (FASM) turned them into the "computer" format ie binary.尽管您将值写入十进制,但汇编程序 (FASM) 将它们转换为“计算机”格式,即二进制。 To get a decimal output you must convert the result into "output" format ie ASCII.要获得十进制输出,您必须将结果转换为“输出”格式,即 ASCII。 The method is extensively described in books and on the net.该方法在书籍和网络上有广泛的描述。 Here an example for your needs (FASM, MSDOS, .com):以下是您需要的示例(FASM、MSDOS、.com):
format binary as "com"
use16
org 100h
mov al, 10 ; bin: 00001010b
mov bl, 5 ; bin: 00000101b
add bl, al
sub bl, 1
movzx ax, bl
call AX_to_DEC
mov dx, DECIMAL
mov ah, 9
int 21h
;mov ah, 0
;int 16h
ret
DECIMAL DB "00000$" ; place to hold the decimal number
AX_to_DEC:
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
mov di, DECIMAL ; target string DECIMAL
Second_Loop:
pop ax ; get back pushed digit
or al, 00110000b ; AL to ASCII
mov [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 [di], '$' ; End-of-string delimiter for INT 21 / FN 09h
ret
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.