trying to learn how to run inline asm in c

error: suffix operands not valid for fstp

  • (2 Pages)
  • +
  • 1
  • 2

25 Replies - 6287 Views - Last Post: 29 June 2010 - 06:30 PM Rate Topic: -----

#1 ishkabible  Icon User is offline

  • spelling expret
  • member icon




Reputation: 1623
  • View blog
  • Posts: 5,710
  • Joined: 03-August 09

trying to learn how to run inline asm in c

Posted 28 June 2010 - 01:54 PM

ok so when i run the following code i get the error 'suffix operands not valid for fstp'.
#include <stdio.h>

int main() {

    float arg1, arg2, add, sub, mul, div ;

    printf( "Enter two numbers : " );
    scanf( "%f%f", &arg1, &arg2 );

    /* Perform floating point Addition, Subtraction, Multiplication & Division */
    __asm__ ( "fld %1;"
              "fld %2;"
              "fadd;"
              "fstp %0;" : "=g" (add) : "g" (arg1), "g" (arg2) ) ;

    __asm__ ( "fld %2;"
              "fld %1;"
              "fsub;"
              "fstp %0;" : "=g" (sub) : "g" (arg1), "g" (arg2) ) ;

    __asm__ ( "fld %1;"
              "fld %2;"
              "fmul;"
              "fstp %0;" : "=g" (mul) : "g" (arg1), "g" (arg2) ) ;

    __asm__ ( "fld %2;"
              "fld %1;"
              "fdiv;"
              "fstp %0;" : "=g" (div) : "g" (arg1), "g" (arg2) ) ;

    printf( "%f + %f = %f\n", arg1, arg2, add );
    printf( "%f - %f = %f\n", arg1, arg2, sub );
    printf( "%f * %f = %f\n", arg1, arg2, mul );
    printf( "%f / %f = %f\n", arg1, arg2, div );

    return 0 ;
}




but when i take the suffix's out as in the next bit of code, the program crashes,

#include <stdio.h>

int main() {

    float arg1, arg2, add, sub, mul, div ;

    printf( "Enter two numbers : " );
    scanf( "%f%f", &arg1, &arg2 );

    /* Perform floating point Addition, Subtraction, Multiplication & Division */
    __asm__ ( "fld %1;"
              "fld %2;"
              "fadd;"
              "fstp 0;" : "=g" (add) : "g" (arg1), "g" (arg2) ) ;

    __asm__ ( "fld %2;"
              "fld %1;"
              "fsub;"
              "fstp 0;" : "=g" (sub) : "g" (arg1), "g" (arg2) ) ;

    __asm__ ( "fld %1;"
              "fld %2;"
              "fmul;"
              "fstp 0;" : "=g" (mul) : "g" (arg1), "g" (arg2) ) ;

    __asm__ ( "fld %2;"
              "fld %1;"
              "fdiv;"
              "fstp 0;" : "=g" (div) : "g" (arg1), "g" (arg2) ) ;

    printf( "%f + %f = %f\n", arg1, arg2, add );
    printf( "%f - %f = %f\n", arg1, arg2, sub );
    printf( "%f * %f = %f\n", arg1, arg2, mul );
    printf( "%f / %f = %f\n", arg1, arg2, div );

    return 0 ;
}




i'm guessing this is a very situation based issue(what i'm using to compile and on what system) so i'm using code::blocks with the GNU GCC compiler running windows vista with and AMD proc, i know very little about asm (very little, extremely little) i am however very interested in the subject, but due to my extremely limited understanding of the topic i would be ok with someone who has better incite on the topic telling me to wait until some other point.

This post has been edited by ishkabible: 28 June 2010 - 01:55 PM


Is This A Good Question/Topic? 0
  • +

Replies To: trying to learn how to run inline asm in c

#2 NickDMax  Icon User is offline

  • Can grep dead trees!
  • member icon

Reputation: 2250
  • View blog
  • Posts: 9,245
  • Joined: 18-February 07

Re: trying to learn how to run inline asm in c

Posted 28 June 2010 - 02:38 PM

Question #1: what compiler are you using. There is no standard for inline assembly so to help you out we need to know which compiler you are using. (I guess gcc?)

