text editor in c using video memory
I am creating text editor using C. Can you explain these macros?
#define Ad (unsigned char far *)0xb8000000
#define 开发者_StackOverflowPos(y,x) (2*((y)*80+x))
#define Write(y,x,ch) *(Ad+Pos(y,x))=ch
#define WriteA(y,x,fb) *(Ad+1+Pos(y,x))=fb
Back in the dark ages of console programs, people used to output text by writing directly to the screen buffer.
The console screen buffer is organized as a continuous array of pairs of bytes, describing the displayed character and its attributes (color, background color, and eventually blinking).
In your case, the screen buffer seems to be at 0xb800000 (Ad
). Pos
translates a screen position (y,x)
to a memory offset in the screen buffer, assuming a screen width of 80.
Write
changes the displayed character at the specified position, while WriteA
changes the character's color.
My goodness, it's like 1990 all over again...
- Ad is the address of the section of memory which the video card (in text mode) maps onto its character generator. So if you write an ASCII 'A' to *Ad, then you'll get an 'A' at the top left of the screen.
- Pos is a macro which calculates an offset from the top left of the screen for an x,y position. (It's a dangerously broken macro, because there are no () around the 'x'.)
- Write writes a char to an (x,y) position
- WriteA writes character attributes (color, etc) to an (x,y) position.
I don't want to be harsh, but you're going to struggle to write a text editor if you're struggling at this level.
You do not want to use them.
Those macros are for old hardware, where the screen could be accessed directly.
- the first macro just specifies the base address of the screen (and
far
is not recognized by the Standard C) - the second macro converts a screen position to an offset
- the 3rd one writes a character to the screen
- the 4th one writes a colour to the screen
Again, you do not want to use those macros.
- Ad return the address of the beginning of the video memory
- Pos translate a x, y position into an absolute position in memory (assuming 80 chars width)
- Write put the character ch at the x,y position
- WriteA set the font color at the x,y position
精彩评论