Feedjit

Articles for you

Saturday, June 1, 2013

Generate a beautiful Sound from the System Internal Speaker in Assembly language

.model small
.stack
.data
array dw 262,400,262,400,294,800,262,800,349,800,330,1000
;tne db 20
.code
main:
mov ax,@data
mov ds,ax
in al,61h                ;Read Port B
jmp $+2                    ;Wait
or al,3                    ;Set timer bits on 1 and 2
out 61h,al                ;Send to Port B
mov al,0B6h                ;Configuration word for sound
out 43h,al                ;Sned to Command register
;mov cl,5
;repead:
;call happy_bday
;dec cl
;cmp cl,0
;jne repead
;jmp finish


happy_bday:
mov dx,12                ;Number of Frequencies
mov si, offset array    ;SI points to array
next:
mov bx,[si]                ;Get first value from array
call play                ;Call play function
dec dx                   ; Decrement repeat routine count
cmp dx, 0                 ; Is DX (repeat count) = to 0
jnz  next               ; If not zero jump to NEXT
jmp stop                ;Jump to stop

play:
mov ax,0                ;Clear ax values
mov ax,bx                ;Copy frequency from bx to ax
out 42h,al                ;Send lower byte
mov al,ah                ;Copy higher byte from ah to al
out 42h,al                ;Send higher byte
inc si                    ;Increment to point to next value
inc si

Delay_time_to_play:
mov cx,1000                 ; Repeat loop 1000 times
Outer_Loop:           
mov bx,cx                ;Save cx to bx
mov cx,1000            ;Inner loop to be repeated 10000 times
Inner_Loop:
jmp $+2                  ;Wait
loop Inner_Loop            ;Loops to inner loop
jmp $+2                    ;Wait
mov cx,bx                ;Restore outer cx value
LOOP Outer_Loop            ; Jump repeatedly to Outer loop until cx=0
ret                        ;return

stop:
mov bl,0FCh                ;Set 1 and 2 bit of timer to zero again
in al,61h                ;Read port B
jmp $+2                    ;Wait
and al,bl                ;Change 1 and 2 bit ingnoring the rest
out 61h,al                ;Send to port B

finish:                   
mov ah,4ch                ;Service # to end program
int 21h                    ;Call OS to do the job
end main   

Read More

Articles for you