Prev Intro Next   Home Email me

Tutorial 6 - Some Basic Graphics

Not many application programs are written in assembler entirely these days. It is most usual to find the assembler code embedded in code in a high level language, such as C++ or Pascal. (if I use assembler at all these days, I use it embedded inside programs for Turbo Pascal). The principal reason for using assembler is because of the increased speed of execution which it gives, and one area where this speed is most appreciated is in the area of computer graphics.

This single tutorial will cover how to set up a graphics mode, how to return to text mode, and also how to place a single pixel on the screen. This can all be done using various interupts, but for faster pixel plotting, it is best to use direct memory accesses. The trouble with this is that each different screen resolution requires a different method of plotting pixels.

Switching Screen Modes

The interrupt used for switching between screen modes, and for all graphics work is interrupt 10h. Subfunction 0 of this interrupt sets the screen mode, depending upon the value of the number in the AL register. A list of the basic graphics modes are given below.

Mode
Type
Text Res
Graphics Res
Colours
Mode
Type
Text Res
Graphics Res
Colours
0
text
25 x 40
320 x 200
16
13
graphics
25 x 40
320 x 200
16
1
text
25 x 40
320 x 200
16
14
graphics
25 x 80
640 x 200
16
2
text
25 x 80
640 x 200
16
15
graphics
25 x 80
640 x 350
mono
3
text
25 x 80
640 x 200
16
16
graphics
25 x 80
640 x 350
16
4
graphics
25 x 40
320 x 200
4
17
graphics
30 x 80
640 x 480
mono
5
graphics
25 x 40
320 x 200
4
18
graphics
30 x 80
640 x 480
16
6
graphics
25 x 80
640 x 200
mono
19
graphics
25 x 40
320 x 200
256

When writing programs which use graphics, one should remember to return the display to text mode just before the program finishes. Mode 3 is a standard mode, which is appropriate for most programs to switch to before ending. The following is a stub of code which switches the display to graphics modes (640 x 480 x 16) and then back to text mode again before ending.

;=========================================
; Basic program to change graphics modes
;=========================================
mov ah,00			;subfunction 0
mov al,18			;select mode 18 (or 12h if prefer)
int 10h				;call graphics interrupt
;==== Graphics code here ====
mov ah,00			;again subfunc 0
mov al,03			;text mode 3
int 10h				;call int
mov ah,04ch
mov al,00			;end program normally
int 21h
 

Displaying and Reading Back Pixels - Simply

The displaying and reading back of pixels on the screen is again simply done using interrupts. The interrupt in question is again int 10h, this time subfunctions 0Ch and 0Dh or decimal 12 and 13. The first of these displays a pixel on the screen at any resolution (provided a graphics mode) at the co-ordinates specified by the values in the cx and dx registers. The colour value is specified in the al register. The second function reads the value of the pixel in memory again given by cx and dx, except this time it returns the colour in al. Below is a sample program which will display a square in blue in the middle of the screen.

jmp start
;=========================================
; Basic program to draw a rectangle
;=========================================
  mode 		db	18	;640 x 480
  x_start	dw	100
  y_start	dw	100
  x_end		dw	540
  y_end		dw	380
  colour	db	1	;1=blue
;=========================================
start:
  mov ah,00			;subfunction 0
  mov al,mode			;select mode 18 (or 12h if prefer)
  int 10h			;call graphics interrupt
;==========================
  mov al,colour			;colour goes in al
  mov ah,0ch
  mov cx, x_start		;start drawing lines along x
drawhoriz:
  mov dx, y_end			;put point at bottom
  int 10h
  mov dx, y_start		;put point on top
  int 10h
  inc cx			;move to next point
  cmp cx, x_end			;but check to see if its end
  jnz drawhoriz
drawvert:			;(y value is already y_start)
  mov cx, x_start		;plot on left side
  int 10h
  mov cx, x_end			;plot on right side
  int 10h
  inc dx			;move down to next point
  cmp dx, y_end			;check for end
  jnz drawvert
;==========================
readkey:
  mov ah,00
  int 16h			;wait for keypress
;==========================
end:
  mov ah,00			;again subfunc 0
  mov al,03			;text mode 3
  int 10h			;call int
  mov ah,04ch
  mov al,00			;end program normally
  int 21h
 
For more graphics click HERE

Prev Intro Next   Home Email me