struct addressing in assember (GAS)

Programming, for all ages and all languages.
Post Reply
User avatar
os64dev
Member
Member
Posts: 553
Joined: Sat Jan 27, 2007 3:21 pm
Location: Best, Netherlands

struct addressing in assember (GAS)

Post by os64dev »

Does anyone know how to address structure members in assembler:

for instance

Code: Select all

typedef struct test{
    int c;
} test;

static test myInfo;
can be addressed in C/C++ with myInfo.c = 0 but how does this work in assembler specifcally GAS.

movq $0, myInfo.c does not work do i always use the following method ?

Code: Select all

movq $myInFo, %rsi;
movq $0, 0(%rsi)
regards
Author of COBOS
User avatar
XCHG
Member
Member
Posts: 416
Joined: Sat Nov 25, 2006 3:55 am
Location: Wisconsin
Contact:

Post by XCHG »

In some assemblers like MASM and TASM you can create the actual structure and address it like STRUCTURE.MEMBER just like you do in C but in NASM you can hardly pull that off as it does not any intrinsic support for structures. What you can do is to create a label as the beginning of the structure, get the offset (memory address) of that structure and move values into it which will lead into moving values into the members of that structure. Here is an example. Hope the comments help you understand it. The code is written in NASM:

Code: Select all

  ; -----------------
  %DEFINE OFFSET                          ; Define the keyword OFFSET
  ; -----------------
  [SECTION .bss]                          ; Uninitialized section
  TESTStruct:                             ; The beginning of the structure
    A   RESD    0                         ; The first member of the structure
    B   RESD    0                         ; The second member of the structure
    C   RESD    0                         ; The third member of the structure
  TESTStruct_END:                         ; End of the structure (not necessary)
  ; -----------------
  [SECTION .text]                         ; The code section
  MOV     EBX , OFFSET TESTStruct         ; *EBX is the address of the structure
  MOV     DWORD PTR [EBX] , EAX           ; First member of the structure (A)
  MOV     DWORD PTR [EBX + 0x04] , EAX    ; Second member of the structure (B)
  MOV     DWORD PTR [EBX + 0x08] , EAX    ; Third member of the strucure (C)
Note that the members of that structure are each 32-bits or 4 bytes long which is why I have added 0, 4 and then 8 to the base memory location of the label in order to access its members. If they were defined as RESQ for example (8 bytes/QWORD), then I had to access them like this:

Code: Select all

  ; -----------------
  %DEFINE OFFSET                          ; Define the keyword OFFSET
  ; -----------------
  [SECTION .bss]                          ; Uninitialized section
  TESTStruct:                             ; The beginning of the structure
    A   RESQ    0                         ; The first member of the structure
    B   RESQ    0                         ; The second member of the structure
    C   RESQ    0                         ; The third member of the structure
  TESTStruct_END:                         ; End of the structure (not necessary)
  ; -----------------
  [SECTION .text]                         ; The code section
  MOV     EBX , OFFSET TESTStruct         ; *EBX is the address of the structure
  MOV     DWORD PTR [EBX] , EAX           ; DWORD #0 of the First member (A)
  MOV     DWORD PTR [EBX + 0x04] , EAX    ; DWORD #1 of the First member (A)
  MOV     DWORD PTR [EBX + 0x08] , EAX    ; DWORD #0 of the Second member (B)
  MOV     DWORD PTR [EBX + 0x0C] , EAX    ; DWORD #1 of the Second member (B)
  MOV     DWORD PTR [EBX + 0x010] , EAX   ; DWORD #0 of the Third member (C)
  MOV     DWORD PTR [EBX + 0x014] , EAX   ; DWORD #1 of the Third member (C)
On the field with sword and shield amidst the din of dying of men's wails. War is waged and the battle will rage until only the righteous prevails.
User avatar
Solar
Member
Member
Posts: 7615
Joined: Thu Nov 16, 2006 12:01 pm
Location: Germany
Contact:

Post by Solar »

Unless I am mistaken, the topic title points towards the OP using GAS, not NASM. 8)

Unfortunately, I don't know the solution, but I thought I'd point out that the problem isn't solved yet.
Every good solution is obvious once you've found it.
User avatar
mystran
Member
Member
Posts: 670
Joined: Thu Mar 08, 2007 11:08 am

Re: struct addressing in assember (GAS)

Post by mystran »

os64dev wrote:Does anyone know how to address structure members in assembler:

for instance

Code: Select all

typedef struct test{
    int c;
} test;

static test myInfo;
can be addressed in C/C++ with myInfo.c = 0 but how does this work in assembler specifcally GAS.

movq $0, myInfo.c does not work do i always use the following method ?

Code: Select all

movq $myInFo, %rsi;
movq $0, 0(%rsi)
One thing you can do is define the field offsets as constants, which will allow you to refer to them by name (sort of).. more or less like this:

Code: Select all

Assuming C structure:

struct vect {
  signed x,y;
};

You could use something like:

#define vect_x 0
#define vect_y 4

   movl $0, vect_x(%esi)
   movl $0, vect_y(%esi)

Where %esi has a pointer to the vect, and "signed" is assume to be 4-bytes. You could use symbols (.equ or .set) instead of preprocessor directives if you want.

edit: you could even do fancier stuff by using the macrosystem in GAS, but I've never bothered to learn it so can't help with that..
Last edited by mystran on Thu Mar 29, 2007 4:42 am, edited 1 time in total.
The real problem with goto is not with the control transfer, but with environments. Properly tail-recursive closures get both right.
User avatar
mystran
Member
Member
Posts: 670
Joined: Thu Mar 08, 2007 11:08 am

Post by mystran »

Oh and, to refer to static/global variables, you can use:

Code: Select all

   movl $0, (vect_x + my_vect)
Or something similar... gas knows how to do integer arithmetics.
The real problem with goto is not with the control transfer, but with environments. Properly tail-recursive closures get both right.
User avatar
binutils
Member
Member
Posts: 214
Joined: Thu Apr 05, 2007 6:07 am

Post by binutils »

User avatar
os64dev
Member
Member
Posts: 553
Joined: Sat Jan 27, 2007 3:21 pm
Location: Best, Netherlands

Post by os64dev »

thanks for the replies. i get i now
Author of COBOS
Post Reply