News:

MASM32 SDK Description, downloads and other helpful links
MASM32.com New Forum Link
masmforum WebSite

Can CS,DS,ES,FS,GS, and SS be used in Win32?

Started by Mark Jones, April 12, 2005, 07:17:21 PM

Previous topic - Next topic

Mark Jones

dsouza123, is there any stipulations regarding the use of EDI and ESI? I guess ESP/EBP could be utilized also, with a lot of trickery. I was under the assumption that EDI and ESI were also reserved for some purpose? It's hard being a noob. :toothy
"To deny our impulses... foolish; to revel in them, chaos." MCJ 2003.08

hutch--

Mark,

In Windows you are required to preserve EBX ESI EDI ESP and EBP so you can make a mess of EAX ECX and EDX. Now the catch is the other way is that if you are using any of EAX ECX or EDX you must preserve them if you call a procedure that works according to the Windows convention.
Download site for MASM32      New MASM Forum
https://masm32.com          https://masm32.com/board/index.php

MazeGen

Quote from: Mark Jones on April 13, 2005, 05:22:17 PM
Ok then as a hack, it should be possible to:

1. push FS onto the stack
2. use FS in a local routine
3. pop FS?

GS does not like this at all... access violation.

You can't use segment register as general-purpose register in windows, beause it contains index to some tabels with segment descriptors.
It seems that GS in all versions of windows contains NULL selector, what means it is unused. Anyway, value of GS is oficially reserved and you shouldn't depend on the value. But if you like playing with it, you can fill it with values 0, 1, 2, 3 without any problems (it still means NULL selector).

Quote from: Mark Jones on April 13, 2005, 05:22:17 PMStrange, MOV FS,EAX does not give an error. It compiles to MOV FS,AX however.

That's special case. From the Intel instruction set reference:
Quote
When the processor executes the instruction with a 32-bit general-purpose register, it assumes that the 16 least-significant bits of the general-purpose register are the destination or source operand.
In fact, such instruction really compile to MOV FS,EAX in MASM, but most of dissemblers and debuggers (like OllyDbg) shows it like MOV FS,AX, probably because it doesn't want to confuse the users with different operand sizes. You can distinguish between EAX and AX in the instruction according to whether the first byte of the opcode is 66h (operand-size prefix) or not.

Quote from: Mark Jones on April 13, 2005, 05:22:17 PMAlso is it possible to use the debug registers DR0-7 for anything in Win32?
MOV DR0,EAX  yields a "privledged command" error.
You normally can't in user mode (ring3). But you can fill them in windows via structured exception handler. See CONTEXT structure.

AeroASM

In protected mode you will get an error if you put a value into a segment register that does not point to a valid selector in the GDT (or LDT??). I painfully found this out after spending weeks working out that my OS kept triple-faulting because I put 16h, not 16.