flat assembler
Message board for the users of flat assembler.

Index > Macroinstructions > Quick Question on Macros

Author
Thread Post new topic Reply to topic
Imagist



Joined: 13 Jun 2004
Posts: 114
Location: Pennsylvania (USA)
Imagist 23 Aug 2004, 18:14
How exactly are macros assembled? Does the assembler simply insert the code into the program like this?:

Code:
jmp code
label var1
db 10
label var2
db 11

code:
   switch var1, var2
   
macro switch a,b{
mov ax,[a]
mov bx,[b]
mov [b],ax
mov [a],bx
}
    


assembles to:

Code:
jmp code
label var1
db 10
label var2
db 11

code:
   mov ax,[var1]
   mov bx,[var2]
   mov [var2],ax
   mov [var1],bx
    


Or is the macro called from a different spot, like functions in C, allowing recursion?

_________________
Many things are possible. Few things are likely.
Post 23 Aug 2004, 18:14
View user's profile Send private message Visit poster's website Reply with quote
vid
Verbosity in development


Joined: 05 Sep 2003
Posts: 7105
Location: Slovakia
vid 23 Aug 2004, 19:49
you are right with your result, except one thing, macro has to be defined before it is used. Your example will be preprocessed to
Code:
jmp code 
label var1 
db 10 
label var2 
db 11 

code: 
   switch var1, var2
    

and then during assembly you will get error saying "switch" is not name of instruction or directive.
Post 23 Aug 2004, 19:49
View user's profile Send private message Visit poster's website AIM Address MSN Messenger ICQ Number Reply with quote
Imagist



Joined: 13 Jun 2004
Posts: 114
Location: Pennsylvania (USA)
Imagist 24 Aug 2004, 03:04
So recursive macros are impossible. Darn. And I should do:

Code:
jmp code
label var1
db 10
label var2
db 11

macro switch a,b{
   mov ax,[a]
   mov bx,[b]
   mov [b],ax
   mov [a],bx
}

code:
   switch var1, var2
    


In my dk_lib (a library for using fasm as an HLL; school project) I have the following macro:

macro dk_setupGUI{
format PE GUI 4.0
entry start

include '%fasminc%\win32ax.inc'
};end macro dk_setupGUI

Will this work?

Also, do includes work in a similar way to macros, like:

Code:
;this is in a file called switch.asm
push ax
push bx
pop ax
pop bx
    


Code:
;this is in a file called very-bad-code.asm
mov ax, 10
mov bx, 11

include 'switch.asm'

;now ax should be 11 and bx 10
    


so that very-bad-code.asm assembles to:

Code:
mov ax, 10
mov bx, 11

push ax
push bx
pop ax
pop bx
    


I have many questions, and I am glad there are many here to help, becuase the library is due on next Monday!

_________________
Many things are possible. Few things are likely.
Post 24 Aug 2004, 03:04
View user's profile Send private message Visit poster's website Reply with quote
pelaillo
Missing in inaction


Joined: 19 Jun 2003
Posts: 878
Location: Colombia
pelaillo 24 Aug 2004, 03:38
Imagist wrote:
Also, do includes work in a similar way to macros...

Yes, but macros are more flexible. They have arguments, so you could do many things you cannot with includes.

Think on macros as text search and replace, but advanced.

Includes are useful as a way to split source code in more files, either because is easier to manage smaller files or to use one part in different projects.

Code:
; Without using registers:
macro switch a,b{ 
   push [a] 
   push [b] 
   pop [a] 
   pop [b] 
}    

Quote:

Will this work?

If you are calling this macro from a source that have access to dk_setupGUI macro definition it will work. But if the definition is accessible only through 'win32ax.inc' it won't work.
i.e. this type of recursiveness does not work.
Code:
;filename: test.inc
macro dk_setupGUI{ 
format PE GUI 4.0 
entry start 

include 'test.inc' 
};end macro dk_setupGUI 
    
Post 24 Aug 2004, 03:38
View user's profile Send private message Yahoo Messenger Reply with quote
Tomasz Grysztar



Joined: 16 Jun 2003
Posts: 8356
Location: Kraków, Poland
Tomasz Grysztar 24 Aug 2004, 09:32
Quote:
So recursive macros are impossible.

This is not really true. The "invoke" nesting macros from WIN32AX.INC file in FASMW package demonstrate some kind of the macro recursion in action. A bit complicated, but possible.
Post 24 Aug 2004, 09:32
View user's profile Send private message Visit poster's website Reply with quote
halyavin



Joined: 21 Aug 2004
Posts: 42
halyavin 28 Aug 2004, 14:31
And use recursion instead of cycles in compicated macros; it is often helps. You can't write program that sorts array at compilation time using 'repeat' macro.
Post 28 Aug 2004, 14:31
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-2024, Tomasz Grysztar. Also on GitHub, YouTube.

Website powered by rwasa.