Also what platform
Was This Post Helpful? 0
  • +
  • -

#3 ishkabible  Icon User is offline

  • spelling expret
  • member icon




Reputation: 1623
  • View blog
  • Posts: 5,710
  • Joined: 03-August 09

Re: trying to learn how to run inline asm in c

Posted 28 June 2010 - 04:05 PM

View PostNickDMax, on 28 June 2010 - 01:38 PM, said:

Question #1: what compiler are you using. There is no standard for inline assembly so to help you out we need to know which compiler you are using. (I guess gcc?)
Also what platform


ishkabible said:

i'm guessing this is a very situation based issue(what i'm using to compile and on what system) so i'm using code::blocks with the GNU GCC compiler running windows vista with and AMD proc, i know very little about asm (very little, extremely little) i am however very interested in the subject, but due to my extremely limited understanding of the topic i would be ok with someone who has better incite on the topic telling me to wait until some other point.


so yes im using gcc as i said in the last statement in my first post. if i was unclear about something please tell me so i can correct next time.

This post has been edited by ishkabible: 28 June 2010 - 04:07 PM

Was This Post Helpful? 0
  • +
  • -

#4 ishkabible  Icon User is offline

  • spelling expret
  • member icon




Reputation: 1623
  • View blog
  • Posts: 5,710
  • Joined: 03-August 09

Re: trying to learn how to run inline asm in c

Posted 28 June 2010 - 04:30 PM

o and i was just wondering, not that my skill level is any where near being able to do this but i was wondering if this is how things like OpenGL and DirectX access your GPU. if with dedication i could learn to do something with just my graphics card or using the same methods OpenGL or DirectX uses to access any GPU i would like to know just becuase i am interested in the subject. o and one more thing, i am assuming that when you call asm("asm code here") it is a compile time thing so there is no way to call dynamic inline asm right.

This post has been edited by ishkabible: 28 June 2010 - 04:33 PM

Was This Post Helpful? 0
  • +
  • -

#5 Oler1s  Icon User is offline

  • D.I.C Lover
  • member icon

Reputation: 1395
  • View blog
  • Posts: 3,884
  • Joined: 04-June 09

Re: trying to learn how to run inline asm in c

Posted 28 June 2010 - 04:44 PM

Quote

but i was wondering if this is how things like OpenGL and DirectX access your GPU
No. For any piece of hardware, there must be a driver provided. On Windows, for example, drivers are provided by implementation makers, like ATI and Nvidia. On Macs, the drivers are provided by Apple.

The OS manufacturer will provide some mechanism to build up the driver. On Windows, there are the driver development kit and related resources.

Quote

is a compile time thing so there is no way to call dynamic inline asm right
Right, assembly must be assembled. Unless your program is an assembler...
Was This Post Helpful? 1
  • +
  • -

#6 ishkabible  Icon User is offline

  • spelling expret
  • member icon




Reputation: 1623
  • View blog
  • Posts: 5,710
  • Joined: 03-August 09

Re: trying to learn how to run inline asm in c

Posted 28 June 2010 - 05:01 PM

thanks Oler1s, but i'm still having problems with floating point numbers in my inline asm program, fstp for some reason causes the % to be invalid but without it crashes and i think rightly if i understand this corectly, in the version that crashes isn't it storing the value of variable 0 as the address of add(/sub/mul/div), im not sure what this would do but it seems to me like it would cause them to all equal nothing so when i try to print them nothing happens.
Was This Post Helpful? 0
  • +
  • -

#7 Oler1s  Icon User is offline

  • D.I.C Lover
  • member icon

Reputation: 1395
  • View blog
  • Posts: 3,884
  • Joined: 04-June 09

Re: trying to learn how to run inline asm in c

Posted 28 June 2010 - 06:14 PM

The second snippet you posted is definitely wrong. Can you actually compile the first? I think g constrains to integer operands effectively. Someone who knows the gcc inline asm syntax should help out on this though.

And, big warning for what it's worth. If you want to learn to work with assembly, inline assembly isn't a good starting point.
Was This Post Helpful? 0
  • +
  • -

