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:

JavaScript

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:

JavaScript

Unfortunately, this line crashes my program:

JavaScript

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