读取第二个 BMP 文件时汇编程序崩溃

Assembly program crashes when reading second BMP file

我有一个从位图打印到屏幕的程序,问题是它成功打印了第一个文件,但没有打印第二个文件,即使我交换 BMP 文件,第一个文件也总是打印但不打印第二个

我在调试器中发现它在 readHeaderBMP 的 int 21h 上崩溃了,虽然不知道为什么

主要内容:

        mov [BMPX],0
        mov [BMPY],0
        mov [BMPHeight], 200
        mov [BMPWidth], 320
        push offset menu
        call openFile
        push bp
        call createBMP
        pop bp

        mov [BMPX],120
        mov [BMPY],68
        mov [BMPHeight], 25
        mov [BMPWidth], 80
        push offset menuSelect
        call openFile
        push bp
        call createBMP
        pop bp

文件管理:

          proc openFile
        push bp
        mov bp, sp
        mov dx, [bp + 4]
        xor ax, ax
        mov ah, 3Dh
        int 21h
        mov [fileHandle], ax
        pop bp
        ret 2
    endp openFile

    proc readHeaderBMP
        xor ax, ax
        mov ah, 3Fh
        mov bx, [fileHandle]
        mov cx, 54
        lea dx, [BMPHeader]
        int 21h
        mov [Error], ax
        ret
    endp readHeaderBMP

    proc readPaletteBMP
        mov ah, 3Fh
        mov cx, 400h
        lea dx, [BMPPalette]
        int 21h
        ret
    endp readPaletteBMP

    proc copyPalleteVRAM
        lea si, [BMPPalette]
        mov cx, 256
        mov dx, 3C8h
        xor al, al
        out dx, al
        inc dx
        copyPalleteVRAM_convert:
            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 copyPalleteVRAM_convert
        ret
    endp copyPalleteVRAM

    proc showBMP
        push cx
        mov ax, 0A000h
        mov es, ax
        mov cx, [BMPHeight]

        mov ax, [BMPWidth] 
        xor dx, dx
        mov si, 4
        div si
        mov bp, dx

        mov dx, [BMPX]

        showBMP_nextLine:
            push cx
            push dx

            mov di, cx  
            add di, [BMPY] 



            mov cx, di
            shl cx, 6
            shl di, 8
            add di, cx
            add di, dx


            mov ah, 3fh
            mov cx, [BMPWidth]  
            add cx, bp  
            mov dx, offset BMPMaxLine
            int 21h

            mov cx, [BMPWidth]  
            dec cx
            mov si, offset BMPMaxLine
            showBMP_nextLine_movsbLoop:
                push cx
                mov cx, [ds:si]
                mov [es:di], cx
                inc si
                inc di
                pop cx
            loop showBMP_nextLine_movsbLoop

            pop dx
            pop cx

        loop showBMP_nextLine

        pop cx
        ret
    endp showBMP

    proc closeFile
        mov ah, 3Eh
        mov bx, offset FileHandle
        int 21h
        ret
    endp closeFile

    proc createBMP
        call readHeaderBMP
        call readPaletteBMP
        call copyPalleteVRAM
        call showBMP
        call closeFile
        ret
    endp createBMP

和一些数据:

RESET               equ 0
MAX_WIDTH           equ 320
AMOUNT_OF_COLORS    equ 256
HEADER_SIZE         equ 54
COLOR_SIZE          equ 4

    BMPHeader       db HEADER_SIZE dup (RESET)
    BMPPalette      db AMOUNT_OF_COLORS * COLOR_SIZE dup (RESET)
    BMPX            dw ?
    BMPY            dw ?
    BMPWidth        dw ?
    BMPHeight       dw ?
    BMPMaxLine      db MAX_WIDTH dup (RESET)

    menu            db "menu.bmp", RESET
    menuSelect      db "msl.bmp", RESET

    fileHandle      dw ?

我找到了解决办法,我在turbo debugger中检查了怎么回事,然后我看到在文件相关程序中,在关闭文件中的int 21h行,而不是设置cf = 0,ax = 0,行 set cf = 1, ax 06h,这意味着关闭文件时出错,问题是我将 bx 设置为不是 FileHandler,而是 FileHandler

的地址
    proc closeFile
        mov ah, 3Eh
        mov bx, offset FileHandle ;THIS LINE IS NOT CORRECT
        int 21h                   ;THIS LINE RETURNED AN ERROR
        ret
    endp closeFile

应该是mov bx, [FileHandle]

我经常遇到这种情况,尤其是在 C 语言中,即使我了解计算机体系结构,但我总是在编码时遇到问题而不注意它。