■◆输出带颜色和重新定位光标两段小程序编译有关问题◆■
■◆输出带颜色和重新定位光标两段小程序编译问题◆■
以下是我在Linux0.01内核分析与操作系统设计一书中看到的例子程序,程序一是输出带颜色的HELLO WORLD功能,程序二是重新定位光标的功能,但编译有错误,本人新手,请各位指教。
程序一:
/* hello_s.c */
#define BLACK 0
#define BLUE 1
#define GREEN 2
#define CYAN 3
#define RED 4
#define MAGENTA 5
#define BROWN 6
#define LIGHTGRAY 7
#define DARKGRAY 8
#define LIGHTBLUE 9
#define LIGHTGREEN 10
#define LIGHTCYAN 11
#define LIGHTRED 12
#define LIGHTMAGENTA 13
#define YELLOW 14
#define WHITE 15
#define BLINK 28
void write_string(char *pstring, int color)
{
char far *pvideo = (char far *)0xB8000000;
while(*pstring)
{
*pvideo=*pstring;
pstring++;
pvideo++;
*pvideo=color;
pvideo++;
}
}
void main(){
write_string("Hello,world!", RED);
}
编译错误:
C:\c>gcc hello_s.c -o hel.exe
hello_s.c: In function `write_string':
hello_s.c:22: syntax error before '*' token
hello_s.c:25: `pvideo' undeclared (first use in this function)
hello_s.c:25: (Each undeclared identifier is reported only once
hello_s.c:25: for each function it appears in.)
hello_s.c: In function `main':
hello_s.c:33: warning: return type of `main' is not `int'
程序二:
/* Compiled by DJGPP */
#include <stdio.h>
#include <pc.h>
void update_cursor(int row, int col)
{
USHORT position=(row*80) + col;
/* cursor LOW port to VGA INDEX register */
outb(0x3D4, 0x0F);
outb(0x3D5, (UCHAR)(position&0xFF));
/* cursor HIGH port to vga INDEX register */
outb(0x3D4, 0x0E);
outb(0x3D5, (UCHAR)((position>>8)&0xFF));
}
void main(){
clrscr();
update_cursor(10, 10);
}
编译错误:
C:\c>gcc cursor.c -o cur.exe
cursor.c: In function `update_cursor':
cursor.c:7: `USHORT' undeclared (first use in this function)
cursor.c:7: (Each undeclared identifier is reported only once
cursor.c:7: for each function it appears in.)
cursor.c:7: parse error before "position"
cursor.c:10: `UCHAR' undeclared (first use in this function)
cursor.c:10: `position' undeclared (first use in this function)
cursor.c: In function `main':
cursor.c:16: warning: return type of `main' is not `int'
------解决方案--------------------
程序一需要改动的地方:
char far *pvideo = (char far *)0xB8000000;
改为
char *pvideo = (char *)0xB8000000L;
void main(){
write_string("Hello,world!", RED);
}
改为
int main(){
write_string("Hello,world!", RED);
return 0;
}
程序二
在程序头部加入
typedef unsigned short USHORT
或者在相应头文件中加入
void main(){
clrscr();
update_cursor(10, 10);
}
改为
int main(){
clrscr();
update_cursor(10, 10);
return 0;
}
以下是我在Linux0.01内核分析与操作系统设计一书中看到的例子程序,程序一是输出带颜色的HELLO WORLD功能,程序二是重新定位光标的功能,但编译有错误,本人新手,请各位指教。
程序一:
/* hello_s.c */
#define BLACK 0
#define BLUE 1
#define GREEN 2
#define CYAN 3
#define RED 4
#define MAGENTA 5
#define BROWN 6
#define LIGHTGRAY 7
#define DARKGRAY 8
#define LIGHTBLUE 9
#define LIGHTGREEN 10
#define LIGHTCYAN 11
#define LIGHTRED 12
#define LIGHTMAGENTA 13
#define YELLOW 14
#define WHITE 15
#define BLINK 28
void write_string(char *pstring, int color)
{
char far *pvideo = (char far *)0xB8000000;
while(*pstring)
{
*pvideo=*pstring;
pstring++;
pvideo++;
*pvideo=color;
pvideo++;
}
}
void main(){
write_string("Hello,world!", RED);
}
编译错误:
C:\c>gcc hello_s.c -o hel.exe
hello_s.c: In function `write_string':
hello_s.c:22: syntax error before '*' token
hello_s.c:25: `pvideo' undeclared (first use in this function)
hello_s.c:25: (Each undeclared identifier is reported only once
hello_s.c:25: for each function it appears in.)
hello_s.c: In function `main':
hello_s.c:33: warning: return type of `main' is not `int'
程序二:
/* Compiled by DJGPP */
#include <stdio.h>
#include <pc.h>
void update_cursor(int row, int col)
{
USHORT position=(row*80) + col;
/* cursor LOW port to VGA INDEX register */
outb(0x3D4, 0x0F);
outb(0x3D5, (UCHAR)(position&0xFF));
/* cursor HIGH port to vga INDEX register */
outb(0x3D4, 0x0E);
outb(0x3D5, (UCHAR)((position>>8)&0xFF));
}
void main(){
clrscr();
update_cursor(10, 10);
}
编译错误:
C:\c>gcc cursor.c -o cur.exe
cursor.c: In function `update_cursor':
cursor.c:7: `USHORT' undeclared (first use in this function)
cursor.c:7: (Each undeclared identifier is reported only once
cursor.c:7: for each function it appears in.)
cursor.c:7: parse error before "position"
cursor.c:10: `UCHAR' undeclared (first use in this function)
cursor.c:10: `position' undeclared (first use in this function)
cursor.c: In function `main':
cursor.c:16: warning: return type of `main' is not `int'
------解决方案--------------------
程序一需要改动的地方:
char far *pvideo = (char far *)0xB8000000;
改为
char *pvideo = (char *)0xB8000000L;
void main(){
write_string("Hello,world!", RED);
}
改为
int main(){
write_string("Hello,world!", RED);
return 0;
}
程序二
在程序头部加入
typedef unsigned short USHORT
或者在相应头文件中加入
void main(){
clrscr();
update_cursor(10, 10);
}
改为
int main(){
clrscr();
update_cursor(10, 10);
return 0;
}