News:

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

Main Menu

Storing and restoring segment registers

Started by fredrikhr, March 12, 2025, 06:43:40 AM

Previous topic - Next topic

sinsi

Quote from: NoCforMe on March 13, 2025, 10:03:38 AMStill confused by your answer:
What's the difference between 16- and 32-bit modes and 16- and 32-bit instructions?

What perplexes me is your statement
QuoteBut code designed to run in a 32-bit mode will not run (for the most part) in a 16-bit CPU mode.
You know what 32-bit instructions are, 32-bit mode is a mode of the CPU.
When a computer boots it is in 16-bit mode (real mode). You then switch the CPU to 32-bit mode (protected mode).
With either mode you can use 32-bit instructions, with a few limitations. For example
    mov al,[edx]Legal in real mode, but due to segmentation EDX must be less than 65536 (0000FFFF maximum).
There are instructions which only work in protected mode and will cause the CPU to fault (#GP is common).

Windows versions up to Windows ME were basically a real mode EXE that had a built-in DOS extender to allow it to switch to protected mode. The biggest advantages were a flat memory model (using 64KB sectors was a real PITA) and hardware multitasking (although Windows used software multitasking).

Nowadays, protected mode is called compatibility mode and is a subset of 64-bit (long) mode.

FORTRANS

Hi,

Quote from: NoCforMe on March 13, 2025, 10:03:38 AMStill confused by your answer:
What's the difference between 16- and 32-bit modes and 16- and 32-bit instructions?

What perplexes me is your statement
QuoteBut code designed to run in a 32-bit mode will not run (for the most part) in a 16-bit CPU mode.

   Well, it's complicated.  An overly simplified explanation
follows.  Maybe, sort of.

   16-bit code is normally real mode (without protection) using a
segment:offset addressing scheme.  32-bit code is then protected
mode with selector based addressing.

   Current Intel (or other's) documentation should be used for a
good description of the operating characteristics of a 32-bit X86
processor.  The best I could scavenge is quoted below.  Best
meaning what I got before quitting.

QuoteINTEL 80386 PROGRAMMER'S REFERENCE MANUAL 1986

PART II SYSTEMS PROGRAMMING

Chapter 4 Systems Architecture
----------------------------------------------------------------------------

Many of the architectural features of the 80386 are used only by systems
programmers. This chapter presents an overview of these aspects of the
architecture.

The systems-level features of the 80386 architecture include:

  Memory Management
  Protection
  Multitasking
  Input/Output
  Exceptions and Interrupts
  Initialization
  Coprocessing and Multiprocessing
  Debugging

These features are implemented by registers and instructions, all of which
are introduced in the following sections. The purpose of this chapter is not
to explain each feature in detail, but rather to place the remaining
chapters of Part II in perspective. Each mention in this chapter of a
register or instruction is either accompanied by an explanation or a
reference to a following chapter where detailed information can be obtained.

(...)

Chapter 6 Protection

(...)

6.2 Overview of 80386 Protection Mechanisms

Protection in the 80386 has five aspects:

  1. Type checking
  2. Limit checking
  3. Restriction of addressable domain
  4. Restriction of procedure entry points
  5. Restriction of instruction set

(...)

Chapter 16 Mixing 16-Bit and 32 Bit Code

(...)

The 80386 functions most efficiently when it is possible to distinguish
between pure 16-bit modules and pure 32-bit modules. A pure 16-bit module
has these characteristics:

 . All segments occupy 64 Kilobytes or less.
 . Data items are either 8 bits or 16 bits wide.
 . Pointers to code and data have 16-bit offsets.
 . Control is transferred only among 16-bit segments.

A pure 32-bit module has these characteristics:

. Segments may occupy more than 64 Kilobytes (zero bytes to 4 gigabytes).
. Data items are either 8 bits or 32 bits wide.
. Pointers to code and data have 32-bit offsets.
. Control is transferred only among 32-bit segments.

HTH,

Steve N.

daydreamer

Quote from: sinsi on March 13, 2025, 10:52:45 AMYou know what 32-bit instructions are, 32-bit mode is a mode of the CPU.
When a computer boots it is in 16-bit mode (real mode). You then switch the CPU to 32-bit mode (protected mode).
With either mode you can use 32-bit instructions, with a few limitations. For example
    mov al,[edx]Legal in real mode, but due to segmentation EDX must be less than 65536 (0000FFFF maximum).
There are instructions which only work in protected mode and will cause the CPU to fault (#GP is common).
to fill or zero memory area using dword = 4 byte wide is faster than word and bytes = use 32bit register with non adressing way

there is a special load instruction that helps you use bigger adress range than 0-65535 indirectly
it helps you translate an adress in 1mb memory range to split between segment register and 16 bit register
but with games using many megabytes you need a dos extender

my none asm creations
https://masm32.com/board/index.php?topic=6937.msg74303#msg74303
I am an Invoker
"An Invoker is a mage who specializes in the manipulation of raw and elemental energies."
Like SIMD coding

sinsi

Quote from: daydreamer on March 14, 2025, 02:39:45 AM
Quote from: sinsi on March 13, 2025, 10:52:45 AMYou know what 32-bit instructions are, 32-bit mode is a mode of the CPU.
When a computer boots it is in 16-bit mode (real mode). You then switch the CPU to 32-bit mode (protected mode).
With either mode you can use 32-bit instructions, with a few limitations. For example
    mov al,[edx]Legal in real mode, but due to segmentation EDX must be less than 65536 (0000FFFF maximum).
There are instructions which only work in protected mode and will cause the CPU to fault (#GP is common).
to fill or zero memory area using dword = 4 byte wide is faster than word and bytes = use 32bit register with non adressing way

there is a special load instruction that helps you use bigger adress range than 0-65535 indirectly
it helps you translate an adress in 1mb memory range to split between segment register and 16 bit register
but with games using many megabytes you need a dos extender


It was an attempt to show a simple example.
I didn't want to gfet into unreal mode/flat real mode, way to complicated.