Getting eax,edx into char[4]

I am using Micro-C (http://www.dunfield.com) and need to access the pentium's cpu timestampusing opcode 0x0f31.
I can't use the datatype "long" as that is not supported by Micro-C. I need to get the 32bit contents of the two registers eax and edx into two char[4] arrays. How can I do that? Something like this:

unsigned char h[4],l[4];

  asm {
    rdtsc1   db      0fh
    rdtsc2   db      31h
             mov     _h,eax
             mov     _l,edx
  }

The inline assembler doesn't know about the RDTSC opcode so I fake it using DB.
Also the inline assembler doesn't understand eax or edx so I can't actually use those register names.

LVL 6
zebadaAsked:
Who is Participating?
 
dimitryConnect With a Mentor Commented:
First, I think that High part is in EDX and low part of
the counter is in EAX.
Second the solution depends how your compiler processes inline assembler...
BorlandC can compile correctly next stuff:

#pragma inline
unsigned char h[4],l[4];

void getRdtsc( void )
{
 asm {
   rdtsc1   db      0fh
   rdtsc2   db      31h
   mov dword ptr h, edx
   mov dword ptr l, eax
 }
}

And binary is:
getRdtsc:
 55         ; push bp
 8BEC       ; mov  bp, sp
 0F31       ; rdtsc
 6689160A00 ; mov [0000A], edx
 66A30600   ; mov [00006], eax
 5D         ; pop bp
 C3         ; retf
0
 
imladrisCommented:
Presumably, given that you are faking the opcode, the simplest solution is to just fake the whole thing. Generate the machine code with a regular assembler, and put the whole mess in with db and the inline assembler.

0
 
zebadaAuthor Commented:
great idea :)
But I'm no assembler programmer.
So how would I do that?
What are the opcodes?
What are the operands for:
1. globally defined char[4] variables
2. locally defined char[4] variables
3. function argument char[4] variables

Regards
Paul
0
Upgrade your Question Security!

Your question, your audience. Choose who sees your identity—and your question—with question security.

 
AvonWyssCommented:
If the inline assember is a 286 assembler (16-bit), you can use the following code:

asm {
  db 0fh,31h
  db 66h
  mov dword ptr h, edx
  db 66h
  mov dword ptr l, eax
}

The 66h (size prefix) opcode will tell the processor to use 32-bit registers instead of 16-bit registers.
0
 
zebadaAuthor Commented:
Thanks Dimitry, AvonWyss,

I will have a chance to check this out on the weekend.
I'll let you know how it goes.

See next related question...
0
 
DanRollinsCommented:
Another option:

unsigned char h32[4],l32[4];
asm {
    db  0fh
    db  31h
    mov word ptr [h32], dx
    mov word ptr [l32], ax
    db  c1h, eah, 10h  ; shr edx,16
    db  c1h, e8h, 10h  ; shr eax,16
    mov word ptr [h32+2], dx
    mov word ptr [l32+2], ax
}

but why are you messing around with a 16-bit compiler?  Someday soon, lots of people will have computers, not just the big governments.  When that day comes, computers may support 18 bits or even 20!

-- Dan
0
 
AvonWyssCommented:
Gee, I want a 21.7 bit computer... :-p
0
 
zebadaAuthor Commented:
Yeah, and I'll never use more than 640K of memory either :)

You're right, the real problem is me using a 16 bit compiler. I'd be much happier using a 32 or 64 bit compiler but the stuff I'm doing is for a free software project and I'm not prepared to go buy expensive stuff just so I can write free stuff for other people.
Do you know where I can get my hands on a free 32bit C compiler that compiles an executable with no reliance on any operating system? - i.e. it is totally self contained.

With MicroC I can create a program and with the tools supplied I can create a floppy and use that floppy to boot the PC into an "embedded" mode to run my executable with no dos just a *very* simple, single level directory/file access.

Or is there some way to prevent dos from screwing with my program? i.e. can I prevent ALL interrupts ALL of the time?

Paul

0
 
AvonWyssCommented:
CLI <- clear interrupt flag, diables ANY hardware interrupt at CPU level
STI <- set interrupt flag, re-enables the interrupts

For more selective control, you can toggle the bits in the interrupt mask register of the PICs (Programmable Interrupt Controller).
0
 
DanRollinsCommented:
>> Do you know where I can get my hands on a free 32bit C compiler...

Check out:
  http://www.borland.com/bcppbuilder/freecompiler

This is used in schools all over the place.  The free version does not provide an IDE.  dimitry's comment indicates that his BCC seems to compile _asm blocks just fine.

>>.. that compiles an executable with no reliance on any operating system? - i.e. it is totally self contained.

>>Or is there some way to prevent dos from screwing with my program?

There is nothing about C/C++ the that requires access to or uses an operating system. ... until you start using O/S features such as disk file access.  The standard libraries will, of neccessity, need to know how to start up a program and how to allocate memory, but all of that can be handled manually when it is really needed.

>>Or is there some way to prevent dos from screwing with my program..

In what way does the OS screw with your program?  

-- Dan

0
 
zebadaAuthor Commented:
I was using Borland's turbo C.
You are right about nothing in C/C++ requires a particular OS but surely some operating system dependence is introduced during the linking stage (stdin, stdout etc).

"Screws with" = interrupts.

If using CLI STI gaurantees that I won't be interrupted, even by the clock tick interrupt then I may be able to run under dos and a 32 bit compiler etc...

I need to play with all these options you guys have thrown at me - it'll be a busy weekend.

Hmmm - wonder how I can dodge going to this poncy dinner party tonight...

0
 
AvonWyssCommented:
CLI and STI will guarantee that you are not interrupted. Really.
0
 
zebadaAuthor Commented:
Sorry to leave this outstanding for so long - I just haven't had time to try out these suggestions. I will hopefully ba able to try over the next few weeks. If I don't get time I will allocate points anyway. Thanks for the info.
0
All Courses

From novice to tech pro — start learning today.