简体   繁体   中英

64bit NASM file handling problems

I managed to write a NASM program on my 64bit Linux system which removes non-letter symbols from an input and prints each word in separate line. The problem is that I get RCX = -1 where i have to get the readed character number , and as a result I get segmentation fault. I've already spent hours trying to figure out how to fix this bug. Hope you guys will be able to help me. Thanks in advance.

Heres my code:

section .data

file1   db "data", 0
file2   db "results", 0

text        times 255 db 0
textSize    equ $ - text
buff        times 255 db 0
buffSize    equ $ - buff    


section .text
global main
main:
    mov         rax, 2
    mov     rdi, file1
    mov     rsi, 0          ;read only
    mov     rdx, 0x7777
    syscall                 ;open file1
    mov     rbx, rax        ;save fd to rbx
    mov     rsi, text           ; a pointer to the current character 

    mov     rax, 0
    mov     rdi, rbx        ;fd of file1
    mov     rsi, text
    mov     rdx, textSize
    syscall                 ;read the text from file1 

    mov     rax, 3
    mov     rdi, rbx
    syscall                 ;close file1

    mov     rcx, rax        ; rcx  - character counter

    mov     rbx, buff       ;rbx will be our buffer

    cmp     rcx, 0
    je      exit            ; if nothing to read - exit

process_loop1:
    mov     dl, byte[rsi]

    cmp     byte[rsi], 0x41     ; "A"
    jl      inc1
    cmp     byte[rsi], 0x5a     ; "Z"
    jle     save
    cmp     byte[rsi], 0x61     ; "a"
    jl      inc1
    cmp     byte[rsi], 0x7a     ; "z"
    jle     save
    jmp     inc1                ;check text

inc1:
    inc     rsi
    dec     rcx
    jnz     process_loop1
    jmp     print

save:                   
    mov     byte [ebx], dl
    jmp     inc2            ;save letters


inc2:
    inc     rsi
    inc     rbx
    dec     rcx
    jnz     process_loop2
    jmp     print



process_loop2:
    mov     dl, byte[rsi]

    cmp     byte[rsi], 0x41     ; "A"
    jl      enter
    cmp     byte[rsi], 0x5a     ; "Z"
    jle     save
    cmp     byte[rsi], 0x61     ; "a"
    jl      enter
    cmp     byte[rsi], 0x7a     ; "z"
    jle     save
    jmp     enter



enter:
    mov     byte [ebx], 10      ;enter
    inc     rsi
    inc     rbx
    dec     rcx
    jnz     process_loop1
    jmp     print

print:                  
    mov         rax, 2
    mov     rdi, file2
    mov     rsi, 1      ;write only
    mov     rdx, 0x7777
    syscall                     ;open file2
    mov     rbx, rax    ;save fd to rbx


    mov     rax, 1
    mov     rdi, rbx
    mov     rsi, buff
    mov     rdx, buffSize
    syscall                 ;print result

    mov     rax, 3
    mov     rdi, rbx
    syscall                 ;close file2
    jmp     exit

exit:
    mov     rax, 60
    mov     rdi, 0
    syscall
section .data
    filename db 'AVG.asm'


section .bss
    buffer resb 2000
    fd_in resb 1

section .text
    global _start
_start:
        mov rax,2
        mov rdi,filename
        mov rsi,0
        mov rdx,0777
        syscall

        mov [fd_in],rax

        mov rax,0
        mov rdi,[fd_in]     
        mov rsi,buffer
        mov rdx,2000
        syscall


        mov rax,1
        mov rdi,1
        mov rsi,buffer
        mov rdx,2000
        syscall



        mov rax,3
        mov rdi,[fd_in]
        syscall

        mov rax,60
        mov rdi,0
        syscall

You have a sys_close between the sys_read and the time you try to check the number of bytes received. Thus, you are checking the return value of the close, not the read. Also note that rcx is destroyed by syscall so you can't just move up the mov rcx, rax line.

Also, in a few places you use [ebx] instead of [rbx].

Furthermore, you probably want use O_CREAT for the result file and only write as many bytes as you have processed, not buffSize .

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM