-1

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.

Amit Singh Tomar
  • 8,380
  • 27
  • 120
  • 199
  • How did you set `dx` to `10`? If it was literally `10` then you probably set it to `10` decimal which got converted at the input to `1010` binary in `dx` which then got converted back to `10` decimal again and printed. Also, just to be sure and depending upon your assembler, double check the `add dx,"0"` instruction. `"0"` usually means a string, whereas `'0'` would be a character (which is what you want). But I think your problem is that you're giving it a decimal number to start with. – lurker Jul 27 '13 at 20:19
  • @mbratch Just updated the code which I am running using NASM on DosBOx0.7 and I checked add dx,"0"(dx=1) gives me 49 as output on my DosBox. – Amit Singh Tomar Jul 27 '13 at 20:23
  • Voting to close as why is this code not working. – Ciro Santilli OurBigBook.com Oct 28 '15 at 13:41

1 Answers1

1

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.

lurker
  • 56,987
  • 9
  • 69
  • 103
  • Thanks @mbratch ,It worked fine now ,Just one doubt would like to ask you , I wanted to read the low memory presented in x86 real mode and added int 0x12 to this program and output coming out to be 640 ,so how should I read it 640kb or what? – Amit Singh Tomar Jul 27 '13 at 20:39
  • @AmitSinghTomar I would suggest adding that as a new stackoverflow.com question. You'll get more viewers of it that way. – lurker Jul 27 '13 at 20:41
  • @AmitSinghTomar there's a little info on `int 12h` at http://wiki.osdev.org/Detecting_Memory_%28x86%29. I think you just need to check for the successful execution of the `int` as shown, then put the value `ax` into `dx` and run your program. – lurker Jul 27 '13 at 20:54
  • Yes @mbratch ,I have been following this link for last one week and the way I am doing is just calling int 0x12 and commenting out mov ax,dx . Now value in ax is in binary which will be converted to decimal in my program and output coming out 640 which I believe is 640. – Amit Singh Tomar Jul 27 '13 at 21:01
  • @AmitSinghTomar that makes sense. And the `640` does indeed represent `640kb` per the documentation. – lurker Jul 27 '13 at 22:25
  • I just read this code again and got into bit of confusion,When I use 10b as input to dx ,how its printing 2 ,in case of 10b first 0 is pushed to stack and then 1 is pushed ,while printing 1 is poped out first ,add dx,"0" will cause 1 to be printed on screen and then 0 is poped out which causes 0 to be printed out ,how come 2 printed out ,what point of time value to dx become 50 so that 2 is printed .Hope you would clarify on that also. – Amit Singh Tomar Jul 28 '13 at 06:07
  • @AmitSinghTomar the code is doing a single decimal digit at a time. So think of it at a higher level: it's dividing the number by 10, getting a quotient and remainder (which will be between 0 and 9), adding "0" to the remainder, then printing that character. It repeats the process with the quotient in place of the last number until it's zero. So if you start with 10b, divided by 10 (in `si`) gives a quotient of 0 (in `ax`) and remainder of 2 (in `dx`). It then is done with that loop and in the second loop pops off the 2, adds "0" and prints it. Getting ASCII 2. – lurker Jul 28 '13 at 10:36