首页 > 解决方案 > 直接写入显存

问题描述

我听说 int 10h, ah = 0Ch 很慢,为了获得合理的速度,我需要进入内存并将值放入我想要的像素中,我将视频模式设置为13hwith int 10h。更改视频模式的调用:

mov ah , 0
mov al , 13h
int 10h

这是我为了在给定坐标中放置一个像素而编写的程序:

drawFaster proc
    ; bp + 2 - row (dx)
    ; bp + 4 - column (cx)
    ; bp + 6 - color
    mov bp , sp
    pushad
    mov ax , 0A000h - presumably the memory address of the screen
    mov es , ax
    mov ax , [bp + 2]
    mov bx , [bp + 4]
    mov cx , 320
    mul cx

    add ax , bx
    mov di , ax
    mov ax , [bp + 6]

    mov [es:di] , al 

    popad
    ret 6
endp

调用函数的代码:

    push bp
    mov ax , 30
    push ax
    mov cx , 50
    push cx
    mov dx , 50
    push dx
    call drawFaster
    pop bp

但由于某种原因,这不会产生任何结果,我不知道内存地址是否不正确或者是否有其他问题。我需要你的帮助。谢谢!

奇怪的是下面这段代码有效

mov ax , 0A000H
mov es , ax
mov [es:0] , 30

但以下代码没有:

mov ax , 0A000H
mov bx , 0
mov es , ax
mov [es:bx] , 30

标签: assemblymemoryx86-16vga

解决方案


我找到了一种解决方法,但它可能不是最好的方法,而不是使用es(因为它不起作用)

我将数据段的地址更改为0A000H

然后我用它改变像素, mov [di] , al它就可以工作了!

但是,如果您尝试这样做,请务必将数据段的地址移回其原始位置。

如果有人感兴趣,我的功能处于工作状态:

drawFaster proc
    ; bp + 2 - row (dx)
    ; bp + 4 - column (cx)
    ; bp + 6 - color
    mov bp , sp ; changing bp to access the stack segment
    ; pushing the values to not lose them in the process of calling the function
    push ax
    push bx
    push cx
    push dx
    mov ax, 0A000h ; 0A000h is the video memory address (for some video modes like 13h)
    mov ds , ax
    mov ax , [bp + 2] ; getting the row ( Y )
    mov bx , [bp + 4] ; getting the column ( X )
    mov cx , 320 ; multiplying the row by 320 to get the offset, and then adding the column
    mul cx

    add ax , bx
    mov di , ax
    mov ax , [bp + 6] ; getting the desired color to paint the pixel in 
    mov [di] , al ; changing the color of the chosen pixel with the desired color
    ; changing the data segment's address back to its original state 
    ; VERY CRUCIAL PLEASE DON'T MISS THIS IF YOU DO IT THE SAME WAY
    mov ax , @data
    mov ds, ax
    ; changing the values back to what they were
    pop dx
    pop cx
    pop bx
    pop ax
    ret 6
endp

推荐阅读