-
assembly
what is wrong with this code. I don't know assembly and am not tyring to learn(yet atleast:D), but I took this from a site and it gives me two errors. Could someone help please?
Code:
inline void FloatToInt(int *int_pointer, float f){
__asm__("fld f");
__asm__("mov edx, int_pointer");
__asm__("FRNDINT");
__asm__("fistp dword ptr [edx]");
}
-
-
Code:
/tmp/cch2cud4.s: Assembler messages:
/tmp/cch2cud4.s:10: Error: too many memory references for `mov'
/tmp/cch2cud4.s:12: Error: junk `ptr [edx]' after expression
-
Perhaps its because that's written in the ASM for a VC++ compiler, and you're compiling it with gcc
-
You have to be careful you are using the correct syntax for assembly. Unlike C/C++, assembly can vary. For inline assembly, you MUST know whether your compiler uses AT&T or Intel syntax. You MUST also know how to convert between the two.
-
It sounds a lot like a problem with using Intel syntax instead of at&t
try:
Code:
__asm__("movl int_pointer, %edx");
as for
Code:
__asm__("fistp dword ptr [edx]");
No idea, we didn't get into the floating point part of asm during my class.
-
You might get away with using .intel_syntax noprefix (example) if you want to stick with the intel syntax and are working with the gnu assembler.
-
I changed what thantos said and it got rid of one error. I changed it back and tried with -masm=att and it got those two errors still. If I stick with the original code and compile with -masm=intel i get one error about the memory references for mov.
-
using -masm=intel is the error occuring with my mov? if so change it back. my move is at&t syntax
-
ah screw it. Here is what I was trying to do test this and see if it was true or not, but if any of ya'll want to see it seemed pretty interesting.
-
Modify this to suit:
1. You need to declare variables with global scope for proper linkage, so, for example:
Code:
/*globals*/
int *int_ptr __asm__ ( "int_ptr" );
float floatval __asm__( "floatval" );
2. Then in your FloatToInt fn, just doing it by assignment, ie.:
Code:
inline void FloatToInt(int *int_pointer, float f)
{
int_ptr=int_pointer;
floatval=f;
__asm__("fld floatval");
__asm__("mov edx, int_ptr");
__asm__("FRNDINT");
__asm__("fistp dword ptr [edx]");
}
You'll probably want to rewrite the fn so it doesn't take any parameters and make other changes to use the global variables directly - I just did it this way as proof of concept.
Used: gcc -o test.exe test.c -masm=intel (winxp, gcc 3.2.3)
Output:
10000000 sin and cos computed in 3781 ticks with standard math funcs
10000000 sin and cos computed in 1125 ticks with fast[cos/sin]
And change void main to int main in the original example, of course. ;)
******************************
edit: Just noticed the name 'Linuxdude' so, for linux:
1. Remove conio.h, assert.h includes and getch(); in 'main' fn.
2. You need to declare variables with global scope for proper linkage, so, for example:
Code:
/*globals*/
/*with linux you don't need to use following syntax - normal declaration is sufficient*/
int *int_ptr __asm__ ( "int_ptr" );
float floatval __asm__( "floatval" );
3. Then in your FloatToInt fn, just doing it by assignment, ie.:
Code:
inline void FloatToInt(int *int_pointer, float f)
{
int_ptr=int_pointer;
floatval=f;
__asm__(".intel_syntax noprefix"); /*kills error: too many memory references for mov*/
__asm__("fld floatval");
__asm__("mov edx, int_ptr");
__asm__("FRNDINT");
__asm__("fistp dword ptr [edx]");
}
You'll probably want to rewrite the fn so it doesn't take any parameters and make other changes to use the global variables directly - I just did it this way as proof of concept.
Used(fedora, gcc 3.3.2): gcc -o test.exe test.c -masm=intel -lm
Output:
10000000 sin and cos computed in 4410000 ticks with standard math funcs
10000000 sin and cos computed in 3450000 ticks with fast[cos/sin]
(slower machine)
-
thanx. I of course changed the void to int and got rid of the asserts didn't see a need for them and I got rid of getch(). I am in a terminal:)thanx very much