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)

05 September 2012

ror x86

shift right ->(digit paling kanan pindah letak kat depan)

katakan a = 1001   ; 9

ror a, 1  ->>  1100 ; c
ror a, 2  ->>  0110 ; 6
ror a, 3  ->>  0011 ; 3


katakan eax = 90 00 00 09      <  1001 0000 0000 0000 0000 0000 0000 1001
ror eax, 1 >>    c8 00 00 04     < 1100 1000 0000 0000 0000 0000 0000 0100
ror eax, 2 >>    64 00 00 02     < 0110 0100 0000 0000 0000 0000 0000 0010
ror eax, 3 >>    32 00 00 01     < 0011 0010 0000 0000 0000 0000 0000 0001