Linux X86:在不引用标准库的情况下进行系统调用
我编写了从stdin读取并写入stdout的代码:Linux X86:在不引用标准库的情况下进行系统调用,linux,assembly,x86,system-calls,att,Linux,Assembly,X86,System Calls,Att,我编写了从stdin读取并写入stdout的代码: #include <stdio.h> #include <unistd.h> int main() /* copy input to output */ { char buf[BUFSIZ]; int n; while ((n = read(0, buf, BUFSIZ)) > 0) write(1, buf, n); return 0; } 它工作正常,但
#include <stdio.h>
#include <unistd.h>
int main() /* copy input to output */
{
char buf[BUFSIZ];
int n;
while ((n = read(0, buf, BUFSIZ)) > 0)
write(1, buf, n);
return 0;
}
它工作正常,但我不确定如何使用普通汇编进行“读”和“写”系统调用(即,将数字移动到某些寄存器中,并使用“int 0x80”执行系统调用)
我的目标是使它即使使用“-nostlib”选项编译也能正常工作。提示:32位x86是旧的、慢的、怪异的和不推荐的。您应该改用amd64
Linux i386的系统调用列表在Linux源代码中提供:
或者在
asm/unistd_32.h
中的glibc头中。你可以而且应该包括
另见
Linux i386的系统调用列表在Linux源代码中提供:
或者在
asm/unistd_32.h
中的glibc头中。你可以而且应该包括
另见
.text
.globl _start
_start:
pushl %ebp
movl %esp, %ebp
andl $-16, %esp #16 bit alignment
subl $8224, %esp #space for local variables
jmp _READ
_WRITE:
movl 8220(%esp), %eax
movl %eax, 8(%esp)
leal 28(%esp), %eax
movl %eax, 4(%esp)
movl $1, (%esp)
call write
int $0x80
_READ:
movl $8192, 8(%esp) #buffer length
leal 28(%esp), %eax
movl %eax, 4(%esp)
movl $0, (%esp)
call read
movl %eax, 8220(%esp)
cmpl $0, 8220(%esp)
jg _WRITE
movl $0, %eax
leave
ret
.text
.globl _start
_start:
pushl %ebp
movl %esp, %ebp
andl $-16, %esp #16 bit alignment
subl $8224, %esp #space for local variables
jmp _READ
_WRITE:
movl 8220(%esp), %edx
leal 28(%esp), %ecx
movl $1, %ebx
movl $4, %eax
int $0x80
_READ:
movl $8192, %edx #buffer length
leal 28(%esp), %ecx
movl $0, %ebx
movl $3, %eax
int $0x80
movl %eax, 8220(%esp)
cmpl $0, 8220(%esp)
jg _WRITE
movl $1, %eax
movl $0, %ebx
int $0x80
$ as --32 hel.s -o hel.o
$ ld -melf_i386 hel.o -o hel