hello all,
In the following lines of code AX contains the hexadecimal value B(11 in decimal). This program suppose to convert the hexa value into decimal value but it is not giving the result instead it executes the AGAIN : labled instruction iteratively. Most importantly, the DIV DX gives correct result(AX=0001, DX =0001) when it is executed first time, on second time its result is unexpected(AX=3333, DX=0003). I don't understand what am I missing. please give me any idea.

Bye.

START:    MOV AX,Bh    ; Initialize AX to B(hex).
MOV CX,0    ;  INITIALIZE THE DIGIT COUNTER
MOV BX,10    ;  INITIALIZE THE DIVISOR
MOV DX,0    ;  TO BE USED IN DIVISION AND TO AVOID GARBAGE
AGAIN :  DIV BX          ; QUOTIENT AX = AX/BX AND REMAINDER DX = AX%BX
PUSH DX      ; SAVE/KEEP THE REMAINDER(I.E DIGIT)
INC CL          ; CL = CL+1, INCREMENTS THE COUNTER BY 1
CMP AX,0      ; CHECK WETHER THE QUOTIENT IS ZERO
JA AGAIN      ; IF NOT ZERO REPEAT THE DIVISION
NEXT :    POP DX        ; REMOVE THE DIGIT FROM STACK
ADD AL, 30H  ; MAKE/CONVERT THE DIGIT INTO ASCII/CHAR DIGIT
MOV AH,2 ;O/S FUNCTION 2
INT 21H
LOOP NEXT    ; REPEAT THE STEPS UNTILL THE DIGIT COUNTER IS ZERO
Posted on 2009-03-15 07:47:39 by xshadow
DX must be rezeroed before each division. Remember that the entire content of the DX::AX pair is being divided.

Simply move your "AGAIN" lable at the "MOV DX,0" instruction and it should work as you intend.
Posted on 2009-03-15 12:27:05 by Raymond

I did as you told and now the program's flow is correct but still it is not printing the decimal value converted from hexadecimal value. I shall be very thankful to you if you could spend some more time on it.

Regards.
Posted on 2009-03-15 15:00:57 by xshadow
It could very well be that the result is effectively printed. However, if you are exiting the program immediately after the last instruction, your eyes may not be fast enough to see the result before the screen is refreshed. Finish your program with a "wait" of some kind (such as waiting for a keystroke before exiting) so that you have time to see your result on the screen.
Posted on 2009-03-15 20:57:13 by Raymond
``NEXT :    POP DX         ; REMOVE THE DIGIT FROM STACK        ADD AL, 30H   ; MAKE/CONVERT THE DIGIT INTO ASCII/CHAR DIGIT``

I think you mean
``next:  pop dx       add dl,30h``

Posted on 2009-03-15 22:29:40 by sinsi
absolutely I do. thanks
Posted on 2009-03-16 05:59:59 by xshadow
Don't forget an exit at the end...
``  mov ah,4ch  int 21h``

Posted on 2009-03-16 06:08:44 by sinsi