Question for assembly language programmers

Started by
5 comments, last by Raab314159 21 years, 1 month ago
Do you know what the FLAT memory model means? I know it's possible to adress up to 4 GB, but what about segement registers? Are there still different segments like in real mode? Secondly, have you ever managed to draw, for example, pixels on the Windows desktop without using an API function, but just by writing to video memory like in mode 13H? I ask you this because I used to program in real mode and now that I'm programming in protected mode, everything seems to work different. [edited by - Raab314159 on March 5, 2003 2:32:14 PM] [edited by - Raab314159 on March 5, 2003 2:41:28 PM] [edited by - Raab314159 on March 5, 2003 3:32:54 PM]
Advertisement
Flat memory model doesn't have "pages"... Page addressing looked like this: A000:0001

where the first number was multiplied by 16 and added to the second... a pretty useless idea nowdays.


Flat memory addressing on IA-32 (or x86 if you want to call it that) is simple addressing like 0xA0001.

I don't know about real-mode programs, but in protected mode, the "segment" registers (CS, DS, SS, etc) are now "selectors". Remember hearing how protected mode keeps programs inside their own address space? A segment is the processor's way of separating up memory -- if your code tries to access memory outside of its segment, you get your typical "segment violation" crash.

I don't remember exactly how the selector numbers are laid out, or how the processor determines which process has read/write access to certain areas (I hated Operating Systems class)... someone else may have more experience with those.



As for the second part of the question -- I'm fairly sure that the video memory is not in one of the areas of memory that any of your programs have access to... The only TRUE way to get at that memory is to write a kernel-mode driver (you WILL need a book to learn how to make drivers)

If all you're concerned with is drawing to the desktop DC, you can simply use HWND 0 (zero) in the Win32 API drawing functions.

[edited by - Nypyren on March 5, 2003 3:45:54 PM]
Two second Google search: "Essentially, a flat model operates as if there were no segments".

Also, I don''t believe you can just start writing to video memory in windows; most likely the program would crash with an access violation. That''s one of the reasons DirectX was developed, to overcome the fact that drawing graphics in windows was a gigantic pain in the butt.
quote:but in protected mode, the "segment" registers (CS, DS, SS, etc) are now "selectors".


Selectors select segments from the GDT and LDT don''t they? Does windows actaully use them?
quote:what about segement registers? Are there still different segments like in real mode?

Yes (segmentation cannot be disabled), but the concept of ''segment'' has changed; see below.

quote:have you ever managed to draw, for example, pixels on the Windows desktop without using an API function, but just by writing to video memory like in mode 13H?

Yes. On Win9x (unlike NT, where ''all your hardware are belong to us''), you could:
1) shut down GDI and go directly to the framebuffer
2) get CPL0 access via one of several security holes and do the same
3) use VBE (VESA BIOS extensions) calls to set a mode and then map the FB - this gets you SVGA resolution with a linear frame buffer à la Mode 13. Sweet!

#1 does not work on NT; sadly, they also haven''t bothered to support VBE (or they didn''t want an alternative to DirectDraw/Graphics/whatever it''s called now).
Now that I think about it, #2 is possible at the moment, but surely they''re going to patch the kernel object ACL hole soon? :S

Whatever way you go about it, you need to know what you''re doing. One question: why?


quote:Flat memory model doesn''t have "pages"... Page addressing looked like this: A000:0001

Page is a bit misleading. I''ve heard the segment address referred to as paragraph; page usually denotes a 4 KB (or 2 / 4 MB, if so set) range of memory.


quote:Flat memory addressing on IA-32 (or x86 if you want to call it that) is simple addressing like 0xA0001.

That''s the way it appears to the app. However, everything is still relative to the segments referenced by cs, ds, es, ss - you just don''t notice (a segment is basically a chunk of memory with a base address, length, and type/flags).

quote:I don''t know about real-mode programs, but in protected mode, the "segment" registers (CS, DS, SS, etc) are now "selectors". Remember hearing how protected mode keeps programs inside their own address space? A segment is the processor''s way of separating up memory -- if your code tries to access memory outside of its segment, you get your typical "segment violation" crash.

This is a contradiction to the above. As Anthracks states, flat mode means segments are hidden as much as possible, i.e. base of segment = 0, length = 4 gb (may be truncated though for OS memory, non-exec stack etc.).

> I don''t remember exactly how the selector numbers are laid out
A selector is an index (offset actually) into a descriptor table, which holds segment parameters.

quote:or how the processor determines which process has read/write access to certain areas (I hated Operating Systems class)... someone else may have more experience with those.

Usually, access control is done with paging - each app has a different mapping, and OS pages are marked supervisor (=> can only be accessed from CPL0).

quote:As for the second part of the question -- I''m fairly sure that the video memory is not in one of the areas of memory that any of your programs have access to... The only TRUE way to get at that memory is to write a kernel-mode driver (you WILL need a book to learn how to make drivers)

Yes, that is the intent. This can be circumvented, though, as above.
E8 17 00 42 CE DC D2 DC E4 EA C4 40 CA DA C2 D8 CC 40 CA D0 E8 40E0 CA CA 96 5B B0 16 50 D7 D4 02 B2 02 86 E2 CD 21 58 48 79 F2 C3
Protected mode memory management on the X86 is rather complex. What I suggest doing is getting the Intel Architecture manuals (volume 3 is the Systems Programming manual) and reading it (although it will probably seem overwhelming at first.)

There are lots of tutorials on protected mode out there so you may wish to invest some time in writing a program for DOS (this will only work if you have DOS or Win9X so you can boot to pure DOS mode) which jumps into protected mode itself.

You don''t have to know how to make use of all of its features, but try setting up a GDT and perhaps trapping interrupts. You''ll then understand how selectors work and just how the OS can restrict programs.

It''s a lot of work, but worth understanding, because unless you experience it firsthand, all of this segment/selector/LDT/GDT/TSS/IDT/paging/privilege/etc. stuff can be very confusing.

Other, more modern ISAs (Alpha, SPARC, MIPS, etc.) don''t have different modes of operation as the X86 does and their MMUs are far simpler and usually based only on pages. AFAIK, the only recent big-name architectures that are also fairly ugly MMU-wise are PowerPC and IA-64, but nowhere near as bad as X86.


----
Bart
----Bart
Thanks a lot, guys. You convinced me that it''s not an impossible task to learn about protected mode, so I will drop my letter of application for the butcher job and keep on programming!

This topic is closed to new replies.

Advertisement