#8 ishkabible  Icon User is offline

  • spelling expret
  • member icon




Reputation: 1623
  • View blog
  • Posts: 5,710
  • Joined: 03-August 09

Re: trying to learn how to run inline asm in c

Posted 28 June 2010 - 06:23 PM

i was looking at a list of asm x86 instructions and while reading though i found FNINIT, it said it initialized the floating point proc, dose c do this for me, dose c do this but inline asm dose it's own thing, i tried it by adding in __asm__("finit;") before running any other inline assembly but it dosn't seem to have an effect. i have MSVC++ 2008 express should i use it look into Intel syntax instead.
Was This Post Helpful? 0
  • +
  • -

#9 NickDMax  Icon User is offline

  • Can grep dead trees!
  • member icon

Reputation: 2250
  • View blog
  • Posts: 9,245
  • Joined: 18-February 07

Re: trying to learn how to run inline asm in c

Posted 29 June 2010 - 06:30 AM

:) I am sorry for now seeing that you mentioned the compiler and platform in your first post. The GCC inline assembler syntax must have made my eyes cross because I swear I didn't see that.

You will probably find inline assembly easier with Microsoft than GCC (which apparently has a very strange syntax, I looked it up last night and really could not find a very clear reference).

lets see if we can get this working under Microsoft. I like to start by asking the compiler how it would do it, so I will start with a C program.

#include <stdio.h>

int main() {
    float arg1 = 3.14159, 
          arg2 = 2.0, add, sub, mul, div ;

    //Addition
    add = arg1 + arg2;
    
    //subtraction
    sub = arg1 - arg2;
    
    //multiplication
    mul = arg1 * arg2;
    
    //division
    div = arg1 / arg2;
    
    printf("results: %f, %f, %f, %f\n", add, sub, mul, div);
    return 0;
}


Now I want to see what the compiler did: At the command line I compiled this as:
> "C:\Program Files (x86)\Microsoft Visual Studio 10.0\VC\bin\cl.exe" "C:\CProjects\Forum Help\asmfpu\FpuInstructions.c" /c /EHsc /FaFpuInstructions.asm /I "C:\Program Files (x86)\Microsoft Visual Studio 10.0\VC\include" 
Microsoft (R) 32-bit C/C++ Optimizing Compiler Version 16.00.30319.01 for 80x86
Copyright (C) Microsoft Corporation.  All rights reserved.


and I got the following back (I added a couple of more comments):
; Listing generated by Microsoft (R) Optimizing Compiler Version 16.00.30319.01 

	TITLE	C:\CProjects\Forum Help\asmfpu\FpuInstructions.c
	.686P
	.XMM
	include listing.inc
	.model	flat

INCLUDELIB LIBCMT
INCLUDELIB OLDNAMES

_DATA	SEGMENT
$SG2643	DB	'results: %f, %f, %f, %f', 0aH, 00H
_DATA	ENDS
PUBLIC	__real@40000000
PUBLIC	__real@40490fd0
PUBLIC	_main
EXTRN	_printf:PROC
EXTRN	__fltused:DWORD
;	COMDAT __real@40000000
; File c:\cprojects\forum help\asmfpu\fpuinstructions.c
CONST	SEGMENT
__real@40000000 DD 040000000r			; 2
CONST	ENDS
;	COMDAT __real@40490fd0
CONST	SEGMENT
__real@40490fd0 DD 040490fd0r			; 3.14159
; Function compile flags: /Odtp
CONST	ENDS
_TEXT	SEGMENT
_add$ = -24						; size = 4
_sub$ = -20						; size = 4
_arg1$ = -16						; size = 4
_mul$ = -12						; size = 4
_div$ = -8						; size = 4
_arg2$ = -4						; size = 4
_main	PROC
; Line 3
	push	ebp
	mov	ebp, esp
	sub	esp, 24					; 00000018H
; Line 4: arg1 = 3.14159
	fld	DWORD PTR __real@40490fd0
	fstp	DWORD PTR _arg1$[ebp]
; Line 5: arg2 = 2.0
	fld	DWORD PTR __real@40000000
	fstp	DWORD PTR _arg2$[ebp]

