Seg mistake ... on hello world

This code is very simple and I get a seg error on my x86_64 Linux system. It bothers me very much. Just start with asm, please be patient!

Assembled with NASM nasm -f elf64 test.asm

related to ld -o test test.o

 SECTION .text GLOBAL _start _start: ; print name mov eax,4 ; sys_write mov ebx,1 ; stdout mov ecx,name ; start address of name mov edx,1 ; length int 80H ; syscall ; exit program mov eax,1 ; sys_exit mov ebx,0 ; success int 80H ; sys_call SECTION .data name DB 'R' 

My car: Gentoo x86_64 nomultilib! I compiled my own kernel without IA32 emulation. I had to say that my system is only a 64-bit system. Are this attribute errors that I get?

 $ uname -a Linux rcepeda 4.4.1-2-ARCH #1 SMP PREEMPT Wed Feb 3 13:12:33 UTC 2016 x86_64 GNU/Linux 

Decision

use 64-bit registers and 64-bit Linux manager

use syscall (not int 80H).

Thanks Nate and Michael

32-bit SYSCALL Linux table

64-bit SYSCALL Linux table

 SECTION .text GLOBAL _start _start: ; print name mov rax,1 ; sys_write mov rdi,1 ; stdout mov rsi,name ; start address of name mov rdx,7 ; length syscall ; exit program mov rax,60 ; sys_exit mov rdi,0 ; success syscall SECTION .data name DB "Rafael",10 

.

 rafael@rcepeda ~/asm $ ./a.out Rafael 
+5
source share
1 answer

You work in 64-bit mode, but this is 32-bit code. If you need 64-bit code, you will have to rewrite it.

You must use 64-bit rax, rbx , etc. registers. And on 64-bit Linux, system calls are no longer made with int 80h , but with the new syscall instruction. See http://cs.lmu.edu/~ray/notes/linuxsyscalls/ for an example (note that AT & T assembler syntax is used instead of Intel).

Alternatively, you can keep the code the same and compile and link it in 32-bit mode using nasm -f elf32 and ld -m elf_i386 . But then you learn (relatively) outdated technology. (Edit: Actually, it seems that 32-bit compatibility is not enabled on your specific system, so this will not work for you.)

+3
source

Source: https://habr.com/ru/post/1243551/


All Articles