Zac*_*ack 4 linux x86 assembly nasm windows-subsystem-for-linux
我一直在寻找汇编教程,我正在尝试运行一个hello world程序.我在Windows上使用Ubuntu上的Bash.
这是集会:
section .text
global _start ;must be declared for linker (ld)
_start: ;tells linker entry point
mov edx,len ;message length
mov ecx,msg ;message to write
mov ebx,1 ;file descriptor (stdout)
mov eax,4 ;system call number (sys_write)
int 0x80 ;call kernel
mov eax,1 ;system call number (sys_exit)
int 0x80 ;call kernel
section .data
msg db 'Hello, world!', 0xa ;string to be printed
len equ $ - msg ;length of the string
Run Code Online (Sandbox Code Playgroud)
我正在使用这些命令来创建可执行文件:
nasm -f elf64 hello.asm -o hello.o
ld -o hello hello.o -m elf_x86_64
Run Code Online (Sandbox Code Playgroud)
我运行它:
./hello
Run Code Online (Sandbox Code Playgroud)
然后该程序似乎在没有分段错误或错误的情况下运行,但它不产生输出.
我无法弄清楚为什么代码不会产生输出,但我想知道在Windows上使用Ubuntu上的Bash是否与它有关?为什么不产生输出,我该如何解决?
Mic*_*tch 12
问题出在Ubuntu for Windows(适用于Linux的Windows子系统)上.它仅支持64位syscall接口,而不int 0x80支持32位x86系统调用机制.
除了无法int 0x80在64位二进制文件中使用(32位兼容性)之外,Windows上的Ubuntu(WSL)也不支持运行32位可执行文件.
你需要从使用转换int 0x80为syscall.这并不困难.一组不同的寄存器用于a syscall,系统调用号与32位对应号不同.Ryan Chapman的博客提供了有关syscall界面,系统调用及其参数的信息.Sys_write并Sys_exit以这种方式定义:
Run Code Online (Sandbox Code Playgroud)%rax System call %rdi %rsi %rdx %r10 %r8 %r9 ---------------------------------------------------------------------------------- 0 sys_read unsigned int fd char *buf size_t count 1 sys_write unsigned int fd const char *buf size_t count 60 sys_exit int error_code
使用syscallclobbers RCX和R11寄存器.他们被认为是不稳定的 不要依赖它们之后的相同值syscall.
您的代码可以修改为:
section .text
global _start ;must be declared for linker (ld)
_start: ;tells linker entry point
mov edx,len ;message length
mov rsi,msg ;message to write
mov edi,1 ;file descriptor (stdout)
mov eax,edi ;system call number (sys_write)
syscall ;call kernel
xor edi, edi ;Return value = 0
mov eax,60 ;system call number (sys_exit)
syscall ;call kernel
section .data
msg db 'Hello, world!', 0xa ;string to be printed
len equ $ - msg ;length of the string
Run Code Online (Sandbox Code Playgroud)
注意:在64位代码中,如果指令的目标寄存器是32位(如EAX,EBX,EDI,ESI等),则处理器零将结果扩展到 64位寄存器的高32位.mov edi,1具有相同的效果mov rdi,1.
这个答案不是编写64位代码的入门读物,而是关于使用syscall接口的.如果您对编写调用C库的代码的细微差别感兴趣,并且符合64位System V ABI,那么可以使用合理的教程来启动Ray Toal的NASM教程.他讨论了堆栈对齐,红区,寄存器用法以及64位System V调用约定的基本概述.
| 归档时间: |
|
| 查看次数: |
2009 次 |
| 最近记录: |