1

So, I have a string that I need to pass to a function in x86-64 assembly code. Think of it in c as something like this:

char str[] = "Abcdef"

void fun(char *str)

How do I do it? Should I pass each character as equivalent hex value and pass to reg? I'm not sure how to do it.

Peter Cordes
  • 328,167
  • 45
  • 605
  • 847
Maxsteel
  • 1,922
  • 4
  • 30
  • 55
  • 1
    *This will help you :* http://stackoverflow.com/questions/5188239/passing-a-pointer-to-an-assembly-function – Mxsky Oct 03 '15 at 18:29

1 Answers1

4

Well, your prototype says it all -- you don't pass a string to the function, you pass a pointer. So the string needs to be in memory somewhere, and you put the address of that memory into the argument register. In assembly code, this looks something like:

# declare a data segment object containg the string
    .data
str:
    .string "Abcdef"

# code to call fun(char *) with the string:
    .text
    movq $str, %rdi
    call fun
Chris Dodd
  • 119,907
  • 13
  • 134
  • 226
  • Hey Chris, what if I had string in an address say 0x1515ac15 and has length = 9. How do I pass the address to rdi? just pass 0x1515ac15 to rdi? That causes it to pass last 3 chars of the string to function. – Maxsteel Oct 04 '15 at 02:34