flat assembler
Message board for the users of flat assembler.

Index > Macroinstructions > struc(t) relative offset

Author
Thread Post new topic Reply to topic
RIxRIpt



Joined: 18 Apr 2013
Posts: 50
RIxRIpt 12 Feb 2015, 18:35
Answered by @revolution

Is it possible to get offset relative to struc(t) base?

Here's not working example of my idea:
Code:
include 'win32a.inc'

macro skip_to nbyte {
        local b
        b = nbyte - $ ; I need offset inside struc(t)
        if b > 0
                db b dup ?
        end if
}

struct myStruct
        skip_to 8 ;skips to byte #8 (db 8 dup ?)
        value1 db ?
        skip_to 16 ;skips to byte #16 (db 7 dup ?)
        value2 db ?
ends

s myStruct
s2 myStruct

db 'x'

    


I've also tried this way, but it says 'incomplete macro.'
Code:
macro skip_to nbyte {
        local b
        b = nbyte - ..
        if b > 0
                db b dup ?
        end if
}

macro class name {
        struc name \{
                local ..
                ..:
}

macro endc {
        \}
}

class myStruct
        skip_to 8 ;skips to byte #8 (db 8 dup ?)
        .value1 db ?
        skip_to 16 ;skips to byte #16 (db 7 dup ?)
        .value2 db ?
endc

s myStruct
s2 myStruct

db 'x'
    

_________________
Привет =3
Admins, please activate my account "RIscRIpt"


Last edited by RIxRIpt on 13 Feb 2015, 11:53; edited 1 time in total
Post 12 Feb 2015, 18:35
View user's profile Send private message Visit poster's website Reply with quote
revolution
When all else fails, read the source


Joined: 24 Aug 2004
Posts: 20363
Location: In your JS exploiting you and your system
revolution 13 Feb 2015, 08:57
Perhaps this is what you are after:
Code:
macro skip_to nbyte,beginning {
        local b
        b = nbyte + beginning - $
        if b > 0
                db b dup ?
        end if
}

struc myStruct {
        local ..strucBegin
    ..strucBegin:
        skip_to 8,..strucBegin ;skips to byte #8 (db 8 dup ?)
        .value1 db ?
        skip_to 16,..strucBegin ;skips to byte #16 (db 7 dup ?)
        .value2 db ?
}

s myStruct
s2 myStruct

db s.value1
db s.value2
db s2.value1
db s2.value2    
Note that struct/ends are macros defined in win32a.inc and have a few restrictions when trying to do things like alignment.
Post 13 Feb 2015, 08:57
View user's profile Send private message Visit poster's website Reply with quote
RIxRIpt



Joined: 18 Apr 2013
Posts: 50
RIxRIpt 13 Feb 2015, 11:18
revolution wrote:
Perhaps this is what you are after

Yes.
revolution wrote:
Note that struct/ends are macros defined in win32a.inc and have a few restrictions when trying to do things like alignment.

macro\struct.inc if to be more precise Rolling Eyes. I didn't read the whole macros, I just saw virtual at 0 and thought that `$` would be relative to the beginning of a struct.

Is it possible to do define something like following macro? Because I wouldn't like to write `local ..strucBegin`, `..strucBegin:` each time I define a struc.

Code:
macro class name {
        struc name \{
                __CLASS_BEGINNING = $
} 

macro endc { 
        \} 
}
    

Currently I have only one alternative idea, which I wouldn't like to use either:
Code:
;define a macro which I will have to write in the beginning of each struc
macro STRUC_BEGIN {
        __STRUC_BEGINNING = $
}

macro skip_to nbyte { 
        local b 
        b = nbyte + __STRUC_BEGINNING - $ 
        if b > 0 
                db b dup ?
        else if b < 0
                display 'skip_to error!', 13, 10
                err
        end if
} 

struc myStruct {
        STRUC_BEGIN
        skip_to 8 ;skips to byte #8 (db 8 dup ?) 
        .value1 db ? 
        skip_to 16 ;skips to byte #16 (db 7 dup ?) 
        .value2 db ? 
}
    

_________________
Привет =3
Admins, please activate my account "RIscRIpt"
Post 13 Feb 2015, 11:18
View user's profile Send private message Visit poster's website Reply with quote
revolution
When all else fails, read the source


Joined: 24 Aug 2004
Posts: 20363
Location: In your JS exploiting you and your system
revolution 13 Feb 2015, 11:26
Maybe something like this:
Code:
macro class name {
        struc name \{
                local ..strucBegin
                class@begin equ ..strucBegin
            ..strucBegin:
} 

endc fix }

