flat assembler
Message board for the users of flat assembler.
Index
> Macroinstructions > For loop [EXAMPLE] |
Author |
|
Kazyaka 01 Feb 2013, 15:28
I wrote a For...EndFor macros based on this post.
I think it's better then old macro. Code: macro For param* { local endfor,for macro end_for \{ endfor: \} match variable==start To stop,param \{ display "A" var equ variable mov [variable],start for: _for equ for cmp [variable],stop \} ja endfor } macro EndFor { add [var],1 jmp _for end_for purge end_for restore _for restore for restore end_for restore var } Code: include 'win32ax.inc' .code start: For i=1 To 2 invoke MessageBox,HWND_DESKTOP,"i",invoke GetCommandLine,MB_OK For j=1 To 3 invoke MessageBox,HWND_DESKTOP,"j",invoke GetCommandLine,MB_OK EndFor EndFor invoke ExitProcess,0 .end start i dd ? j dd ? #Edit Last edited by Kazyaka on 01 Feb 2013, 16:18; edited 3 times in total |
|||
01 Feb 2013, 15:28 |
|
revolution 01 Feb 2013, 15:42
Just quickly I notice that the EndFor macro addresses [i] directly. I assume you should be using [variable] instead.
|
|||
01 Feb 2013, 15:42 |
|
uart777 19 Mar 2013, 08:54
Kazyaka: It's great to see that beginner macro writers like you are learning, but I see serious problems here.
As a general rule, one should never use automatic [] when referring to parameters in language runtime macros like .if/.while/.for, etc. This prevents a direct number or register from being sent (i becomes [i], r becomes [r] and [r] becomes [[r]] which is an error): * It changes FASM's "ideal mode" syntax: address/[value]. Same in TASM and disassemblers * Will all of your macros do this or just For? Not If or While? Note that For is slightly higher-level than If and While * Inside For and everywhere else, you will be referring to the index as [i] * This address/[value] inconsistency will cause endless bugs. Any C/C++ programmers reading? Imagine if you changed the address/*value notation in some cases but not others. It would cause Hell! * FASM's .if/.while macros don't do this and for good reasons. By the way, you might want to test your macros first before you think they should replace the ones included with FASM Additional problems: No error checking? Assuming unsigned, "ja" should be "jae" for correct count. "add [var],1" should be "inc var". No reason to re/define "macro end_for" for every appearance of For. Try this instead: Code: macro For [p] { common local ..start, ..end ?START equ ..start ?END equ ..end define ?s 0 match =0 i==x =To n, ?s p \{ define ?s 1 ?INDEX equ i mov i, x ?START: cmp i, n jae ?END \} if ?s eq 0 'Syntax error' For end if } macro EndFor { inc ?INDEX jmp ?START ?END: restore ?START, ?END, ?INDEX } |
|||
19 Mar 2013, 08:54 |
|
< Last Thread | Next Thread > |
Forum Rules:
|
Copyright © 1999-2024, Tomasz Grysztar. Also on GitHub, YouTube.
Website powered by rwasa.