[Home]
[Edit this page]
[Recent Changes]
[Special Pages]
[Help]
x86ASMFAQ_Graphics
How do I draw to the screen?
First you need to get in graphics mode. Then to draw to the screen you need to move a byte to the framebuffer via it's memory-mapped IO ports. In an indexed mode (256 colors), you move index that is mapped to a color via the palette, in non-indexed modes you move the actual RGB values. Red is usually at the lowest offset and blue at the highest.
For a taste here's putPixel for mode 0x13 (in real-mode as it is much more common)
If you use this code you may notice something. Y grows downward. The upper-left corner of the screen is (0,0) and the lower-right is (319,199).
How do I get in graphics mode?
In a protected-mode operating system, you'll probably need to go through the OS or a special API to change the graphics mode, or in fact, do most anything graphics related.
In real-mode or raw protected-mode you should use BIOS or the VESA BIOS Extensions. See the VBE3 specification for information on how to access VBE in protected-mode and for specifics. The simplest mode to use in real-mode is mode 0x13 which can be entered with the following code...
What is the framebuffer?
The framebuffer refers to a chunk of memory on the video card that holds the state of the screen. Usually it accessible through memory-mapped IO located at physical address 0xA0000 (A000:0000 in real-mode).
What is the palette?
The palette allows you to define the actual colors that are displayed on the screen for the values that you write to the graphics screen buffer in 8-bit (256 color) or lower color depths. It is unnecessary in 15-bit+ modes. Basically, the color for any given pixel in an 8-bit or lower video mode is actually used as an index into the palette and the RGB values in the palette are sent to the VGA+ card's DAC and then on to the electron gun. The palette can be used to change colors of displayed screen objects without having to change the actual data in the video buffer. The most common effects are fade-in and -out. The palette can be accessed using VBE or OS services, or by direct access in actual real-mode or OS code. For the latter case, here's the ports used for accessing it:
If you want to read an RGB color value for any specific color index, write the index to port 3C7h and read in all three RGB values from port 3C9h. To write, write the index to port 3C8h and write three RGB values to port 3C9h. If you're writing to consecutive entries, you needn't increment the index because it will auto-increment the proper index for you after a read or write of three bytes. Note that by default the DAC width is set to 6-bits, making each read, green, and blue value in the range of 0-63 (0 to (2^6)-1). With this DAC width, 262,144 colors are possible. A VBE call can be made to expand it. Something pretty cool is that you can read and write values at the same time, so you can do things such as shifting the the entire palette in one direction (i.e. moving all RGB values up or down).
What is double buffering?
Double Buffering operates from the fact that video memory is extremely slow compared to system memory (moreso with reads than writes), so a buffer is made by the program and used as if it were truly the video memory, except that on a vertical retrace the system memory buffer is quickly copied to the true video memory. If you're interested in actually seeing the performance difference, check out two versions of a fire program I wrote: http://www.programmersheaven.com/c/MsgBoard/read.asp?Board=1&MsgID=143081&Setting=A9999F0301
How do I get into high resolution modes?
With no OS support you should use the VESA BIOS Extensions to access higher resolutions. You can find the VBE Specification at http://www.vesa.org/vbe3.pdf.
Otherwise you most likely should go through the OS for simple graphics, or a graphics library like OpenGL or DirectX for graphics-intensive programs.
[Edit this page] [Page history] [What links here] [Discuss this topic] [Printer Friendly]
x86ASMFAQ_Graphics
How do I draw to the screen?
First you need to get in graphics mode. Then to draw to the screen you need to move a byte to the framebuffer via it's memory-mapped IO ports. In an indexed mode (256 colors), you move index that is mapped to a color via the palette, in non-indexed modes you move the actual RGB values. Red is usually at the lowest offset and blue at the highest.
For a taste here's putPixel for mode 0x13 (in real-mode as it is much more common)
;ax=x, bx=y cl=color es=0xA000
putPixel:
mov di, bx ;di=bx
shl bx, 6 ;bx=bx*64
shl di, 8 ;di=di*256
add di, bx ;di=di+bx (or bx*64+bx*256 which is bx*320)
add di, ax ;di=x+y*320
mov [es:di], cl ;store the pixel
ret ;that's all
If you use this code you may notice something. Y grows downward. The upper-left corner of the screen is (0,0) and the lower-right is (319,199).
How do I get in graphics mode?
In a protected-mode operating system, you'll probably need to go through the OS or a special API to change the graphics mode, or in fact, do most anything graphics related.
In real-mode or raw protected-mode you should use BIOS or the VESA BIOS Extensions. See the VBE3 specification for information on how to access VBE in protected-mode and for specifics. The simplest mode to use in real-mode is mode 0x13 which can be entered with the following code...
;to enter mode 0x13
mov ax, 0x13
int 0x10
;to leave mode 0x13
mov ax, 3
int 0x10
What is the framebuffer?
The framebuffer refers to a chunk of memory on the video card that holds the state of the screen. Usually it accessible through memory-mapped IO located at physical address 0xA0000 (A000:0000 in real-mode).
What is the palette?
The palette allows you to define the actual colors that are displayed on the screen for the values that you write to the graphics screen buffer in 8-bit (256 color) or lower color depths. It is unnecessary in 15-bit+ modes. Basically, the color for any given pixel in an 8-bit or lower video mode is actually used as an index into the palette and the RGB values in the palette are sent to the VGA+ card's DAC and then on to the electron gun. The palette can be used to change colors of displayed screen objects without having to change the actual data in the video buffer. The most common effects are fade-in and -out. The palette can be accessed using VBE or OS services, or by direct access in actual real-mode or OS code. For the latter case, here's the ports used for accessing it:
Port# | R/W | Description 3C7h | W | Read index 3C8h | W | Write index 3C9h | R/W | Data port
If you want to read an RGB color value for any specific color index, write the index to port 3C7h and read in all three RGB values from port 3C9h. To write, write the index to port 3C8h and write three RGB values to port 3C9h. If you're writing to consecutive entries, you needn't increment the index because it will auto-increment the proper index for you after a read or write of three bytes. Note that by default the DAC width is set to 6-bits, making each read, green, and blue value in the range of 0-63 (0 to (2^6)-1). With this DAC width, 262,144 colors are possible. A VBE call can be made to expand it. Something pretty cool is that you can read and write values at the same time, so you can do things such as shifting the the entire palette in one direction (i.e. moving all RGB values up or down).
What is double buffering?
Double Buffering operates from the fact that video memory is extremely slow compared to system memory (moreso with reads than writes), so a buffer is made by the program and used as if it were truly the video memory, except that on a vertical retrace the system memory buffer is quickly copied to the true video memory. If you're interested in actually seeing the performance difference, check out two versions of a fire program I wrote: http://www.programmersheaven.com/c/MsgBoard/read.asp?Board=1&MsgID=143081&Setting=A9999F0301
How do I get into high resolution modes?
With no OS support you should use the VESA BIOS Extensions to access higher resolutions. You can find the VBE Specification at http://www.vesa.org/vbe3.pdf.
Otherwise you most likely should go through the OS for simple graphics, or a graphics library like OpenGL or DirectX for graphics-intensive programs.
[Edit this page] [Page history] [What links here] [Discuss this topic] [Printer Friendly]
