China DOS Union

-- Unite DOS · Advance DOS · Grow DOS --

Union site: www.cn-dos.net Forum site: www.cn-dos.net/forum
DOS stands for freedom, openness and progress. Let us work hard, learn from the openness and GNU spirit of FreeDOS and Linux, and together build and grow a free GNU GPL world!

中国DOS联盟论坛
The time now is 2026-08-01 02:21
中国DOS联盟论坛 » DOS开发编程 & 发展交流 (开发室) » [Help] About DJGPP protected mode? View 882 Replies 7
Original Poster Posted 2003-09-26 00:00 ·  中国 江苏 扬州 联通
初级用户
Credits 169
Posts 21
Joined 2003-09-07 00:00
22-year member
UID 9566
Gender Female
Status Offline
I’m a beginner learning DJGPP. All the introductions emphasize that it can access 4G of space at one time. I feel its syntax is about the same as C, but when I programmed in C before, I allocated memory through arrays or pointers, so in DJGPP, how should I access such a large address space?
I noticed that after an error, DJ reports a segment address and an offset. How are these things set?
Please give me some guidance, experts! Thanks
Floor 2 Posted 2003-09-26 00:00 ·  新加坡 星和视界(StarHub)宽带
初级用户
Credits 169
Posts 29
Joined 2003-07-09 00:00
23-year member
UID 6654
Gender Male
Status Offline
You should use the _far* family of functions in sys/farptr.h to access memory. The following function prototypes are from the libc help documentation of djgpp v2.0x.

#include

unsigned char _farpeekb(unsigned short selector, unsigned long offset);
unsigned short _farpeekw(unsigned short selector, unsigned long offset);
unsigned long _farpeekl(unsigned short selector, unsigned long offset);

void _farpokeb(unsigned short sel, unsigned long off, unsigned char val);
void _farpokew(unsigned short sel, unsigned long off, unsigned short val);
void _farpokel(unsigned short sel, unsigned long off, unsigned long val);

void _farsetsel(unsigned short selector);
unsigned short _fargetsel(void);

void _farnspokeb(unsigned long offset, unsigned char value);
void _farnspokew(unsigned long offset, unsigned short value);
void _farnspokel(unsigned long offset, unsigned long value);

unsigned char _farnspeekb(unsigned long offset);
unsigned short _farnspeekw(unsigned long offset);
unsigned long _farnspeekl(unsigned long offset);

