寻找新线路
Finding a new line
我是汇编新手,我的任务是读取文件名并打印该文件中的偶数行。我不知道如何找到一条新线。我知道我必须 cmp 13 和 10,因为那是马车 return,但我没有成功。任何帮助将不胜感激。这是我目前拥有的:
assume cs:code, ds:data
data segment
message1 db 'Name of the file: $'
message2 db 'Even linis: $'
maxFileName db 12
lFileName db ?
fileName db 12 dup (?)
buffer db 8 dup (?), '$'
openErrorMsg db 'Open error!$'
readErrorMsg db 'Read error!$'
data ends
code segment
start:
mov ax, data
mov ds, ax
;print initial message
mov ah, 09h
mov dx, offset message1
int 21h
;read name of the file
mov ah, 0ah
mov dx, offset maxFileName
int 21h
;convert name of the file to ASCIIZ
mov al, lFileName
xor ah, ah
mov si, ax
mov fileName[si], 0
;open file
mov ah, 3dh
mov al, 0
mov dx, offset fileName
int 21h
jc openError ;error <=> CF=1
mov bx, ax
;end of line
mov dl, 10
mov ah, 02h
int 21h
mov dl, 13
mov ah, 02h
int 21h
;print second message
mov ah, 09h
mov dx, offset message2
int 21h
;end of line
mov dl, 10
mov ah, 02h
int 21h
mov dl, 13
mov ah, 02h
int 21h
read:
mov ah, 3fh
mov dx, offset buffer
mov cx, 8 ;read 8 char
int 21h
jc readError ;error <=> CF=1
cmp buffer, 0Dh
je read
;save bytes number and add $ for printing
mov si, ax
mov buffer[si], '$'
afisare:
;print what have we read
mov ah, 09h
int 21h
cmp si, 8
je read ;if we read 8 bytes, we have yet to read all
jmp endPb ;nu exista erori
openError:
mov ah, 09h
mov dx, offset openErrorMsg
int 21h
jmp endPb
readError:
mov ah, 09h
mov dx, offset readErrorMsg
int 21h
endPb:
mov ax,4c00h
int 21h
mov ax,4c00h
int 21h
code ends
end start
问题出在您的 read
代码上。我添加了评论以使您更容易看出问题所在:
read:
mov ah, 3fh ; Read from file or device
mov dx, offset buffer ; Read destination
mov cx, 8 ; Read 8 characters into buffer
int 21h ; BIOS call
jc readError ; Check for error in carry flag
;;; vvv Here's your problem
cmp buffer, 0Dh ; Check to see if the memory address of
; buffer is 0x0d
;;; ^^^ Here's your problem
je read ; If the address is 0x0d, jump back and read again
如上所述,问题在于您将错误的东西与 0x0d
进行了比较。您可能打算这样做:
cmp [buffer], 0dh
虽然这会比较数据而不是 buffer
的地址文字,但它仍然不完整。您真的想比较每个读取的 byte 以查看它是否包含 ASCII 13。我会把它留给您,因为这是家庭作业:)
我是汇编新手,我的任务是读取文件名并打印该文件中的偶数行。我不知道如何找到一条新线。我知道我必须 cmp 13 和 10,因为那是马车 return,但我没有成功。任何帮助将不胜感激。这是我目前拥有的:
assume cs:code, ds:data
data segment
message1 db 'Name of the file: $'
message2 db 'Even linis: $'
maxFileName db 12
lFileName db ?
fileName db 12 dup (?)
buffer db 8 dup (?), '$'
openErrorMsg db 'Open error!$'
readErrorMsg db 'Read error!$'
data ends
code segment
start:
mov ax, data
mov ds, ax
;print initial message
mov ah, 09h
mov dx, offset message1
int 21h
;read name of the file
mov ah, 0ah
mov dx, offset maxFileName
int 21h
;convert name of the file to ASCIIZ
mov al, lFileName
xor ah, ah
mov si, ax
mov fileName[si], 0
;open file
mov ah, 3dh
mov al, 0
mov dx, offset fileName
int 21h
jc openError ;error <=> CF=1
mov bx, ax
;end of line
mov dl, 10
mov ah, 02h
int 21h
mov dl, 13
mov ah, 02h
int 21h
;print second message
mov ah, 09h
mov dx, offset message2
int 21h
;end of line
mov dl, 10
mov ah, 02h
int 21h
mov dl, 13
mov ah, 02h
int 21h
read:
mov ah, 3fh
mov dx, offset buffer
mov cx, 8 ;read 8 char
int 21h
jc readError ;error <=> CF=1
cmp buffer, 0Dh
je read
;save bytes number and add $ for printing
mov si, ax
mov buffer[si], '$'
afisare:
;print what have we read
mov ah, 09h
int 21h
cmp si, 8
je read ;if we read 8 bytes, we have yet to read all
jmp endPb ;nu exista erori
openError:
mov ah, 09h
mov dx, offset openErrorMsg
int 21h
jmp endPb
readError:
mov ah, 09h
mov dx, offset readErrorMsg
int 21h
endPb:
mov ax,4c00h
int 21h
mov ax,4c00h
int 21h
code ends
end start
问题出在您的 read
代码上。我添加了评论以使您更容易看出问题所在:
read:
mov ah, 3fh ; Read from file or device
mov dx, offset buffer ; Read destination
mov cx, 8 ; Read 8 characters into buffer
int 21h ; BIOS call
jc readError ; Check for error in carry flag
;;; vvv Here's your problem
cmp buffer, 0Dh ; Check to see if the memory address of
; buffer is 0x0d
;;; ^^^ Here's your problem
je read ; If the address is 0x0d, jump back and read again
如上所述,问题在于您将错误的东西与 0x0d
进行了比较。您可能打算这样做:
cmp [buffer], 0dh
虽然这会比较数据而不是 buffer
的地址文字,但它仍然不完整。您真的想比较每个读取的 byte 以查看它是否包含 ASCII 13。我会把它留给您,因为这是家庭作业:)