initial commit: a mess of assembly code
[fmap.git] / x86_64_sse2_x87 / fasm / examples / x86 / life.asm
blobd6dbb9a2572ff8c5e1f6e7bdf903dd21cda695e7
2 ; Life - fasm example program
4 ; Controls:
5 ; arrow keys - move cursor
6 ; Space - switch cell
7 ; Enter - move to next generation
8 ; Esc - exit program
10 include '80186.inc'
12 org 100h
13 jumps
15 mov di,screen_data
16 xor al,al
17 mov cx,80*50*2
18 rep stosb
20 mov ax,3
21 int 10h ; set text mode
22 mov ah,1
23 mov ch,20h
24 int 10h ; hide cursor
25 mov ax,1003h
26 xor bx,bx
27 int 10h ; enable background intensity
29 mov ax,1100h
30 mov bp,DCh_pattern
31 mov cx,1
32 mov dx,0DCh
33 mov bx,1000h
34 int 10h
36 mov ax,0B800h
37 mov es,ax
38 xor di,di
39 mov ax,0DCh
40 mov cx,80*25
41 rep stosw
43 redraw_screen:
44 mov si,[cursor_y]
45 imul si,80
46 add si,[cursor_x]
47 and byte [screen_data+si],8
48 or byte [screen_data+si],2
50 mov si,screen_data
51 xor di,di
52 mov cx,50
53 draw_screen:
54 push cx
55 mov cx,80
56 draw_line:
57 mov ah,[si+80]
58 lodsb
59 shl al,4
60 and ah,0Fh
61 or al,ah
62 inc di
63 stosb
64 loop draw_line
65 pop cx
66 add si,80
67 loop draw_screen
69 wait_for_key:
70 xor ah,ah
71 int 16h
72 cmp ah,1
73 je exit
74 cmp ah,1Ch
75 je next_generation
76 cmp ah,39h
77 je switch_cell
78 cmp ah,4Bh
79 je cursor_left
80 cmp ah,4Dh
81 je cursor_right
82 cmp ah,48h
83 je cursor_up
84 cmp ah,50h
85 je cursor_down
86 jmp wait_for_key
88 switch_cell:
89 mov si,[cursor_y]
90 imul si,80
91 add si,[cursor_x]
92 xor byte [screen_data+si],8
93 jmp redraw_screen
95 cursor_left:
96 cmp [cursor_x],1
97 jbe wait_for_key
98 call clear_cursor
99 dec [cursor_x]
100 jmp redraw_screen
101 cursor_right:
102 cmp [cursor_x],78
103 jae wait_for_key
104 call clear_cursor
105 inc [cursor_x]
106 jmp redraw_screen
107 cursor_up:
108 cmp [cursor_y],1
109 jbe wait_for_key
110 call clear_cursor
111 dec [cursor_y]
112 jmp redraw_screen
113 cursor_down:
114 cmp [cursor_y],48
115 jae wait_for_key
116 call clear_cursor
117 inc [cursor_y]
118 jmp redraw_screen
120 next_generation:
121 call clear_cursor
122 mov si,screen_data+81
123 mov di,screen_data+80*50+81
124 mov cx,48
125 process_screen:
126 push cx
127 mov cx,78
128 process_line:
129 xor bl,bl
130 mov al,[si+1]
131 and al,1
132 add bl,al
133 mov al,[si-1]
134 and al,1
135 add bl,al
136 mov al,[si+80]
137 and al,1
138 add bl,al
139 mov al,[si-80]
140 and al,1
141 add bl,al
142 mov al,[si+80+1]
143 and al,1
144 add bl,al
145 mov al,[si+80-1]
146 and al,1
147 add bl,al
148 mov al,[si-80+1]
149 and al,1
150 add bl,al
151 mov al,[si-80-1]
152 and al,1
153 add bl,al
154 mov al,byte [si]
155 mov byte [di],al
156 cmp bl,1
157 jbe clear_cell
158 cmp bl,4
159 jae clear_cell
160 cmp bl,2
161 je cell_ok
162 mov byte [di],0Fh
163 jmp cell_ok
164 clear_cell:
165 mov byte [di],0
166 cell_ok:
167 inc si
168 inc di
169 loop process_line
170 pop cx
171 add si,2
172 add di,2
173 loop process_screen
174 push es
175 push ds
176 pop es
177 mov si,screen_data+80*50
178 mov di,screen_data
179 mov cx,80*50
180 rep movsb
181 pop es
182 jmp redraw_screen
184 exit:
185 mov ax,3
186 int 10h
187 int 20h
189 clear_cursor:
190 mov si,[cursor_y]
191 imul si,80
192 add si,[cursor_x]
193 mov al,byte [screen_data+si]
194 cmp al,2
195 je empty_cell
196 mov byte [screen_data+si],0Fh
198 empty_cell:
199 mov byte [screen_data+si],0
202 cursor_x dw 40
203 cursor_y dw 25
205 DCh_pattern:
206 db 8 dup 0
207 db 8 dup 0FFh
209 screen_data rb 80*50*2