汇编语言修改输入字符串;

Assembly Language modify input string;

我正在尝试编写一个从文件 text.txt 中获取字符串的汇编程序。然后程序将字符串编码为 ROT13 并使用 printf 显示结果。但是,似乎我未能读取文件并陷入 "nextChar:" 中的无限循环。我确定我错过了什么,但不确定是什么。

谢谢

;%include "glibc"

section .data  ;section declaration
getFmt: dd "%c",10,0
fileFmt: dd "%s",10,0
countFmt: dd "%d",10,0
output: dd "output.txt",10,0
writeCode: dd "w",10,0
readCode: dd "r",10,0
EOF: db -1          ;I read that this may need to be 0, instead of -1
Filename db "text.txt",0

section .bss
char:

buf: resb 1

section .text   ;section declaration


global main
extern fopen
extern fclose
extern printf   ;you can use this for debugging, otherwise not needed
extern getchar
extern putchar
extern scanf

main: 

    mov  ebx, Filename  ; push file name to ebx
    push readCode       ;set file to "read" mode
    push ebx            ;push file name
    call fopen          ;open file
    add esp,8
    cmp eax,0           ;see if file opened correctly
    jne nextChar
    ret
nextChar:
    push ebx            ; Push file handle on the stack
    mov ebx,eax         ; Save handle of opened file in ebx    push ebx
    push dword [char]   ;get the next char from input file
    push getFmt         ;
    call getchar        ;use getchar function   
    add esp,12          ;clear the stack

    cmp eax,0 ; A returned null indicates error or EOF
    jle done ; If we get 0 in eax, close up & return

    add dword [char],13 ;add 13 to the ASCII value
    push dword [char]   ; push to print
    push getFmt         ;
    call printf         ;use printf function    
    add esp, 8          ;clear the stack
    jmp nextChar

done:  
    push ebx ; Push the handle of the file to be closed
    call fclose ; Closes the file whose handle is on the stack
    add esp,4 ; Clean up the stack
    ret ; Go home

如果 getchar returns 0,你有一个将退出的循环。如果您的文件中从来没有零字节,则不会发生这种情况。相反,getchar 将继续 return EOF 并且您的循环将无限期地继续。

检查您环境中 EOF 的值(通常为 -1,但始终在 0-255 范围之外)并将其用作循环的限制器。