Page 1 of 1

structure typedefs in gas syntax

Posted: Mon May 24, 2004 11:40 am
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

Re: structure typedefs in gas syntax

Posted: Tue Jul 06, 2010 7:10 pm
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?

Re: structure typedefs in gas syntax

Posted: Wed Jul 07, 2010 5:00 pm
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.

Re: structure typedefs in gas syntax

Posted: Wed Jul 07, 2010 5:37 pm
by Owen
Hmm.. i/l/o registers... branch delay slot...

That example code SPARC?

Re: structure typedefs in gas syntax

Posted: Wed Jul 07, 2010 5:47 pm
by fronty
Correct.

Re: structure typedefs in gas syntax

Posted: Thu Jul 08, 2010 6:36 am
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 ?

Re: structure typedefs in gas syntax

Posted: Thu Jul 08, 2010 7:03 am
by fronty
You have it right.

Re: structure typedefs in gas syntax

Posted: Tue Jul 13, 2010 5:23 am
by 0000
Okay, nice, Thanks you very much