1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
|
;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
;; __ __ _______ __ _ _______ __ __ _______ __ _ ;;
;; | | | || _ || | | || || |_| || _ || | | | ;;
;; | |_| || |_| || |_| || ___|| || |_| || |_| | ;;
;; | || || || | __ | || || | ;;
;; | || || _ || || || || || _ | ;;
;; | _ || _ || | | || |_| || ||_|| || _ || | | | ;;
;; |__| |__||__| |__||_| |__||_______||_| |_||__| |__||_| |__| ;;
;; ;;
;; ;;
;; HANGMAN - An implementation of the Hang Man game in assembly (Emu8086) ;;
;; ;;
;; Copyright (C) 2011 Fabien LOISON ;;
;; Copyright (C) 2011 Mathilde BOUTIGNY ;;
;; Copyright (C) 2011 Vincent PEYROUSE ;;
;; Copyright (C) 2011 Germain CARRÉ ;;
;; Copyright (C) 2011 Matthis FRENAY ;;
;; ;;
;; HangMan is free software: you can redistribute it and/or modify ;;
;; it under the terms of the GNU General Public License as published by ;;
;; the Free Software Foundation, either version 3 of the License, or ;;
;; (at your option) any later version. ;;
;; ;;
;; This program is distributed in the hope that it will be useful, ;;
;; but WITHOUT ANY WARRANTY; without even the implied warranty of ;;
;; MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the ;;
;; GNU General Public License for more details. ;;
;; ;;
;; You should have received a copy of the GNU General Public License ;;
;; along with this program. If not, see <http://www.gnu.org/licenses/>. ;;
;; ;;
;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
;;
;; Contains the functions used everywhere in the program.
;;
;; Index:
;; _draw_ui() -- Draws the user interface on the screen.
;; _clear_working() -- Clears the working part of the screen.
;; _print_header() -- Prints the HANGMAN logo on the screen.
;; _print_help(HELP_STR) -- Prints the help message on the bottom of
;; the screen.
;; _move_cursor(POS_X, POS_Y) -- Moves the cursor on the screen to
;; (POS_X, POS_Y).
;; _input_letter() -- Waits for input and returns an uppercase
;; letter, KB_ESC, KB_BKSP or KB_ENTER.
;; _clear_screen() -- Clears the screen.
;; _memcpy(MEMCPY_SRC, -- Copy bytes to an other place in the
;; MEMCPY_DEST, memory.
;; MEMCPY_LEN)
;; _strlen(STRLEN_STR) -- Counts the number of bytes that composes
;; a string.
;; _inttostr(I2S_INT) -- Convert integers into strings.
;; _input_field(IF_MSG, -- Display an input field.
;; IF_MAXLEN,
;; IF_EWD)
;;
KB_ESC equ 0x1B
KB_BKSP equ 0x08
KB_ENTER equ 0x0D
;============================================================= _draw_ui() ====
;; Draws the user interface on the screen.
;; The UI looks like that:
;; +------------------------------------+
;; | |
;; | H A N G M A N |
;; | |
;; +------------------------------------+
;; | |
;; | |
;; | |
;; | Menu/Game/Animation |
;; | |
;; | |
;; | |
;; +------------------------------------+
;; | Informations/help |
;; +------------------------------------+
;; Usage:
;; call _draw_ui
_draw_ui:
;Backup registers
push ax
push bx
push cx
push dx
;Draw the header and help parts
mov ah, 0x07
mov al, 0 ; Clear
mov bh, COLOR_HEADER ; Color
mov cx, 0 ; (0,0) +-----------+
mov dh, ROWS ; | |
mov dl, COLS ; +-----------+ (COLS,ROWS)
int 0x10
;Draw the working part
mov ah, 0x07
mov al, 0 ; Clear
mov bh, COLOR_ACTIVE ; Color
mov ch, header_height+1 ; (0,header_height+1) +-----------+
mov cl, 0 ; | |
mov dh, ROWS-2 ; | |
mov dl, COLS ; +-----------+ (COLS,ROWS-2)
int 0x10
;Draw the header
call _print_header
;Restore registers
pop dx
pop cx
pop bx
pop ax
ret
;======================================================= _clear_working() ====
;; Clears the working part of the screen.
;; +------------------------------------+
;; | |
;; | H A N G M A N |
;; | |
;; +------------------------------------+
;; | | \
;; | | |
;; | | | Clear
;; | Menu/Game/Animation | | that
;; | | | part
;; | | |
;; | | /
;; +------------------------------------+
;; | Informations/help |
;; +------------------------------------+
;; Usage:
;; call _clear_working
_clear_working:
;Backup registers
push ax
push bx
push cx
push dx
;Clear the working part
mov ah, 0x07
mov al, 0 ; Clear
mov bh, COLOR_ACTIVE ; Color
mov ch, header_height+1 ; (0,header_height+1) +-----------+
mov cl, 0 ; | |
mov dh, ROWS-2 ; | |
mov dl, COLS ; +-----------+ (COLS,ROWS-2)
int 0x10
;Restore registers
pop dx
pop cx
pop bx
pop ax
ret
;======================================================== _print_header() ====
;; Prints the HANGMAN logo on the screen
;; Usage:
;; call _print_header
_print_header:
mov POS_X, (COLS-header_len)/2
mov POS_Y, 0
mov ah, 0x09
mov dx, offset header
;Header
header_loop:
call _move_cursor
int 0x21
inc POS_Y
add dx, header_len
cmp POS_Y, header_height
jne header_loop
ret
;================================================== _print_help(HELP_STR) ====
;; Prints the help message on the bottom of the screen.
;; Usage:
;; mov HELP_STR, offset <string_label>
;; call _print_help
;; Function arg:
HELP_STR dw 0 ; The address of the help string to print, the string must end
; with a '$' char and its length can't be higher than 78 chars;
_print_help:
;Backup registers
push ax
push dx
;Move the cursor at the bottom of the screen
mov POS_X, 1
mov POS_Y, ROWS-1
call _move_cursor
;Print the text
mov ah, 0x09
mov dx, HELP_STR
int 0x21
;Restore registers
pop dx
pop ax
ret
;============================================= _move_cursor(POS_X, POS_Y) ====
;; Moves the cursor on the screen to (POS_X, POS_Y).
;; Usage:
;; mov POS_X, <X_POS>
;; mov POS_Y, <Y_POS>
;; call _move_cursor
;; Function args
POS_X db 0 ; The x position of the cursor
POS_Y db 0 ; The y position of the cursor
_move_cursor:
;Backup registers
push ax
push bx
push dx
;Move the cursor
mov ah, 0x02
mov dh, POS_Y ; Row
mov dl, POS_X ; Column
mov bh, 0 ; Page
int 0x10
;Restore registers
pop dx
pop bx
pop ax
ret
;======================================================== _input_letter() ====
;; Waits for input and returns an uppercase letter, KB_ESC, KB_BKSP or KB_ENTER.
;; Usage:
;; call _input_letter
;; Returns:
LETTER db 0 ;An upper case letter (or KB_ESC, KB_BKSP, KB_ENTER).
_input_letter:
;Backup registers
push ax
input_letter_st:
;Wait for input
mov ax, 0x0000
int 0x16
;Check if it is a special char (Esc, BkSp, Enter)
cmp al, KB_ESC
je end_input_letter
cmp al, KB_BKSP
je end_input_letter
cmp al, KB_ENTER
je end_input_letter
;Check if the char is an upper case letter
cmp al, 'A' ; al < 'A' -> input_letter_st
jl input_letter_st ;
cmp al, 'Z' ; al <= 'Z' -> end_input_letter
jle end_input_letter ;
;Check if the char is a lower case letter
cmp al, 'a' ; al < 'a' -> input_letter_st
jl input_letter_st ;
cmp al, 'z' ; al > 'z' -> input_letter_st
jg input_letter_st ;
;The char is a lowercase letter, convert it into uppercase
sub al, 0x20
end_input_letter:
mov LETTER, al
;Restore registers
pop ax
ret
;======================================================== _clear_screen() ====
;; Clears the screen.
;; Usage:
;; call _clear_screen
_clear_screen:
;Backup registers
push ax
push bx
push cx
push dx
;Clear the screen
mov ah, 0x07
mov al, 0 ; Clear
mov bh, 00001111b ; White on black
mov cx, 0 ; (0,0) +-----------+
mov dh, ROWS ; | |
mov dl, COLS ; +-----------+ (COLS,ROWS)
int 0x10
;Move the cursor to (0,0)
mov POS_X, 0
mov POS_Y, 0
call _move_cursor
;Restore registers
pop dx
pop cx
pop bx
pop ax
ret
;=========================== _memcpy(MEMCPY_SRC, MEMCPY_DEST, MEMCPY_LEN) ====
;; Copy bytes to an other place in the memory.
;; Usage:
;; mov MEMCPY_SRC, offset <src>
;; mov MEMCPY_DEST, offset <dest>
;; mov MEMCPY_LEN, <len>
;; call _memcpy
;; Function args:
MEMCPY_SRC dw 0 ; The source address
MEMCPY_DEST dw 0 ; The destination address
MEMCPY_LEN db 0 ; The number of bytes to copy
_memcpy:
;Backup registers
push ax
push bx
push cx
push dx
;Push the source bytes in the stack
mov ax, 0
mov bx, MEMCPY_SRC
mov cl, MEMCPY_LEN
memcpy_pshloop:
mov al, [bx]
push ax
inc bx
dec cl
cmp cl, 0
jne memcpy_pshloop
;Pop the bytes from the stack to the destination
mov bx, MEMCPY_DEST
mov ah, 0
mov al, MEMCPY_LEN
add bx, ax
dec bx
mov cl, MEMCPY_LEN
memcpy_poploop:
pop ax
mov [bx], al
dec bx
dec cl
cmp cl, 0
jne memcpy_poploop
;Restore registers
pop dx
pop cx
pop bx
pop ax
ret
;==================================================== _strlen(STRLEN_STR) ====
;; Counts the number of bytes that composes a string.
;; NOTE: The string must end with the '$' char !
;; Usage:
;; mov STRLEN_STR, offset <str>
;; call _strlen
;; Function args:
STRLEN_STR dw 0 ; The address of the string
;; Returns:
STRLEN_LEN db 0 ; The length of the string
_strlen:
;Backup registers
push ax
push bx
mov bx, STRLEN_STR
mov STRLEN_LEN, 0
strlen_loop:
mov al, [bx]
cmp al, '$'
je strlen_end
inc STRLEN_LEN
inc bx
jmp strlen_loop
strlen_end:
;Restore registers
pop bx
pop ax
ret
;===================================================== _inttostr(I2S_INT) ====
;; Convert integers into strings.
;; Usage:
;; mov I2S_INT, <int>
;; call _inttostr
;; Args:
I2S_INT dw 0 ;The integer to convert
;; Return:
I2S_STR dw "0000" ;The result string
_inttostr:
;Backup registers
push ax
push bx
push cx
push dx
;"Split" the number
mov ax, I2S_INT
mov bx, 10
mov cx, 4
i2s_split_loop:
mov dx, 0
div bx
push dx
dec cx
cmp cx, 0
jnz i2s_split_loop
;Convert in string
mov bx, offset I2S_STR
mov cx, 4
i2s_str_loop:
pop ax
add ax, '0'
mov [bx], al
inc bx
dec cx
cmp cx, 0
jnz i2s_str_loop
;Restore registers
pop dx
pop cx
pop bx
pop ax
ret
;================================ _input_field(IF_MSG, IF_MAXLEN, IF_EWD) ====
;; Display an input field.
;; Usage:
;; mov IF_MSG, offset <src>
;; mov IF_MAXLEN, <len>
;; mov IF_EWD, <0/1>
;; call _input_field
;; Function args:
IF_MSG dw 0 ; The message address (must end with '$')
IF_MAXLEN db 0 ; The maximum len of the input (max = 30)
IF_EWD db 0 ; End with $ ? (0 = False, 1 = True)
;; Returns:
IF_WORD db "-------------------------------" ; The input word
_input_field:
;Backup registers
push ax
push bx
push cx
push dx
;Clear the screen (draw the UI)
call _draw_ui
mov HELP_STR, offset if_help
call _print_help
;Fill IF_WORD with spaces or $
mov cl, 31
mov bx, offset IF_WORD
if_fill_word_loop:
cmp IF_EWD, 1 ; ' ' or '$' ?
je if_fill_word_dollard
mov [bx], ' '
jmp if_fill_word_dollard_end
if_fill_word_dollard:
mov [bx], '$'
if_fill_word_dollard_end:
inc bx
dec cl
cmp cl, 0
jne if_fill_word_loop
;Display the message
;Center the message
mov ax, IF_MSG
mov STRLEN_STR, ax
call _strlen
mov ah, 0
mov al, STRLEN_LEN
mov bl, 2
div bl
mov ah, COLS / 2
sub ah, al
mov POS_X, ah
mov POS_Y, header_height
add POS_Y, 5
call _move_cursor
;Print the message
mov ah, 0x09
mov dx, IF_MSG
int 0x21
;Display the field
;Center the field
mov ah, 0
mov al, IF_MAXLEN
mov bl, 2
div bl
mov ah, COLS / 2
sub ah, al
mov POS_X, ah
add POS_Y, 2
call _move_cursor
;print the field
mov ah, 0x07
mov al, 0 ; Clear
mov bh, COLOR_FIELD ; Color
mov ch, POS_Y ; x1
mov cl, POS_X ; x1
mov dh, POS_Y ; y2
mov dl, POS_X ; x2
add dl, IF_MAXLEN ; ~~
inc dl ; ~~
int 0x10
mov if_wlen, 0
;Intput field main loop
if_loop:
call _input_letter
cmp LETTER, KB_ENTER ; Validate
je if_loop_end
cmp LETTER, KB_ESC ; skip esc
je if_loop
cmp LETTER, KB_BKSP ; Remove last letter
je if_loop_bksp
;Add the letter if the buffer is not full
mov ah, IF_MAXLEN
cmp if_wlen, ah
je if_loop
;buff
mov bx, offset IF_WORD
mov ah, 0
mov al, if_wlen
add bx, ax
mov al, LETTER
mov [bx], al
inc if_wlen
;disp
inc POS_X
call _move_cursor
mov ah, 0x06
mov dl, LETTER
int 0x21
jmp if_loop
if_loop_bksp:
cmp if_wlen, 0 ;empty
je if_loop
;buff
mov bx, offset IF_WORD
mov ah, 0
mov al, if_wlen
add bx, ax
dec bx
cmp IF_EWD, 1
je if_loop_ewd
mov [bx], ' '
jmp if_loop_ewdend
if_loop_ewd:
mov [bx], '$'
if_loop_ewdend:
dec if_wlen
;disp
call _move_cursor
mov ah, 0x06
mov dl, ' '
int 0x21
dec POS_X
jmp if_loop
if_loop_end:
;Restore registers
pop dx
pop cx
pop bx
pop ax
ret
;Var
if_wlen db 0
;Help
if_help db 0xDA,"A-Z",0xBF," Try a letter ",0xDA,"Enter",0xBF," Validate"
db " $"
|