Server Help

Trash Talk - ASM in GCC

Cyan~Fire - Thu Sep 02, 2004 9:13 pm
Post subject: ASM in GCC
I'm just nearing the end of modding the MERV source for it to compile under MinGW (Dev-C++). I just have one linker error left, and that's with converting the ASM code into AT&T syntax. Here's the code:
Code: Show/Hide
Uint32 IMULHIDWORD(Uint32 A, Uint32 B)
{
   Uint32 HDW;
   
#ifdef __GNUC__ //Cyan~Fire: Added GCC-style asm
    __asm("movl _A, %eax");
    __asm("imull _B");
    __asm("movl %edx, _HDW");
#else
   __asm
   {
      mov      eax, A
      imul   B
      mov      HDW, edx
   }
#endif

   return HDW;
}


The errors I get are:
algorithms.cpp: undefined reference to `A'
algorithms.cpp: undefined reference to `B'
algorithms.cpp: undefined reference to `HDW'

Anybody know why?

Thanks,
Cyan
Mr Ekted - Thu Sep 02, 2004 9:50 pm
Post subject:
You can do the same operation if the compiler supports 64-bit operators. In VC this would be something like:

Code: Show/Hide
Uint32 IMULHIDWORD(Uint32 A, Uint32 B)
{
unsigned __int64 product;

product = (unsigned __int64)A * (unsigned __int64)B;

return ((Uint32)(product >> 32));
}


Of course it's much less efficient unless the compiler is really good at optimization.[/code]
Cyan~Fire - Fri Sep 03, 2004 10:28 am
Post subject:
Well I'm trying to leave this code as near to the original as possible... thanks, though.
Grelminar - Fri Sep 03, 2004 11:27 pm
Post subject:
First, that's not a linker error. Second, that's not valid gcc inline asm syntax. This page should help.
Cyan~Fire - Sat Sep 04, 2004 1:14 am
Post subject:
It was a linker error, but I did solve it now with that site. I had seen a similar site before, but it hadn't addressed how to address (lol) local vars. Thanks icon_smile.gif

Just for anyone who's curious:
Code: Show/Hide
Uint32 IMULHIDWORD(Uint32 A, Uint32 B)
{
   Uint32 HDW;
   
#ifdef __GNUC__ //Cyan~Fire: Added GCC-style asm
    asm("imull %0" : "=d"(HDW) : "a"(A), "q"(B));
#else
   __asm
   {
      mov      eax, A
      imul   B
      mov      HDW, edx
   }
#endif

   return HDW;
}

So much simpler in GCC...
Mr Ekted - Sat Sep 04, 2004 3:15 am
Post subject:
MS conforms to Intel ASM spec. That GCC stuff is like reading hex data. icon_sad.gif
Cyan~Fire - Sat Sep 04, 2004 11:42 am
Post subject:
Eh, it's easy to read once you understand what it's doing.
Smong - Sat Sep 04, 2004 1:22 pm
Post subject:
You might want to put "memory" in the clober section.

After reading the page that was linked to I am thinking why does this say numwords when the suffix is 'l'?
Code: Show/Hide
#define rep_stosl(value, dest, numwords) \
__asm__ __volatile__ ( \
  "cld\n\t" \
  "rep\n\t" \
  "stosl" \
  : : "a" (value), "D" (dest), "c" (numwords) \
  : "%ecx", "%edi" )

Cyan~Fire - Sat Sep 04, 2004 2:38 pm
Post subject:
I probably should just to be safe, but I've already tested the build and sent the source to Catid, so I think I'll just leave it alone for now.
Mr Ekted - Sat Sep 04, 2004 6:14 pm
Post subject:
Cyan~Fire wrote:
Eh, it's easy to read once you understand what it's doing.


The point is, the MS version shows you the actual assembly, instruction for instruction. Making a whole new language that hides the operations and their sequences is like using C++.
Cyan~Fire - Sat Sep 04, 2004 9:43 pm
Post subject:
Yeah, just like C and PERL and ASM and all those other stupid high-level languages! Bah! Let's just write in machine code!!
Mr Ekted - Sat Sep 04, 2004 10:03 pm
Post subject:
Sigh. When you write in ASM, you are trying to talk directly to the machine you are targetting. In the case of Intel/AMD, the instruction set is a known. Trying to disguise instructions with crazy symbols or pretending it's higher level than it is is not helpful. One line, one instruction, one opcode.
D1st0rt - Sat Sep 04, 2004 10:13 pm
Post subject:
Cyan, why write in machine code when you can use 0's and 1's? $$

Some old guy> What?! You had 0's?! Back in my day, we had to use the letter 'O'
Mine GO BOOM - Sun Sep 05, 2004 12:24 am
Post subject:
I've actually used someone's machine in which it required you to toggle switches to talk to it. They made their own computer, like back in the original computer type days. Much easier to do now.
Grelminar - Sun Sep 05, 2004 1:54 am
Post subject:
Mr Ekted wrote:
The point is, the MS version shows you the actual assembly, instruction for instruction. Making a whole new language that hides the operations and their sequences is like using C++.


I don't think you get it, Ek. The gcc method is actually more powerful and more transparent than the msvc method.

First, there's nothing hidden: gcc writes your instructions directly into the assembly output, as written. Of course, substitutions are performed where input and output are concerned, but msvc also performs substitutions, so I see no difference there.

Second, consider trying to imul a variable and a constant. In msvc, you'd have to mov at least one into a register, perform the imul, and then move the result into a variable. In gcc, you just tell it where you want the input and output to go, and it takes care of the rest. Specifically, it can perform register allocation so that the desired input and output values are already in the correct registers, and save those extra mov instructions. Also, you tell gcc which registers are clobbered by your operation, so it can optimize better by eliminating spills that msvc would have to include, since it doesn't know what registeres are clobbered.
All times are -5 GMT
View topic
Powered by phpBB 2.0 .0.11 © 2001 phpBB Group