accessing protected mode registers via assembly in C

V

volvox

hi, is it possible to access protected mode registers (EAX ,
EBX,ECX...) via assembly in C .
 
K

Keith Thompson

volvox said:
hi, is it possible to access protected mode registers (EAX ,
EBX,ECX...) via assembly in C .

It's not possible to access registers or to use assembly at all in
standard C.
 
R

Rod Pemberton

volvox said:
hi, is it possible to access protected mode registers (EAX ,
EBX,ECX...) via assembly in C .

Yes, you can. But, not with standard C.

Also, it's not wise to do so unless you know assembly. And, you know
'intimate' details of how the compiler generates code.

DJGPP has the C '__asm__()' directive for assembly. OpenWatcom has the C
'_asm{}' directive and '#pragma aux' for assembly.

An example of an 'intimate' details you may need to know, most C routines
have a prolog and epilog that does things which are hidden from C
programmers.

Prolog:
1) saves the current stack frame
2) creates a new stack frame
3) push the passed parameters onto a local stack

Epilog:
1) removes current stack frame
2) restores the prior stack frame
3) assembly return statement

The implementation of these routines vary from compiler to compiler and even
within the same compiler. GCC based compilers use the ESP and EBP registers
to do stackframes. DJGPP generates (at least) four different prolog and
epilog sequences. OpenWatcom doesn't use an ESP/EBP stackframe at all. OW
uses the Intel assembly 'ret imm16' instruction to adjust ESP.

Also, many compilers dedicate certain registers for their own use. They are
called: 'callee-savee registers'. The values of these registers must be
preserved. For OW, the registers change for different options.

DJGPP ebx,edi,exi,ebp,ds,es,ss
OW -3s ebx,edi,esi,ebp
OW -3r ebx,edi,esi,ebp,ecx,edx

Another hurdle is this: it's very difficult to outperform the optimization
of a C compiler. Why? Because,
1) the C compiler optimizes the code it produces.
2) the C compiler won't optimize your "optimized" assembly code to work with
it's code.

This creates a conflict in the way the registers, stack, etc., are used by
your code and the compilers code. This introduces many wasteful
instructions into the equation to get the two code pieces working together.
In the cases I've seen, the compiler only needed to change an offset or two,
and perhaps reorder an instruction to do the same thing in C as my very
efficient hand coded routines...


Rod Pemberton
 
J

Jack Klein

Certainly not on an ARM, MIPS, Z80, 8051, or 99% of the other C
implementations in existence.
Yes, you can. But, not with standard C.

Then why not stop right there? Why wander into completely oft-topic
and potentially useless material?
Also, it's not wise to do so unless you know assembly. And, you know
'intimate' details of how the compiler generates code.

DJGPP has the C '__asm__()' directive for assembly. OpenWatcom has the C
'_asm{}' directive and '#pragma aux' for assembly.

What in the original post leads you to believe that the OP is using
DJGPP?
An example of an 'intimate' details you may need to know, most C routines
have a prolog and epilog that does things which are hidden from C
programmers.

Nobody needs to know any of these "intimate" details to do anything
that can be discussed topically in this group.

Please spare comp.lang.c the long, off-topic rambles.
 
R

Rod Pemberton

Jack Klein said:
Then why not stop right there? Why wander into completely oft-topic
and potentially useless material?

To show him the difficulties he'll face if he choses not to stick with C.
Nobody needs to know any of these "intimate" details to do anything
that can be discussed topically in this group.

You read my entire post and _DIDN'T_ realize I was discouraging him from
doing assembly in C?

If you didn't know assembly, would you try assembly in C after reading my
post?

Most of your posts are useful and intelligent, but today you are acting like
ass... Hopefully, you'll return to normal tomorrow.


Rod Pemberton
 
D

Dave Thompson

It's not possible to access registers or to use assembly at all in
standard C.

In addition to this (and other) correct answers that you can't, your
(OP's) question isn't even correct, assuming you mean >=386, as you
probably do as people using other architectures generally (are forced
to) know enough to realize they need to say which.

EAX etc. are 32-bit registers usable in both 32-bit protected mode and
32-bit real mode, although the latter is not too often used. AX etc.
are 16-bit registers usable in both 16-bit protected mode and 16-bit
real mode, although the former is almost never used.

- David.Thompson1 at worldnet.att.net
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

No members online now.

Forum statistics

Threads
474,432
Messages
2,571,682
Members
48,796
Latest member
Greg L.

Latest Threads

Top