Thread: gcc inline asm: illegal instruction (core dump)

  1. #1
    Registered User Sargnagel's Avatar
    Join Date
    Aug 2002
    Posts
    166

    Angry gcc inline asm: illegal instruction (core dump)

    This is my first try with GCC inline assembly. I followed an example from this paper (page 3) and wrote a small test program:
    Code:
    #include <stdio.h>
    
    int main()
    {
    	int i;
    	float a[4] = {2.0}, b[4] = {3.0}, c[4] = {-1.0};
    	
    	__asm__("movaps %1, %%xmm0 \n\t"	/* copy vector a[] to SSE register xmm0 */
    		"movaps %2, %%xmm1 \n\t"	/* copy vector b[] to SSE register xmm1 */
    		"divps %%xmm0, %%xmm1 \n\t" /* divide xmm0 by xmm1 and write result to xmm1 */
    		"movaps %%xmm1, %0"			/* copy xmm1 to vector c[]	*/
    		: "=m"	(c[0])	/*	output %0	*/
    		: "m"	(a[0]),	/*	input %1	*/
    		  "m"	(b[0]));/*	input %2	*/
    	
    	for(i = 0; i < 4; ++i)
    		printf("%f", c[i]);
    	printf("\n");
    	
    	return 0;	
    }
    It compiles well with GCC 3.3.1 (cygwin):
    gcc -pedantic -W -Wall -masm=intel -o test.exe divtest.c

    But the test program just crashes with "illegal instruction".
    I guess there is an obvious mistake in my source code, but as I already wrote this is my first try.
    Yes, my CPU supports SSE (Intel Celeron Tualatin (PIII core)).
    Thank you for your help.

    The stack trace:
    Code:
    Exception: STATUS_PRIVILEGED_INSTRUCTION at eip=004010DF
    eax=BF800000 ebx=00000004 ecx=610CB16C edx=00000002 esi=00000000 edi=00000000
    ebp=0022FEF0 esp=0022FE80 program=C:\test.exe
    cs=001B ds=0023 es=0023 fs=0038 gs=0000 ss=0023
    Stack trace:
    Frame     Function  Args
    0022FEF0  004010DF  (00000001, 616020CC, 0A040330, 0022FF24)
    0022FF40  61005018  (610CFEE0, FFFFFFFE, 000003D4, 610CFE04)
    0022FF90  610052ED  (00000000, 00000000, 8043138F, 00000000)
    0022FFB0  004014C1  (00401055, 037F0009, 0022FFF0, 77E787F5)
    0022FFC0  0040103C  (00000000, 00000000, 7FFDF000, 00000000)
    0022FFF0  77E787F5  (00401000, 00000000, 000000C8, 00000100)
    End of stack trace
    Here is the example from the paper I mentioned above:
    Code:
    for (i=0;i<100;i+=4)
    __asm__ __volatile__ (
    "movaps %1, %%xmm0 \n\t"
    "movaps %2, %%xmm1 \n\t"
    "addps %%xmm0, %%xmm1 \n\t"
    "movaps %%xmm1, %0"
    :
    "=m" (a[i])
    :
    "m" (b[i]),
    "m" (c[i]));

  2. #2
    Registered User Codeplug's Avatar
    Join Date
    Mar 2003
    Posts
    4,981
    You posted the awnser: "STATUS_PRIVILEGED_INSTRUCTION".

    The OS doesn't allow one (or more) of the instructions to execute in user mode.

    Try it on a 95/98 machine if you have one. Otherwise, try running the code as an NT service.

    gg

  3. #3
    Registered User Sargnagel's Avatar
    Join Date
    Aug 2002
    Posts
    166
    Does this mean I have to run the test program as an administrator?
    W2k forces admin rights in order to use SSE instructions? That sounds crazy to me ....

    I've just tried "run as" admin, but the same error occurs. I will log on as admin later ...

  4. #4
    Registered User Codeplug's Avatar
    Join Date
    Mar 2003
    Posts
    4,981
    From MSDN:
    The privileged instruction exception occurs when these instructions are executed because typical Windows NT applications execute in a nonprivileged (user) mode. Only code executing in kernel mode has the necessary rights to execute privileged instructions. Kernel mode code is typically found in device drivers.
    Unless you know how to write device drivers for NT, you will probably be better off on a Linux box.

    gg

  5. #5
    Registered User Sargnagel's Avatar
    Join Date
    Aug 2002
    Posts
    166
    Originally posted by Codeplug
    From MSDN: Unless you know how to write device drivers for NT, you will probably be better off on a Linux box.

    gg
    Another reason for me to migrate to Linux completely! If I remember correctly, Linux Kernel >=2.4 supports SSE ... I will check my Debian installation.

    Thank you very much for your help, Codeplug!

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Code review
    By Elysia in forum C++ Programming
    Replies: 71
    Last Post: 05-13-2008, 09:42 PM
  2. gcc asm code syntax
    By Uberapa in forum C Programming
    Replies: 4
    Last Post: 06-15-2007, 01:16 AM
  3. SSH Hacker Activity!! AAHHH!!
    By Kleid-0 in forum A Brief History of Cprogramming.com
    Replies: 15
    Last Post: 03-06-2005, 03:53 PM
  4. Certain functions
    By Lurker in forum C++ Programming
    Replies: 3
    Last Post: 12-26-2003, 01:26 AM
  5. My graphics library
    By stupid_mutt in forum C Programming
    Replies: 3
    Last Post: 11-26-2001, 06:05 PM