关于C语言背景音乐音乐播放的问题

Haruka 2008-03-08 03:11:28
各位仁兄帮我看下这个测试程序,每次运行时,输入数据回车后总会出现CPU遇到无效指令。程序的主要作用是播放在后台播放音乐,前台可以输入字符。但如果把main的do循环中的putchar(getchar())改成putchar()就不会出现上述情况,不知为什么.
#include<dos.h>
#include<stdio.h>
void install(void interrupt (*)(),int inum);
void interrupt new_timer(void);
void MySound(unsigned short);
void NoSound(void);
void main()
{
int i=9;
install(new_timer,0x1c);
do{
putchar(getchar());
}while(i--);
}
void install(void interrupt (*faddr)(void),int inum)
{
disable();
setvect(inum,faddr);
enable();
}
void interrupt new_timer(void)
{
void interrupt (*old_timer)()=getvect(0x1c);
static short frep[]={147,131,220,131,0},*f=frep;
static char i=0;
if(*f!=0)
{
if(i==5)
{
MySound(*f++);
i=0;
}
else i++;
}
else{
install(old_timer,0x1c);
NoSound();
}
}
void MySound(unsigned short frep)
{
union
{
unsigned short division;
char c[2];
}x;
x.division=119328UL/frep;
outportb(0x43,0xb6);
outportb(0x42,x.c[0]);
outportb(0x42,x.c[1]);
outportb(0x61,inportb(0x61)|3);
}
void NoSound(void)
{
outportb(0x61,inportb(0x61)&0xfc);
}
...全文
563 2 打赏 收藏 转发到动态 举报
AI 作业
写回复
用AI写文章
2 条回复
切换为时间正序
请发表友善的回复…
发表回复
liyahui1228 2009-06-09
  • 打赏
  • 举报
回复
呵呵
ppzzhhsky 2008-03-11
  • 打赏
  • 举报
回复
getchar()获取的是字符,putchar()中带的参数应该是数字
改成putchar(sizeof(getchar()))就不会有问题了

70,020

社区成员

发帖
与我相关
我的任务
社区描述
C语言相关问题讨论
社区管理员
  • C语言
  • 花神庙码农
  • 架构师李肯
加入社区
  • 近7日
  • 近30日
  • 至今
社区公告
暂无公告

试试用AI创作助手写篇文章吧