### 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