News:

Masm32 SDK description, downloads and other helpful links
Message to All Guests
NB: Posting URL's See here: Posted URL Change

Main Menu

Segmented or flat

Started by ahsat, April 02, 2024, 03:08:14 PM

Previous topic - Next topic

ahsat

Does Windows 11 allow segmented assembly programs, or do they force a flat segment only in 32 bit mode?

BugCatcher

Even in 64 bit you have ds and cs registers ..data segment code segment

C3

Quote from: ahsat on April 02, 2024, 03:08:14 PMDoes Windows 11 allow segmented assembly programs, or do they force a flat segment only in 32 bit mode?

You can use segments in your program. But you dont need to use the MODEL. Microsoft Directives Reference helps you, it says this (.MODEL (32-bit MASM)).

You dont need to set CS,DS,SS registers anymore like in 16bit code.

sinsi

In 64-bit mode CS, DS, ES and SS all have base=0 and limit=2^64-1. FS and GS are used by the OS.

If you mean defining segments in your code, you can have as many as you want, but it's all linear.

ahsat

Quote from: C3 on April 03, 2024, 01:14:00 AMYou dont need to set CS,DS,SS registers anymore like in 16bit code.
Oh, but you are wrong there. Running multiple devices in a device driver, you only need to use different values in ds and ss, and the code can be written like it is supporting one device.

ahsat

Quote from: BugCatcher on April 03, 2024, 01:09:06 AMEven in 64 bit you have ds and cs registers ..data segment code segment
I am not sure, but I think the 64 bit architecture forces all the segment registers to zero. I think that otherwise, Intel would be supporting native 16 bit mode.

C3

Quote from: ahsat on April 03, 2024, 03:15:58 AM
Quote from: C3 on April 03, 2024, 01:14:00 AMYou dont need to set CS,DS,SS registers anymore like in 16bit code.
Oh, but you are wrong there. Running multiple devices in a device driver, you only need to use different values in ds and ss, and the code can be written like it is supporting one device.

Ok. I have not had any case where I should modify Segment Register values in 64bit land. I have not been working in Kernel level tough, only ring 3. And I remember there are not many cases listed in Intel manuals.

ahsat

Where can I find out how Windows 11 enters an assembly language program in 32 and 64 bit mode? ie, what is in the registers, how large is the stack etc.

C3

Quote from: ahsat on April 03, 2024, 03:24:05 AMWhere can I find out how Windows 11 enters an assembly language program in 32 and 64 bit mode? ie, what is in the registers, how large is the stack etc.

I'm using Visual Studio and use it's default entry points. You can change default with settings or with a parameter to ml64.exe. 32bit uses "END start" to define start: label as entry point. Search the default stack size from Google. I can change that with the Visual Studio also. Had never issues of stack overflow :D

ahsat

Quote from: C3 on April 03, 2024, 03:48:10 AMYou can change default with settings
But what is in the registers when Windows enters your/my program. That is documented somewhere, and they usually load up the registers with useful information.

C3

Quote from: ahsat on April 03, 2024, 04:04:50 AM
Quote from: C3 on April 03, 2024, 03:48:10 AMYou can change default with settings
But what is in the registers when Windows enters your/my program. That is documented somewhere, and they usually load up the registers with useful information.

Ok, I think you need to search about PE File / PE Loader.


Vortex

#12
Hi ahsat,

Opposed to the 64 Kb segmented memory model of the ancient DOS system, the modern 32\64 bit programming adopts the flat memory model making easier coding. You don't have to swich from one memory bank to another one in the flat memory address spacing. Hutch's help files :

\masm32\help\asmintro.chm
QuoteFlat memory Model

A program written in native 32 bit Windows format is created in what is called FLAT memory model which has a single segment that contains both code and data. The programs must be run on a 386 or higher processor.

Differing from earlier 16 bit code that used combined segment and offset addressing with a 64k segment limit, FLAT memory model works only in offsets and has a range of 4 gigabytes. This makes assembler easier to write and the code is generally a lot faster.

All segment registers are automatically set to the same value with this memory model and this means that segment / offset addressing must NOT be used in 32 bit programs that run in 32 bit Windows.
For programmers who have written code in DOS, a 32 bit Windows PE executable file is similar in some respects to a dos COM file, they have a single segment that can contain both code and data and they both work directly in offsets, neither use Segment / Offset addressing.

The defaults in flat-model programs are NEAR code addressing and NEAR data addressing within the range of 4 gigabytes.

The FS and GS segment registers are not normally used in application programs but are used in some instances by the operating system.

QuoteRegisters

Registers are conceptually a special working area within the processor that are faster than memory operands and are designed to work with the processors opcodes.

Registers in an Intel or compatible processor are a very limited resource when writing assembler, you have eight general purpose registers, EAX, EBX, ECX, EDX, ESI, EDI, ESP and EBP. In most instances ESP and EBP should be left alone as they are mainly used for entry and exit of procedures.

This means affectively, you have six 32 bit registers to write you code with plus any other memory locations that are useful in the procedure. ESI and EDI can be used in the normal manner in most instances but neither can be accessed at a BYTE level, you can read the low WORD of ESI as SI and the low WORD of EDI as DI.

NoCforMe

#13
Quote from: ahsat on April 03, 2024, 04:04:50 AMBut what is in the registers when Windows enters your/my program. That is documented somewhere, and they usually load up the registers with useful information.
Sounds like you might want to write a li'l test program that first thing stores all registers in memory variables and then prints them out. Remember, at your program's entry point, the registers are just as the OS program loader left them.

I think the only useful info you'll find there is maybe a pointer to the "command line" used to invoke the program.
Go and read the post that sinsi linked to. There is no useful information in the registers as they're set at your program's entry point. (Not even the "command tail".)

BTW, meta-note to the OP: All the things you posted here would be better put in the active coding sub-forums, the Campus, Workshop or the Laboratory, rather than in this somewhat obscure one (nothing you've posted here really relates to "projects"). Just sayin'.
Assembly language programming should be fun. That's why I do it.