获取作为参数传递的字符



我想用NASM System V ABI x86-64 (Intel语法)编写一个可以在C程序中使用的函数。

下面是函数的原型:
char *rindex(const char *s, int c);

因此,我按顺序检索参数(const char *s=rdi,int c=rsi)

首先,我将字符存储在寄存器rsi中,并将其放入ah中:

segment .text
global rindex:function
rindex:
mov ah, byte [rsi] ;; get the character
[...]

不幸的是,这一行使我的程序崩溃了:

rindex("hello world", 'o') // segfault

为什么不可能获得char,正确的方法是什么?

我调用"您的第一个参数(const char *s = rdi),因为它是指向char的指针。你的第二个参数(int c = rsi)是int。要访问s所指向的字符串元素,可以使用mov ah, byte ptr [rdi]。但是你的第二个参数不是指针,rsi包含c的值。要读取它,可以从esi读取,因为int的值适合rsi的低32位。

最新更新