flat assembler
Message board for the users of flat assembler.

Index > Macroinstructions > Recursion in macros

Author
Thread Post new topic Reply to topic
Cgtk



Joined: 10 May 2020
Posts: 14
Cgtk
Hello, I need to implement a construction like this using fasm macros:
Code:
macros recursion{
random 1, 2; min, max
if random_result = 2
   recursion
end if

}
    


How i can do this?
Post 11 Jul 2020, 17:13
View user's profile Send private message Reply with quote
DimonSoft



Joined: 03 Mar 2010
Posts: 807
Location: Belarus
DimonSoft
Cgtk wrote:
Hello, I need to implement a construction like this using fasm macros:
Code:
macros recursion{
random 1, 2; min, max
if random_result = 2
   recursion
end if

}
    


How i can do this?

First of all note that you mix preprocessor and compiler stuff. In this particular case you’ll have an endless recursion. Still, the general idea is the following: write a macro that defines your macro and call it before recursive call of the macro itself. For your case, it would look something like:
Code:
macro def_recursion
{
  macro recursion
  \{
    random 1, 2; min, max
    if random_result = 2
      def_recursion
      recursion
    end if
  \}
}

def_recursion    
I write this directly in a browser (just like you do), so might make some mistake but the general idea is this.
Post 11 Jul 2020, 19:44
View user's profile Send private message Visit poster's website Reply with quote
Cgtk



Joined: 10 May 2020
Posts: 14
Cgtk
Thanks!
Post 11 Jul 2020, 21:18
View user's profile Send private message Reply with quote
Roman



Joined: 21 Apr 2012
Posts: 814
Roman
And how use this macro ?
Post 12 Jul 2020, 12:22
View user's profile Send private message Reply with quote
DimonSoft



Joined: 03 Mar 2010
Posts: 807
Location: Belarus
DimonSoft
Roman wrote:
And how use this macro ?
The first question is what the macro should do in the first place. I guess, this code was given just as a generic example of something that should happen inside. Like I said before, mixing preprocessor and assember features might make the macro in its current implementation useless and buggy.
Post 12 Jul 2020, 15:51
View user's profile Send private message Visit poster's website Reply with quote
Cgtk



Joined: 10 May 2020
Posts: 14
Cgtk
Hello again, I'm trying to implement a recursion construct like this
Code:

macro big_macros{

macro num_1 param1
\{
random 1, 2

if param1 = 1
   if randnum = 2
      num_2
   end if
end if
\}


macro num_2
\{

num_1 0  ; error here

\}
}

start:
   big_macros

    


The compiler throws the error: "illegal instruction"
What is wrong?


Last edited by Cgtk on 20 Jul 2020, 10:35; edited 1 time in total
Post 19 Jul 2020, 19:46
View user's profile Send private message Reply with quote
DimonSoft



Joined: 03 Mar 2010
Posts: 807
Location: Belarus
DimonSoft
DimonSoft wrote:
First of all note that you mix preprocessor and compiler stuff.
Post 19 Jul 2020, 21:20
View user's profile Send private message Visit poster's website Reply with quote
Cgtk



Joined: 10 May 2020
Posts: 14
Cgtk
DimonSoft wrote:
DimonSoft wrote:
First of all note that you mix preprocessor and compiler stuff.

I am writing macros on fasm for the first time and don't know how to do it properly. Could you tell me how you can implement a macro similar in functionality to the macro that I described in the post earlier?
Post 20 Jul 2020, 10:02
View user's profile Send private message Reply with quote
revolution
When all else fails, read the source


