YASM(8086)汇编中如何逐词校验并统计符合要求的单词数
汇编项目:统计不含B/b/C/c的单词数量问题
项目需求
- 核心任务:统计文件前两列中**不含字母'B'/'b'/'C'/'c'**的单词数量(单词定义为非空格字符组成的连续序列)
- 文件格式:每行包含6列,列之间以分号
;分隔- 前两列:长度1-20的字符串,包含字母、数字和空格
- 第3-5列:范围-100到100的整数
- 第6列:保留两位小数的实数,范围-9.99到9.99
- 测试用例:输入行
a A a a aba aca;BA A C BA a;1;1;1;1.00,正确输出应为6
当前问题
已实现命令行读取文件名、文件读写、结果输出等功能,但逐词校验逻辑存在错误,导致统计结果不正确,请求协助修正。
当前代码
org 100h %include 'yasmmac.inc' section .text startas: macPutString 'Write outpu file name', crlf, '$' ; Reading file from command line .commandLine: mov bx, 82h mov si, 0h jmp .checkInputFile ; Turns it into ASCIIZ .checkInputFile: mov cl, [bx+si] cmp cl, 20h jl .addZero inc si jmp .checkInputFile .addZero: mov byte [bx+si], 0h xor si, si ; Saving writing file mov al, 128 mov dx, writingFile call procGetStr macNewLine ; Open reading file mov dx, bx call procFOpenForReading jnc .writingFileIsOpened macPutString 'Error while opening reading file', '$' exit ; Atidarome rasymo faila .writingFileIsOpened: mov [readingDescriptor], bx mov dx, writingFile call procFCreateOrTruncate jnc .openWritingFile macPutString 'Error while opening writing file', '$' jmp .writingError ; Save writing descriptor .openWritingFile: mov [writingDescriptor], bx ; Reads first line call procReadLine ;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;; ; Main loop .whileNotEndOfFile: xor di, di xor si, si call procReadLine ; Check first two columns ;mov al, ';' mov di, line mov al, [di] cmp al, ' ' je .nextWord cmp al, ';' je .nextWord ; Checking words .checkWord: mov al, [di] inc di cmp al, byte 'B' je .nextWord cmp al, byte 'b' je .nextWord cmp al, byte 'C' je .nextWord cmp al, byte 'c' je .nextWord cmp al, byte ' ' je .addNumber cmp al, byte ';' je .semicolon jmp .checkWord .nextWord: call procNextWord jmp .checkWord .semicolon: call procAddNumber inc si cmp si, 0x2 je .skipLine jmp .nextWord .addNumber: call procAddNumber jmp .nextWord ; If this is not the end of file, repeat loop .skipLine: cmp [readLastLine], byte 0 je .whileNotEndOfFile ; Hexadecimal convertion to decimal mov dx, lineCount mov ax, [lineCount] call procUInt16ToStr call procPutStr macNewLine mov si, dx .writingToFile: lodsb cmp al, 0 jne .writingToFile sub si, dx lea cx, [si-1] mov bx, [writingDescriptor] mov ah, 40h int 21h ; Closing Files .end: mov bx, [writingDescriptor] call procFClose .writingError: mov bx, [readingDescriptor] call procFClose macPutString 'Program ends', crlf, '$' exit %include 'yasmlib.asm' ; void procReadLine() ; Read line to buffer ‘eilute’ procReadLine: push ax push bx push cx push si mov bx, [readingDescriptor] mov si, 0 .loop: call procFGetChar ; End if the end of file or error cmp ax, 0 je .endOfFile jc .endOfFile ; Putting symbol to buffer mov [line+si], cl inc si ; Check if there is \n? cmp cl, 0x0A je .endOfLine jmp .loop .endOfFile: mov [readLastLine], byte 1 .endOfLine: mov [line+si], byte '$' mov [lineLenght], si pop si pop cx pop bx pop ax ret ;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;; procAddNumber: push si push ax push bx push cx push dx ;lineCount++ mov ax, word [lineCount] inc ax mov [lineCount], ax pop dx pop cx pop bx pop ax pop si ret procNextWord: .loop: inc di mov al, [di] cmp al, byte ' ' je .loop jmp .t .t: cmp al, byte ';' je .t2 jmp .return .t2: inc di mov al, [di] cmp al, byte ' ' je .t2 jmp .return .return: ret section .data readingDescriptor: dw 0000 writingFile: times 128 db 00 writingDescriptor: dw 0000 readLastLine: db 00 line: db 64 times 66 db '$' lineLenght: dw 0000 lineCount: times 128 db 00
内容的提问来源于stack exchange,提问作者Kurbamit
相关产品推荐
相关产品推荐

