I'm trying to make a piano using 8086 assembly and Turbo Assembler (TASM) in Ideal mode. When I run everything is fine and when I click a key there is a sound but when I click again the whole screen prints garbage data.
Can you see the problem in this TASM code?
IDEAL
MODEL small
STACK 100h
DATASEG
Welcome db "Welcome! Just Try Clicking those buttons on your keyboard $"
CODESEG
start:
mov ax, @data
mov ds, ax
call printWelcome
jmp loopGetKey
loopGetKey:
call getKey
jmp loopGetKey
exit:
mov ax, 4c00h
int 21h
proc printWelcome
mov dx, offset Welcome
mov ah, 9
int 21h
ret
endp printWelcome
proc getKey
waitForKey:
;Check the status of the keyboard
mov ah, 1
int 16h
jz waitForKey
;read key
mov ah, 0
int 16h
;exit if ESC is pressed
cmp al, 1Bh
je exit
cmp al, 61h
je soundA
cmp al, 73h
je soundS
cmp al, 64h
je soundD
cmp al, 66h
je soundF
cmp al, 67h
je soundG
cmp al, 68h
je soundH
cmp al, 6Ah
je soundJ
cmp al, 6Bh
je soundK
cmp al, 6ch
je soundL
jne @continue
soundA:
push 2280
call makeSound
soundS:
push 2031
call makeSound
soundD:
push 1809
call makeSound
soundF:
push 1715
call makeSound
soundG:
push 1521
call makeSound
soundH:
push 1355
call makeSound
soundJ:
push 1207
call makeSound
soundK:
push 2031
call makeSound
soundL:
push 2031
call makeSound
@continue:
ret
endp getKey
proc makeSound
push bp
mov bp, sp
in al, 61h
or al, 00000011b
out 61h, al
mov al, 0b6h
out 43h, al
mov ax, [bp+4]
out 42h, al
mov al, ah
out 42h, al
call delay
call stopSound
ret 4
endp makeSound
proc stopSound
push ax
in al, 61h
and al, 11111100b
out 61h, al
pop ax
ret
endp stopSound
proc delay
push cx
push dx
push ax
mov cx, 0FH
mov dx, 4240H
mov ah, 86H
int 15H
pop ax
pop dx
pop cx
ret
endp delay
END start
Look at your proc called 'makeSound', the end of it.
You pushed bp to the stack, but didn't pop it back. It's a big mistake.
You also used "ret 4" instead of 2. It's 2 since you only pushed one word before calling the proc.
The end should look like that:
pop bp
ret 2
endp makeSound
I also saw that you used '@' in some of your labels. You should use 2 of them (@@). For example:
@@myLabel:
**code**
jmp @@myLabel