Joined: 24 Aug 2004
Posts: 17624
Location: In your JS exploiting you and your system
revolution
There is a problem here:
Code:
marco big_macros{    
marco should be macro

Also use backslash \ to escape the curly brackets, not forward slash /
Post 20 Jul 2020, 10:07
View user's profile Send private message Visit poster's website Reply with quote
Cgtk



Joined: 10 May 2020
Posts: 14
Cgtk
revolution wrote:
There is a problem here:
Code:
marco big_macros{    
marco should be macro

Also use backslash \ to escape the curly brackets, not forward slash /

Oh, thank you, fixed the post with the code. Unfortunately this is just my typo in the example and the compiler is not swearing because of this.
Post 20 Jul 2020, 10:34
View user's profile Send private message Reply with quote
revolution
When all else fails, read the source


Joined: 24 Aug 2004
Posts: 17624
Location: In your JS exploiting you and your system
revolution
Works for me.
Code:
1 passes, 0 bytes.    
Post 20 Jul 2020, 10:42
View user's profile Send private message Visit poster's website Reply with quote
Cgtk



Joined: 10 May 2020
Posts: 14
Cgtk
Try to compile this


Description:
Download
Filename: trashgen.asm
Filesize: 55.26 KB
Downloaded: 36 Time(s)

Post 20 Jul 2020, 11:09
View user's profile Send private message Reply with quote
DimonSoft



Joined: 03 Mar 2010
Posts: 807
Location: Belarus
DimonSoft
Build your source code with Ctrl+F8 if you use FASMW IDE, then feed the fas-file to PREPSRC tool in the %FASM%\TOOLS\WIN32 directory (easily buildable from the source code). PREPSRC might give you sense of what is going on.
Post 20 Jul 2020, 11:13
View user's profile Send private message Visit poster's website Reply with quote
Cgtk



Joined: 10 May 2020
Posts: 14
Cgtk
DimonSoft wrote:
Build your source code with Ctrl+F8 if you use FASMW IDE, then feed the fas-file to PREPSRC tool in the %FASM%\TOOLS\WIN32 directory (easily buildable from the source code). PREPSRC might give you sense of what is going on.


I can't compile my code. The compiler throws the error: "illegal instruction".
Post 20 Jul 2020, 11:25
View user's profile Send private message Reply with quote
revolution
When all else fails, read the source


Joined: 24 Aug 2004
Posts: 17624
Location: In your JS exploiting you and your system
revolution
Cgtk: Please reduce your code from the 55kB you posted there. Just post the necessary parts showing the problem.
Post 20 Jul 2020, 11:34
View user's profile Send private message Visit poster's website Reply with quote
Cgtk



Joined: 10 May 2020
Posts: 14
Cgtk
revolution wrote:
Cgtk: Please reduce your code from the 55kB you posted there. Just post the necessary parts showing the problem.


Description:
Download
Filename: reducedcode.asm
Filesize: 1.71 KB
Downloaded: 37 Time(s)

Post 20 Jul 2020, 11:52
View user's profile Send private message Reply with quote
Cgtk



Joined: 10 May 2020
Posts: 14
Cgtk
DimonSoft wrote:
Build your source code with Ctrl+F8 if you use FASMW IDE, then feed the fas-file to PREPSRC tool in the %FASM%\TOOLS\WIN32 directory (easily buildable from the source code). PREPSRC might give you sense of what is going on.

Sorry for a very stupid question, but what should I submit as an input and output file?
Post 20 Jul 2020, 11:58
View user's profile Send private message Reply with quote
revolution
When all else fails, read the source


Joined: 24 Aug 2004
Posts: 17624
Location: In your JS exploiting you and your system
revolution
There is still a lot of extraneous stuff in there, but the error output shows that you are calling gen_trash_func from within itself.

You need to regenerate the macro within itself if you want to do a recursive call.

See the fasm win32ax.inc code for how allow_nesting does it.
Post 20 Jul 2020, 12:04
View user's profile Send private message Visit poster's website Reply with quote
Cgtk



Joined: 10 May 2020
Posts: 14
Cgtk
Thank you, I will definitely look at it.
Post 20 Jul 2020, 12:15
View user's profile Send private message Reply with quote
DimonSoft



Joined: 03 Mar 2010
Posts: 807
Location: Belarus
DimonSoft
Cgtk wrote:
DimonSoft wrote:
Build your source code with Ctrl+F8 if you use FASMW IDE, then feed the fas-file to PREPSRC tool in the %FASM%\TOOLS\WIN32 directory (easily buildable from the source code). PREPSRC might give you sense of what is going on.

Sorry for a very stupid question, but what should I submit as an input and output file?

The fas-file you got as the input and any file name as output (it will be a text file with step-by-step explanations of what preprocessor sees and how does it (pre)process it).
Post 20 Jul 2020, 13:35
View user's profile Send private message Visit poster's website 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 GitHub, YouTube, Twitter.

Website powered by rwasa.