; Line 8: addition
	fld	DWORD PTR _arg1$[ebp]
	fadd	DWORD PTR _arg2$[ebp]
	fstp	DWORD PTR _add$[ebp]

; Line 11: subtraction
	fld	DWORD PTR _arg1$[ebp]
	fsub	DWORD PTR _arg2$[ebp]
	fstp	DWORD PTR _sub$[ebp]

; Line 14: multiplication
	fld	DWORD PTR _arg1$[ebp]
	fmul	DWORD PTR _arg2$[ebp]
	fstp	DWORD PTR _mul$[ebp]

; Line 17: division
	fld	DWORD PTR _arg1$[ebp]
	fdiv	DWORD PTR _arg2$[ebp]
	fstp	DWORD PTR _div$[ebp]


; Line 19: printf
	fld	DWORD PTR _div$[ebp]
	sub	esp, 8
	fstp	QWORD PTR [esp]
	fld	DWORD PTR _mul$[ebp]
	sub	esp, 8
	fstp	QWORD PTR [esp]
	fld	DWORD PTR _sub$[ebp]
	sub	esp, 8
	fstp	QWORD PTR [esp]
	fld	DWORD PTR _add$[ebp]
	sub	esp, 8
	fstp	QWORD PTR [esp]
	push	OFFSET $SG2643
	call	_printf
	add	esp, 36					; 00000024H
; Line 20
	xor	eax, eax
; Line 21
	mov	esp, ebp
	pop	ebp
	ret	0
_main	ENDP
_TEXT	ENDS
END


so the basic syntax seems to be fld to load arg1, then preform the operation with arg2 as an argument, and then use fstp to store the result. (note: looking at this reference I don't think your assembly instructions were correct).

now that I have a little clue what I am doing I will try to substitute one of them for inline assembly. Note that you really want to work in baby steps, get 1 working before you do all of them.

#include <stdio.h>

int main() {
    float arg1 = 3.14159, 
          arg2 = 2.0, 
          result1;
    
    __asm {
	fld     arg1
	fadd	arg2
	fstp	result1
    };
    
    printf("%f + %f = %f\n", arg1, arg2, result1);
    return 0;
}


note that I had to change the variable name "add" because it is a reserved word in MASM (i.e. its an instruction).

The program compiles fine:
> "C:\Program Files (x86)\Microsoft Visual Studio 10.0\VC\bin\cl.exe" "C:\CProjects\Forum Help\asmfpu\MyfpuInst.c" /MT /O2 /EHsc /fp:fast /I "C:\Program Files (x86)\Microsoft Visual Studio 10.0\VC\include" 

Microsoft (R) 32-bit C/C++ Optimizing Compiler Version 16.00.30319.01 for 80x86
Copyright (C) Microsoft Corporation.  All rights reserved.

MyfpuInst.c
Microsoft (R) Incremental Linker Version 10.00.30319.01
Copyright (C) Microsoft Corporation.  All rights reserved.

/out:MyfpuInst.exe 
MyfpuInst.obj 

> Process Exit Code: 0
> Time Taken: 00:01


and seems to run fine too:
> "C:\CProjects\Forum Help\asmfpu\MyfpuInst.exe " 
3.141590 + 2.000000 = 5.141590

> Process Exit Code: 0
> Time Taken: 00:00



Sorry I could not help with the GCC version.

The best advice I can give you for working with assembly language: Ask a C compiler how it would do it before you try.
Was This Post Helpful? 1
  • +
  • -

#10 ishkabible  Icon User is offline

  • spelling expret
  • member icon




Reputation: 1623
  • View blog
  • Posts: 5,710
  • Joined: 03-August 09

Re: trying to learn how to run inline asm in c

Posted 29 June 2010 - 01:53 PM

im new to MSVC++ i dont know how to do the following
1)use c instead c++, if that even matters for this
2)what and where that file is that you used to see what the compiler did
Was This Post Helpful? 0
  • +
  • -

#11 Oler1s  Icon User is offline

  • D.I.C Lover
  • member icon

Reputation: 1395
  • View blog
  • Posts: 3,884
  • Joined: 04-June 09

Re: trying to learn how to run inline asm in c

