This program is to print name without using the header files

void main()
{
int i;
char far *s=(char far *)0xb8000000l ;
*(s+2)='p';
*(s+4)='r';
*(s+6)='i' ;
*(s+8)='i' ;
*(s+10)='t';
}

please explain the meaning of the line char far *s=(char far *)0xb8000000l ;

Dani AI

Generated

As said, that line is just making a pointer that targets the text-mode video buffer (the place the BIOS/adapter keeps characters for the screen). On IBM‑PC compatibles the colour text buffer lives at physical address 0xB8000 (segment B800h:offset 0000h). In 16‑bit compilers a "far" pointer holds a segment:offset pair, and some old code encodes that pair into a 32‑bit hex literal (segment in the high 16 bits, offset in the low 16 bits) before casting. (wiki.osdev.org)

“far” is a nonstandard, 16‑bit era qualifier that tells the compiler to generate segmented (segment:offset) accesses; a far data pointer was typically 4 bytes (segment + offset) while a near pointer was 2 bytes. Modern 32/64‑bit compilers don’t need it (it’s ignored or absent), so is right that you’ll normally drop it today. (en.wikipedia.org)

Why the original code wrote to offsets +2, +4, +6…? Text mode stores one byte for the character and one byte for its attribute (colour), interleaved. It’s clearer and faster to write 16‑bit character+attribute words instead of alternating byte stores. Example (only valid in real‑mode or kernel code / emulators):

volatile unsigned short *video = (volatile unsigned short *)0xB8000;
video[0] = (unsigned short)('P' | (0x07 << 8)); /* 'P' with attribute 0x07 (light grey on black) */

See the VGA/text‑mode layout for details. (wiki.osdev.org)

A few practical cautions: the direct memory trick works in DOS/real‑mode or inside a kernel or emulator; it will fault from normal user programs on modern OSes unless you map physical memory explicitly. If you’re compiling for a real DOS target, prefer MK_FP()/dos.h to build far pointers portably; otherwise remove the old far casts and use the linear 0xB8000 address only in appropriate environments. (wiki.osdev.org)

Thanks to , and for the original pointers — this fills in why the literal is written that way and what you must consider when trying it today.

Recommended Answers

All 5 Replies

Put simply, it gives you a pointer to the starting address of text mode video memory. Since the address doesn't change, you can cast it to a pointer to char and assign to offsets from that pointer to write to the appropriate memory blocks.

why this
(char far *)0xb8000000l is used??
please explain its meaning??

Your code is alittle old. In old 16 bit systems like DOS 6.0 there is a different memory addressing system named "segment" and far mentioned how the pointer can access to segments. But in nowadays 32 bit systems addressing is not via segment so there is no need to mention far.

I mean you could re-write your program as below

void main()
{
int i;
char *s=(char *)0xb8000000l ;
*(s+2)='p';
*(s+4)='r';
*(s+6)='i' ;
*(s+8)='i' ;
*(s+10)='t';
}

>why this
>(char far *)0xb8000000l is used??
>please explain its meaning??
Did you not read my post? I already explained that:

Since the address doesn't change, you can cast it to a pointer to char

It's taking a literal address and casting it to a pointer to char.

This is the first address(first row and column) of VDU memory where we see output.

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.