无法从 MASM 中的文件中读取数字
Unable to read a number from file in MASM
这里的问题是我必须在 MASM.I 中找到给定限制的斐波那契数列必须从 file.Like 中读取该限制,例如存储在名为 [=19= 的文件中的 5 ].
这是我的代码
.model small
.stack
.data
msg0 db 'enter the filename $'
msg1 db 'file found $'
msg2 db 'file not found $'
msg3 db 'file read successfull $'
msg4 db 'file read not successfull $'
newline db 10,13,'$'
limit dw ?
filename db 50 dup(?)
filedata db 50 dup(?)
pointerpos db 100 dup(?)
.code
print macro msg
push ax
push dx
mov ah,09h
mov dx,offset msg
int 21h
pop dx
pop ax
endm
.startup
print msg0
mov si,offset filename
call readstring
mov dx,offset filename
mov al,00h
mov ah,3dh
int 21h
jc failed
print msg1
mov bx,ax
mov al,00h
mov dx,offset pointerpos
mov ah,42h
int 21h
jc failed3
mov dx,offset filedata
mov cx,1
mov ah,3fh
int 21h
jc failed1
print newline
print msg3
mov si,offset filedata
call readlimit
mov ax,limit
call display
.exit
failed: print newline
print msg2
.exit
failed1: print newline
print msg4
.exit
failed3: print newline
.exit
readstring proc near
push ax
push si
l1: mov ah,01h
int 21h
cmp al,0dh
je skip
mov [si],al
inc si
jmp l1
skip: mov al,0h
mov [si],al
pop si
pop ax
ret
readstring endp
readlimit proc near
push ax
push bx
push cx
push dx
mov bx,0
mov cx,10
l2: mov al,[si]
cmp al,'0'
jb last
cmp al,'9'
ja last
sub al,30h
mov ah,0
push ax
mov ax,bx
mul cx
mov bx,ax
pop ax
add bx,ax
last: mov limit,bx
pop dx
pop cx
pop bx
pop ax
ret
readlimit endp
display proc near
push ax
push bx
push cx
push dx
mov bx,10
mov cx,0
l5: mov dx,0
div bx
cmp ax,0
je l4
inc cx
push dx
jmp l5
l4: pop dx
add dl,30h
mov ah,02h
int 21h
loop l4 ;decrement cl by 1 and check if cl==0
pop dx
pop cx
pop bx
pop ax
ret
display endp
end
我得到的错误是当我打印从文件中读取的值时出现了一些垃圾值。
注意:我没有尝试打印斐波那契数列,因为我无法从文件中读取数字。
您的代码在打开文件之前看起来没有问题。
从这里不清楚你想做什么。您将 pointerpos 定义为 100 字节的缓冲区,但我不明白为什么!它不应该只是一个包含您刚刚打开的文件中的偏移量的双字吗?
无论如何,接下来的几行没有像它应该的那样使用这个 DOS 功能:
mov al,00h
mov dx,offset pointerpos
mov ah,42h
int 21h
jc failed3
Seek 函数要求您在 CX:DX 中传递文件偏移量。
如果例如您的示例值 5 放置在文件的第一个字节中,然后 CX 和 DX 都必须设置为零。
可以这样写:
pointerpos dd 0
...
mov al, 00h
mov dx, [pointerpos]
mov cx, [pointerpos+2]
; handle already in BX
mov ah, 42h
int 21h
jc failed3
我还在您的 display 例程中发现了一个错误。您需要保证至少完成1次推送,否则程序会崩溃!
更改您的代码
l5:
mov dx,0
div bx
cmp ax,0
je l4
inc cx
push dx
jmp l5
至
l5:
mov dx,0
div bx
inc cx
push dx
cmp ax,0
jne l5
这里的问题是我必须在 MASM.I 中找到给定限制的斐波那契数列必须从 file.Like 中读取该限制,例如存储在名为 [=19= 的文件中的 5 ].
这是我的代码
.model small
.stack
.data
msg0 db 'enter the filename $'
msg1 db 'file found $'
msg2 db 'file not found $'
msg3 db 'file read successfull $'
msg4 db 'file read not successfull $'
newline db 10,13,'$'
limit dw ?
filename db 50 dup(?)
filedata db 50 dup(?)
pointerpos db 100 dup(?)
.code
print macro msg
push ax
push dx
mov ah,09h
mov dx,offset msg
int 21h
pop dx
pop ax
endm
.startup
print msg0
mov si,offset filename
call readstring
mov dx,offset filename
mov al,00h
mov ah,3dh
int 21h
jc failed
print msg1
mov bx,ax
mov al,00h
mov dx,offset pointerpos
mov ah,42h
int 21h
jc failed3
mov dx,offset filedata
mov cx,1
mov ah,3fh
int 21h
jc failed1
print newline
print msg3
mov si,offset filedata
call readlimit
mov ax,limit
call display
.exit
failed: print newline
print msg2
.exit
failed1: print newline
print msg4
.exit
failed3: print newline
.exit
readstring proc near
push ax
push si
l1: mov ah,01h
int 21h
cmp al,0dh
je skip
mov [si],al
inc si
jmp l1
skip: mov al,0h
mov [si],al
pop si
pop ax
ret
readstring endp
readlimit proc near
push ax
push bx
push cx
push dx
mov bx,0
mov cx,10
l2: mov al,[si]
cmp al,'0'
jb last
cmp al,'9'
ja last
sub al,30h
mov ah,0
push ax
mov ax,bx
mul cx
mov bx,ax
pop ax
add bx,ax
last: mov limit,bx
pop dx
pop cx
pop bx
pop ax
ret
readlimit endp
display proc near
push ax
push bx
push cx
push dx
mov bx,10
mov cx,0
l5: mov dx,0
div bx
cmp ax,0
je l4
inc cx
push dx
jmp l5
l4: pop dx
add dl,30h
mov ah,02h
int 21h
loop l4 ;decrement cl by 1 and check if cl==0
pop dx
pop cx
pop bx
pop ax
ret
display endp
end
我得到的错误是当我打印从文件中读取的值时出现了一些垃圾值。
注意:我没有尝试打印斐波那契数列,因为我无法从文件中读取数字。
您的代码在打开文件之前看起来没有问题。
从这里不清楚你想做什么。您将 pointerpos 定义为 100 字节的缓冲区,但我不明白为什么!它不应该只是一个包含您刚刚打开的文件中的偏移量的双字吗?
无论如何,接下来的几行没有像它应该的那样使用这个 DOS 功能:
mov al,00h
mov dx,offset pointerpos
mov ah,42h
int 21h
jc failed3
Seek 函数要求您在 CX:DX 中传递文件偏移量。
如果例如您的示例值 5 放置在文件的第一个字节中,然后 CX 和 DX 都必须设置为零。
可以这样写:
pointerpos dd 0
...
mov al, 00h
mov dx, [pointerpos]
mov cx, [pointerpos+2]
; handle already in BX
mov ah, 42h
int 21h
jc failed3
我还在您的 display 例程中发现了一个错误。您需要保证至少完成1次推送,否则程序会崩溃!
更改您的代码
l5:
mov dx,0
div bx
cmp ax,0
je l4
inc cx
push dx
jmp l5
至
l5:
mov dx,0
div bx
inc cx
push dx
cmp ax,0
jne l5