Skip to content
Advertisement

x86_64 Assembly Linux System Call Confusion

I am currently learning Assembly language on Linux. I have been using the book ‘Programming From the Ground Up’ and all the examples are 32-bit. My OS is 64-bit and I have been trying to do all the examples in 64-bit. I am having trouble however:

.section .data

.section .text
.global _start
_start:
movq $60, %rax
movq $2, %rbx
int $0x80

This merely just calls the Linux exit System call or it should. Instead it causes a SEG FAULT and when I instead do this

.section .data

.section .text
.global _start
_start:
movq $1, %rax
movq $2, %rbx
int $0x80

it works. Clearly the problem is the value I move to %rax. The value $1 that I use in the second example is what ‘Programming From the Ground Up’ said to use however multiple sources on the Internet have said that the 64-bit System Call Number is $60. Reference What am I doing wrong? Also what other issues should I watch out for and what should I use for a reference? Just in case you need to know, I am on Chapter 5 in Programming From The Ground Up.

Advertisement

Answer

You’re running into one surprising difference between i386 and x86_64: they don’t use the same system call mechanism. The correct code is:

movq $60, %rax
movq $2,  %rdi   ; not %rbx!
syscall

Interrupt 0x80 always invokes 32-bit system calls. It’s used to allow 32-bit applications to run on 64-bit systems.

For the purposes of learning, you should probably try to follow the tutorial exactly, rather than translating on the fly to 64-bit — there are a few other significant behavioral differences that you’re likely to run into. Once you’re familiar with i386, then you can pick up x86_64 separately.

User contributions licensed under: CC BY-SA
10 People found this is helpful
Advertisement