Assembly 无法从MASM中的文件中读取数字
这里的问题是,我必须在MASM中找到达到给定极限的斐波那契级数。我必须从一个文件中读取该极限。例如,5存储在一个名为“Test.txt”的文件中 这是我的密码Assembly 无法从MASM中的文件中读取数字,assembly,readfile,masm,Assembly,Readfile,Masm,这里的问题是,我必须在MASM中找到达到给定极限的斐波那契级数。我必须从一个文件中读取该极限。例如,5存储在一个名为“Test.txt”的文件中 这是我的密码 .model small .stack .data msg0 db 'enter the filename $' msg1 db 'file found $' msg2 db 'file not found $' msg3 db 'file read successfull $' msg4 d
.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
这里我得到的错误是,当我打印从文件中读取的值时,出现了一些垃圾值
注意:我没有试着打印斐波那契数列,因为我无法从文件中读取数字 您的代码在文件被打开时看起来正常。
现在还不清楚你想做什么。您将指针式操作定义为100字节的缓冲区,但我不明白为什么!它不应该仅仅是一个dword,包含刚刚打开的文件中的偏移量吗 无论如何,下一行没有像应该的那样使用此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
我还发现您的显示例程中有一个错误。您需要确保至少完成1次推送,否则程序将崩溃更改代码
l5:
mov dx,0
div bx
cmp ax,0
je l4
inc cx
push dx
jmp l5
到
谢谢,先生,这起作用了。问题实际上是显示程序。seek功能是一个尝试,但它不起作用。实际上它不需要。没有它,程序工作得很好。为什么这个问题在得到回答时被搁置?
l5:
mov dx,0
div bx
inc cx
push dx
cmp ax,0
jne l5