2014-12-08 57 views
0

我必須從鍵盤輸入中讀取文件的名稱,然後在屏幕上打印來自此文件的偶數行 我確實使用int的3dh函數打開了文件21h,但我的問題是如何逐行閱讀以便只打印偶數行?我並不完全理解如何使用回車或換行。這是我到目前爲止已經完成:從程序集中的文件中讀取偶數行

assume cs:code, ds:data 
data segment 
    msg db 'Give the name of the file: $' 
    fileName db 12,?,13 dup (?) 
    buffer db 21 dup (?) 
    openErrorMsg db 'The file does not exist.$' 
    handler dw 0 
data ends 
code segment 
start: 
    mov ax, data 
    mov ds, ax 

    mov ah, 09h 
    mov dx, offset msg 
    int 21h 

    mov ah, 0ah 
    mov dx, offset fileName 
    int 21h 

    mov bl, fileName[1] 
    mov bh, 0 
    add bx, offset fileName 
    add bx, 2 
    mov byte ptr [bx], 0 

    mov ah, 3dh 
    mov al, 0 
    mov dx, offset fileName+2 
    int 21h 

    jc openError 
    ; ?? - 

openError: 
     mov ah, 09h 
     mov dx, offset openErrorMsg 
     int 21h 
     jmp endPrg 
    endPrg: 
     mov ah, 3eh 
     mov bx, handler 
     int 21h 

     mov ax,4c00h 
     int 21h 

code ends 
end start 
+0

不要忘了返回的句柄保存於你在哪裏apparantly卡住點你的第一個動作。然後使用Dirk Wolfgang Glomp給出的建議來處理文件。 – 2014-12-14 21:50:45

回答

1

回車(0DH)和換行(0AH)是一些在ASCII碼的控制字符的。回車命令打印機或其他輸出系統(如顯示器)將光標位置移動到同一行的第一個位置。換行將光標移動到下一行,以便它們一起開始新行。如果輸出位於最後一行,則屏幕內容向上滾動,輸出開始於新的最後一行。

如果我們只想打印文本文件的偶數行,那麼我們必須逐字節比較文本以找到「0Dh,0Ah」的序列或至少「0Dh」的字節。注意:一個linux文本文件只包含「0Dh」而不包含「0Ah」。爲了使用DOS電傳輸出功能,我們必須在我們想要打印的文本後面放置一個「$」。

爲了加載文件,我們可以使用DOS讀取功能。我更喜歡將整個文本文件一次加載到RAM中,並在另一步中比較和打印偶數行。但是另一種方法是,我們只能加載單個字節用於比較和打印,然後在下一步中加載下一個單個字節,依此類推。

RBIL-> inter61b.zip-> INTERRUP.F

--------D-213F------------------------------- 
INT 21 - DOS 2+ - "READ" - READ FROM FILE OR DEVICE 
AH = 3Fh 
BX = file handle 
CX = number of bytes to read 
DS:DX -> buffer for data 
Return: CF clear if successful 
    AX = number of bytes actually read (0 if at EOF before call) 
CF set on error 
    AX = error code (05h,06h) (see #01680 at AH=59h/BX=0000h) 
Notes: data is read beginning at current file position, and the file position 
    is updated after a successful read 
the returned AX may be smaller than the request in CX if a partial 
    read occurred 
if reading from CON, read stops at first CR 
under the FlashTek X-32 DOS extender, the pointer is in DS:EDX 
BUG: Novell NETX.EXE v3.26 and 3.31 do not set CF if the read fails due to 
    a record lock (see AH=5Ch), though it does return AX=0005h; this 
    has been documented by Novell 
SeeAlso: AH=27h,AH=40h,AH=93h,INT 2F/AX=1108h,INT 2F/AX=1229h