程序代码:
stack segment
dw 512 dup(?) ;uint16_t task_stack[512];uint16_t sp = sizeof(stack);
stack ends
data segment
music_freq ;uint16_t music_freq[] = {...};
dw 262,294,330,350,393,441
dw 495,525,589,661,700,786
dw 882,990,1112,1248
music_time dw 800 ;uint16_t music_time = 800;
messg1 db 'Thank you for using it','$' ;const char *
messg2 db 0ah,0dh,'it is a music program','$' ;const char *
messg3 db 0ah,0dh,'please choose mode(Q:exit other key:continue)','$' ;const char *
messg4 db 0ah,0dh,'input error!$' ;const char *
messg5 db 0ah,0dh,'you can play the music now(key:0-F,Q:exit the program)!','$' ;const char *
messg6 db 0ah,0dh,'$' ;const char *
messg7 db 0ah,0dh,'thank you for using it,any key to exit!','$' ;const char *
data ends
code segment
assume cs:code,ds:data,ss:stack
main proc far
start:
mov ax,data
mov ds,ax
mov ax,stack
mov ss,ax
lea dx,messg1
mov ah,09h
int 21h ;puts(messg1);
lea dx,messg2
mov ah,09h
int 21h ;puts(messg2);
lea dx,messg3
mov ah,09h
int 21h ;puts(messg3);
input:
mov ah,01h
int 21h
cmp al,'Q'
jz exit ;if (getchar() == 'Q') goto exit;
lea dx,messg5
mov ah,09h
int 21h ;puts(messg5);
lea dx,messg6
mov ah,09h
int 21h ;puts(messg6);
l1:
mov ah,01h ;char ch;
int 21h
cmp al,'Q'
jz exit1 ;if ((ch = getchar()) == 'Q') goto exit1;
cmp al,30h
jb error
cmp al,46h
ja error ;else if ((ch < '0') || (ch > 'F')) goto error;
cmp al,40
jb num ;else if (ch < '(') goto num; ????
jmp abcdef ;else goto abcdef;
num:
sub al,30h
mov dl,2
mul dl
lea bx,music_freq ;uint16_t *mf = music_freq;
mov si,ax ;uint16_t i = (uint16_t)((ch - (uint8_t)0x30) << 1);
mov cx,[bx+si] ;uint16_t freq = mf[i];
mov dx,800
call gensound ;gensound(800, freq);
jmp l1 ;goto l1;
abcdef:
sub al,31h
mov dl,2
mul dl
lea bx,music_freq
mov si,ax ;i = (uint16_t)((ch - (uint8_t)0x31) << 1);
mov cx,[bx+si] ;count = mf[i]
mov dx,800
call gensound ;gensound(800, freq);
jmp l1 ;goto l1;
error:
lea dx,messg4
mov ah,09h
int 21h ;puts(messg4);
jmp l1 ;goto l1;
exit:
mov ah,4ch
int 21h ;exit(0);
exit1:
lea dx,messg7
mov ah,09h
int 21h ;puts(messg7);
mov ah,01h
int 21h ;getchar();
mov ah,4ch
int 21h ;exit(0);
main endp
gensound proc near ;void gensound(uint16_t time, uint16_t freq);
push dx ;task_stack[--sp] = time;
;#define D6 (6) #define D4 (4) #define D1 (1)
;#define _TIMER(2) #define _RHRL (3) #define _MODE (3) #define _BIN(0)
;#define TIMER2 (_TIMER << D6) #define LHMODE (_RHRL << D4)
;#define MODE3 (_MODE << D1) #define BIN _BIN
mov al,0b6h ;#define CTRL_WORD (TIMER2 | LHMODE | MODE3 | BIN)
;#define TIMER_CONTROLLER (0x43)
out 43h,al ;write_port(TIMER_CONTROLLER, CTRL_WORD);/*pseudocode*/
mov dx,8h
mov ax,3208h
div cx ;uint16_t pulse = 0x83208 / freq;
;#define TIMER2_PORT (0x42)
out 42h,al ;write_port(TIMER2_PORT, LOBYTE(pulse));/*pseudocode*/
mov al,ah
out 42h,al ;write_port(TIMER2_PORT, HIBYTE(pulse));/*pseudocode*/
;#define 8255_PB (0x61)
in al,61h ;uint8_t pb = read_port(8255_PB);
mov ah,al
;#define SET_PB0 (1) #define SET_PB1 (1 << 1)
or al,3 ;pb |= (SET_PB0 | SET_PB1);
out 61h,al ;write_port(8255_PB, pb);
l3:
push dx ;task_stack[--sp] = 0x83208 % freq;
push ax ;task_stack[--sp] = pulse;
mov dx,8h
mov ax,0f05h ;uint32_t init_t = 80f05h;
s1:
sub ax,1
sbb dx,0 ;--init_t;
jnz s1 ;if (HIWORD(init_t) != 0) goto s1;
pop ax
pop dx ;init_t = MAKELONG(task_stack[sp++], task_stack[sp++]);
dec bx ;????
jnz l3 ;if (???? != 0) goto l3;
mov al,ah
out 61h,al ;write_port(8255_PB, HIBYTE(LOWORD(init_t));
pop dx ;time = task_stack[sp++];
ret ;return;
gensound endp
code ends
end start
你先看看