Here's an example my_putchar
in GCC x86-64 inline assembly (in Intel syntax, converting to AT&T should be trivial).
Compiles with:
gcc -ggdb -masm=intel -o gcc_asm_putchar gcc_asm_putchar.c
Edit: rdi
was missing from clobbered registers. Fixed.
Here's the code:
int main(void)
{
char my_char;
for (my_char = 'a'; my_char <= 'z'; my_char++)
my_putchar(my_char);
my_char = '
';
my_putchar(my_char);
return 0;
}
void my_putchar(char my_char)
{
int dword_char;
dword_char = (int)my_char;
asm volatile(
".intel_syntax noprefix;"
"mov r10,rsp;" // save rsp.
"sub rsp,8;" // space for buffer, align by 8.
"mov [rsp],al;" // store the character into buffer.
"mov edi,1;" // STDOUT.
"mov rsi,rsp;" // pointer to buffer.
"mov edx,1;" // string length in bytes.
"mov eax,1;" // WRITE.
"syscall;" // clobbers rcx & r11.
"mov rsp,r10;" // restore rsp.
".att_syntax prefix;"
/* outputs */
:
/* inputs: eax */
: "a"(dword_char)
/* clobbered regs */
: "rcx", "rdx", "rsi", "rdi", "r10", "r11"
);
}
与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…