1

I am new to assembly. I am trying to do this:

SECTION .data

SECTION .bss

SECTION .text
    global _start

_start:
    nop
    mov rax, 067FEh
    mov bx, ax
    mov cl, bh
    mov ch, bl
    nop

Everytime I run this , I get a segmentation fault. I used gdb to test where it went wrong. It appeared that every time after mov rax, 067FEh, it said the program received SIGSEGV. I tried replacing rax with eax or ax, but it still gave the fault. When I tried to look up the value in rax, it was 067FEh. I can't figure out what happened there. Can anybody help?

Sep Roland
  • 33,889
  • 7
  • 43
  • 76
  • A move of an immediate into a register will not cause a segfault. We have a processor mode problem here, most likely. First of all, what assembler are you using? (`nasm`, `yasm`?) Secondly, is that everything? What is the CPU supposed to do after it executes the `nop`? Third, are you telling your assembler to output a 64-bit ELF, so that the CPU is in the correct mode when it tries to execute this? Are you executing the output binary directly? I would also expect a `[BITS 64]` or similar directive at the top of the file. – Jonathon Reinhart Jan 19 '15 at 04:24
  • Does your assembler use OPCODE SRC, DEST or OPCODE DEST, SRC? – user3344003 Jan 19 '15 at 04:42
  • I used nasm. nasm -f elf64 -g filename.asm. – William Chan Jan 19 '15 at 05:41
  • 1
    I tried out your code with eax instead of rax and nasm in 32 bit Linux. It worked without giving out an error: `nasm -g -f elf32 blub.asm` and after that `ld -m elf_i386 -static -o blub blub.o` – Welcor Jan 19 '15 at 08:27
  • 1
    Are you able to execute the next instruction ? i.e., `mov bx, ax` – User.1 Jan 19 '15 at 09:28

1 Answers1

1

The SIGSEGV is coming from that fact that you are dropping out of the .text section. You need to add:

mov eax, 1
int 0x80

to properly exit the program. If you do not do that, the code with continue to execute past your program (usually into a bunch of 00 00 bytes). Also, you do not need the section .data and section .bss declarations because you are not using them.

Sep Roland
  • 33,889
  • 7
  • 43
  • 76
Markian
  • 322
  • 1
  • 12
  • Please don't post answers that just repeat what existing answers on other questions say. Instead, flag the question as a duplicate. e.g. in this case, [What happens if there is no exit system call in an assembly program?](https://stackoverflow.com/q/49674026) (if that's actually what's going on; the OP *claims* it's faulting after the `mov rax, 067FEh`, not after the final `nop`, but that's implausible and it isn't a [mcve] of that. I'm just going to close the question anyway because it's not useful to future readers, especially with that title.) – Peter Cordes Jan 13 '23 at 01:48
  • Also, the question is 64-bit code, so the right way to _exit is `mov eax, 60` / `syscall`. (or `231` for `exit_group` like libc `_exit()` uses) – Peter Cordes Jan 13 '23 at 01:49
  • Some of the useful canonical Q&As are linked in the FAQ section of https://stackoverflow.com/tags/x86/info (Some of them probably aren't the best canonical for the topic; if you find better, let me know so I can edit.) – Peter Cordes Jan 13 '23 at 01:57