简单 ASM 代码上的分段错误 [英] Segmentation Fault on simple ASM code

查看:87
本文介绍了简单 ASM 代码上的分段错误的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

对于我尝试在 ubuntu 64 位版本下创建 NASM 示例并在组装并链接到 ELF 后执行它时的问题.当我执行

For my Question when I tried to create a example of NASM under ubuntu 64-bit version and execute it after assembled and linked into ELF. It return error messages as below when I execute

NASM -f elf64 -o firstasm.o firstasm.asmld -o firstasm firstasm.o第一节

NASM -f elf64 -o firstasm.o firstasm.asm ld -o firstasm firstasm.o firstasm

分段错误(核心转储)

我的 NASM 代码将在我尝试执行简单的 write() 和 exit() 函数的下方

My NASM code would be below where I tried to perform simple write() and exit() function

section .data ;Data segment

msg db "This line is test", 0x0a 

section .text ;text segment
global _start  ;Default entry point for ELF linking

_start:

; SYSCALL : write (1,msg,14)
xor rax,rax
xor rbx,rbx
xor rcx,rcx
xor rdx,rdx
mov rax,64 ; make a syscall write 4
mov rbx,1 ; put 1 into rbx and also stdout is 1
mov rcx,msg ;put address of string in rcx
mov rdx,19 ; put length of string into rdx
int 0x80   ; call kernel to made syscall

; SYSCALL : exit(0)
xor rax,rax
xor rbx,rbx
mov rax,93 ; make a syscall exit 93
mov rbx, 0  ; store 0 argument into rbx, success to exit
int 0x80

有人可以指出我的 NASM 代码有什么问题,以及解决分段错误(核心转储)"问题的建议.感谢任何可以提供帮助的人.

Can someone pointed me what is problem to my NASM code and suggestions to fix the problem of "Segmentation fault (core dumped)". Appreciate thanks to anyone could help.

推荐答案

呃,你从哪里得到系统调用号?你要把它们从空中拉出来吗?

Uh, where are you getting the system call numbers? Are you pulling them out of the air?

64 位 sys_exit = 6032 位 sys_exit = 1

64bit sys_exit = 60 32bit sys_exit = 1

64 位 sys_write = 132 位 sys_write = 4

64bit sys_write = 1 32bit sys_write = 4

Linux 64 位系统调用列表

Linux 32 位系统调用列表

x86_64 的 Linux 系统调用表

上面的链接将显示哪些寄存器用于什么.

The above link will show what registers are used for what.

32位系统调用-int 0x80没有使用64位寄存器,寄存器参数不同.64 位系统调用是 - syscall.

the 32 bit system call - int 0x80 does not use the 64bit registers and the register parameters are different. The 64 bit system call is - syscall.

32 位 sys_exit:

32 bit sys_exit:

mov     ebx, ERR_CODE
mov     eax, sys_exit  ; 1
int     80h

64 位 sys_exit:

64 bit sys_exit:

mov     rdi, ERR_CODE
mov     rax, sys_exit  ; 60
syscall

看到区别了吗?

如果你想为你的系统创建一个系统调用名称和号码的 inc 文件(可能由于某种原因它们不同)

if you want to create an inc file of the system call names and numbers for YOUR system (maybe they are different for some reason)

grep __NR /usr/include/asm/unistd_64.h | grep define | sed -e 's/\#/\%/' -e 's/__NR_/sys_/' > unistd_64.inc

当然,为您的系统调整 unistd_64.h 的路径.它对于 32 位来说是一样的,但我相信该文件名为 unistd_32.h.

of course, adjust the path to unistd_64.h for your system. It will be the same for 32 bit but the file is called unistd_32.h I believe.

既然我已经向您展示了 exit sys 调用和提供的链接之间的区别,您就可以将 write 系统调用修正为正确的.

Now that I showed you the difference between the exit sys call, and with the provided links, you can fix your write system call to be correct.

这篇关于简单 ASM 代码上的分段错误的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