Want to win a PS4? Go Premium and enter to win our High-Tech Treats giveaway. Enter to Win

x
?
Solved

Getting eax,edx into char[4]

Posted on 2002-03-19
14
Medium Priority
?
815 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
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
  • 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 400 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
[Webinar] Lessons on Recovering from Petya

Skyport is working hard to help customers recover from recent attacks, like the Petya worm. This work has brought to light some important lessons. New malware attacks like this can take down your entire environment. Learn from others mistakes on how to prevent Petya like worms.

 
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
 
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

Free Tool: IP Lookup

Get more info about an IP address or domain name, such as organization, abuse contacts and geolocation.

One of a set of tools we are providing to everyone as a way of saying thank you for being a part of the community.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Here in this article, you will get a step by step guidance on how to restore an Exchange database to a recovery database. Get a brief on Recovery Database and how it can be used to restore Exchange database in this section!
If something goes wrong with Exchange, your IT resources are in trouble.All Exchange server migration processes are not designed to be identical and though migrating email from on-premises Exchange mailbox to Cloud’s Office 365 is relatively simple…
This course is ideal for IT System Administrators working with VMware vSphere and its associated products in their company infrastructure. This course teaches you how to install and maintain this virtualization technology to store data, prevent vuln…
How to fix incompatible JVM issue while installing Eclipse While installing Eclipse in windows, got one error like above and unable to proceed with the installation. This video describes how to successfully install Eclipse. How to solve incompa…

610 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