DosBox停止但代码中没有错误

时间:2015-04-09 12:54:48

标签: assembly x86 dos dosbox

我在程序集中编写了一个代码来上传一个bmp文件。当我运行程序时,调试器显示没有错误或警告,但DosBox停止工作。 谢谢你的帮助!

IDEAL
MODEL small
STACK 0f500h
DATASEG
filename db 'simonP.bmp',0
filehandle dw ?
Header db 54 dup (0)
Palette db 256*4 dup (0)
ScrLine db 320 dup (0)
ErrorMsg db 'Error',13,10,'$'

CODESEG

start:
    mov ax, @data
    mov ds, ax

    ;graphic mode
    mov ax, 13h
    int 10h

    call OpenFile
    call ReadHeader
    call ReadPalette
    call CopyPalette
    call CopyBitMap



proc OpenFile
        xor al,al
        mov dx, offset filename
        int 21h
        jc openError
        mov [filehandle], ax

        ret

openError:
    mov dx, offset ErrorMsg
    mov ah, 9h
    int 21h
    ret

ENDP OpenFile


proc ReadHeader
    mov ah, 3fh
    mov bx, [filehandle]
    mov cx, 54
    mov dx, offset Header
    int 21h 
    ret
ENDP ReadHeader


proc ReadPalette
    mov ah, 3fh
    mov cx, 400h
    mov dx, offset Palette
    int 21h
    ret
ENDP ReadPalette


proc CopyPalette
    mov si,offset Palette
    mov cx, 256
    mov dx, 3C8h
    mov al,0

    out dx,al

    inc dx

PalLoop:
    mov al,[si+2]
    shr al, 2

    out dx, al
    mov al, [si+1]
    shr al, 2
    out dx,al
    mov al, [si]
    shr al, 2
    out dx,al
    add si, 4

    loop PalLoop
    ret
ENDP CopyPalette


proc CopyBitMap
    mov ax, 0A000h
    mov es, ax
    mov cx, 200

PrintBMPLoop:
    push cx
    mov di, cx
    shl cx, 6
    shl di, 8
    add di, cx

;read one line
    mov ah, 3fh
    mov cx, 320
    mov si,offset ScrLine
    rep movsb   ;copy line to the screen
                ;rep movsb is the same as the following code:
                ;mov es:di, ds:si
                ;inc si
                ;inc di
                ;dec cx ----- loop until cx=0
    pop cx
    loop PrintBMPLoop
    ret
ENDP CopyBitMap



exit:
    mov ax, 4c00h
    int 21h
END start

1 个答案:

答案 0 :(得分:2)

  1. OpenFile proc错过mov ah, 3Dh来调用DOS函数。
  2. openError 代码最好不要返回调用者,而是通过Terminate函数退出到DOS。
  3. ReadHeader proc应该测试错误。
  4. ReadPalette proc未命中mov bx, [filehandle],并且还应测试错误。
  5. CopyBitMap proc忘记实际读取文件中的数据。它只复制空缓冲区200次。此外,由于一次性错误,它永远不会填满屏幕的顶部扫描线。
  6. call CopyBitMap后,您需要插入jmp exit。现在你正在进入 OpenFile proc。
  7. 我建议对 CopyBitMap proc

    进行这些更改
     ...
     cld         ;All to often forgotten!
    PrintBMPLoop:
     push cx
     dec cx      ;The famous one-off-error!
     mov di, cx
     shl cx, 6
     shl di, 8
     add di, cx
     ;read one line
     mov ah, 3fh
     mov bx, [filehandle]
     mov cx, 320
     mov dx, offset ScrLine
     int 21h
     jc ...
     mov si, dx
     rep movsb   ;copy line to the screen
     pop cx
     loop PrintBMPLoop
     ...