please explain me this multi-digit print program
CodeGuru Home VC++ / MFC / C++ .NET / C# Visual Basic VB Forums Developer.com
Results 1 to 4 of 4

Thread: please explain me this multi-digit print program

Hybrid View

  1. #1
    Join Date
    Dec 2011
    Posts
    7

    Question please explain me this multi-digit print program

    Please consider this code:

    .model small
    .stack 100h
    .code

    .startup

    mov AX,235

    ; in 16 bit DIV
    ; dividend = AX
    ; divisor = any register, bx in this case
    ; quotient = AX
    ; remainder = DX

    mov cx,0
    mov bx,10

    Next:
    mov dx,0
    div bx
    push dx
    inc cx
    cmp ax,0
    ja Next

    show:
    pop dx
    mov dh,0
    add dl,48
    mov ah,02
    int 21h
    loop show

    mov ah,4ch ; terminate to DOS successfuly
    int 21h
    end

    I know the logic is "divide the number repeatedly, pushing remainders in stack, and then display one by one poping back from stack" My confusion is this;
    ax = 235
    after 1st div: remainder = dx = 5, and quotient = ax = 23
    after 2nd div:remainder = dx = 3, and quotient = ax = 2
    after 3rd div: remainder = dix = 0, and quotient = ax = 0.2

    poping should yield:035 but it gives 235 correctly. I wonder what happens with last div that is gives 2 correctly. Please help me.

    Best Regards

  2. #2
    Join Date
    Jun 2010
    Location
    Germany
    Posts
    2,594

    Re: please explain me this multi-digit print program

    No black magic at all: An integral division of n by m with n < m always yields a quotient of 0 and a remainder of n. It's simply your manual calculation of the last step that is wrong. (Besides, the AX register never could hold a fractional value like 0.2 anyway... )

    BTW, your "terminate to DOS successfully" code is broken or at least easily breakable: It relies on Int 21h Function 02h to return 0 in AL or at least leave it unchanged, which is not the case (see http://www.ctyme.com/intr/rb-2554.htm). Therefore your program will exit properly but return a non-zero completion code which (commonly) indicates failure.

    Please use code tags when posting code. The labels are somewhat hard to spot in assembly language code lacking proper formatting like that above.
    I was thrown out of college for cheating on the metaphysics exam; I looked into the soul of the boy sitting next to me.

    This is a snakeskin jacket! And for me it's a symbol of my individuality, and my belief... in personal freedom.

  3. #3
    Join Date
    Feb 2012
    Location
    Fremont,CA
    Posts
    36

    Re: please explain me this multi-digit print program

    This is not valid assembly code
    Make sure you check out my >>Facebook Development Blog and Website<<

  4. #4
    Join Date
    Jun 2010
    Location
    Germany
    Posts
    2,594

    Re: please explain me this multi-digit print program

    I didn't actually try to assemble it, but to me it looks perfectly valid (although, admittedly, not nicely formtted). Why do you think it isn't?
    I was thrown out of college for cheating on the metaphysics exam; I looked into the soul of the boy sitting next to me.

    This is a snakeskin jacket! And for me it's a symbol of my individuality, and my belief... in personal freedom.

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  


Windows Mobile Development Center


Click Here to Expand Forum to Full Width

This is a CodeGuru survey question.


Featured


HTML5 Development Center