そのため、ユーザーからファイル名を取得し、ファイルを開いて読み取り、コンテンツを画面に出力するプログラムをアセンブリで作成しています。私の目標は、最終的にファイル内の単語数をカウントすることですが、今はコンテンツを読み取って、これがすべて正しく行われていることを確認するだけです。
これが私のコードです(それはたくさんあります、私は知っています)。画面を印刷する機能はまだ実装していません。
[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
ご覧のとおり、コードがどこに到達するかを確認するためだけに、コードにいくつかのブレークストリングと jc failureJumps があります。そのままでは、コードは機能しません。実行時にファイル名を要求しますが、最初のブレーク後に失敗し、特に「シグナル SIGSEGV により終了しています」という長いエラーが表示されます。また、ecx レジスタの値が ffffffff であることにも言及しています。「printLn ecx」行をコメントアウトすると、プログラムは動作しているように見えます (「Second break!」になります)。これ: myfile.txt)。私が間違っていることを誰かが知っていますか?前もって感謝します!