macro skip_to nbyte {
        local b
        b = nbyte + class@begin - $
        if b > 0
                db b dup ?
        end if
}

class myStruct
        skip_to 8 ;skips to byte #8 (db 8 dup ?)
        .value1 db ?
        skip_to 16 ;skips to byte #16 (db 7 dup ?)
        .value2 db ?
endc

s myStruct
s2 myStruct

db s.value1
db s.value2
db s2.value1
db s2.value2    
Post 13 Feb 2015, 11:26
View user's profile Send private message Visit poster's website Reply with quote
RIxRIpt



Joined: 18 Apr 2013
Posts: 50
RIxRIpt 13 Feb 2015, 11:52
Surprised awesome, thank you @revolution !
Post 13 Feb 2015, 11:52
View user's profile Send private message Visit poster's website Reply with quote
RIxRIpt



Joined: 18 Apr 2013
Posts: 50
RIxRIpt 16 Feb 2015, 12:46
skip_to worked incorrectly when there was defined a sub-class in the class. I've found a simple fix:
Code:
macro classend {
        restore class@beginning
}
endc fix classend }
    
Post 16 Feb 2015, 12:46
View user's profile Send private message Visit poster's website Reply with quote
baldr



Joined: 19 Mar 2008
Posts: 1651
baldr 20 Feb 2015, 19:13
RIxRIpt,

Is . (dot) not sufficient? rb .+#-$ where # is an offset into struc (implications involved).
Post 20 Feb 2015, 19:13
View user's profile Send private message Reply with quote
RIxRIpt



Joined: 18 Apr 2013
Posts: 50
RIxRIpt 20 Feb 2015, 22:02
baldr wrote:
RIxRIpt,

Is . (dot) not sufficient? rb .+#-$ where # is an offset into struc (implications involved).

If I understood you correctly you've suggested to use . (dot) instead of class@beginning, right?
Code:
macro skip_to nbyte { 
        local b 
        b = nbyte + . - $ 
        if b > 0 
                db b dup ? 
        end if 
}
    

I've got an error: undefined symbol 's..strucBegin?0.'.
Or have you meant to use rb .+#-$ instead of skip_to macro?
Code:
class myStruct 
        rb .+8-$
        .value1 db ? 
        rb .+16-$
        .value2 db ? 
endc 

s myStruct
    

I've also got an error: undefined symbol 's'.
If you've suggested second variant, I think it's less readable. And you can't define behavior if you are trying to skip `negative` number of bytes (for example a custom error message).

I'm not against simplifying the solution, but I'm happy with the current one Smile

_________________
Привет =3
Admins, please activate my account "RIscRIpt"
Post 20 Feb 2015, 22:02
View user's profile Send private message Visit poster's website Reply with quote
revolution
When all else fails, read the source


Joined: 24 Aug 2004
Posts: 20363
Location: In your JS exploiting you and your system
revolution 21 Feb 2015, 03:30
If you use the dot (.) then you have to define it somewhere:
Code:
struc abc {
 .:  ;<--- define the value of dot
 ;do other stuff here
}    
Post 21 Feb 2015, 03:30
View user's profile Send private message Visit poster's website Reply with quote
baldr



Joined: 19 Mar 2008
Posts: 1651
baldr 21 Feb 2015, 09:15
RIxRIpt,
Code:
macro class ...& {
  struc ...& \{
    .:
}

skip_to fix rb .-$+
endc fix }

class myClass a, b
  skip_to 8
  .value1 db a
  skip_to 16
  .value2 db b
endc

s myClass 1, 2
s2 myClass 3, 4

db s.value1
db s.value2
db s2.value1
db s2.value2    
That's what I'd meant.
Post 21 Feb 2015, 09:15
View user's profile Send private message Reply with quote
RIxRIpt



Joined: 18 Apr 2013
Posts: 50
RIxRIpt 21 Feb 2015, 10:51
baldr,
Well, yes that's a bit simpler solution, thanks.
But if you define a class without arguments there will be an error `invalid macro arguments`, so & should be removed for classes without arguments.

By the way, I couldn't find any info about & except the change log :\
Post 21 Feb 2015, 10:51
View user's profile Send private message Visit poster's website Reply with quote
baldr



Joined: 19 Mar 2008
Posts: 1651
baldr 21 Feb 2015, 14:58
RIxRIpt,

That was just a sketch, you may apply []+common or whatever you want.

Changelog entry was sufficient, I think. Tomasz prefers terse but exhaustive definitions as I know.
Post 21 Feb 2015, 14:58
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.