pointers - C passes value instead of address to assembly function (x64) -


i need pass address instead of value of field c assembly function, , have no idea why end value instead of address.

c code:

long n = 1,ret = 0; fun(&n, &ret); //the rest omitted 

assembly code:

.globl fun fun:  pushq %rbp movq %rsp, %rbp  movq 16(%rbp), %rax #my n address movq 24(%rbp), %rbx #my ret address  cmpq $0, %rax //the rest omitted 

when peek values of %rax , %rbx gdb can see have values in registers:

breakpoint 1, fun () @ cw.s:6 6       movq 16(%rbp), %rax #my n address (gdb) s 7       movq 24(%rbp), %rbx #my ret address (gdb) s 9       cmpq $0, %rax (gdb) p $rax $1 = 1 (gdb) p $rbx $2 = 0 

i don't see whats wrong code. i'm sure &n makes c pass address instead of value. following solution provided here, no luck.

calling c function in assembly

update:

i'm running lxle (it's fork of ubuntu) on amd x86_64. compiler used gcc (ubuntu 4.8.2-19ubuntu1) , gnu assembler (gnu binutils ubuntu) 2.24. makefile:

cw: cw.c cw.o     gcc cw.o cw.c -o cw cw.o: cw.s     -gstabs -o cw.o cw.s 

what architecture on? compiler generated code fun? did write yourself?

the code using r* registers , question mentions "x64", assume it's amd64/x86-64/x64 architecture. you're reading things stack (which you've commented "my n/ret address") assume expect function arguments there i'm not aware of abi on cpu family passes first arguments function on stack.

if wrote yourself, need read on calling conventions of abi operating system/compiler uses, because unless you're on obscure operating system not pass (the first few) function arguments on stack. you're reading random values stack happen match compiler happened put values in calling function.

if you're on linux or other unix-like system use sysv abi first 2 arguments function in rdi, rsi registers. if you're on windows, rcx, rdx. assuming arguments int/long/pointers. if arguments structs, floating point or such, other rules apply.


Comments

Popular posts from this blog

php - failed to open stream: HTTP request failed! HTTP/1.0 400 Bad Request -

java - How to filter a backspace keyboard input -

java - Show Soft Keyboard when EditText Appears -