Posted 29 June 2010 - 02:09 PM

Quote

use c instead c++, if that even matters for this
End your files with .c instead of .cpp. MSVC will compile as c.

Quote

what and where that file is that you used to see what the compiler did
See compiler documentation. There are compiler flags you use, so that the compiler generates the assembly.

For example, for MSVC, looking at the documentation gets you the compiler flag to use. You can also see what NickDMax posted.

> "C:\Program Files (x86)\Microsoft Visual Studio 10.0\VC\bin\cl.exe" "C:\CProjects\Forum Help\asmfpu\FpuInstructions.c" /c /EHsc /FaFpuInstructions.asm /I "C:\Program Files (x86)\Microsoft Visual Studio 10.0\VC\include"That's from his post. Right above your question...

So you can Google for gcc's documentation and see how to generate the assembly listing.
Was This Post Helpful? 1
  • +
  • -

#12 ishkabible  Icon User is offline

  • spelling expret
  • member icon




Reputation: 1623
  • View blog
  • Posts: 5,710
  • Joined: 03-August 09

Re: trying to learn how to run inline asm in c

Posted 29 June 2010 - 02:12 PM

hey thanks NickDMax i got this code to work,
#include <stdafx.h>
#include <stdio.h>

int main() {
    float arg1 = 3.14159, 
    arg2 = 2.0, add, sub, mul, div ;
    bool EXIT;
    //Addition
	__asm {
			fld  arg1
			fadd arg2
			fstp add
	}
    __asm {
			fld  arg1
			fsub arg2
			fstp sub
	}
    __asm {
			fld  arg1
			fmul arg2
			fstp mul
	}
	__asm {
			fld  arg1
			fdiv arg2
			fstp div
	}
    printf("results: %f, %f, %f, %f\n", add, sub, mul, div);
	scanf("%b",EXIT);
    return 0;
}



Was This Post Helpful? 0
  • +
  • -

#13 NickDMax  Icon User is offline

  • Can grep dead trees!
  • member icon

Reputation: 2250
  • View blog
  • Posts: 9,245
  • Joined: 18-February 07

Re: trying to learn how to run inline asm in c

Posted 29 June 2010 - 02:16 PM

There are a couple of ways to get MSVC++ to compile as C -- the easiest is just to name you file with a .c rather than a .cpp -- the compiler will default to its C compiler mode.

(there is also a command line switch which if you are using the IDE you can set from somewhere... since I don't use the IDE I don't really know where).

Next when you tell the compiler to compile to assembly -- again this is kind of easier to do from the command line -- I generally use the /Fa[file name here] command line argument.

so for example: cl.exe myprog.c /c /EHsc /Famyprog.asm

then you should find myprog.asm in the current directory.
/c told the compiler not to link (compile only)
/EHsc told the compiler which exceptions to use -- this is needed for console programs (not really sure why myself).
/Fa[file name] -- produces an assembly language listing.

(optional)
/O2 -- optimized, you should generally start without the optimizations, then compare with them in. The listings will generally be very different.
Was This Post Helpful? 0
  • +
  • -

#14 KYA  Icon User is offline

  • g++ jameson.cpp -o beverage
  • member icon

Reputation: 3120
  • View blog
  • Posts: 19,163
  • Joined: 14-September 07

Re: trying to learn how to run inline asm in c

Posted 29 June 2010 - 02:19 PM

Project Properties->C/C++->Advanced->"Compile As"

to force it regardless of file extension.
Was This Post Helpful? 1
  • +
  • -

#15 NickDMax  Icon User is offline

  • Can grep dead trees!
  • member icon

Reputation: 2250
  • View blog
  • Posts: 9,245
  • Joined: 18-February 07

Re: trying to learn how to run inline asm in c

Posted 29 June 2010 - 02:29 PM

@KYA so how do you get it to generate an assembly language listing from the GUI?

I guess just add /Fa to the "Addition Options" under Project Properties->C/C++->Command Line ?

Project Properties->C/C++->Output Files>Assembler Output>
Was This Post Helpful? 0
  • +
  • -

  • (2 Pages)
  • +
  • 1
  • 2