在Dos下想輸出文字到螢幕上,除了呼叫C/C++提供的函數 printf, cout 或 Bios提供的軟體中斷 int 10, 是否有直接控制硬體的方法呢? 答案是有的,可以直接把要顯示的資料搬到A/B Segment。用以下的code來做範例 (Compile using Watcom)
typedef unsigned char BYTE;
typedef unsigned short WORD;
typedef unsigned int DWORD;
enum Colour{ BLACK=0, BLUE, GREEN, CYAN, RED, MAGNETA, BROWN, LGRAY, \
DGRAY=8, LBLUE, LGREEN, LCYAN, LRED, LMAGNETA, YELLOW, WHITE};
#define COLOUR(forecolour, backcolour) ((backcolour) << 4 | (forecolour) & 0x0F)
void WriteString(int y, int x, BYTE colour, char *string)
{
volatile WORD *pvram;
for (DWORD i=0;string[i]!=0; x++, i++) {
pvram = (volatile WORD *)0xB8000 + y * 80 + x;
*pvram = (BYTE)string[i] | colour << 8;
}
}
void WriteCharacter(int y, int x, BYTE colour, BYTE ch)
{
volatile WORD * pvram;
pvram = (volatile WORD *)0xB8000 + (y * 80 + x) ;
*pvram = ch | (colour << 8);
}
int main()
{
WriteCharacter(1,0, COLOUR(RED, BLACK), 'H');
WriteCharacter(1,1, COLOUR(RED, BLACK), 'i');
WriteString(1, 3, COLOUR(GREEN, BLACK), "Martin.");
}
在螢幕座標(y,x) = (1, 0) 秀出 Hi Martin.
ps. 如果想畫一個點陣圖請參考 mode 13h, 解析度為320*200, 256色, 透過寫入A000:0來完成
題外話, 我用了以上的技巧寫了一個Dos版的source code Editor, 名稱叫E editor (下圖)。 平常工作時都會開個Dos模擬器跑Dos tool,享受Dos單純的美。
Key:
F1: Copy Line
F2: Paste Line
F3: Delete Line
F10: Save File
下載: (password: martinlee)
E Editor