Solved

Getting eax,edx into char[4]

Posted on 2002-03-19
14
805 Views
Last Modified: 2008-02-01
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.

0
Comment
Question by:zebada
  • 5
  • 4
  • 2
  • +2
14 Comments
 
LVL 16

Expert Comment

by:imladris
ID: 6880879
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
 
LVL 6

Author Comment

by:zebada
ID: 6880892
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
 
LVL 11

Accepted Solution

by:
dimitry earned 100 total points
ID: 6882104
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
 
LVL 14

Expert Comment

by:AvonWyss
ID: 6889970
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
 
LVL 6

Author Comment

by:zebada
ID: 6890027
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
 
LVL 49

Expert Comment

by:DanRollins
ID: 6917632
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
Highfive Gives IT Their Time Back

Highfive is so simple that setting up every meeting room takes just minutes and every employee will be able to start or join a call from any room with ease. Never be called into a meeting just to get it started again. This is how video conferencing should work!

 
LVL 14

Expert Comment

by:AvonWyss
ID: 6917695
Gee, I want a 21.7 bit computer... :-p
0
 
LVL 6

Author Comment

by:zebada
ID: 6917709
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
 
LVL 14

Expert Comment

by:AvonWyss
ID: 6917903
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
 
LVL 49

Expert Comment

by:DanRollins
ID: 6919246
>> 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
 
LVL 6

Author Comment

by:zebada
ID: 6919289
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
 
LVL 14

Expert Comment

by:AvonWyss
ID: 6919364
CLI and STI will guarantee that you are not interrupted. Really.
0
 
LVL 6

Author Comment

by:zebada
ID: 7315179
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

Featured Post

What Security Threats Are You Missing?

Enhance your security with threat intelligence from the web. Get trending threat insights on hackers, exploits, and suspicious IP addresses delivered to your inbox with our free Cyber Daily.

Join & Write a Comment

Suggested Solutions

In this article, I will show you HOW TO: Suppress Configuration Issues and Warnings Alert displayed in Summary status for ESXi 6.5 after enabling SSH or ESXi Shell.
What is Backup? Backup software creates one or more copies of the data on your digital devices in case your original data is lost or damaged. Different backup solutions protect different kinds of data and different combinations of devices. For e…
Internet Business Fax to Email Made Easy - With eFax Corporate (http://www.enterprise.efax.com), you'll receive a dedicated online fax number, which is used the same way as a typical analog fax number. You'll receive secure faxes in your email, fr…
This video discusses moving either the default database or any database to a new volume.

759 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question

Need Help in Real-Time?

Connect with top rated Experts

18 Experts available now in Live!

Get 1:1 Help Now