Thread: Is this assembly?

  1. #1
    Registered User
    Join Date
    Jun 2008
    Posts
    17

    Question Is this assembly?

    Im currently correcting warnings on source code on Visual Studio.net 2003.

    Im down to the last 4, but I dont know what it is. This is the warning:

    Compiling...
    d3d_render.cpp
    3dc\win95\PENTIME.H(13) : warning C4068: unknown pragma
    3dc\win95\PENTIME.H(18) : warning C4068: unknown pragma
    3dc\win95\PENTIME.H(23) : warning C4068: unknown pragma
    3dc\win95\PENTIME.H(36) : warning C4068: unknown pragma
    And the code its refering to in pentime.h is:

    Code:
    //pentime.h
    extern unsigned long int rdtsc_lo(void);
    extern unsigned long int rdtsc_hi(void);
    extern unsigned long int rdtsc_mid(void);
    
    #define ProfileStart() \
    {						   \
    	int time = rdtsc_lo();
    #define ProfileStop(x) \
    	textprint("%s %d\n",x,rdtsc_lo()-time); \
    }
    
    #pragma aux rdtsc_lo = \
    	"db 0fh, 31h"	\
    	value [eax]		\
    	modify [edx];
    
    #pragma aux rdtsc_hi = \
    	"db 0fh, 31h"	\
    	value [edx]		\
    	modify [eax];
    
    #pragma aux rdtsc_mid = \
    	"db 0fh, 31h"	\
    	"shr eax, 10h" \
    	"shl edx, 10h" \
    	"add eax, edx" \
    	value [eax]		\
    	modify [edx];
    So is this assembly and is it for Watcom compilers? And if so can I convert this to Visual Studio?

    Thanks.
    Last edited by Bassquake; 06-09-2008 at 07:19 AM. Reason: Adding extra code snippet to make clearer

  2. #2
    Kernel hacker
    Join Date
    Jul 2007
    Location
    Farncombe, Surrey, England
    Posts
    15,677
    Yes, it's reading the time-stamp-counter (TSC).

    If you are using Visual Studio 2005 or later, you should be able to use __rdtsc() for all of the above, and wrap it the follwoing way:
    Code:
    unsigned int rdtsc_lo()
    {
       return (unsigned int)__rdtsc();
    }
    
    unsigned int rdtsc_hi()
    {
        return (unsigned int)(__rdtsc() >> (__int64)32);
    }
    
    unsigned int rdtsc_mid()
    {
       return (unsigned int)(__rdtsc() >> (__int64)16);
    }
    --
    Mats
    Compilers can produce warnings - make the compiler programmers happy: Use them!
    Please don't PM me for help - and no, I don't do help over instant messengers.

  3. #3
    Registered User
    Join Date
    Jun 2008
    Posts
    17
    Thanks for reply. I changed the code slightly to:

    Code:
    unsigned long int rdtsc_lo()
    {
       return (unsigned long int)__rdtsc();
    }
    to match the function, but it complains that:

    error C3861: '__rdtsc': identifier not found, even with argument-dependent lookup
    If I look at the definition of it, it points to the winnt.h file so I tried including it in this file to no avail even though its in the include directory in the projects settings.

    Unfortunatley I can only use Visual Studio 2003 as Im currently porting a game and its the most compatible compiler.

    Is there another way?

  4. #4
    Kernel hacker
    Join Date
    Jul 2007
    Location
    Farncombe, Surrey, England
    Posts
    15,677
    Ok, so you can use your own inline assembler for __rdtsc() then - there is an excample of this in WIkipedia:
    http://en.wikipedia.org/wiki/RDTSC

    --
    Mats
    Compilers can produce warnings - make the compiler programmers happy: Use them!
    Please don't PM me for help - and no, I don't do help over instant messengers.

  5. #5
    Registered User
    Join Date
    Jun 2008
    Posts
    17
    Ok. I put the following in pentime.h:

    Code:
    __declspec(naked)unsigned __int64 __cdecl rdtsc_lo(void)
    {
       __asm
       {
          rdtsc
          ret       ; return value at EDX:EAX
       }
    }
    which I assume replaces:

    Code:
    #pragma aux rdtsc_lo = \
    	"db 0fh, 31h"	\
    	value [eax]		\
    	modify [edx];
    Compiler gives no warning now, but what do those values in the original one mean? Like the "db 0fh, 32h"? Is what I put in replaces it correct? And where can I find out how to replace whats in here:

    Code:
    #pragma aux rdtsc_mid = \
    	"db 0fh, 31h"	\
    	"shr eax, 10h" \
    	"shl edx, 10h" \
    	"add eax, edx" \
    	value [eax]		\
    	modify [edx];
    Sorry for dumbness, Ive never handled assembly before.
    Last edited by Bassquake; 06-09-2008 at 09:29 AM. Reason: Accidently added post icon smily

  6. #6
    Kernel hacker
    Join Date
    Jul 2007
    Location
    Farncombe, Surrey, England
    Posts
    15,677
    No, you need to ADD the __rdtsc() function as defined in the wiki article on top of the code I wrote.

    The
    Code:
    db 0x0f, 0x31
    is the rdtsc instruction - apparently Watcom compiler doesn't understand this particular assembler instruction, so the code had to be added manually.

    The _lo, _hi, _mid functions are taking three different portions of the 64-bits that rdtsc returns: lo takes the low 32 bits, hi takes the high 32 bits, and mid takes 32 bits with 16 removed at the top and 16 at the bottom.

    By the way, you shouldn't have to add "long" to the unsigned int - the long and regular int are both 32 bits in Visual Studio [even for 64-bit builds].

    --
    Mats
    Compilers can produce warnings - make the compiler programmers happy: Use them!
    Please don't PM me for help - and no, I don't do help over instant messengers.

  7. #7
    Registered User
    Join Date
    Jun 2008
    Posts
    17
    Sorry, I dont really know what you mean. Do you mean like so:

    Code:
    __declspec(naked) unsigned __int64 __cdecl rdtsc(void)
    {
       __asm
       {
          rdtsc
          ret       ; return value at EDX:EAX
       }
    
    }
    unsigned int rdtsc_lo()
    {
       return (unsigned int)__rdtsc();
    }
    Still gives a error C3861: '__rdtsc': identifier not found, even with argument-dependent lookup

  8. #8
    C++まいる!Cをこわせ!
    Join Date
    Oct 2007
    Location
    Inside my computer
    Posts
    24,654
    I believe mats wants you to do:
    __declspec(naked) unsigned __int64 __cdecl __rdtsc(void)
    Remember, rdtsc != __rdtsc (different names).
    Quote Originally Posted by Adak View Post
    io.h certainly IS included in some modern compilers. It is no longer part of the standard for C, but it is nevertheless, included in the very latest Pelles C versions.
    Quote Originally Posted by Salem View Post
    You mean it's included as a crutch to help ancient programmers limp along without them having to relearn too much.

    Outside of your DOS world, your header file is meaningless.

  9. #9
    Registered User
    Join Date
    Jun 2008
    Posts
    17
    Ah yes. Forgot to post that I did that. Thanks.

    I have one last assembly bit to fix now. Its a fair bit more complicated. Its in the same pentime header file. Ill paste it here for now to avoid making another new thread, and hope to get some clues from some of you clever peeps.

    Code:
    /* Test to see if we have a Pentium or not. Note that this test is reliable 
     * enough for a tools project (where we can put in an overide switch) but not
     * for a released product.
     */
    extern unsigned char Pentium(void);
    #pragma aux Pentium = \
    					"pushfd" \
    					"pop eax" \
    					"or eax, 00200000h" \
    					"push eax" \
    					"popfd" \
    					"pushfd" \
    					"pop eax" \
    					"mov ecx, eax" \
    					"and eax, 00200000h" \
    					"cmp eax, 0" \
    					"je not_Pentium" \
    					"mov eax, ecx" \
    					"and eax, 0ffdfffffh" \
    					"push eax" \
    					"popfd" \
    					"pushfd" \
    					"pop eax" \
    					"and eax, 00200000h" \
    					"cmp eax, 0" \
    					"jne not_Pentium" \
    	"is_Pentium: 	 mov al, 1" \
    					"jmp finish" \
    	"not_Pentium:    mov al, 0" \
    	"finish: 		 nop" \
    					value [al] \
    					modify [eax ecx]

  10. #10
    Amazingly beautiful user.
    Join Date
    Jul 2005
    Location
    If you knew I'd have to kill you
    Posts
    254
    Considering the fact that any computer you are likely to deal with will be running a pentium or later at this point, why not look at the places that call this function, and see if you even need to worry about non pentium processors? a simple
    Code:
    unsigned char Pentium(void)
    {
      return 1;
    }
    could suffice.
    Programming Your Mom. http://www.dandongs.com/

  11. #11
    Registered User
    Join Date
    Jun 2008
    Posts
    17
    Couldnt find any reference in the source that asks for the Pentium() function.

    Seems the pentime.h which it resides in is called up at various points in the source as:

    Code:
    #define PENTIUM_PROFILING_ON 0
    #define PROFILING_ON 0
    #if PENTIUM_PROFILING_ON
    #include "pentime.h"
    #else
    #if SupportWindows95
    #define ProfileStart();
    #define ProfileStop(x);
    #endif
    #endif
    Except for the direct3d file which calls up the pentime.h without the if statement above. As the defines are all set at 0 throughout theyre never used. So maybe I can just remove all references for it.

    The final output for this is the xbox which uses a Pentium III so should be all right.

  12. #12
    Kernel hacker
    Join Date
    Jul 2007
    Location
    Farncombe, Surrey, England
    Posts
    15,677
    You may want to figure out what the PENTIUM_PROFILING does, because if you ever need to figure out where you are spending how much time, you may find it comes in handy.

    But for getting it to work, I'd ignore it.

    --
    Mats
    Compilers can produce warnings - make the compiler programmers happy: Use them!
    Please don't PM me for help - and no, I don't do help over instant messengers.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Learning Assembly
    By mrafcho001 in forum Tech Board
    Replies: 5
    Last Post: 03-12-2006, 05:00 PM
  2. C to assembly interface
    By Roaring_Tiger in forum C Programming
    Replies: 4
    Last Post: 02-04-2005, 03:51 PM
  3. assembly language...the best tool for game programming?
    By silk.odyssey in forum Game Programming
    Replies: 50
    Last Post: 06-22-2004, 01:11 PM
  4. True ASM vs. Fake ASM ????
    By DavidP in forum A Brief History of Cprogramming.com
    Replies: 7
    Last Post: 04-02-2003, 04:28 AM
  5. C,C++,Perl,Java
    By brusli in forum C Programming
    Replies: 9
    Last Post: 12-31-2001, 03:35 AM