flat assembler
Message board for the users of flat assembler.

Index > Main > A loop question

Author
Thread Post new topic Reply to topic
bolzano_1989



Joined: 06 Oct 2011
Posts: 4
bolzano_1989
I'm trying to create a loop in FASM.
Could you help me identify why the following code gives me an infinite loop and what I should do to fix this error?

Code:
include '%fasminc%/win32ax.inc'

.data
  endline DB 10,13,0
  numwritten DD ?
  inchar     DB ?
  outhandle  DD ?
  inhandle   DD ?
  numread    DD ?

.code

  start:
        invoke  AllocConsole
        invoke  GetStdHandle,STD_OUTPUT_HANDLE
        mov [outhandle],eax                   
        xor eax, eax
        mylabel:
                inc eax
                invoke  WriteConsole,[outhandle],"Hello",5,numwritten,0
                invoke  WriteConsole,[outhandle],endline,1,numwritten,0
                cmp eax,10
                jne mylabel
        invoke  GetStdHandle,STD_INPUT_HANDLE
        mov [inhandle],eax
        invoke  ReadConsole,[inhandle],inchar,1,numread,0
        invoke  ExitProcess,0
.end start    
Post 07 Oct 2011, 05:44
View user's profile Send private message Reply with quote
goldenspider



Joined: 16 May 2011
Posts: 38
goldenspider
Aha , Because EAX will be change. So,

xor eax, eax
mylabel:
inc eax
push eax
invoke WriteConsole,[outhandle],"Hello",5,numwritten,0
invoke WriteConsole,[outhandle],endline,1,numwritten,0
pop eax
cmp eax,10
jne mylabel
Post 07 Oct 2011, 06:06
View user's profile Send private message Reply with quote
Madis731



Joined: 25 Sep 2003
Posts: 2141
Location: Estonia
Madis731
invoke and usually every other code uses eax as return value therefore its trashed every time.
You have two options:
1) use a different register (esi, edi, ebx) that should be preserved
2) after inc eax use push eax and just before cmp eax,10 use pop eax

The second method is the safest and you can even use eax as a scratchpad between the push/pop. There are other methods, like using a memory area to handle the counting. Use your imagination.
Post 07 Oct 2011, 06:08
View user's profile Send private message Visit poster's website Yahoo Messenger MSN Messenger Reply with quote
ManOfSteel



Joined: 02 Feb 2005
Posts: 1154
ManOfSteel
You should push eax after incrementing it and pop it back before comparing it to 10. You can't trust the API to save registers.

EDIT: geez there's an invasion in progress. Smile
Post 07 Oct 2011, 06:09
View user's profile Send private message Reply with quote
bolzano_1989



Joined: 06 Oct 2011
Posts: 4
bolzano_1989
Thank you ManOfSteel, Madis731, goldenspider for your clear explanation and your fix Smile .
Post 07 Oct 2011, 07:12
View user's profile Send private message Reply with quote
Display posts from previous:
Post new topic Reply to topic

Jump to:  


< Last Thread | Next Thread >
Forum Rules:
You cannot post new topics in this forum
You cannot reply to topics in this forum
You cannot edit your posts in this forum
You cannot delete your posts in this forum
You cannot vote in polls in this forum
You cannot attach files in this forum
You can download files in this forum


Copyright © 1999-2020, Tomasz Grysztar. Also on YouTube, Twitter.

Website powered by rwasa.