我有一項任務要做。我會盡量解釋清楚。
當您運行程式時,它會要求輸入(讀取檔案和寫入檔案)。
讀取檔案的行范圍為 [1; 999]。每行有六列。每列用分號 (;) 分隔。
第一列和第二列包含范圍 [1; 中的文本符號;20]。
第三 - 第五列包含整數 [-100; 100]。
最后一列包含浮點數 [-9.99; 9.99]。點后有兩個符號。
檔案示例:
firstA;lB;lC;lD;lE;lF
A11;bas hello;0;0;5;1.15
B12; good day;-100;11;78;1.33
任務: 輸出:只有第一列和第二列,不包含數字和符號“B”、“C”。
輸出:
firstA,因為只有這一列沒有“B”、“C”和數字。
到目前為止,我已經撰寫了程式,它只是丟棄了數字和符號。我無法找出完整TASK的解決方案。
我的程式
%include 'yasmmac.inc'
;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
org 100h
;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
section .text
startas:
macPutString 'Reading file name:', crlf, '$' ;Input for reading file
mov al, 128
mov dx, readingFile
call procGetStr
macNewLine
macPutString 'Writing file name: ', crlf, '$' ;Input for writing file
mov al, 128
mov dx, writingFile
call procGetStr
macNewLine
push readingFile
push writingFile
call function
exit
;Main Function
function:
push bp
mov bp, sp
sub sp, 4
push dx
push bx
push ax
mov dx, [bp 6]
call procFOpenForReading
jnc .secondFOpen
macPutString 'Error while opening file', crlf, '$'
jmp .end
.secondFOpen:
mov [bp-2], bx
mov dx, [bp 4]
call procFCreateOrTruncate
jnc .filter
macPutString 'Error while opening writing file', crlf, '$'
jmp .close
.filter:
mov [bp-4], bx
.whileNotTheEnd:
mov bx, [bp-2]
call procFGetChar
jnc .c1
macPutString 'Error while reading file', crlf, '$'
jmp .close
.c1:
cmp ax, 0 ; Checks if it is not the end of the file
jne .check
jmp .close ; If the end - close the file
.check:
mov al, cl
cmp al, ';' ; Checks if ';'
jne .c2
.c2:
cmp al, 30h ; Number checking
jge .c3
jmp .c4
.c3:
cmp al, 39h ; Number checking
jle .checkEnd
jmp .c4
.c4:
cmp al, 'B'
jne .c5
jmp .checkEnd
.c5:
cmp al, 'C'
jne .writing
jmp .checkEnd
.writing:
mov bx, [bp-4]
call procFPutChar
jnc .checkEnd
macPutString 'Error while writing file', crlf, '$'
jmp .close
.acheckEnd:
cmp ax, 0
jne .nextIteration
jmp .close
.nextIteration:
jmp .whileNotTheEnd
.close:
mov bx, [bp-4]
call procFClose
.closeReadingFile:
mov bx, [bp-2]
call procFClose
.end:
ret4
;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
%include 'yasmlib.asm'
;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
section .data
writingFile:
times 255 db 00
readingFile:
times 255 db 00
duomenys:
times 255 db 00
;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
section .bss
uj5u.com熱心網友回復:
在網上找不到太多關于“yasmmac.inc”的內容,但我相信我對這些procFGetChar和procFPutChar宏所做的解釋不會太遠......
如果您不使用模塊化方法,任務將顯得過于復雜。您需要將更簡單的子任務委派給單獨的子例程。此外,從資料的來源中抽象出來是有意義的。不要從檔案中讀取單個字符并立即對其進行分類。不,從檔案中獲取完整的行并從記憶體副本中處理請求的欄位。這將變得更容易,更不容易出現與檔案訪問相關的錯誤。
.whileNotTheEnd:
; Fetch one line from the file
call .fgets ; -> DI (AX BX CL)
test di, di
jz .close ; Normal EOF
; Process fields 1 and 2 from the memory copy
mov si, Buffer
call .field ; -> SI (AX BX)
call .field ; -> SI (AX BX)
jmp .whileNotTheEnd
.close:
mov bx, [bp-4]
call procFClose
mov bx, [bp-2]
call procFClose
文本檔案中的每一行都以回車和換行對 (13,10) 結束。因此,找到 10 表示 EOL。
從 DOS 接收到 AX=0 意味著檔案結束。如果在緩沖區中還沒有字符時發生這種情況,則表示正常 EOF。
; ----------------------
; IN (bp) OUT (di) MOD (ax,bx,cl)
.fgets:
mov bx, [bp-2] ; Handle
xor di, di ; Counts characters
.more:
call procFGetChar ; -> AX CL CF
jc .err1
test ax, ax ; EOF ?
jz .eof
mov [Buffer di], cl
inc di
cmp cl, 10
jne .more
.ret:
ret
.eof:
test di, di ; If DI==0 then Normal EOF
jz .ret
.err1:
macPutString 'Error while reading file', crlf, '$'
pop ax ; (*) Forget about `call fgets`
jmp .close
; ----------------------
field子程式實作了這個程式的魔力,但即使它也會將字符的分類委托給另一個子程式。這將增強可讀性。根據經驗,我嘗試將每個子程式保持在一個螢屏(25 行)的范圍內。因為此代碼將以 SI 指向當前欄位結束,所以繼續第二個欄位將非常容易。
; ----------------------
; IN (si) OUT (si) MOD (ax,bx)
.field:
mov bx, si ; Remember the start of this field
.check:
lodsb
cmp al, ";"
je .write
call .test ; -> CF
jnc .check
.skip:
lodsb ; Skip remainder of this (bad) field
cmp al, ";"
jne .skip
ret
.write:
push si ; (1)
mov si, bx ; Send this field to file
lodsb
.w1:
call .fputc ; -> (AX BX)
lodsb
cmp al, ";"
jne .w1
pop si ; (1) SI points after the ";"
ret
; ----------------------
; IN (al) OUT (CF)
.test:
cmp al, "0"
jb .OK
cmp al, "9"
jbe .NOK
cmp al, "B"
je .NOK
cmp al, "C"
je .NOK
.OK:
clc
ret
.NOK:
stc
ret
; ----------------------
; IN (al,bp) OUT () MOD (ax,bx)
.fputc:
mov bx, [bp-4] ; Handle
call procFPutChar ; -> AX CF
jc .err2
test ax, ax
jz .err2
ret
.err2:
macPutString 'Error while writing file', crlf, '$'
pop ax ; (*) Forget about `call .fputc`
pop ax ; (*) Forget about `call .field`
jmp .close
; ----------------------
(*) 錯誤在.fgets中退出,.fputc需要平衡堆疊!他們需要“忘記”在to .close可以安全通過call之前所做的 sjmp。
轉載請註明出處,本文鏈接:https://www.uj5u.com/houduan/533187.html
下一篇:從另一個檔案的一行中獲取資料
