Discussion Locked

Accessing 32 bit registers in C.

0 helpful
5 replies
Closed to new repliesThis discussion is closed. It stays readable, but it takes no new replies.

In masm it is possible to access 32 bit registers
using the .386 directive, how can I do it in the C programming language?
I would appreciate a small sample code block showing how to do it.

5 replies

by Member 27464914

The answer depends on which development tool and for which platform you are developing, and also the context in which you need to access the registers.
Since the registers are used by all running program code, reading them after a call to a function is likely to give you entirely wrong results as they may have been overwritten by the exit code of this function.
If you are programming for the DOS platform, you may use the i86.h library to access interrupts and reading/setting the registers inthat context.
Direct manipulation of the registers you can do in inline assembly, that is (in most windows/dos compilers):

unsigned long some_c_variable,some_other_c_variable
__asm {
mov eax, some_c_variable
mov eax, ecx
mov some_other_c_variable, eax
}

by mark.r.weisskopf

As the previous answer alluded to, the use of resisters in a C program is controlled by the compiler, the type of optimization, and many other factors. If what you are trying to do is get the program to do is store local long variables in a 32 bit register in stead of on the stack, some compillers support the "register" keyword

Good Luck
Mark

This discussion is closed to new replies.