-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathAssembly.asm
201 lines (158 loc) · 5.43 KB
/
Assembly.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
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
.model small
.data
sequence DB 100 dup(?) ; buffer to store generated sequence
player_input DB 100 dup(?) ; buffer to store player's input
correct DB 1 ; flag to track correctness of input
level DB 3 ; initial game level
win_msg DB 10,13,"CORRECT ANSWER --- NEXT LEVEL ", "$"
lose_msg DB 10,13,"WRONG ANSWER --- END THE GAME ", "$"
prompt_msg DB 10,13,"TRY THE SYMBOL","$"
exit_msg DB 10,13,"GAME OVER! PRESS ANY KEY TO EXIT", "$"
.stack 256 ; Explicitly define stack size
.code
main PROC FAR
mov ax, @data ; Properly initialize data segment (.startup )
mov ds, ax
start_game:
call generate_sequence
call display_sequence
call get_player_input
call check_input
cmp correct, 1
je next_level
jmp game_over
next_level:
lea dx, win_msg
mov ah, 09h
int 21h
inc level
cmp level, 10 ; Add maximum level check
jl start_game
jmp game_over
game_over:
lea dx, exit_msg ; Added exit message
mov ah, 09h
int 21h
mov ah, 00h ; Wait for key press
int 16h
mov ah, 4Ch ; Proper exit to DOS
int 21h
main ENDP
generate_sequence PROC NEAR
push cx ; Save registers
push si
mov al, level ; initial value for generating the sequence
mov ah, 0
mov cx, ax ; cx register determines number of loop iterations
xor si, si ; SI register as index for storing values
generate_loop:
push cx ; Save loop counter
; Better random number generation
mov ah, 00h ; Get system time
int 1Ah ; CX:DX now contains clock ticks
mov al, dl ; Use lower part of tick count
and al, 0Fh ; Limit to 0-15
add al, '0' ; Convert to ASCII
mov [sequence + si], al ; Store in sequence
inc si
pop cx ; Restore loop counter
loop generate_loop
pop si ; Restore registers
pop cx
ret
generate_sequence ENDP
display_sequence PROC NEAR
push cx
push si
mov al, level
mov ah, 0
mov cx, ax
xor si, si
display_loop:
mov al, [sequence + si]
mov ah, 0Eh ; BIOS teletype output
int 10h
inc si
loop display_loop
call delay
call clear_screen
pop si
pop cx
ret
display_sequence ENDP
get_player_input PROC NEAR
push cx
push di
lea dx, prompt_msg
mov ah, 09h
int 21h
mov al, level
mov ah, 0
mov cx, ax
xor di, di
input_loop:
mov ah, 00h ; Wait for keyboard input
int 16h
mov [player_input + di], al
mov ah, 0Eh ; Display input character
int 10h
inc di
loop input_loop
pop di
pop cx
ret
get_player_input ENDP
check_input PROC NEAR
push cx
push si
push di
mov al, level
mov ah, 0
mov cx, ax
xor si, si
xor di, di
mov correct, 1
compare_loop:
mov al, [sequence + si]
cmp al, [player_input + di]
jne incorrect_input
inc si
inc di
loop compare_loop
pop di
pop si
pop cx
ret
incorrect_input:
mov correct, 0
pop di
pop si
pop cx
ret
check_input ENDP
delay PROC NEAR
push cx
push dx
mov cx, 0000fh ; Increased delay time
delay_loop:
push cx ; Nested loop for more reliable delay
mov cx, 0000fh
inner_delay_loop:
nop
loop inner_delay_loop
pop cx
loop delay_loop
pop dx
pop cx
ret
delay ENDP
clear_screen PROC NEAR
mov ah, 06h ; Scroll function
mov al, 0 ; Clear entire screen
mov bh, 07h ; Default color attribute
mov cx, 0 ; Top-left corner
mov dx, 184Fh ; Bottom-right corner
int 10h
ret
clear_screen ENDP
END main