如何在8086汇编语言中打印精灵?代码无显示问题求助
8086汇编打印精灵无显示问题排查
我尝试在8086汇编语言中打印精灵,已编写如下代码,但运行后没有任何显示效果,请求协助排查问题。
.model small .stack 100h .data sprite DB 00h,00h,00h,00h,00h,00h,00h,00h,0Fh,00h,00h,00h,00h,00h,00h ; 0 DB 0Fh,0Fh,0Fh,00h,00h,00h,00h,00h,0Fh,0Fh,0Fh,07h,00h,00h,00h ; 1 DB 00h,0Fh,0Fh,07h,07h,07h,00h,00h,00h,00h,0Fh,07h,07h,07h,07h ; 2 DB 00h,00h,00h,0Fh,0Fh,07h,07h,07h,07h,00h,00h,00h,0Fh,0Fh,07h ; 3 DB 07h,07h,07h,00h,00h,00h,0Fh,0Fh,07h,07h,07h,07h,00h,00h,00h ; 4 DB 0Fh,0Fh,07h,07h,08h,08h,00h,00h,00h,0Fh,0Fh,07h,07h,08h,08h ; 5 DB 00h,00h,00h,00h,0Fh,07h,07h,07h,07h,00h,00h,00h,00h,0Fh,07h ; 6 DB 07h,07h,07h,00h,00h,00h,07h,07h,0Fh,07h,07h,07h,00h,00h,07h ; 7 DB 07h,07h,07h,0Fh,0Fh,07h,00h,00h,07h,07h,00h,07h,07h,07h,0Fh ; 8 DB 00h,00h,07h,07h,00h,07h,07h,07h,07h,00h,00h,00h,00h,00h,07h ; 9 DB 07h,07h,07h,00h,00h,00h,00h,00h,07h,07h,07h,07h,00h,00h,00h ; 10 DB 00h,00h,00h,07h,07h,07h,00h,00h,00h,00h,00h,00h,07h,07h,00h ; 11 counter1 db ? counter2 db ? bullet_pos_x db 10 .code main proc mov ax, @data mov ds, ax mov si, 0 mov counter1, 0 mov counter2, 0 ; Initialize CX and DX for sprite positioning mov cx, 100 ; Set initial X position within the screen (between 0 and 319) mov dx, 50 ; Set initial Y position within the screen (between 0 and 199) drawOuter6: drawInner6: mov ah, 0ch ; Set AH to 0Ch for plot pixel function mov al, sprite[si] ; Load pixel color into AL int 10h ; Plot the pixel inc cx ; Increment column counter inc counter2 ; Increment inner loop counter inc si ; Move to next pixel in sprite cmp counter2, 9 ; Check inner loop counter jl drawInner6 ; Repeat inner loop if less than 9 pixels mov counter2, 0 ; Reset inner loop counter mov cl, bullet_pos_x ; Load bullet_pos_x into the low byte of CX inc dx ; Increment row counter inc counter1 ; Increment outer loop counter cmp counter1, 20 ; Check outer loop counter jl drawOuter6 ; Repeat outer loop if less than 20 rows end_print: ; Exit to DOS mov ax, 4C00h int 21h main endp end main
问题排查与修复建议
- 未进入图形模式:int 10h的0Ch画点功能仅在图形模式下有效,代码默认处于文本模式,导致画点指令完全无效。需先调用int 10h设置图形模式,比如常用的320x200 256色模式(AH=00h, AL=13h)。
- 循环与精灵数据不匹配:sprite仅定义了12行数据,但外层循环要执行20次,会越界读取内存中的无效数据;同时每行有15个像素数据,内层循环只绘制9个,丢失了精灵的大部分细节。需将外层循环次数改为12,内层循环次数改为15。
- CX寄存器重置错误:外层循环中仅用
mov cl, bullet_pos_x重置X坐标,CH寄存器保留了之前的数值,导致实际X坐标为CH*256 + bullet_pos_x,超出320的屏幕宽度范围,所有像素都画到了屏幕外。需用mov cx, bullet_pos_x直接重置整个CX寄存器(因为bullet_pos_x是8位,CH会自动清0)。 - 颜色值与背景冲突:sprite中大量使用00h(黑色),如果图形模式默认背景为黑色,这些像素会无法分辨;建议跳过黑色像素绘制,或调整精灵颜色值。
修改后的示例代码
.model small .stack 100h .data sprite DB 00h,00h,00h,00h,00h,00h,00h,00h,0Fh,00h,00h,00h,00h,00h,00h ; 0 DB 0Fh,0Fh,0Fh,00h,00h,00h,00h,00h,0Fh,0Fh,0Fh,07h,00h,00h,00h ; 1 DB 00h,0Fh,0Fh,07h,07h,07h,00h,00h,00h,00h,0Fh,07h,07h,07h,07h ; 2 DB 00h,00h,00h,0Fh,0Fh,07h,07h,07h,07h,00h,00h,00h,0Fh,0Fh,07h ; 3 DB 07h,07h,07h,00h,00h,00h,0Fh,0Fh,07h,07h,07h,07h,00h,00h,00h ; 4 DB 0Fh,0Fh,07h,07h,08h,08h,00h,00h,00h,0Fh,0Fh,07h,07h,08h,08h ; 5 DB 00h,00h,00h,00h,0Fh,07h,07h,07h,07h,00h,00h,00h,00h,0Fh,07h ; 6 DB 07h,07h,07h,00h,00h,00h,07h,07h,0Fh,07h,07h,07h,00h,00h,07h ; 7 DB 07h,07h,07h,0Fh,0Fh,07h,00h,00h,07h,07h,00h,07h,07h,07h,0Fh ; 8 DB 00h,00h,07h,07h,00h,07h,07h,07h,07h,00h,00h,00h,00h,00h,07h ; 9 DB 07h,07h,07h,00h,00h,00h,00h,00h,07h,07h,07h,07h,00h,00h,00h ; 10 DB 00h,00h,00h,07h,07h,07h,00h,00h,00h,00h,00h,00h,07h,07h,00h ; 11 counter1 db ? counter2 db ? bullet_pos_x db 10 .code main proc mov ax, @data mov ds, ax ; 设置320x200 256色图形模式 mov ah, 00h mov al, 13h int 10h mov si, 0 mov counter1, 0 mov counter2, 0 ; 初始化精灵起始位置 mov cx, bullet_pos_x ; X坐标 mov dx, 50 ; Y坐标 drawOuter6: drawInner6: mov ah, 0ch ; 画点功能 mov al, sprite[si] ; 取像素颜色 ; 跳过黑色像素,避免和背景重叠 cmp al, 00h je skip_pixel int 10h ; 绘制像素 skip_pixel: inc cx ; X坐标+1 inc counter2 ; 内层计数器+1 inc si ; 下一个精灵像素 cmp counter2, 15 ; 每行15个像素 jl drawInner6 ; 未画完继续 mov counter2, 0 ; 重置内层计数器 mov cx, bullet_pos_x ; 重置X坐标 inc dx ; Y坐标+1 inc counter1 ; 外层计数器+1 cmp counter1, 12 ; 共12行精灵数据 jl drawOuter6 ; 未画完继续 ; 暂停查看结果(按任意键退出) mov ah, 00h int 16h ; 恢复文本模式 mov ah, 00h mov al, 03h int 10h ; 退出DOS mov ax, 4C00h int 21h main endp end main
内容的提问来源于stack exchange,提问作者umaima hashmi
相关产品推荐
相关产品推荐

