2015-12-03 61 views
2

這裏的問題是我必須在MASM中查找斐波那契數列達到給定的限制。我必須從文件讀取該限制。例如,存儲在名爲「 Test.txt的」。無法從MASM文件中讀取一個數字

這裏是我的代碼

.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 

在這裏的錯誤我m到處是一些垃圾值顯示出來,當我打印,我從文件中讀取的值。

注意:我還沒有嘗試打印斐波那契數列,因爲我無法從文件中讀取數字。

+2

爲什麼這個問題擱置,而它已經有了答案? – Midhun0407

回答

2

您的代碼在文件已打開的位置看起來一切正常。
從這裏不清楚你想要做什麼。您將指針pos定義爲100字節的緩衝區,但我不明白爲什麼!它不應該只是一個包含剛剛打開的文件中的偏移量的雙字?

不管怎樣,下一行沒有使用此功能的DOS像它應該:

mov al,00h 
mov dx,offset pointerpos 
mov ah,42h 
int 21h 
jc failed3 

尋求函數需要你傳遞一個文件CX偏移:DX。
如果例如您的示例值被放置在文件的第一個字節中,然後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 

l5: 
mov dx,0 
div bx 
inc cx 
push dx 
cmp ax,0 
jne l5 
+0

感謝先生這工作。問題是真正的顯示常規。查找功能是一個嘗試,但它沒有工作。事實上,它不是沒有它需要的程序很好。 – Midhun0407