Welcome to the most active Linux Forum on the web.
Go Back > Forums > Non-*NIX Forums > Programming
User Name
Programming This forum is for all programming questions.
The question does not have to be directly related to Linux and any language is fair game.


  Search this Thread
Old 09-20-2004, 01:51 AM   #1
LQ Newbie
Registered: Sep 2004
Distribution: redhat
Posts: 2

Rep: Reputation: 0
Question puzzle about g++ compile options for inline assembly program

hi all, I am a newbie of Linux and now have a job with it, recently I meet a puzzle : when I use inline asembly code and a makefile with many optimize options open, the compile result is ok, while run with a segment fault error. the following is a test code:
#include <stdio.h>
#include <memory.h>
typedef unsigned char UChar;
void RGB_test(UChar* pRGBOutBuf, UChar* pRGBInBuf, int width, int height)
int loop_num;
loop_num = (width * height) >> 3;

"movl %0, %%eax \n\t"
"movl %1, %%ebx \n\t"
"movl %2, %%ecx \n\t"

"loop1: \n\t"
"movq (%%ebx), %%mm0 \n\t"
"movq %%mm0, (%%eax) \n\t"
"addl $8, %%eax \n\t"
"addl $8, %%ebx \n\t"
"dec %%ecx \n\t"
"jnz loop1 \n\t"
"emms \n\t"
:"m"(pRGBOutBuf), "m"(pRGBInBuf), "m"(loop_num)
:"%eax", "%ebx", "%ecx"
int main(int argc, char* argv[])
UChar* pRGBIn = new UChar[320 * 240];
UChar* pRGBOut = new UChar[320 * 240];

memset(pRGBIn, 0xff, 320*240);
RGB_test(pRGBOut, pRGBIn, 320, 240);

FILE * pfile_out=NULL;
pfile_out= fopen("test", "wb");
fwrite(pRGBOut, sizeof(UChar), 320*240, pfile_out); //attention!

delete pRGBIn;
delete pRGBOut;
return 1;
The segment fault when run to "fwrite", which always get a null pointer of pfile_out.

and the makefile is:
CXX := /usr/bin/g++
OPT := -march=pentiumpro -mcpu=pentiumpro -fomit-frame-pointer -O1 -fno-exceptions -fno-rtti -fPIC

SRC = RGB_test.cpp
OBJ = RGB_test.o
all: $(SRCS)
$(CXX) $(OPT) -g -c $(SRC)
$(CXX) -Wall -o RGB_test -g $(OBJ)

clean :
rm RGB_test $(OBJ)
when -fomit-frame-pointer or fPIC is omitted from the makefile, the code runs ok.

Thans for your help!
Old 09-20-2004, 03:38 AM   #2
Senior Member
Registered: Jul 2004
Distribution: Ubuntu 7.04
Posts: 1,991

Rep: Reputation: 76
You should always test the return value of fopen() to see if it is NULL. If it is, you should print out the value of errno (#include<errno.h>) to see what the error was.
You can print a human-readable form of the error with printf("%s", strerror(errno)); (for which you also need to include string.h).
Old 09-20-2004, 04:30 AM   #3
LQ Newbie
Registered: Sep 2004
Distribution: redhat
Posts: 2

Original Poster
Rep: Reputation: 0
Thanks for ur advice, in fact if I add a judge like this:
if (pfileOut == NULL)
printf("%s", strerror(errno)); // or printf("anystring");
return -1;
nothing will be print out! and debug with gdb, the errno is 22, which is "invalid argument".

now find a clue, that if the EBX is not used, the result is ok, but the reason is not found and the trouble is not solved yet.


Thread Tools Search this Thread
Search this Thread:

Advanced Search

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is Off
HTML code is Off

Similar Threads
Thread Thread Starter Forum Replies Last Post
inline assembly in gcc gursev Programming 2 03-04-2005 02:17 AM
problems with gcc inline assembly using xmm registers Y0jiMb0 Programming 3 12-05-2004 02:59 PM
Inline Assembly Question tjt Programming 3 08-08-2004 04:38 AM
undefined reference to ... when using inline assembly in C Annie0716 Programming 3 08-01-2004 12:50 AM
subrouine_call in gcc inline assembly sakeeb Programming 4 08-15-2002 10:22 PM > Forums > Non-*NIX Forums > Programming

All times are GMT -5. The time now is 01:47 AM.

Main Menu
Write for LQ is looking for people interested in writing Editorials, Articles, Reviews, and more. If you'd like to contribute content, let us know.
Main Menu
RSS1  Latest Threads
RSS1  LQ News
Twitter: @linuxquestions
Facebook: linuxquestions Google+: linuxquestions
Open Source Consulting | Domain Registration