Asm x86 segmentation fault in reading from file

85 views Asked by At

I'm receiving segmentation fault in 5th line of loop2. This is the part of my code which is reading values from mmapped file byte by byte. Could you please tell me what I'm doing wrong?

.global _start
_start:
    mov $2,%rax
    mov 16(%rsp),%rdi
    mov $02,%rsi
    syscall 

    cmp $0,%rax
    jl exit
1:
    mov %rax,%r8
    mov $9,%rax
    mov $0,%rdi
    mov $4096,%rsi
    mov $0x3,%rdx
    mov $0x1,%r10
    mov $0,%r9
    syscall

    mov $0,%r10
loop2:
    mov (%rax,%r10,1),%r9b
    cmp $32,%r9
    je 2f

    sub $48,%r9b
    mov %r9b,(%r8,%r10,1)

    inc %r10
        jmp loop2
2:
    mov %r10,%rcx
    dec %r10
1

There are 1 answers

2
Ross Ridge On BEST ANSWER

The value in R8 at the time your program crashes is the file descriptor returned by the open syscall. Its value is probably 3 which isn't a valid address. You'll need to stores these values in a range of memory you've properly allocated. You can create a buffer in your program's .bss section or dynamically allocate memory with the brk syscall.