-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathRandomColorCharScreenFill.asm
95 lines (77 loc) · 2.48 KB
/
RandomColorCharScreenFill.asm
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
TITLE Random_Color_Char_Screen_Fill (Chapter 11, Pr 4, Modified) (RandomColorCharScreenFill.asm)
COMMENT !
Student: Rachel Nguyen
Class: CSCI 241
Instructor: Ding
Assignment: Ch10, Random_Color_Char_Screen_Fill
Due Date: 5/4/21
Description:
This program fills each screen cell with a random character,
in a random color. The main purpose of this program is to
reduce system I/O API calls.
!
INCLUDE Irvine32.inc
MAXCOL = 50
MAXROW = 20
.data
outHandle DWORD ?
bufColor WORD MAXCOL DUP (?)
bufChar BYTE MAXCOL DUP (?)
xyPos COORD <0,0>
cellsWritten DWORD ?
.code
main PROC
INVOKE GetStdHandle, STD_OUTPUT_HANDLE ; get standard output handle
mov outHandle, eax
mov ecx, MAXROW ; outer loop count (# rows)
L1:
push ecx ; save outer loop count
mov ecx, MAXCOL ; inner loop count (# col)
mov esi, OFFSET bufColor
mov edi, OFFSET bufChar
L2:
call ChooseColor ; get random color, save to buffer
mov [esi], ax
call ChooseCharacter ; get random char, save to buffer
mov [edi], al
add esi, TYPE bufColor ; point to next color
add edi, TYPE bufChar ; point to next char
LOOP L2
INVOKE WriteConsoleOutputAttribute, outHandle, ADDR bufColor, MAXCOL, xyPos, ADDR cellsWritten ; write color
INVOKE WriteConsoleOutputCharacter, outHandle, ADDR bufChar, MAXCOL, xyPos, ADDR cellsWritten ; write char
inc xyPos.y ; move to next row
pop ecx ; restore outer loop count
LOOP L1
INVOKE SetConsoleCursorPosition, outHandle, xyPos ; set cursor position for wait msg
call WaitMsg
exit
main ENDP
;-----------------------------------------------------------------------
ChooseColor PROC
; Selects a color with 50% probability of red, 25% green and 25% yellow
; Receives: nothing
; Returns: AX = randomly selected color
;-----------------------------------------------------------------------
mov ax, 4
call RandomRange ; choose a color (0-3)
.IF ax < 2 ; red (0-1)
mov ax, 04h
.ELSEIF ax == 2 ; green (2)
mov ax, 02h
.ELSE ; yellow (3)
mov ax, 0Eh
.ENDIF
ret
ChooseColor ENDP
;-----------------------------------------------------------------------
ChooseCharacter PROC
; Randomly selects an ASCII character, from ASCII code 20h to 07Ah
; Receives: nothing
; Returns: AL = randomly selected character
;-----------------------------------------------------------------------
mov al, 59h
call RandomRange ; choose a char (20h-07Ah)
add al, 20h ; AL gets ASCII value
ret
ChooseCharacter ENDP
END main