• Advertisement
Sign in to follow this  

Assembly language and MinGW compilers

This topic is 2572 days old which is more than the 365 day threshold we allow for new replies. Please post a new topic.

If you intended to correct an error in the post then please contact us.

Recommended Posts


Quick one, I hope! I'm having to re-write a portion of my engine code which uses assembly language. I'm compiling on a GCC compiler not Visual C++. The code I'm using is written intended for Visual C++ but as I'm beginning to understand different compilers handle the compiling of assembly code differently. Here's a snippet from the Visual intended bit:

char *pStr = info.vendor;
int n=1;
int *pn = &n;

// set all values to 0 (false)
memset(&info, 0, sizeof(CPUINFO));

// 1: See if we can get CPUID and vendor 's name then
// check for SSE and MMX Support (vendor independant)
__try {
_asm {
mov eax, 0 // eax=0 => CPUID returns vendor name
CPUID // perform CPUID function

mov esi, pStr
mov [esi], ebx // first 4 chars
mov [esi+4], edx // next for chars
mov [esi+8], ecx // last 4 chars

mov eax, 1 // EAX=1 => CPUID returns feature bits
CPUID // perform CPUID (puts feature info to EDX)

From what I hear though the '_asm' function is either native to Visual C++ or is outdated. I can't remember which. Ok so I'm learning how to use the GCC version. It handles it a bit differently, namely for one - putting the source ahead of the destination of the operation. Here's a short demo of how th GCC wants it to be done (apparently this is called AT&T syntax):

movl $1,%eax // AT&T Syntax

As opposed to:

mov eax,1 // Intel syntax

It also doesn't use things like the 'h' for hex values and stuff which probably why my compiler spat out a few lines in the original author's code. Anyway, moving on - part of the tutorial I read said that simply putting a load of instructions in after the asm keyword isn't enough. Strange as that's exactly what does happen in the original code. The tutorial states that doing it this way will cause errors somehow by not informing the compiler properly of changed values in registers. Ok fair enough not really anything to do with me, me being a vanilla game programmer not a software engineer. The tutorial states that the proper way is to use a kind of declaration which has a specific structure/layout. An example is as follows:

int a=10, b;
asm("movl %1, %%eax; movl %%eax, %0;"
:"=r"(B) // output part
:"r"(a) // input part
:"%eax" // 'clobbered' register

Ok fair enough this is a bit more in depth. It also has the instinctive feeling of being a bit 'safer' for some reason, probably due to things being made quite explicit rather than implied which tends to == things getting screwed up by automatic processes not doing what you really want. Apparently the above method is safe and will make the compiler aware of what's took place in the register eax.

So before I go ahead and start re-writing the code in the file to work with my compiler (IDE is Code::Blocks by the way) I would appreciate it if someone could help me with the following questions:

1) Am I on the right track with my current thinking? Have I made any sense at all?

2) Is the extra more padded method described within the last code wraps in this topic the 'right' way to do it with the GCC compiler, will it work ok with Code::Blocks and the MinGW32 thing?

Thanks in advance for any help anyone can offer :)

Share this post

Link to post
Share on other sites
Sign in to follow this  

  • Advertisement