[英]Why I call fork() in C but make sys_clone() syscall instead? Why not sys_fork() syscall?
[英]Why do i need to call syscall write before syscall read to make it accept user input?
在下面的x64汇编程序中,我试图从命令行读取单个字符,然后将其打印回来。 在实际阅读角色之前,你会注意到我正在执行看似无用的写作。
该程序编译并在运行时停止,接受一个字符作为输入,按下返回后将该字符写回并退出。 没有虚假写入,它不会停止并接受用户输入并退出。 这是为什么?
TEST.ASM:
global _start
section .text
_start:
mov r8, stack
mov rax, 1 ; syscall write
mov rdi, 1 ;
mov rsi, r8 ;
mov rdx, 1 ; Why do I need to do this write
syscall ; to make the following read work?
mov rax, 0 ; syscall read
mov rdi, 0 ;
mov rsi, r8 ;
syscall ; Read a character from the cli.
mov rax, 1 ; syscall write
mov rdi, 1 ;
mov rsi, r8 ;
mov rdx, 1 ;
syscall ; Print the character back to the cli.
mov rax, 60 ;
mov rdi, 0 ;
syscall ; Exit nicely.
section .bss
stack: resb 131072
我在Ubuntu 16.04上使用nasm
来编译如下:
nasm -f elf64 -o test.o test.asm
ld -m elf_x86_64 -o test test.o
./test
问题是,当你在没有第一次写入的情况下尝试时,你不会使用mov rdx, 1
将rdx
设置为1.因此,你不知道你的系统调用读取了多少。 在这里,您可以查看syscall表
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.