我想用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位。