-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathmusic-box.asm
227 lines (198 loc) · 5.29 KB
/
music-box.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
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
.model small
.stack 100h
.data
.code
; Get a series of characters from the keyboard
; and store them in stack
saveRegs MACRO
PUSH AX
PUSH BX
PUSH CX
PUSH DX
PUSH SI
endm
restoreRegs MACRO
POP SI
POP DX
POP CX
POP BX
POP AX
endm
PLAY MACRO frequency
saveRegs
;This macro receives the tone
MOV AX,frequency ;and sends to call the procedures
CALL setFreq
CALL activateSpkr
restoreRegs
ENDM
;SEND THE FREQUENCY IN TWO SEPARATE BYTES TO THE PORT.
setFreq PROC
OUT 42h, AL
MOV AL, AH
OUT 42h, AL
RET
setFreq ENDP
;------------------------------------------------------------------------------------
speakerOn PROC
MOV AL, 182
OUT 43H, AL
IN AL, 61h
OR AL, 11B
OUT 61h, AL
RET
speakerOn ENDP
speakerOff PROC
IN AL, 61h
AND AL, 11111100b
OUT 61h, AL
RET
speakerOff ENDP
;------------------------------------------------------------------------------------
activateSpkr proc ;Activate the horn and place the name of
CALL speakerOn ;The key
MOV CX, 07H ; Wait 0.5 second
MOV DX, 0A120H ;before turning off the speaker
MOV AH, 86H
INT 15H
CALL speakerOff ;Horn disconnects and returns.
RET
activateSpkr endp
;------------------------------------------------------------------------------------
keyPress PROC
MOV AH,08H
INT 21H
CALL toLower
RET
keyPress ENDP
; Convert a character to lower case
toLower PROC
CMP AL, 65 ;'A'
JB CONTINUE ;IF THE KEY IS LESS THAN 'A' IT DOES NOTHING
CMP AL, 90 ;'Z'
JA CONTINUE ;IF THE KEY IS GREATER THAN 'Z' IT DOES NOTHING
ADD AL, 32 ;Converts uppercase to lowercase.
CONTINUE:
RET
toLower ENDP
;------------------------------------------------------------------------------------
PLAYBACK PROC
beginPop:
CMP BX, 0 ;if the counter is 0 then there is nothing to print
JE leavePrint
DEC BX
ADD BP,2
MOV AX,[BP-2] ;get the character from the stack
CMP AL,'q' ;DO high
JNE S1 ;IF NOT THE EXPECTED KEY, JUMP TO CHECK NEXT.
PLAY 2280 ;IF IT IS THE EXPECTED KEY, IT GENERATES THE CORRESPONDING SOUNDE
JMP beginPop ;AFTER THE SOUND, RESTART TO WAIT FOR ANOTHER SOUND.
S1:
CMP AL,'w' ;RE high
JNE S2
PLAY 2031
JMP beginPop
S2:
CMP AL,'e' ;MI high
JNE S3
PLAY 1809
JMP beginPop
S3:
CMP AL,'r' ;FA high
JNE S4
PLAY 1715
JMP beginPop
S4:
CMP AL,'t' ;SOL high
JNE S5
PLAY 1521
JMP beginPop
S5:
CMP AL,'y' ;LA high
JNE S6
PLAY 1355
JMP beginPop
leavePrint:
RET
S6:
CMP AL,'u' ;TI high
JNE S7
PLAY 1207
JMP beginPop
S7:
CMP AL,'i'
JNE beginPop
PLAY 1140
jmp beginPop
;addCount:
; ADD BX, 1
; JMP beginPop
PLAYBACK ENDP
;------------------------------------------------------------------------------------
player PROC
push bp
mov bp,sp
MOV BX, 0
BEGIN:
CALL keyPress
CMP AL, 27 ; ESC
JNE KEYED
MOV sp,bp ;restore sp
POP bp
RET
KEYED:
;if the key is p then move onto printing the stack
CMP AL, 'p' ;
JNE SAVEKEY
CALL PLAYBACK
MOV sp,bp ;restore sp
POP bp
RET
SAVEKEY:
PUSH AX ;save the key pressed in the stack
SUB BP,02
INC BX ;increment the counter
cmp al, 'q'
jne k2
play 2280
JMP BEGIN
k2: cmp al, 'w'
jne k3
play 2031
JMP BEGIN
k3: cmp al, 'e'
jne k4
play 1809
JMP BEGIN
k4: cmp al, 'r'
jne k5
play 1715
JMP BEGIN
ch1:
jmp begin
k5: cmp al, 't'
jne k6
PLAY 1521
JMP ch1
k6: cmp al, 'y'
jne k7
PLAY 1355
JMP ch1
ch2:
jmp ch1
k7: cmp al, 'u'
jne k8
PLAY 1207
JMP ch2
k8: cmp al, 'i'
jne ch2
PLAY 1140
JMP ch2
player ENDP
start:
MOV AX, @data
MOV DS, AX
CALL player
MOV AX, 4C00H
INT 21H
END start