GIDForums  

Go Back   GIDForums > Computer Programming Forums > C++ Forum
User Name
Password
Register FAQ Members List Calendar Search Today's Posts Mark Forums Read

 
 
Thread Tools Search this Thread Rate Thread
  #1  
Old 20-Jul-2009, 15:34
bobcory bobcory is offline
New Member
 
Join Date: May 2005
Posts: 8
bobcory is on a distinguished road
Question

Putting a 64 bit word into two 32 bit registers and vice versa


This is one of those simple questions that is going to embarrass me once I know the answer but here goes

I want to use 64 bit words in VC++ as follows eg

__int64 iTest=0xAAAABBBBCCCCDDDD;

I then want to very simply put iTest into a couple of 32 bit registers using inline asm so that, for example,

eax == AAAABBBB

and

edx = CCCCDDDD

I then want to mess with eax and edx (eg doing shifts) and then put them back into iTest

Speed is absolutely crucial as this is part of a program that will run for days if i am not careful

Many thanks
Bob

Edit PS
I note that the disassembler does the following

323: __int64 i64Test=0xaaaabbbbccccdddd;
10003FE8 mov dword ptr [ebp-18h],0CCCCDDDDh
10003FEF mov dword ptr [ebp-14h],0AAAABBBBh

so I guess that is the answer (ie copy the memory locations to the registers)

Edit PPS
This does it:

__int64 iTest=0xaaaabbbbccccdddd;
__int64 *iptTest=&iTest;
_asm{
mov ecx, iptTest
mov edx, [ecx]
mov eax, [ecx+4]
}

Anybody got anything quicker or more elegant?
  #2  
Old 21-Jul-2009, 10:20
bobcory bobcory is offline
New Member
 
Join Date: May 2005
Posts: 8
bobcory is on a distinguished road

Re: Putting a 64 bit word into two 32 bit registers and vice versa


I think I have got this sussed now. There is very little stuff on the web re __int64 and inline asm so this may be helpful to somebody one day

Code:
int Long64BitIntTo32BitIntsLocal(__int64 lngWord, unsigned int *intFront, unsigned int *intEnd){ //Extract all the bits from a 64 bit word and put them into two 32 bit words //Written 21st July 2009 //Store the address of the 64 bit word __int64 *iptPointer=&lngWord; //Move the two sections to the two 32 bit ints _asm{ mov esi, iptPointer ;Put the Address of the 64 bit word into esi mov eax, dword ptr[esi] ;Put the VALUE of the BACK 32 bits into eax ... mov edi, intEnd ;Put the ADDRESS of BACK 32 bits into edi mov dword ptr[edi], eax ;Put the VALUE into *intEnd to mov eax, dword ptr[esi+4] ;Put the VALUE of the FRONT 32 bits into eax ... mov edi, intFront ;Put the ADDRESS of FRONT 32 bits into edi mov dword ptr[edi], eax ;Put the VALUE into *intFront to } return 0; }

Call it like this

Code:
unsigned int intFront; unsigned int intEnd; __int64 iTest=0xaaaabbbbccccdddd; Long64BitIntTo32BitIntsLocal(iTest, &intFront, &intEnd); //Note that intFront and intEnd are OUTPUT ie this is the result we wanted!

I find passing pointers to function calls a bit tricky as it seems to me that if you declare a pointer "eg int * ptrPointer" it doesn't exist until it does something so function calls don't seem to work if you do the following:

Code:
//THIS WILL NOT WORK unsigned int *intFront; unsigned int *intEnd; __int64 iTest=0xaaaabbbbccccdddd; Long64BitIntTo32BitIntsLocal(iTest, intFront, intEnd);

The above version of Long64BitIntTo32BitIntsLocal is deliberately complicated to show the use of pointers in asm!

This is an easy version of Long64BitIntTo32BitIntsLocal

Code:
int Long64BitIntTo32BitIntsLocalX(__int64 lngWord, unsigned int *intFront, unsigned int *intEnd){ //Extract all the bits from a 64 bit word and put them into two 32 bit words //Written 21st July 2009 //Store the address of the 64 bit word __int64 *iptPointer=&lngWord; //Set up temporary stores unsigned int int1; unsigned int int2; //Move the two sections to the temporary stores _asm{ mov ecx, iptPointer ;Put the Address of the 64 bit word into ecx mov eax, [ecx] ;Put the contents into eax ... mov int2, eax ;... and then into store mov eax, [ecx+4] ;Put the contents into eax ... mov int1, eax ;... and then into store } //Put the contents in the right place *intFront=int1; *intEnd =int2; return 0; }
 
 

Recent GIDBlogNot selected for officer school by crystalattice

Thread Tools Search this Thread
Search this Thread:

Advanced Search
Rate This Thread
Rate This Thread:

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

vB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Forum Jump

Network Sites: GIDNetwork · GIDWebHosts · GIDSearch · Learning Journal by J de Silva, The

All times are GMT -6. The time now is 14:58.


vBulletin, Copyright © 2000 - 2010, Jelsoft Enterprises Ltd.