I’m trying to turn two decimal numbers into floating point numbers, divide them and show the result in 16 bit assembly for a class project without using the floating point unit. I have no idea why whenever I run it prints weird symbols. I have no idea what is wrong with it. Is it a problem of overflow?

.model small
.stack 100h
.data
    decimal_number1 dw 1234    
    decimal_number2 dw 5678     
    output_string db "Result: $"

.code
    start:
        mov ax, [decimal_number1]
        mov bx, ax
        shr bx, 8            
        and ax, 0FFh     

        mov dx, bx                
        mov cx, ax              

        mov ax, [decimal_number2]
        mov bx, ax
        shr bx, 8                 
        and ax, 0FFh             

        add dx, bx               
        adc cx, ax                
        push ax

        mov ah, 02h               
        lea dx, [output_string]   
        int 21h                   
        pop ax

        mov cx, 10                
        mov si, 6                 
float_convert_and_display:
        xor dx, dx                
        div cx                    
        push dx                  
        test ax, ax               
        jnz float_convert_and_display 

float_pop_and_display:
        pop dx                    
        add dl, '0'              
        mov ah, 02h               
        int 21h                   
        loop float_pop_and_display 

        mov ah, 4Ch               
        int 21h                   

end start

I have no idea why whenever I run it prints weird symbols. I have no idea what is wrong with it. Is it a problem of overflow?

1

There are 1 best solutions below

0
Sep Roland On
mov ah, 02h               
lea dx, [output_string]   
int 21h

You need to use the DOS.PrintString function 09h


loop float_pop_and_display

The float_pop_and_display code is using the loop instruction that depends on the CX register to contain the actual number of items that the float_convert_and_display code pushed on the stack. Sadly it contains the const divider 10, and so you are popping too many bytes off the stack. You can solve it with a dedicated counter or a sentinel on the stack:

        mov  cx, 10                
        push cx                   ; Sentinel
float_convert_and_display:
        xor  dx, dx
        div  cx
        push dx                  
        test ax, ax               
        jnz  float_convert_and_display

        pop  dx
float_pop_and_display:                    
        add  dl, '0'              
        mov  ah, 02h               
        int  21h
        pop  dx
        cmp  dx, cx
        jb   float_pop_and_display

See Displaying numbers with DOS for more about this particular conversion.