Question

I am new to assembly language and trying to learn it by means of code ,I found a piece of code which says it will convert binary to decimal and output as ascii on to screen .Below is the code

 org 100h
    push    ax
    push    cx
    push    dx
    push    si
    mov     dx,10
    mov     ax,dx   ;Assuming number to print starts in DX
    mov     si,10   ;decimal 10
    xor     cx,cx   ;Initialize count at 0

    NonZero:
    xor     dx,dx   ;Clear last remainder
    div     si
    push    dx      ;Save digits in reverse order
    inc     cx
    or      ax,ax   ;Is original number down to 0 yet?
    jnz     NonZero ;No, continue looping

    mov     ah,02h

    WriteDigitLoop:
    pop     dx
    add     dx,"0"  ;Convert to ASCII
    int     21h     ;  and print
    loop    WriteDigitLoop

    EndDecimal:
    pop     si
    pop     dx
    pop     cx
    pop     ax

Now lets assume ,initial value of dx is 10 ,so I guess output should comes out to be 2 . but actual output coming out to be 10 which is obvious as per code flow.

If it is a problem then what changes should I make to rectify this.

Était-ce utile?

La solution

The program looks fine.

However, your input isn't correct. In this instruction:

mov     dx,10

You're setting the input to 10 decimal, but you are intending to set it to 10 binary. The binary value in dx at that point is 1010 (because you set it to 10 decimal). So the output is coming out 10, which is what you'd expect. If you want to set dx to 10 binary, you can do this:

mov     dx,10b

Then when you run your program, the output should be 2.

Licencié sous: CC-BY-SA avec attribution
Non affilié à StackOverflow
scroll top