assembly - 无法从 MASM 中的文件读取数字

标签 assembly readfile masm

这里的问题是我必须在 MASM 中找到达到给定限制的斐波那契数列。我必须从文件中读取该限制。例如存储在名为“Test.txt”的文件中的 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

我还在您的显示例程中发现了一个错误。需要保证至少1次push完成,否则程序会崩溃!
更改您的代码

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

关于assembly - 无法从 MASM 中的文件读取数字,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/34057938/

相关文章:

rust - rust 主fn读取目录结果错误无法编译

java - 如何使用BufferedReader一次又一次地读取同一个txt文件?

c - 如何在 FreeDOS 中链接masm 和C?

assembly - MASM 会在背后更改指令吗?

assembly - 将换行符输出到文件时出现问题

c - 将汇编语言转换为C语言代码

c - 在 C 中读取包含 long 数组的文件

c - 堆栈上的局部变量和 EBP 之间是什么?

assembly - 为什么我们必须清理堆栈

assembly - 在 8086 汇编(16 位模式)中读取堆栈