所以我在程序集中编写一个程序,从用户那里获取文件名,打开文件,读取文件,然后将内容打印到屏幕上。我的目标是最终计算文件中的单词数量,但是现在我只是想让它读取内容以验证我是否正确地完成了这一切。
这是我的代码(我知道很多)。我还没有实现打印屏幕的功能:
[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)。有谁知道我做错了什么?提前致谢!
答案 0 :(得分:0)
错误可能是您将DOS定义为0f1h。正确的值是21h。
以下是维基百科上的DOS中断列表:
https://en.wikipedia.org/wiki/MS-DOS_API#DOS_INT_21h_services
希望有所帮助!