0

I have a structure declared as

strucr mystruct_ {
    char cv[12];
    int i;
    short int sv[12];
};

and a x86 function declared as:

void myproc(int x, short int y, struct mystruct_s)

and need to access argument s.sv[4] using [ebp+m]. Will the value of m be equal to 40 (s starts at ebp+16 and sv[4] is the 24-th byte of the struct) or am I doing something wrong? I am not sure how passing a structure to x86 works.

Peter Cordes
  • 328,167
  • 45
  • 605
  • 847
  • With what compiler / OS? There are different calling conventions. – Nate Eldredge Jan 28 '21 at 14:52
  • Linux using nasm compiler – pkonrad1229 Jan 28 '21 at 15:08
  • 1
    Nasm is the assembler; I was asking about the C compiler. But all compilers on Linux should follow the [System V ABI](https://wiki.osdev.org/System_V_ABI) so that is the place to look for details on how structs are to be passed as parameters and laid out in memory. – Nate Eldredge Jan 28 '21 at 15:56
  • 1
    Looking at GCC output is one way to find out exactly how the compiler on your platform passes structs, sometimes easier than reading the calling convention docs. [How to remove "noise" from GCC/clang assembly output?](https://stackoverflow.com/q/38552116). For *returning* structs by value, in i386 System V, there's [Calling convention for function returning struct](https://stackoverflow.com/q/4931195). (Looking for a proper duplicate for *passing*, I closed this before noticing what it was really about.) – Peter Cordes Jan 28 '21 at 19:25
  • 1
    I'm not finding a 32-bit i386 System V duplicate of this; reopened. That calling convention is really simple for structs and does just put the struct on the stack. Related: [How C structures get passed to function in assembly?](https://stackoverflow.com/q/57766693) / [What are the calling conventions for UNIX & Linux system calls (and user-space functions) on i386 and x86-64](https://stackoverflow.com/q/2535989) / [Where is the x86-64 System V ABI documented?](https://stackoverflow.com/q/18133812) (also pointers to the i386 ABI doc.) / and like I said just try it in https://godbolt.org/ – Peter Cordes Jan 28 '21 at 20:08
  • `[ebp+40]` appears to be right, according to [gcc](https://godbolt.org/z/c6P6YK). If it's not working, could you provide a [mcve] of what doesn't work? – Nate Eldredge Jan 28 '21 at 22:05

0 Answers0