Turbo C++ ... Segment Offset

Question about which tools to use, bugs, the best way to implement a function, etc should go here. Don't forget to see if your question is answered in the wiki first! When in doubt post here.
Post Reply
User avatar
matias_beretta
Member
Member
Posts: 101
Joined: Mon Feb 26, 2007 3:39 pm

Turbo C++ ... Segment Offset

Post by matias_beretta »

Well, I'm writing my realmode os with Turbo C++ and when I want to write to the video memory i have to:

asm push 0xb800;
asm pop es;
asm mov di, 0;
asm mov byte ptr es:[di], 'M'
asm inc di
asm mov byte ptr es:[di], 7
asm inc di

this writes M on the screen, but it is written in assembly

is there any way of accessing memory with a segment and an offset from C?

thanks
Matías Beretta
raevin
Member
Member
Posts: 33
Joined: Tue Dec 28, 2004 12:00 am

Re: Turbo C++ ... Segment Offset

Post by raevin »

matias_beretta wrote:Well, I'm writing my realmode os with Turbo C++ and when I want to write to the video memory i have to:

asm push 0xb800;
asm pop es;
asm mov di, 0;
asm mov byte ptr es:[di], 'M'
asm inc di
asm mov byte ptr es:[di], 7
asm inc di

this writes M on the screen, but it is written in assembly

is there any way of accessing memory with a segment and an offset from C?

thanks
Have you looked at any of the beginner printf() functions various operating system development tutorials give you?
User avatar
JamesM
Member
Member
Posts: 2935
Joined: Tue Jul 10, 2007 5:27 am
Location: York, United Kingdom
Contact:

Post by JamesM »

Not AFAIK. Your best bet is to go into protected mode.
jnc100
Member
Member
Posts: 775
Joined: Mon Apr 09, 2007 12:10 pm
Location: London, UK
Contact:

Post by jnc100 »

There is nothing defined in the C standard for accessing memory via segments, because segments are pretty intel-specific. There are compiler-specific extensions however. I don't know about Turbo C++, but open watcom allows something like:

Code: Select all

__segment seg;
seg = 0xb800;
unsigned short int __based (seg) *dispmem;
dispmem[y * 80 + x] = ch | (attrib << 8);
Obviously its not portable, but then segments aren't anyway.

Regards,
John.
User avatar
Brynet-Inc
Member
Member
Posts: 2426
Joined: Tue Oct 17, 2006 9:29 pm
Libera.chat IRC: brynet
Location: Canada
Contact:

Post by Brynet-Inc »

Code: Select all

#define MK_FP( seg, off ) \
         ((void far *)(((unsigned long)(seg) << 16)|(unsigned)(off)))

unsigned char far *video_mem = (char far *)MK_FP(0xB800, 0x0000);
This "might" work under "Turbo C"..

I can't verify it will though, I simply used my google skills...

Good luck.. 8)
Image
Twitter: @canadianbryan. Award by smcerm, I stole it. Original was larger.
frank
Member
Member
Posts: 729
Joined: Sat Dec 30, 2006 2:31 pm
Location: East Coast, USA

Post by frank »

You could create far_peek and far_poke functions in assembly.
User avatar
Combuster
Member
Member
Posts: 9301
Joined: Wed Oct 18, 2006 3:45 am
Libera.chat IRC: [com]buster
Location: On the balcony, where I can actually keep 1½m distance
Contact:

Post by Combuster »

IIRC Turbo C has got MK_FP and several related functions predefined in one of its headers (include dos.h / io.h?). Since TC supports far pointers (seg:off pairs), you should be able to write directly to video memory without resolving to (inline) assembly.

TC3 does come with an extensive help system. You should look up the mentioned functions and headers to find the things you need.
"Certainly avoid yourself. He is a newbie and might not realize it. You'll hate his code deeply a few years down the road." - Sortie
[ My OS ] [ VDisk/SFS ]
User avatar
matias_beretta
Member
Member
Posts: 101
Joined: Mon Feb 26, 2007 3:39 pm

okay but...

Post by matias_beretta »

int main(void)
{
unsigned int far *screen;
*screen = MK_FP(0xb800, 0)
}

ERROR!!!: Nonportable pointer conversion in function main

What does it means???
Matías Beretta
Post Reply