Assume the memory address you want to access is:
unsigned long ulAddr = 0x0000a000;
Use _farpokeb(_dos_ds, ulAddr, byte) to write one byte, and use _farpeekb() to read one byte. _dos_ds is a system predefined value, so just use it directly.
If you want to access a continuous memory address range, then you can:
unsigned int i, wLen = 0x1000;
_farsetsel(_dos_ds);
for (i = 0; i < wLen; i++) {
printf("0x%02hX\t", _farnpeekb(ulAddr + i);
}

ps: I suggest you read the djgpp faq carefully. If you asked this kind of question on the djgpp mailing list, they would only answer you with faq 10.2 or 18.x.


Floor 3 Posted 2003-09-28 00:00 ·  中国 陕西 西安 教育网
高级用户
★★
OS/2女孩
Credits 639
Posts 183
Joined 2003-06-14 00:00
23-year member
UID 5148
Gender Female
Status Offline
Hmm, how did you do it before in C??

Using the MALLOC and FREE functions to allocate memory, right? I believe that’s what you did.

Then keep using them now, they will still be your old friends.

As for pointers, DJGPP has not abandoned the concept of pointers in C.

But note that this is the pointer concept of the C language, not the concept as implemented by TC and the like.
(Of course, every language always depends on its implementation to a greater or lesser extent.)

So, you can still use “C language pointers”

===================================================
So what is protected mode?

It is actually an evolution in programming, another abstraction (ABSTRACT).

It abstracts the memory access mechanism of 80386 and above machines, freeing countless programmers from the heavy memory access mechanism
(from the shackles of the lower level).

So, congratulations. You can use abstracted methods to access 4G of memory now, instead of using complicated memory paging access techniques
(in fact this is not possible, because under DOS7.1 you can only use 2G at most, and in the WINXP series 4G is accessible,
but half of that is reserved for the system)

====================================================

So, how do you access this memory? Very simple:

Use the MALLOC function and allocate a 2G block of memory to a pointer.
(Of course, your machine won’t have that much memory, so first try some big number like 20M,
which would have been unimaginable before)

Please note: there is one difference between this pointer and the pointers you used before.

That is, there is no distinction between near and far. (Things like FAR MALLOC and FAR FREE are no longer needed.)
In this way, your pointer can map directly to your memory, and there is no need to care about shackles like 640K or 1M
anymore.

What are you waiting for, go try it!





===============================================

Also, there is another technical detail, namely:

After allocation through the MALLOC function, please do not try to use functions like MEM to check the remaining amount of memory
. Even though you allocated 20M or more, in fact they have not been initialized (actually it’s not exactly like that, but
explaining it this way makes it easier for everyone to understand). Only when you actually write 20M of content into it
will it really be allocated and used.

To put it simply, after using MALLOC to call large memory, then using a function like MEM to check memory allocation, you will not see
any change. This is very different from using TC before (I really don’t know what they were thinking, dizzy)


Completed on 2003/09/26
REM 喜欢DOS,因为它的简单
REM 喜欢OS/2,因为它不再矫饰
REM 喜欢BASIC,因为它并不幼稚
REM 喜欢GNU,因为它杂乱无章
Floor 4 Posted 2003-09-28 00:00 ·  中国 陕西 西安 教育网
高级用户
★★
OS/2女孩
Credits 639
Posts 183
Joined 2003-06-14 00:00
23-year member
UID 5148
Gender Female
Status Offline
ATLaS
It’s clear that you have a solid foundation in programming with DJGPP and the like. Are you interested in joining my group?

Take part in the Chinese localization work for DJGPP. Right now we are writing a Chinese DJGPP FAQ and
a DJGPP user guide (one is a translation, the other is a complete rewrite)

If you’re interested, reply to the thread.
REM 喜欢DOS,因为它的简单
REM 喜欢OS/2,因为它不再矫饰
REM 喜欢BASIC,因为它并不幼稚
REM 喜欢GNU,因为它杂乱无章
Floor 5 Posted 2003-09-29 00:00 ·  新加坡 星和视界(StarHub)宽带
初级用户
Credits 169
Posts 29
Joined 2003-07-09 00:00
23-year member
UID 6654
Gender Male
Status Offline
to lemonhall:
Hehe, sure! Some time ago I really did spend some time studying djgpp. I originally had a project I planned to do under djgpp, but because of the multithreading issue, plus schedule requirements, I switched back to watcom c. But I’ve always remained quite interested in djgpp.
Is there any specific work I can help with (because of work, my progress may be a bit slow, hehe)?
Floor 6 Posted 2003-09-29 00:00 ·  中国 陕西 西安 教育网
高级用户
★★
OS/2女孩
Credits 639
Posts 183
Joined 2003-06-14 00:00
23-year member
UID 5148
Gender Female
Status Offline
The specific work is to write a DJGPP usage guide. You’re relatively familiar with memory and protected mode, so could you take charge of writing that part?
Write a first draft and let me have a look.
Send it to my mailbox:
lemonhall@vip.sina.com

My website has changed address: http://lemonhall.533.net/ Go take a look. There haven’t been many updates lately.
REM 喜欢DOS,因为它的简单
REM 喜欢OS/2,因为它不再矫饰
REM 喜欢BASIC,因为它并不幼稚
REM 喜欢GNU,因为它杂乱无章
Floor 7 Posted 2003-09-29 00:00 ·  新加坡 星和视界(StarHub)宽带
初级用户
Credits 169
Posts 29
Joined 2003-07-09 00:00
23-year member
UID 6654
Gender Male
Status Offline
These parts are explained fairly clearly enough in the faq, right? Then I’ll try using some small examples to explain the differences between djgpp and borland c/watcom c? I could include the external interrupt part too, what do you think? But please don’t rush me on the schedule, I’ve got a lot of work stuff too.

My email is atlas_wang@hotmail.com。
Floor 8 Posted 2003-09-30 00:00 ·  中国 广东 广州 北京思博展科科技有限公司
高级用户
★★
OS/2女孩
Credits 639
Posts 183
Joined 2003-06-14 00:00
23-year member
UID 5148
Gender Female
Status Offline
Right, just use short articles to explain it. It needs to be somewhat different from the FAQ, thanks
REM 喜欢DOS,因为它的简单
REM 喜欢OS/2,因为它不再矫饰
REM 喜欢BASIC,因为它并不幼稚
REM 喜欢GNU,因为它杂乱无章
Forum Jump: