flat assembler
Message board for the users of flat assembler.

Index > Macroinstructions > macros general questions

Author
Thread Post new topic Reply to topic
Enko



Joined: 03 Apr 2007
Posts: 676
Location: Mar del Plata
Enko 16 Jul 2011, 16:11
Code:
 macro strtbl name,[string]
     {
      common
        label name dword
      forward
        local label
        dd label
      forward
        label db string,0
     }
    


Quote:

second block for each string declares its local name and defines the table entry holding the address to that string. Third block defines the data of each string with the corresponding label



I nead some help to understand the block.

local label
dd label

will create a label, and writes it value. But at the time the first forward block is processed, label does not have a value until the second forward block

label db string, 0


So, does it mean that the two forward blocks are processed simultaneously but the output goes in two diferent blocks?


Last edited by Enko on 18 Jul 2011, 04:27; edited 1 time in total
Post 16 Jul 2011, 16:11
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 16 Jul 2011, 17:06
No the first forward does not create any label, it is storing a reference to a not-existing-yet label. The second forward is the one which is actually defining labels.

Lets suppose you call it this way: strtbl foo, 'one', 'two', 'three'

The preprocessor would pass the following to the assembler:
Code:
; Common
label foo dword

; First forward
dd label?1
dd label?2
dd label?3

