0

I am trying to store a stack memory address into a register. The point is to call a c function from assembly code that takes in three arguments.

1) A pointer to a function: which I get using leaq label(%rip), %rdi

2) An int: which is simple, movq $2, %rsi

3) A pointer to a memory address: This is the tricky part. This address is lets say stack location 2 = -16(%rsp).

How to pass this stack location to the register %rdx and have the c function write its results there?

Note: This c function is pthread threaded if you need to know.

5
  • 1
    lea -16(%rsp), %rdx Commented Nov 1, 2017 at 2:53
  • 1
    Assembly for which CPU? Commented Nov 1, 2017 at 2:54
  • @EJP x86 architecture Commented Nov 1, 2017 at 3:04
  • So why wasn't that critical information stated in the question? Commented Nov 1, 2017 at 3:36
  • Look at compiler output for foo() { int k; bar(&k); } godbolt.org/g/kfPqic. Commented Nov 1, 2017 at 4:11

1 Answer 1

2

Ok so I did it, thanks. Here is the full solution:

assembly code:

 1 .globl main 2 main: 3 4 pushq %rbp 5 movq %rsp, %rbp 6 subq $8, %rsp 7 8 movq $50, -8(%rbp) 9 10 leaq PrintHello(%rip), %rdi 11 movq $17, %rsi 12 leaq -8(%rbp), %rdx 13 14 callq dispatcher 15 callq wait 16 17 movq -8(%rbp), %rax 18 movq %rax, %rdi 19 callq print_int 20 21 addq $8, %rsp 22 movq $0, %rax 23 popq %rbp 24 retq 

output:

In dispatcher arg = 17 In wait wrap_func: args_data.ret = 50 Hello World! It's me, thread # 17! wrap_func: after assignment-> args_data.ret = 20 Exit from wait 
Sign up to request clarification or add additional context in comments.

Comments

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.