-1

I am having trouble printing the value in al after I divide. I keep getting a weird ascii character, not the 3 like im supposed to be getting. If I put in a instuction mov al,5 it prints perfectly. Any help will be appreciated. Here is the code:

    mov ax,32511
    mov cx,10000

    div cx        ;divide 7eff by 2710.  Result in al will be 3. 

    add al,'0'    ;convert to ascii

    mov ah,2      ;print
    mov dl,al
    int 21h

3 Answers3

1

div cx is actually dividing dx:ax - you should zero dx beforehand for the result you expect.

To expand, it's performing: (dx * 0x10000 + ax) / cx, storing the quotient in ax, and the remainder in dx. If the quotient does not fit in ax, that is: dx >= cx, a divide (overflow) error is raised.

Brett Hale
  • 21,653
  • 2
  • 61
  • 90
  • Tried that already, placing a mov dx,0 right before the print statement still produces the same problem – user2858976 Mar 03 '14 at 20:19
  • @user2858976, you need to put it before the `div` operation. Your bug is there, not the printing. – Leeor Mar 03 '14 at 20:22
0

div cx divides dx:ax by cx. So you should clear dx beforehand.

twin
  • 1,669
  • 13
  • 11
0

Found the issue, it was my own stupidity.

didnt add this first

    mov       ax,@data            ; establish addressability
    mov       ds,ax               ; for the data segment