Solved

Getting eax,edx into char[4]

Posted on 2002-03-19
14
808 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
Live: Real-Time Solutions, Start Here

Receive instant 1:1 support from technology experts, using our real-time conversation and whiteboard interface. Your first 5 minutes are always free.

 
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

Gigs: Get Your Project Delivered by an Expert

Select from freelancers specializing in everything from database administration to programming, who have proven themselves as experts in their field. Hire the best, collaborate easily, pay securely and get projects done right.

Question has a verified solution.

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

Suggested Solutions

Title # Comments Views Activity
IRQL_NOT_LESS_OR_EQUAL for Kernel Timer Thread 7 579
Binary Bomb: Phase 5 3 1,661
Assemlby_ARM 3 513
Assembly Language 5 382
When we purchase storage, we typically are advertised storage of 500GB, 1TB, 2TB and so on. However, when you actually install it into your computer, your 500GB HDD will actually show up as 465GB. Why? It has to do with the way people and computers…
Read about the ways of improving workplace communication.
The Email Laundry PDF encryption service allows companies to send confidential encrypted  emails to anybody. The PDF document can also contain attachments that are embedded in the encrypted PDF. The password is randomly generated by The Email Laundr…
A short tutorial showing how to set up an email signature in Outlook on the Web (previously known as OWA). For free email signatures designs, visit https://www.mail-signatures.com/articles/signature-templates/?sts=6651 If you want to manage em…

785 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