本文介绍了GCC:内联汇编的putchar(炭)的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
溢出,
如何使用内联汇编只有我实现的putchar(炭)程序吗?我想这样做是x86-64的装配。究其原因,我这样做是为了实现自己的标准LIB(或至少它的一部分)。以下是我迄今为止:
how can I implement the putchar(char) procedure using inline assembly only? I would like to do this in x86-64 assembly. The reason for me doing this is to implement my own standard-lib (or at least part of it). Here is what I have so far:
void putchar(char c)
{
/* your code here: print character c on stdout */
asm(...);
}
void _start()
{
/* exit system call */
asm("mov $1,%rax;"
"xor %rbx,%rbx;"
"int $0x80"
);
}
我用编译:
gcc -nostdlib -o putchar putchar.c
感谢您帮助我!
推荐答案
下面是在海湾合作委员会的x86-64内联汇编的例子 my_putchar
(在Intel语法,转换成AT&安培; T应当是微不足道的)。
Here's an example my_putchar
in GCC x86-64 inline assembly (in Intel syntax, converting to AT&T should be trivial).
与编译:
gcc -ggdb -masm=intel -o gcc_asm_putchar gcc_asm_putchar.c
编辑: RDI
从破坏寄丢了。固定的。
rdi
was missing from clobbered registers. Fixed.
这里的code:
int main(void)
{
char my_char;
for (my_char = 'a'; my_char <= 'z'; my_char++)
my_putchar(my_char);
my_char = '\n';
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"
);
}
这篇关于GCC:内联汇编的putchar(炭)的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!