Showing posts with label c. Show all posts
Showing posts with label c. Show all posts

20 December 2012

header file and library(dll) yg perlu utk compile C

Masih keliru berkenaan header file dan library(samada dll atau libfoo.a) yang perlu ketika nak compile aturcara c

Di bawah adalah diskusi yg disedut dari http://mingw-users.1079350.n2.nabble.com/Win-API-documentation-td7302962.html.

-------- mail n -------------------------------------------------------
> For instance, the SafeArrayCreate function described at:
>
> http://msdn.microsoft.com/en-us/library/windows/desktop/ms221234%28v=vs.85%29.aspx
>
> Is that part of the core windows API?

Yes. If you read the document at that URI, it tells you which headers
to #include, which libraries with which to link, and which DLLs are
required at run time. So...

1) Check that MinGW provides the required header(s), (in c:\mingw\include
for a standard installation).

2) Check that the symbols you need to reference are defined, and that
functions you wish to call are declared, in the appropriate MinGW header;
(some are incomplete, due to inadequate documentation from Microsoft).

3) Check that MinGW provides the requisite libraries; (where Microsoft say
to use foo.lib, you look for libfoo.a or libfoo.dll.a, in c:\mingw\lib).

4) Check that the *system* provides the requisite DLLs; (most often, you
will find them in c:\windows\system32).

If all four checks pass, you should be able to use that API, without recourse
to any other resource.


----- mail n+1 -------------------------------------------------------------
> 3) Check that MinGW provides the requisite libraries; (where Microsoft say
> to use foo.lib, you look for libfoo.a or libfoo.dll.a, in c:\mingw\lib).

As an aside, isn't it true that current versions of the GNU linker can
link directly against the DLL, even if the import library libfoo.dll.a
is not available?


------mail n + 2 ---------------------------------------------------------------------
It is. You need to add the directory with .dll files to the linker path.

19 December 2012

entry point MingW vs VS2008

MingW
Kompiler MingW akan tulis 2 fungsi utk entrypoint, walaupun hanya guna satu utk setiap aplikasi.
Jika jenis console, entrypoint adalah pada fungsi  mainCRTStartup.
Jika ada gui(ada windows), entrypoint-nya adalah fungsi _WinMainCRTStartup.

Kedua-dua fungsi tersebut ada pada setiap exe, cuma maklumat pada PE header akan tentukan mana satu yang akan digunakan.



Graf di atas dihasilkan guna IDA (Menu View-> Graphs -> Xref to).
_main adalah fungsi main yang ditulis oleh coder. Sebelum itu terdapat fungsi ___mingw_CRTStartup yang menjadi perantara antara fungsi entrypoint dan _main.


Visual Studio 2008
Project template guna  "Win32 Console Application"

Dari IDA, dapat diketahui, function __tmainCRTStartup() akan panggil _main()(main function yg ditulis oleh coder). Walaubagaimanapun, entrypoint sebenar bukan __tmainCRTStartup(), tetapi adalah wmainCRTStartup().
Features graph pada IDA tak tunjuk wmainCRTStartup() mungkin disebabkan arahan jmp(bukan call) yg digunakan utk masuk __tmainCRTStartup() spt yg ditunjukkan pada address 0x4012C7 pada gambar berikut.

13 September 2012

gcc

Compile:
   gcc -o run.exe sourceCode.c

Assembly file (syntax intel)
    gcc -o run.exe sourceCode.c  -S -masm=intel

32bit (on linux kena install sudo apt-get install gcc-multilib)
    gcc -o run.exe sourceCode.c  -S -masm=intel -m32


Jgn buang temporary file ( .i(substitutions), .s(assembly code), .o(object code))
    gcc -o run.exe sourceCode.c  -S -masm=intel -m32 -save-temps

masih signed dan unsigned

Kes 1
c code:
unsigned int a = 5;
signed int b = -1;
if(a > b)
assembly code:
mov DWORD PTR [esp+24], 5
mov DWORD PTR [esp+28], -1
mov eax, DWORD PTR [esp+28]
cmp eax, DWORD PTR [esp+24]
jae .L2


Kes 2
c code:
     signed int a = 5;
     signed int b = -1;
     if(a > b)

assembly code:
        mov     DWORD PTR [esp+24], 5
        mov     DWORD PTR [esp+28], -1
        mov     eax, DWORD PTR [esp+24]
        cmp     eax, DWORD PTR [esp+28]
        jle     .L2 



Kesimpulan:
jika a dan b adalah signed int, maka compiler akan guna jle.
Tapi jika salah satu unsigned int, maka compiler akan guna jae(sebab eax = esp+28 instead +24)

12 September 2012

signed vs unsigned int

01 void main(){
02     unsigned int a = 5;
03     signed int b = -1;
04     if(a > b)
05         printf("True");
06     else
07         printf("False");
08     }

Hasil >>  False  ( line 07)

Kenapa?
Sebab perbandingan di buat antara signed dan unsigned.
Compiler akan assume perbandingan dibuat guna mode unsigned.

Note:  jika mode signed, assembly instruction yg digunakan adalah jg(jump greater ).
Jika mode unsigned, assembly instruction adalalah ja(jump above )

Data variable a yg disimpan dalam memori adalah 0x00000005.
Data variable b yg disimpan dalam memori adalah 0xffffffff (value adalah -1)

utk jg:
  a > b    # hasilnya true

utk ja:
  a > b    # hasilnya false  (sebab 0x00000005 (a) lebih kecil dari  0xffffffff (b) )

assembly code utk line 04:
mov eax, 0x5
mov ebx, 0xffffffff
cmp eax, ebx
ja binary.true

(kalau both variable adalah signed int, compiler akan gunakan jg, bukan ja)

extra:
JG will jump if ZF = 0 and SF = OF
JA will jump if CF = 0 and ZF = 0
(ref. http://www.unixwiz.net/techtips/x86-jumps.html)

29 April 2011

char array vs char pointer

char j2[100] = "adam:Aku darah anak malaysia";
char *j3;

j3 = j2 ;
printf("&j2=%p j2=%p\n",&j2, j2);
printf("&j3=%p  j3=%x\n", &j3, j3);


>>:

&j2=0x7fff1b810fc0 j2=0x7fff1b810fc0
&j3=0x7fff1b810fb8  j3=1b810fc0



#di compile dan run atas 64bit OS.
&j2 sama dgn j2 dan j3.
&j3 adalah lokasi dimana alamat j2 disimpan.

28 April 2011

string.h (c)

1) strcmp
bil = strcmp(char *strA, char *strB);
if (bil == 0){
>> strA = strB
}



2) strstr
char *s1 = "amanamanam";
char *s2 = "ana";
char *s3;

s3 = strstr(s1, s2);
printf("%p - %p = %f\n",s3, s1, (double)(s3 - s1));
>>>>0x40085e - 0x40085c = 2.000000