سؤال

I have written a code for example

.global _start
.data
str:
    .long 0x1
.text
_start:
  mov  $1, %rax         # system call 1 is write
  mov  $0x21, %rdx
  mov  %rdx, 4(%rax)

  mov  $60, %rax        # system call 60 is exit
  xor  %rdi, %rdi       # we want return code 0
  syscall               # invoke operating system to exit

why do I receive a segmentation fault? When do I use the memory location? How do I use the memory location?

هل كانت مفيدة؟

المحلول

You're trying to write the value in rdx to memory location 5. (rax+4, and rax was initialized to 1).

Are you running this program on a real computer or a simulator?

If you're running it on a computer, you cannot just write to arbitrary memory locations. The operating system allocates a certain amount of memory to a process, and if the process tries to access memory outside this range, this can cause a segmentation fault.

Its okay if you're running it on a simulator and there's no OS. If you want to write to memory, declare a variable in the data segment like this:

.data
myvar:
    .long 0x1
.text
_start:
  #....
  mov  %rdx, myvar
  #...
مرخصة بموجب: CC-BY-SA مع الإسناد
لا تنتمي إلى StackOverflow
scroll top