所以我正在编写一个汇编程序,它从用户那里获取一个文件名,打开文件,读取它,并将内容打印到屏幕上。我的目标是最终让它计算文件中的字数,但现在我只想让它读取内容以验证我是否正确地执行了所有这些操作。
这是我的代码(很多,我知道)。我还没有实现打印屏幕的功能:
[GLOBAL mystart]
[EXTERN _printf]
[EXTERN _atoi]
;define constants for buffer size and interupt
%define DOS 0f1h
%define INBUFFSIZE 40
%define FILEBUFFSIZE 4096
;define macro for printing on screen
%macro printLn 1
mov ah, 09h
mov edx, %1
int DOS
%endmacro
[SECTION .text]
mystart:
readFile:
;prints "Enter the filename: " on screen
printLn string
;takes filename from user
mov ah, 0Ah
mov byte [infile], INBUFFSIZE
mov byte [infile+1], 0
mov edx, infile
int DOS
;pads filename string with 0
movzx ecx, byte [infile+1]
mov byte [infile+ecx+2], 0
;opens the file (read mode)
mov ah, 3Dh
mov al, 0
mov edx, infile+2
int DOS
mov [inhandle], ax
jc failureJump
;reads the file
mov ah, 3Fh
mov bx, [inhandle]
mov edx, filebuff
mov cx, FILEBUFFSIZE
int DOS
jc failureJump
printLn string3
mov [nbytes], ax
;nbytes stores the number of bytes in the file, so that we can loop through it
;prepare for looping through characters
mov ecx, 0
mov cl, [nbytes]
mov ebx, 0
; I want to see how many bytes are in the file
printLn ecx
processFile:
printLn string4
jmp endProcess
failureJump:
printLn string2
endProcess:
ret
[SECTION .data]
string db "Enter a filename: ", 13, 10, '$'
string2 db "Found a dot!", 13, 10, '$'
string3 db "First break!", 13, 10, '$'
string4 db "Second break!", 13, 10, '$'
string5 db "Third break!", 13, 10, '$'
[SECTION .bss]
infile: resb 512
filebuff: resb FILEBUFFSIZE
inhandle: resw 1
nbytes: resw 1
nbytes2: resw 1
如您所见,我在代码中有几个break-strings 和jc failureJumps 只是为了查看它的位置。就目前而言,代码不起作用。执行时,它会询问文件名,但在第一次中断后失败,并出现一个长错误“由于信号 SIGSEGV 而退出”等。它还提到 ecx 寄存器的值是 ffffffff。如果我注释掉“printLn ecx”行,程序似乎可以工作(它进入“第二次休息!”),虽然速度很慢,而且我没有确凿的证据表明它正在正确读取文件(我确实有一个文件这个:myfile.txt)。有谁知道我做错了什么?提前致谢!