In assembly,how do you deal with C struct? -


for example,how prepare parameters syscall sys_wait4:

 asmlinkage long sys_wait4(pid_t pid,unsigned int __user *stat_addr, int options, struct rusage __user *ru) 1120 { 

how deal struct rusage in assembly?

a hello world example deal struct in assembly enough me:)

the members of struct laid out sequentially in memory, possibly padding, , address of struct typically address of first member.

struct bar {     int x;     int y; };  struct foo {     struct bar b;     double d;     int i; };  struct foo f; 

let's &f 0x10. then, &f.b.x (the first member of first member of foo) 0x10. &f.b.y 0x14, since f.b.x 4 bytes (assuming 32-bit machine). &f.d 0x18, , &f.i 0x20. first address not occupied f (in other words, &f + 1) 0x24.

so need in assembly make sure have (stack or heap) space members of struct, , fill space appropriate data, , pass address of first member function.

as example involves assembly, produce writing small test program , compile gcc -s -o0 -g, produce assembly code c code. example:

int func(struct foo * foo) {     return foo->b.x + foo->i; }  int main() {     struct foo foo;     foo.b.x = 42;     foo.b.y = 9;     foo.d = 3.14;     foo.i = 8;     func(&foo);     return 0; } 

in assembly output, will, among other things, see (note: 64-bit asm):

movl    $42, -32(%rbp) movl    $9, -28(%rbp) movabsq $4614253070214989087, %rax movq    %rax, -24(%rbp) movl    $8, -16(%rbp) 

as can see, values 42, 9, (integer bit pattern of 3.14), , 8 being loaded addresses -32, -28, -24, , -16 (relative base pointer). have solaris box handy, couldn't use asmlinkage (which specifies function arguments must passed on stack rather in registers), before function call, see effective address of struct being loaded register:

leaq    -32(%rbp), %rax movq    %rax, %rdi 

with asmlinkage, you'd instead see effective address being pushed onto stack.


Comments

Popular posts from this blog

c# - SharpSVN - How to get the previous revision? -

c++ - Is it possible to compile a VST on linux? -

url - Querystring manipulation of email Address in PHP -