News:

MASM32 SDK Description, downloads and other helpful links
MASM32.com New Forum Link
masmforum WebSite

I can't get a simple loop to work.

Started by eaOn, October 16, 2008, 12:15:29 PM

Previous topic - Next topic

eaOn

I recently tried out a simple loop test. I followed the instruction manual. Although, mnemonics is somewhat true but all I get is an infinite loop result.

Here's my code:
.code

start:
    mov eax,0
    call main

    exit

main proc

    mov eax,sval(input("Enter a number: "))
    mov ecx,0

  .while eax > 0

    print str$(ecx)
    print chr$(13,10)
    sub eax,1
    add ecx,1   

  .endw


; «««««««««««««««««««««««««««««««««««««««««««««««««««««««««««««««««««««««««
; Equal or similar to while syntax   

;    returnHere:
;        print str$(ecx)
;        print chr$(13,10)
;        add ecx,1     
;        cmp eax,0
;        sub eax,1
;        ja returnHere
;
; «««««««««««««««««««««««««««««««««««««««««««««««««««««««««««««««««««««««««

    ret

main endp

end start


After compiling and running it. I get an infinite loop.
Enter a number: 2
.
.
.
2088832679
2088832679
2088832679
2088832679
.
.
.


What I want is suppose to be like this.
Enter a number: 2
0
1
2


I'm running out of idea how to work this out. Need help...  :P

hutch--

try this.


.while SDWORD PTR eax > 0


The current code is unsigned, this makes it signed.
Download site for MASM32      New MASM Forum
https://masm32.com          https://masm32.com/board/index.php

jj2007

eax, ecx, edx are trash registers - they will be modified by print and str$
ebx, esi, edi are reserved registers - they will not be modified. It's called Windows ABI

 push ebx  ; save
 mov ebx, 123
 .while ebx > 0

   print str$(ecx)
   print chr$(13,10)
   sub ebx,1
   add ecx,1  

 .endw
 pop ebx

hutch--

#3
Give this a blast.


; ¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤
    include \masm32\include\masm32rt.inc
; ¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤

comment * -----------------------------------------------------
                        Build this  template with
                       "CONSOLE ASSEMBLE AND LINK"
        ----------------------------------------------------- *

    .code

start:
   
; ¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤

    call main
    inkey
    exit

; ¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤

main proc

    push ebx
    push esi
 
    mov ebx, sval(input("Enter a number: "))
    xor esi, esi            ; set ESI to zero
   
    .while SDWORD PTR ebx >= 0
      print str$(esi),13,10
      sub ebx,1
      add esi,1
    .endw
 
    pop esi
    pop ebx
 
    ret

main endp

; ¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤

end start


LATER: I just cirrected an error, had the ESI EBX registers popped in the wrong order.
Download site for MASM32      New MASM Forum
https://masm32.com          https://masm32.com/board/index.php

eaOn

Thanks hutch-- and jj2007. I got them noted.  :U
Regards,

eaOn