http://blog.csdn.net/visioncat/archive/2005/01/12/250431.aspx
小谈汇编程序中对VGA显存直接操作
最近闲来无聊,找了本80x86的汇编教程学习学习。在对显存直接操作的时候,发现书上说的很不清楚,
还没有说清楚呢,就开始例子了。(感觉像我们蹩脚的英语老师,说虚拟语气会说到不定式)所以,下面我
就来说一个简单的使用例子。希望各位能对各位有所启发。
首先,看一下图形控制器,它的端口是3CEH只要对这个端口传送参数,我们就可以将图形控制器9个寄存
器中的一个设置为当前操作的寄存器。然后通过3CFH传送参数给你刚才选择的寄存器。
其次,我所说的操作是对于VGA的12H模式也就是640×480 16色。
最后,我所用的汇编器是NASM,大家可以到
www.sf.net上找到。
##################################################
寄存器的索引如下
寄存器名称 索引
置位/重置寄存器 0
允许置位/重置寄存器 1
颜色比较寄存器 2
数据循环/功能寄存器 3
读映像选择寄存器 4
模式选择寄存器 5
杂用 6
颜色无关寄存器 7
位屏蔽寄存器 8
关于寄存器的具体功能,在这里不做介绍。大家可以看看《IBM-PC汇编语言程序设计》第二版的10.3。
关于读模式,我看了看《IBM-PC汇编语言程序设计》上面说的还比较清楚。我在这里简单介绍一下写模式
。
通过对3CFH端口进行数据传送,可以对所选寄存器操作。
###################################################
写模式的种类
写模式有4种,其中0模式为默认模式,模式2是模式0的简化版本。模式3是VGA独有的。感觉和模式0差不
多。模式1是对位面的直接操作。
###################################################
操作过程
0 将屏幕设置为 12H 模式
1 通过端口对寄存器进行设置
2 对段地址为0a000h的内存进行写操作。
###################################################
简单的例子
;---------------------------------------------------
;文件:test.asm
;介绍:一个简单的对VGA写的例子。
;现象:在屏幕上出现绿黑相间的竖直条纹。
;语言:汇编语言,使用nasm汇编器
;作者:北斗星君
;邮箱:huangxiangkui@163.com
;---------------------------------------------------
;通过BIOS设置VGA模式
mov ah,00h
mov al,12h
int 10h
;以下对VGA控制器设置
;将VGA控制器,设置为写模式2
mov dx,3ceh
mov al,5
out dx,al ;选择 模式选择寄存器 为当前可用寄存器
mov dx,3cfh
mov al,2
out dx,al ;将模式2设为当前写模式
;对位屏蔽寄存器设置,使传送给0a000h的八个象素中设位1的象素为有效。
mov dx,3ceh
mov al,8
out dx,al ;选择 位屏蔽寄存器 为当前可用寄存器
mov dx,3cfh
mov al,11110000b ;让八个象素前四个有效,后四个无效
out dx,al
;对0a000h地址进行操作
;设置初始参数
mov ax,0a000h ;段地址
mov es,ax
mov bx,00000h ;起始偏移
mov al,0ah ;绿色
mov cx,0ffffh ;循环次数
;对内存循环操作写入
jmp re
re:
mov ,al
inc bx
loop re
上面是一个简单的写入程序,我这里注释的应该相当清楚了。如果有什么问题,我非常欢迎来大家探讨。
上面的代码用nasm编译通过。由于Win32下不能操作BIOS中断。所以,我加了些代码让它在Bochs下运行。
一切正常。
如果有任何问题,可以询问。
STUDIO软件开发组(SDT)
STUDIO Development Team
北斗星君(黄庠魁)
Trackback:
http://tb.blog.csdn.net/TrackBack.aspx?PostId=250431
### Talk about Direct Operations on VGA Video Memory in Assembly Programs
Recently, I've been bored and found an 80x86 assembly tutorial to study. When directly operating on video memory, I found that the book was very unclear. Before making it clear, it started with examples. (It feels like our awkward English teacher who would start talking about subjunctive mood and then jump to infinitives) So, below I'll present a simple example of use. I hope it can inspire everyone.
First, take a look at the graphics controller. Its port is 3CEH. As long as you send parameters to this port, you can set one of the 9 registers of the graphics controller as the currently operated register. Then send parameters to the register you just selected through port 3CFH.
Second, the operation I'm talking about is for VGA mode 12H, which is 640×480 16 colors.
Finally, the assembler I used is NASM. You can find it on
www.sf.net.
##################################################
The indices of the registers are as follows
Register Name Index
Set/Reset Register 0
Enable Set/Reset Register 1
Color Compare Register 2
Data Rotate/Function Register 3
Read Map Select Register 4
Mode Select Register 5
Miscellaneous 6
Color Don't Care Register 7
Bit Mask Register 8
The specific functions of the registers won't be introduced here. Everyone can refer to "IBM-PC Assembly Language Programming" second edition, section 10.3. Regarding the read mode, what's said in "IBM-PC Assembly Language Programming" is relatively clear. I'll briefly introduce the write mode here.
By sending data to port 3CFH, you can operate on the selected register.
###################################################
Types of Write Modes
There are 4 types of write modes. Mode 0 is the default mode. Mode 2 is a simplified version of mode 0. Mode 3 is unique to VGA. It feels similar to mode 0. Mode 1 is for direct operation on bit planes.
###################################################
Operation Process
0 Set the screen to mode 12H
1 Set the registers through ports
2 Perform write operations on memory with segment address 0A000H.
###################################################
Simple Example
;---------------------------------------------------
;File: test.asm
;Introduction: A simple example of writing to VGA.
;Phenomenon: Vertical stripes in green and black appear on the screen.
;Language: Assembly language, using NASM assembler
;Author: Beidou Xingjun
;Email: huangxiangkui@163.com
;---------------------------------------------------
; Set VGA mode through BIOS
mov ah, 00h
mov al, 12h
int 10h
; The following sets up the VGA controller
; Set the VGA controller to write mode 2
mov dx, 3ceh
mov al, 5
out dx, al ; Select the Mode Select Register as the currently available register
mov dx, 3cfh
mov al, 2
out dx, al ; Set mode 2 as the current write mode
; Set the Bit Mask Register to make the pixels with bit 1 set among the eight pixels sent to 0A000H valid.
mov dx, 3ceh
mov al, 8
out dx, al ; Select the Bit Mask Register as the currently available register
mov dx, 3cfh
mov al, 11110000b ; Make the first four of the eight pixels valid and the last four invalid
out dx, al
; Operate on address 0A000H
; Set initial parameters
mov ax, 0A000h ; Segment address
mov es, ax
mov bx, 00000h ; Starting offset
mov al, 0Ah ; Green color
mov cx, 0FFFFh ; Loop count
; Loop and write to memory
jmp re
re:
mov , al
inc bx
loop re
The above is a simple writing program. The comments here should be quite clear. If there are any questions, I'm very welcome to discuss with everyone.
The above code has been compiled successfully with NASM. Since Win32 can't operate BIOS interrupts, I added some code to make it run under Bochs. Everything is normal.
If there are any questions, you can ask.
STUDIO Software Development Group (SDT)
STUDIO Development Team
Beidou Xingjun (Huang Xiangkui)
Trackback:
http://tb.blog.csdn.net/TrackBack.aspx?PostId=250431