【问题标题】:Passing string as argument into a function in x86-64将字符串作为参数传递给 x86-64 中的函数
【发布时间】:2015-12-31 18:25:04
【问题描述】:

所以,我有一个字符串需要传递给 x86-64 汇编代码中的函数。在 c 中可以这样想:

char str[] = "Abcdef"

void fun(char *str)

我该怎么做?我应该将每个字符作为等效的十六进制值传递并传递给 reg 吗?我不知道该怎么做。

【问题讨论】:

标签: c assembly x86-64 calling-convention


【解决方案1】:

好吧,您的原型说明了一切——您没有将字符串传递给函数,而是传递了一个指针。所以字符串需要在内存中的某个地方,然后将该内存的地址放入参数寄存器中。在汇编代码中,这看起来像:

# 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

【讨论】:

  • 嘿,克里斯,如果我在地址中有字符串说 0x1515ac15 并且长度 = 9。我如何将地址传递给 rdi?只需将 0x1515ac15 传递给 rdi?这会导致它将字符串的最后 3 个字符传递给函数。
猜你喜欢
  • 1970-01-01
  • 2020-12-19
  • 2017-04-25
  • 2012-07-26
  • 2010-10-03
  • 1970-01-01
  • 2023-03-16
  • 1970-01-01
相关资源
最近更新 更多