Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

text editor in c using video memory

Tags:

c++

c

I am creating text editor using C. Can you explain these macros?

#define Ad (unsigned char far *)0xb8000000
#define Pos(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
like image 919
tushar Avatar asked Aug 29 '26 13:08

tushar


2 Answers

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.

like image 98
Timbo Avatar answered Sep 01 '26 03:09

Timbo


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.

like image 24
Will Dean Avatar answered Sep 01 '26 04:09

Will Dean