1

I'm doing a sum and sub in Assembly (FASM) trying to get my result in decimal. I write the values that I'll sum int decimal. 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
Nate Eldredge
  • 48,811
  • 6
  • 54
  • 82
  • 2
    _"This question has never been asked before in Stack Overflow"_. Really? To me it looks like you want to print the value of a register, which certainly has been asked before (see e.g. http://stackoverflow.com/questions/15621258/assembly-printing-ascii-number). That fact that you're using FASM rather than NASM/MASM/TASM doesn't really make it a unique question. – Michael May 30 '14 at 14:27

1 Answers1

0

Though you wrote the values decimal, the assembler (FASM) turned them into the "computer" format i.e. binary. To get a decimal output you must convert the result into "output" format i.e. ASCII. The method is extensively described in books and on the net. Here an example for your needs (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
rkhb
  • 14,159
  • 7
  • 32
  • 60