flat assembler
Message board for the users of flat assembler.

Index > Main > Macro concatenation and repeat problem

Author
Thread Post new topic Reply to topic
Plue



Joined: 15 Dec 2005
Posts: 151
Plue 08 Dec 2007, 19:42
Code:
macro MK NUM {
  mov [v_a], NUM
  ; show v_a here
}

repeat  16
        MK %
end repeat
    

That code works perfectly, and shows the numbers from 1 to 16. But when I do this:
Code:
macro MK NUM {
  l#NUM:
}

repeat  16
        MK %
end repeat    


I get that the symbol is already defined. Which it shouldn't be, and isn't if I unroll the repeat manually:
Code:
macro MK NUM {
  l#NUM:
}

MK 1
MK 2
MK 3
MK 4
MK 5
MK 6
...    


What's wrong?

_________________
Roses are red
Violets are blue
Some poems rhyme
And some don't.
Post 08 Dec 2007, 19:42
View user's profile Send private message Reply with quote
LocoDelAssembly
Your code has a bug


Joined: 06 May 2005
Posts: 4624
Location: Argentina
LocoDelAssembly 08 Dec 2007, 19:55
Unrolling would be this:
Code:
l%:
l%:
l%:
l%:
l%:
l%:
...
    


"repeat" does not instantiates the macro multiple times, it occurs only once at preprocessing time, so, what you need is this
Code:
loco@athlon64:~/Desktop$ cat plue.asm
macro MK NUM {
  l#NUM:
} 

rept 6 count
{
  MK count
}

push l1 l2 l3 l4 l5 l6 ; Just to test that the symbols are defined
loco@athlon64:~/Desktop$ fasm plue.asm
flat assembler  version 1.67.24  (16384 kilobytes memory)
1 passes, 12 bytes.    
Post 08 Dec 2007, 19:55
View user's profile Send private message Reply with quote
Plue



Joined: 15 Dec 2005
Posts: 151
Plue 08 Dec 2007, 20:05
What's the use for repeat if rept does it better? Why isn't the first repeat unrolled to this?
mov [v_a], %
mov [v_a], %
mov [v_a], %
mov [v_a], %
mov [v_a], %
mov [v_a], %
Post 08 Dec 2007, 20:05
View user's profile Send private message Reply with quote
LocoDelAssembly
Your code has a bug


Joined: 06 May 2005
Posts: 4624
Location: Argentina
LocoDelAssembly 08 Dec 2007, 20:28
The problem is that the concatenation occurs at preprocessing time only so the assembler layer is not able to create labels attaching the value of % to them. The rept however does textual repetition of the macro so it is instantiated 6 times while the repeat assembler directive does not do any textual repetition, just interpret the lines again incrementing the output.

To summarize, this is the text that the assembler receives with rept
Code:
 l1:
 l2:
 l3:
 l4:
 l5:
 l6:
    

And this with repeat
Code:
repeat 6
  l%:
end repeat    


The rule is that if the macro does not define any label you can use "repeat" with them, otherwise you need the less flexible and more memory consuming (at compile time) "rept".

And concerning your other code with repeat is this
Code:
repeat 6
  mov [v_a], %
end repeat    

And with rept 6, count is
Code:
; rept 6, count {MK count}
  mov [v_a], 1
  mov [v_a], 2
  mov [v_a], 3
  mov [v_a], 4
  mov [v_a], 5
  mov [v_a], 6
    


This time the output will be the same, but the source text that the assembler receives is different.
Post 08 Dec 2007, 20:28
View user's profile Send private message Reply with quote
Tomasz Grysztar



Joined: 16 Jun 2003
Posts: 8267
Location: Kraków, Poland
Tomasz Grysztar 08 Dec 2007, 21:16
Note: when you've got a situation where both REPT and REPEAT will be good for your purpose, choose REPEAT, as it will eat less memory during the compilation, and will assemble a little bit faster, too.
Post 08 Dec 2007, 21:16
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-2023, Tomasz Grysztar. Also on GitHub, YouTube.

Website powered by rwasa.