structure typedefs in gas syntax

Programming, for all ages and all languages.
Post Reply
Adek336

structure typedefs in gas syntax

Post by Adek336 »

Hi all!

There is a useful directive, namely "struc" working well with NASM. All it does it allows me to write code like

Code: Select all

mov ax, MyObject.Field123
The gas manual seems short on the topic.. is there any way I could do such things with gas?

Cheers,
Adrian
0000
Posts: 9
Joined: Tue Jul 06, 2010 6:44 pm

Re: structure typedefs in gas syntax

Post by 0000 »

Hi, (yes I know this is an old topic)

I wondered same question yesturday, I find some information about declaring
structure with Gnu Assembler but It isn't really clear. According to what I understood,
you can declare a structure with the .struct directive and you need to set an alignment

An example of a structure in C:

Code: Select all

typedef struct
{
    int a;
    char b;
    int c;
}my_struct;
Will be:

Code: Select all

.struct 0
.balign 4
a: .int 0
.balign 1
b: .byte 0
.balign 4
c: .int 0
But that's what I understood, not really sure, I could be wrong.
By the way, I have no idea how to denote a member of this structure.

Can anybody help us to clear our mind about this weird poorly documented feature?
fronty
Member
Member
Posts: 188
Joined: Mon Jan 14, 2008 5:53 am
Location: Helsinki

Re: structure typedefs in gas syntax

Post by fronty »

.struct switches to absolute section and sets offset to given expression. Absolute section is section which isn't relocated. Address 0x10 in absolute section will always be 0x10.

This is quite hard for me to explain when I'm this tired, so I try to give good enough example.

Code: Select all

        .struct 0                       ! offset is set to 0x0
        .balign 4
foo:                                    ! foo's address is 0x0, because offset
                                        ! was set so two lines above
        .struct foo + 4                 ! offset is set to foo + 4 = 4 
        .balign 4
bar:                                    ! bar's address is 0x4
Because there is nothing which tells the assembler that you aren't going to issue new .structs anymore, you are left in absolute section after that so you have to jump to needed section by yourself.

Whole example program. I compiled binutils to check it assembles and disassembly looks correct but didn't check if it runs correctly. Inform me if there is some bug. I know you could eliminate half of its size with doing things in more sensible order.

Code: Select all

        .struct 0                       ! offset is set to 0x0
        .balign 4
foo:                                    ! foo's address is 0x0, because offset
                                        ! was set so two lines above
        .struct foo + 4                 ! offset is set to foo + 4 = 4 
        .balign 4
bar:                                    ! bar's address is 0x4

        .section ".data"
        .align 4
        .type   fmt, #object
fmt:    .asciz  "%d %d\n"               ! format string for printf(3)

        .section ".text"
        .align 4
        .global main
        .type   main, #function
main:
        save    %sp, -104, %sp          ! prologue

        ld      [%fp - 8 - foo], %l0    ! load member foo from stack
        mov     10, %l0                 ! assign 10 and write back
        st      %l0, [%fp - 8 - foo]

        ld      [%fp - 8 - bar], %l0    ! load member bar from stack
        mov     20, %l0                 ! assign 20 and write back
        st      %l0, [%fp - 8 - bar]

        sethi   %hi(fmt), %o0           ! copy high bits of fmt's address
        or      %o0, fmt, %o0           ! copy low bits
        ld      [%fp - foo], %o1        ! load foo to second output
        call    printf                  ! call printf(3)
         ld     [%fp - bar], %o2        ! load bar to third output

        mov     0, %i0                  ! return value

        ret
         restore
EDIT: Damn phpBB screwed my indents.
EDIT2: Small modification to the example.
User avatar
Owen
Member
Member
Posts: 1700
Joined: Fri Jun 13, 2008 3:21 pm
Location: Cambridge, United Kingdom
Contact:

Re: structure typedefs in gas syntax

Post by Owen »

Hmm.. i/l/o registers... branch delay slot...

That example code SPARC?
fronty
Member
Member
Posts: 188
Joined: Mon Jan 14, 2008 5:53 am
Location: Helsinki

Re: structure typedefs in gas syntax

Post by fronty »

Correct.
0000
Posts: 9
Joined: Tue Jul 06, 2010 6:44 pm

Re: structure typedefs in gas syntax

Post by 0000 »

hmm ok, I understand now.
Just a last question, (I never code under SPARC) is %fp the frame pointer ? his equivalent is the ebp under x86 ?
fronty
Member
Member
Posts: 188
Joined: Mon Jan 14, 2008 5:53 am
Location: Helsinki

Re: structure typedefs in gas syntax

Post by fronty »

You have it right.
0000
Posts: 9
Joined: Tue Jul 06, 2010 6:44 pm

Re: structure typedefs in gas syntax

Post by 0000 »

Okay, nice, Thanks you very much
Post Reply