64位NASM文件处理问题

时间:2013-11-29 03:46:17

标签: linux assembly linux-kernel x86-64 nasm

我设法在我的64位Linux系统上编写一个NASM程序,它从输入中删除非字母符号,并在单独的行中打印每个单词。问题是我得到RCX = -1,我必须得到readed字符号,结果我得到分段错误。我已经花了好几个小时试图弄清楚如何修复这个bug。希望你们能帮助我。提前谢谢。

继承我的代码:

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

2 个答案:

答案 0 :(得分:1)

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

答案 1 :(得分:0)

sys_close和您尝试检查收到的字节数之间有一个sys_read。因此,您正在检查关闭的返回值,而不是读取的返回值。另请注意,系统调用会销毁rcx,因此您无法向上移动mov rcx, rax行。

此外,在一些地方你使用[ebx]而不是[rbx]。

此外,您可能希望对结果文件使用O_CREAT,并且只写入已处理的字节数,而不是buffSize