; Second forward
label?1 db "one", 0
label?2 db "two", 0
label?3 db "three", 0    
As you can see, each iteration of the second forward inherits the locals of the same iteration of the first forward (this I'm not sure if it is explicitly documented).

The source above would take the assembler stage two passes at minimum because of the forward referencing of each "dd label?X".
Post 16 Jul 2011, 17:06
View user's profile Send private message Reply with quote
Enko



Joined: 03 Apr 2007
Posts: 676
Location: Mar del Plata
Enko 16 Jul 2011, 17:11
A.. perfect. Thats why if label db X is removed, there is a problem with referencing label?1


Thanks for quick replay.
Post 16 Jul 2011, 17:11
View user's profile Send private message Reply with quote
Enko



Joined: 03 Apr 2007
Posts: 676
Location: Mar del Plata
Enko 18 Jul 2011, 04:22
Hy again, here I have some questions again^^

I had made a macro that would encrypt a part of code.
Code:
macro crypt pstart, pend
{
  local i, len, k
     len = pend - pstart - 8
common       
    i = 0
       repeat len
          load  x from pstart + % -1
          load  k from (pend + i)
             x = x xor k
         store x at pstart + % -1
            i = i + 1
           if i = 8
                    i = 0
               end if
      end repeat
  repeat 8
            store 0x90 at pend + % - 1
  end repeat
}
    

pstart is the label from where to start encryptation
pend is the end label.


And you use it like this:
Code:
start_point:
mov eax, ebx
xor ecx, ecx
etc....
end_point:
db 'PASSWORD' ;should be after the endpoint
crypt start_point, end_point
    

The macro will xor the block with the PASSWORD and then replace the password with NOPS.

It actually works fine.
But I have a questions to understand better fasm macros.

Is it posible to pass the encryptation key to the macro as a parameter?

somthing like:
crypt start_point, end_point, 'PASSWORD'


the problem I had is to access each byte of the key separatly
Here goes a pseudocode of what I mean
Code:
macro  crypt _start, _end, [key]{
....
....
....
x = x xor key[i]
i = i + 1       
if i = 8    
   i = 0
end if
    

Thanks in advance
Post 18 Jul 2011, 04:22
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 18 Jul 2011, 05:35
Not that easy, you'll need to resort to the "virtual" trick:(Untested)
Code:
macro crypt pstart, pend, key
{
        local len, k, x
        len = pend - pstart - 8
common  
        repeat len
                load x byte from pstart + % -1
                virtual at 0
                   db key
                   load k byte from (% - 1) mod $
                end virtual

                x = x xor k
                store x at pstart + % -1
        end repeat
        repeat 8
                store 0x90 at pend + % - 1
        end repeat
}    
Or something like that.
Post 18 Jul 2011, 05:35
View user's profile Send private message Reply with quote
Enko



Joined: 03 Apr 2007
Posts: 676
Location: Mar del Plata
Enko 18 Jul 2011, 05:48
why mod $?
and when/where is key count reseted?
for example:

Code:
p1:
db '1234567890'
p2:
crypt p1, p2, 'PASSWORD' 
    

the preprocessor should do:

Code:
'1' xor 'P' ;key[0]
'2' xor 'A' ;key[1]
....
'7' xor 'D' ;key[8]  the keysize is achieved, so it should be resset to 0
'8' xor 'P' ;key[0]
'1' xor 'A' ;key[1]
    

Of course the final result will only be the xored values.

thanks a lot
Post 18 Jul 2011, 05:48
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 18 Jul 2011, 06:24
No, if you call "crypt start_point, end_point, 'PASSWORD'", the preprocessor would pass this to the assembler:
Code:
        len?0 = end_point - start_point - 8 
        repeat len?0
                load x?0 byte from start_point + % -1
                virtual at 0
                   db 'PASSWORD'
                   load k?0 byte from (% - 1) mod $
                end virtual

                x?0 = x?0 xor k?0
                store x?0 at start_point + % -1
        end repeat
        repeat 8
                store 0x90 at end_point + % - 1
        end repeat    
The "mod $" would be calculating "i mod sizeof(key)", since in the virtual block $$ starts at zero, so $ will obviously be the size of the string. Unless I'm missing something, this should be enough to not need the code to reset "i" to zero.

Note that "repeat" does not work like "REPT" preprocessor directive, here the code in interpreted and everything generating data/code is output to the file (unless the CPU instruction or data definition directive is wrapped inside a virtual block).

PS: The "?0" suffixes may not be accurate, I'm just mimicking what the preprocessor does for symbols declared "local", but the numbers I'm using may be wrong.
Post 18 Jul 2011, 06:24
View user's profile Send private message Reply with quote
Enko



Joined: 03 Apr 2007
Posts: 676
Location: Mar del Plata
Enko 18 Jul 2011, 06:36
Ah... now I get it
example if the keysize is 8
Code:
7 mod 8 = 7
8 mod 8 = 0
9 mod 8 = 1
etc...
    

its a very nice trick !

thnx.
Post 18 Jul 2011, 06:36
View user's profile Send private message Reply with quote
Enko



Joined: 03 Apr 2007
Posts: 676
Location: Mar del Plata
Enko 18 Jul 2011, 07:21
Here I'm again!

Code:
format binary as "txt"
FALSE = 0
TRUE equ ~ FALSE

DEBUG equ TRUE


match =TRUE, DEBUG { db 'DEBUG' } ;RESULT false
match TRUE, DEBUG { db 'DEBUG' }  ;RESULT true
match DEBUG, TRUE { db 'DEBUG' }  ;RESULT true
match DEBUG, =TRUE { db 'DEBUG' } ;RESULT true
    

there is somthing I didn't understand with the match directive. I'm trying to emulate the fasm documentation example.

But as you see, with the 4 match combinations, 3 are true.
But as I understand, there should be 2 of them true, or 4 (all) of them.

What I don't understand, is why:
match =TRUE, DEBUG { db 'DEBUG' } this is false

but
match DEBUG, =TRUE { db 'DEBUG' } this is true.
Post 18 Jul 2011, 07:21
View user's profile Send private message Reply with quote
Enko



Joined: 03 Apr 2007
Posts: 676
Location: Mar del Plata
Enko 18 Jul 2011, 07:34
Code:
format binary as "txt"
FALSE = 0
TRUE equ ~ FALSE

DEBUG equ TRUE


match DEBUG, =TRUE { db 'DEBUG, =TRUE',10,13 }
match DEBUG, TRUE { db 'DEBUG, TRUE' ,10,13}  
match TRUE, DEBUG { db 'TRUE, DEBUG' ,10,13}  
match =TRUE, DEBUG { db '=TRUE, DEBUG' ,10,13}


match DEBUG, =FALSE { db 'DEBUG, =FALSE',10,13 }
match DEBUG, FALSE { db 'DEBUG, FALSE' ,10,13}  
match FALSE, DEBUG { db 'FALSE, DEBUG' ,10,13}  
match =FALSE, DEBUG { db '=FALSE, DEBUG' ,10,13}

match DEBUG,=~FALSE  { db 'DEBUG,=~FALSE' ,10,13}
match DEBUG,~FALSE  { db 'DEBUG,~FALSE' ,10,13}
match ~FALSE,DEBUG  { db '~FALSE,DEBUG' ,10,13}
match ~FALSE,DEBUG  { db '~FALSE,DEBUG' ,10,13}
    


actually this output:
Quote:

DEBUG, =TRUE
DEBUG, TRUE
TRUE, DEBUG

DEBUG, =FALSE
DEBUG, FALSE
FALSE, DEBUG

DEBUG,=~FALSE
DEBUG,~FALSE
~FALSE,DEBUG
~FALSE,DEBUG

all blocks with ~false are true

only this two matches are false

the block with =FALSE,DEBUG is False
the block with =TRUE,DEBUG is False


well, I'm going to sleep, becouse here something I don't quit get and I dont understand what.
Post 18 Jul 2011, 07:34
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 18 Jul 2011, 15:38
Not sure what you don't understand but notice you have used "FALSE = 0" which the preprocessor does not have a clue and just pass it verbatim to the assembler and then, when you do "TRUE equ ~ FALSE" it doesn't get transformed to "TRUE equ ~ 0" because the "=" of the previous line means nothing to the preprocessor (so the "~" although if passed to IRPS or used in match it will recognized as a symbol, it won't preform any negation)
Post 18 Jul 2011, 15:38
View user's profile Send private message Reply with quote
Enko



Joined: 03 Apr 2007
Posts: 676
Location: Mar del Plata
Enko 18 Jul 2011, 15:48
Ok, the question I guess should be:
What does "match symbol literally" means when using =? What is the diference beetween using and not using the =?
Post 18 Jul 2011, 15:48
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 18 Jul 2011, 16:51
When you use it you tell match to look for the symbol next to it, if you don't it just puts the data inside the given symbol:
Code:
; Example
match a+b, 1 + 2{display `a, ' is addded with ', `b}

display 13, 10

match a =Enko b, Today we had Enko asking something in the forum {
   ; Unfortunatelly this time the backtick cant work in a single step
   irps word, a\{display \`word, ' '\}
   display 'a guy from Mar del Plata'
   irps word, b\{display ' ',\`word\}
}    
Post 18 Jul 2011, 16:51
View user's profile Send private message 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.