直接写入显存

writing directly to video memory

我听说 int 10h, ah = 0Ch 很慢,为了获得合理的速度,我需要转到内存并将值放入我想要的像素中,我将视频模式设置为13hint 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

我找到了一个解决方法,但这可能不是最好的方法,而不是使用 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