Thanks for the reply,
I tried your (first) code and it did work. the example the professor gave is to be used as reference probably. anyway the thing missing is if i could have the label you use "melody" and save its offset in SI register, but instead of having it in the code, i want to read the melody from a file (txt).
i have been able to read a file into iobuf and then read it back byte by byte but when i try it with your code for music it doesnt work
I really thank you for your time
org 100h ; we are a .com file
; (nasm defaults to "section .text")
mov ah,03Dh ; the open-existing-file function
mov al,0 ; open moppde 0=read,1=write,2=both
mov dx,filename ; address of zero-terminated string
int 021h ; call on Good Old Dos
mov [filehndl],ax ; we'll reuse the filehndl variable,
; but use a different one if you open
; more than one :)
mov ah,03Fh ; the read-from-a-file function
mov bx,[filehndl] ; our new (same as old, probably) file handle
mov cx,0FFh ; max bytes to read
mov dx,iobuf ; address of a buffer to read into
int 021h ; call on Good Old Dos
mov [read_len],ax ; save the number of bytes read
mov ah,03Eh ; close the file
mov bx,[filehndl] ; yeah, file handle in bx
int 021h ; call on Good Old Dos
; set up PIT
mov al, 10111110b
;.......|------ binary 16-bit counter
;....|||------- square wave generator
;..||---------- r/w bits 0-7, then 8-15
;||------------ select counter 2
out 43h, al
mov si, iobuf ; "offset" - if p.j.
playit_sam: ; if she can take it, so can I.
in al, 61h ; enable speaker
or al, 00000011b
;.......|------- enable speaker on timer 2 gate
;......|-------- enable speaker data
out 61h, al
lodsw ; get a note
or ax, ax ; check for zero
jz egress ; coda
out 42h, al ; play it
mov al, ah
out 42h ,al
lodsw ; for this long
call delay
in al, 61h ; Turn Speaker OFF
and al, 11111100b
out 61h, al
lodsw ; for this long
call delay
jmp short playit_sam ; play more
egress:
in al, 61h
and al, 0FCh ; Turn Speaker OFF. Please.
out 61h, al
ret
;------------------
;------------------
delay:
mov cx, ax ; this trashes ax, cx, dx, too :)
cmp cx, byte 1 ; special case, no delay
jz no_delay
shr cx, 3 ; because it sounds okay :)
xor dx, dx
mov ah, 86h ; bios delay - cx:dx microseconds
int 15h
no_delay:
ret
;------------------
; Some equates, for your compositional assistance
; It is said that I'm not artistic.
A0 equ 21728
B0 equ 19328
C0 equ 18244
D0 equ 16144
E0 equ 14080
F0 equ 13668
G0 equ 12176
A1 equ 10864
B1 equ 9664
C1 equ 9122
D1 equ 8072
E1 equ 7040
F1 equ 6834
G1 equ 6088
A2 equ 5432
B2 equ 4832
C2 equ 4561
D2 equ 4063
E2 equ 3520
F2 equ 3417
G2 equ 3044
A3 equ 2712
B3 equ 2416
C3 equ 2280
D3 equ 2032
E3 equ 1810
F3 equ 1708
G3 equ 1522
A4 equ 1356
B4 equ 1208
C4 equ 1140
D4 equ 1016
E4 equ 905
F4 equ 854
G4 equ 762
A5 equ 678
B5 equ 604
C5 equ 558
; Some "durations"
Z equ 1
S equ 10
Q equ 20
P equ 40
R equ 80
L equ 320
; The opus itself.
;
; It is said that I'm not artistic.
;
; Format is "note", "duration", "pause", "note", ... , 0
; Write your own!
melody:
%if 1
dw G2, P, Z, E2, Q, Z, G2, Q, Q, G1, Q, Q
dw G1, Q, Q, C2, Q, Q, B2, Q, Z, C2, Q, Z
dw D2, Q, Q, G2, Q, Q, A3, Q, Z, G2, Q, Z
dw A3, Q, Q, A2, Q, Q, A2, Q, Q, G2, Q, Q
dw E2, Q, Z, G2, Q, Z, F2, Q, Z
dw E2, Q, Z, D2, Q, Q, F2, Q, Z, G2, Q, Z
dw A3, Q, Z, G2, Q, Z, F2, Q, Z, E2, Q, Z
dw F2, Q, Z, E2, Q, Z, D2, Q, Z, B2, Q, Z
dw C2, Q, Z, D2, Q, Z, E2, Q, Z, C2, Q, Z
dw D2, Q, Q, G3, Q, Z, F3, Q, Z, E3, Q, Z
dw D3, Q, Z, C3, Q, Z, B3, Q, Z, A3, Q, Z
dw B3, Q, Z, C3, Q, Z, D3, Q, Z, B3, Q, Q
dw G0, Q, Q, G0, R, Z, 0
%endif
dw C5, R, Q, A0, R, Z, 0
section .data
filename db 'testtext.asm',0
;--------------------------------------------------
section .bss
iobuf resb 0100h
read_len resw 01h
filehndl resw 01h