flat assembler
Message board for the users of flat assembler.

Index > Macroinstructions > noobie questions

Author
Thread Post new topic Reply to topic
Hugh Aguilar



Joined: 15 Nov 2011
Posts: 62
Location: Arizona
Hugh Aguilar
On a positive note, I figured out LOAD and STORE pretty much, and they work very well. I can build a linked list at assemble-time easily.

I do have some questions:

1.) I'm unsure of how the # operator works.
Consider this code:
Code:
base_adr:

macro tester name
{
        local str, past
_#name#:        dd _#name#_-base_adr
_#name#_size:   dd past-str
str:            db '#name#'
past:
load str_size dword from _#name#_size
                db 32-str_size dup 0         ; pad the string with zeros
}
    

If I do this:
Code:
_swap_: 

tester swap
    

I expect to get this:
Code:
_swap_:
_swap:            dd _swap_-base_adr
_swap_size:     dd 4
                      db 'swap'
                      db 28 dup 0
    

Am I understanding all of this correctly?


2.) I'm totally baffled by COMMON, REVERSE and FORWARD. I don't think these are applicable to what I'm doing, but I don't know. Can anybody explain these?


3.) Is there any way for macro arguments to have default values?
I tried this:
Code:
macro tester2 a, b
{
if b eq
        b = 99
end if
        dd a, b
}

tester2 23, 34

tester2 45
    

FASM aborted with an error message on the second invocation of TESTER2. It said that =99 is not a valid instruction.


3.) How do I get a listing from FASMW? Being able to look at what gets generated, would help me a lot in knowing if my macros are generating what they should be generating.

I found the listing program in the tools directory, but I haven't built it yet. Am I to understand that this will generate a listing if used from the command-line?

Thanks for your help --- Hugh
Post 06 Dec 2012, 21:15
View user's profile Send private message Send e-mail Reply with quote
cod3b453



Joined: 25 Aug 2004
Posts: 619
cod3b453
As you understood for (1), the # joins two elements to construct a new identifier.

EDIT: Sorry for the string db "#name#" you actually should do db `name

For (2) here is some example usage:
Code:
macro A a,[b,c]
{
 common
        db a
 forward
        db b+c
 common
        db a
 reverse
        db b+c
}

A 1,\
  2,3,\
  4,5

; db 1
; db 5
; db 9
; db 1
; db 9
; db 5      
Here the parameters in "[...]" may be repeated zero or more times. common will only be performed once, regardless of parameters, while forward and backward will be performed for the number of parameters given.

For (3) you can simply add the default in the definition:
Code:
macro A a,[b=2,c=3]
; ...
A 1,\
  ,,\
  4,5
; ...
    
which will get the same result.
Post 06 Dec 2012, 22:15
View user's profile Send private message Reply with quote
baldr



Joined: 19 Mar 2008
Posts: 1651
baldr
Hugh Aguilar,

LISTING program beneath TOOLS folder makes listing, for preprocessed source you'll need PREPSRC tool. Both require .fas file to be generated beforehand ("Run"|"Build symbols" in FASMW; -s filename switch for command-line compiler).
Post 06 Dec 2012, 23:33
View user's profile Send private message Reply with quote
uart777



Joined: 17 Jan 2012
Posts: 369
uart777
Quote:
I'm totally baffled by COMMON, REVERSE and FORWARD. Can anybody explain these?


Any macro that accepts variable arguments ([va]) must specify how to process them; once or as an entire list (common) or separately in a loop (forward/reverse). It helps to imagine them with invisible braces {}. Example:

Code:
macro pushx [p] { ; push parameters
common            ; do this once...
if ~ p eq         ; if any parameters
 forward          ; for (i=0; i<p.n; i++) {
  pushd p         ; push p[i]
 common           ; } ; end forward
end if            ; once
}

; create successive powers of 2 starting
; at BIT0, from right to left

macro powers [id] {
common local n      ; common { local n=0 }
n=0
forward             ; for (i=0; i<id.n; i++) {
id=1 shl n          ; id[i]=1<<n
n=n+1               ; }
}

; create readable bit structure from
; left to right. example: 0000ABCDb.
; powerz A, B, C, D ; A=8, B=4, C=2, D=1

macro powerz [id] {
common local n
n=1
forward n=n+1
common n=n-2
forward id=1 shl n
n=n-1
}   

; create a list of incremental values starting
; at 0. like "enum" in C. example:
; numeric A, B=7, D ; here, A=0, D=8

macro numeric [id] {
common
local n
n=0                ; n=0
forward
define ?v 0        ; initial value?
match a==b, id \{
 a=b
 n=b               ; set n
 define ?v 1       ; yes, specified
\}
if ?v=0            ; else=n (previous+1)
 id=n
end if
n=n+1              ; n++
}

numeric YES=1, NO=0, NONE=-1,\
TRUE=1, FALSE=0, NULL=0, DEFAULT=0,\
INVALID=-1, INFINITE=-1, DETECT=-1     


Quote:
I'm unsure of how the # operator works.


# concencates names like ## in C. \ escape symbol/s are used for inner blocks: \#, \\#, \\\#, etc.

Code:
; create/initialize "resolution" structures...

; resolution:
; TEXT .name(16)
; NUMBER .w, .h, .bpp

macro resolution [p] {
common
resolutions:
forward
match name==w*h, p \{
 TEXT name\#.name(16)=\`name
 NUMBER name\#.w=w,\
 name\#.h=h, name\#.bpp=32
\}
}

macro define.resolutions {
resolution \
QQVGA  = 160*120,\
HQVGA  = 240*160,\
QVGA   = 320*240,\
WQVGA  = 480*272,\
VGA    = 640*480,\
NTSC   = 720*480,\
WVGA   = 800*480,\
SVGA   = 800*600,\
WSVGA  = 1024*600,\
XGA    = 1024*768,\
SXGA   = 1280*1024,\
HD720  = 1280*720,\
WXGA   = 1280*800,\
WSXGA  = 1440*900,\
HD1080 = 1920*1080
}    


Quote:
FASM aborted with an error message on the second invocation of TESTER2. It said that =99 is not a valid instruction.


In response to: if b eq (if equals nothing/empty), then b=99 is interpreted as =99. To check if a parameter was sent, use: if ~ b eq (if not nothing)

Code:
macro tester a, b { 
if ~ b eq ; if b was sent
 dd a, b
else      ; just a
 dd a
end if 
} 

tester 1
tester 1, 2    
Post 12 Dec 2012, 20:52
View user's profile Send private message Reply with quote
fasmnewbie



Joined: 01 Mar 2011
Posts: 553
fasmnewbie
Well, that is no noob's question. Those who responded are no noobs either. I am out of here as fast as I can. Laughing
Post 30 Jul 2013, 23:18
View user's profile Send private message Visit poster's website Reply with quote
AsmGuru62



Joined: 28 Jan 2004
Posts: 1409
Location: Toronto, Canada
AsmGuru62
Me too -- these macros make my head spin!
Post 31 Jul 2013, 02:01
View user's profile Send private message Send e-mail Reply with quote
fasmnewbie



Joined: 01 Mar 2011
Posts: 553
fasmnewbie
AsmGuru62 wrote:
Me too -- these macros make my head spin!


Hahaha Laughing
Post 23 Feb 2014, 09:09
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 YouTube, Twitter.

Website powered by rwasa.