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;
__asm__
(
"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!
fclose(pfile_out);
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!