Using VGA Graphics in Assembly Language

Nowadays, most every computer’s BIOS implements a bunch of legacy VGA modes, which, if called from any modern operating system, will only cause a rude message to be displayed. Makes you think.

1. If necessary, download and install FASM and QEMU, as described in previous tutorials.

2. In any convenient location, create a new directory named “GraphicsTest”.

3. In the newly created GraphicsTest directory, create a new text file named “GraphicsTest.asm”, containing the following text.

use16       ; 16-bit mode

org 0x7C00 ; address of the boot sector

    mov ah,0x00 ; reset disk
    mov dl,0    ; drive number
    int 0x13    ; call BIOS interrupt routine
    ; load sectors from disk using BIOS interrupt 0x13
    mov ah,0x02 ; read sectors into memory
    mov al,0x10 ; number of sectors to read (more than we need)
    mov dl,0    ; drive number
    mov ch,0    ; cylinder number
    mov dh,0    ; head number
    mov cl,2    ; starting sector number
    mov bx,Main ; memory location to load to 
    int 0x13    ; call BIOS interrupt routine
    jmp Main    ; now that it's been loaded

    ; pad out all but the last two bytes of the sector with zeroes
    times ((0x200 - 2) - ($ - $$)) db 0x00

    dw 0xAA55 ; these must be the last two bytes in the boot sector


	; set mode to VGA 13h and draw the default palette
	call DisplayModeSet13h
	mov ax,0 		; pixel x
	mov bx,0 		; pixel y
	mov cx,[NumberOfColors]
		push ax			; pixel x
		push bx			; pixel y
		mov dx,[NumberOfColors]
		sub dx,cx
		push dx			; pixel color index
		call DisplayPixelDrawXY	
		inc ax
		cmp ax,[ColorsPerRow]
		jb EndIfNewRowNeeded
			mov ax,0
			inc bx
	loop DrawEveryColorInPalette
	NumberOfColors: dw 0x0100
	ColorsPerRow: dw 0x0010


	mov ax,0x13 ; VGA mode 0x13, 320 x 200, 256 color
	int 0x10

	; (posX, posY, color)
	push bp
	mov bp,sp
	push ax
	push bx
	push cx
	push dx
	mov ah,0x0C 	; for int 0x10: draw a pixel
	mov al,[bp+4] 	; color
	mov bh,0x00 	; page number
	mov cx,[bp+8]	; x pos
	mov dx,[bp+6]	; y pos
	int 0x10	; BIOS interrupt 0x10
	pop dx
	pop cx
	pop bx
	pop ax
	pop bp
	ret 6

    times (0x2000 - ($ - $$)) db 0x00

4. Copy the files “BuildEnvironmentSteup-FasmQemu.bat” and “ProgramBuildAndRun-FasmQemu.bat” to the GraphicsTest directory. These files were created in a previous tutorial.

5. Double-click the icon for ProgramBuildAndRun.FasmQemu.bat to run it. A console window will appear, FASM will assemble the code in the GraphicsTest.asm file into GraphicsTest.img. QEMU will then attempt to boot from GraphicsTest.img. A square containing all the colors of the default palette for VGA mode 13h should be visible in the QEMU window.

This entry was posted in Uncategorized and tagged , , , . Bookmark the permalink.

2 Responses to Using VGA Graphics in Assembly Language

  1. Kiran C says:

    This is a wonderful blog for assembly programmers, I want to display a bitmap using assembly language, please reply if it is possible and if at all you have the code for the same please send .

    • Do you mean you want to display a Windows “BMP” in assembly. It’s certainly possible, of course. But I don’t have the code already written for it or anything. You might get started by taking a look at the Java code at “”, converting that into assembly, and combining the result with this post.

Leave a Reply

Fill in your details below or click an icon to log in: Logo

You are commenting using your account. Log Out /  Change )

Google+ photo

You are commenting using your Google+ account. Log Out /  Change )

Twitter picture

You are commenting using your Twitter account. Log Out /  Change )

Facebook photo

You are commenting using your Facebook account. Log Out /  Change )


Connecting to %s