Skip to content
Advertisement

get a char passed as parameter

I would like to write a function in NASM System V ABI x86-64 (Intel Syntax) that I could use in a C program.

Here is the prototype of the function:

char *rindex(const char *s, int c);

I therefore retrieve the parameters in order (const char *s = rdi, int c = rsi)

First, I get the character stored in the register rsi and put it in ah:

segment .text

global rindex:function

rindex:
    mov ah, byte [rsi] ;; get the character
    [...]

Unfortunately, this line crashes my program:

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

Why it is impossible to get the char and what would be the correct way?

Advertisement

Answer

I call “char” your first argument (const char *s = rdi), since it is a pointer to char. Your second argument (int c = rsi) is an int. To access elements of the string pointed by s you can use mov ah, byte ptr [rdi]. But your second argument is not a pointer, rsi contains the value of c. To read it you can read from esi, since the value of an int fits in the lower 32-bits of rsi.

User contributions licensed under: CC BY-SA
1 People found this is helpful
Advertisement