why this assembly piece of code do jmp forever

134 Views Asked by At

I wrote the code , but I don't know why it prompts from user forever, indeed it never ends unless pressing an Enter without giving any number . This the code :

output prompt1;
    inputs number,16 ;
    atoi number ; 
    mov max,ax ;
    mov cx , 10 ;

do:     dec cx ;
    output prompt1;
    inputs number,16;
    atoi number ;
    cmp ax, max;
    jge l1;
    cmp cx , 1;
    jne do ;
    je exit;

l1:     mov max ,ax ;
    jmp do ;

exit:   
    output prompt2;
    itoa number , max;
    output number;

the inputs and output are some user interface facilities and itoa,atoi are integer to ascii and vice versa respectively, but I should stress a point that "atoi" saves the result in the ax register.

And another question i have, how to design{or make} such these{if,while,for C/C++ styled} algorithms more well-styled ?

Thank you so much.

1

There are 1 best solutions below

0
On BEST ANSWER

Maybe, you could solve the problem by inserting a push and pop to make sure, cx isn't changed

between "do" and "cmp cx, 1":

do:     dec cx ;
push cx
output prompt1;
inputs number,16;
atoi number ;
cmp ax, max;
jge l1;
pop cx
cmp cx , 1;
jne do ;
je exit;

Another problem could be the "je exit". Replace it by

jmp exit

or "jmp short exit". This way, it's save, that the cPU won't continue at l1