Halaman

Sabtu, 31 Oktober 2020

CATATAN LINUX

 1. Cek versi kernel :

    uname -r atau uname -a atau cat /proc/version atau dmesg | grep linux 

    info lebih lanjut : uname --help

2. Penjelasan syscall https://blog.packagecloud.io/eng/2016/04/05/the-definitive-guide-to-linux-system-calls/

3. Status, start, stop dan restart service : 

    sudo systemctl status nama_service, contoh sudo systemctl status apache2 jika selesai bisa tekan q.

    sudo systemctl start nama_service, contoh sudo systemctl start apache2

4. 

Jumat, 30 Oktober 2020

CATATAN EXPLOIT

 BUFFER OVERFLOW EXPLOIT

Buffer overflow biasa dimanfaatkan untuk mengexploitasi kerentanan suatu program. Sesuai dengan namanya, buffer overflow berarti meload buffer dengan data yang lebih besar dari daya tampungnya. Variable lokal disimpan di stack memori. Apabila buffer variable lokal ini di overflow, maka data kelebihannya akan tersimpan di stack juga. Stack merupakan bagian penting yang menyimpan data parameter fungsi, variable lokal dan alamat instruksi pointer. Mengubah alamat instruksi pointer di stack akan menyebabkan perubahan alur eksekusi program. Menurut saya ada kemiripan dengan codecave, yaitu sama-sama mengubah alur eksekusi program yang sudah dicompile. Berikut ini skenario pembelokan eksekusi program :

#========================

.section .data

.section .text

.globl _start

jmp dummy

_start:   

    pop    esi    #menyimpan alamat string command shell ke register eax

    #lakukan sesuatu dengan alamat tersebut, terserah mungkin mau di print atau

    #ekseskusi command shell (biasanya dengan syscall 11 execve)

dummy:

    call _start    #call ini bertujuan agar alamat instruksi dibawah disimpan di stack

    .string "simple string"

#========================


Ilustrasi dalam bahasa C :

void main(int argc, char **argv)

{

    char *name[2]; 

    name[0] = "/bin/sh";

    name[1] = NULL;

    //selanjutnya, int execve(char *file, char *argv[], char *env[])

    execve(name[0], name, NULL);

    exit(0);

}

Register yang digunakan :

1. EAX : 0xb - nomor syscall

2. EBX : alamat dari nama program

3. ECX : alamat null-terminated argument-vector, argv (alamat dari nama)

4. EDX : alamat null-terminated environment-vector, env/enp (NULL)







Sumber :

1. https://www.tenouk.com/Bufferoverflowc/Bufferoverflow5.html

2. https://insecure.org/stf/smashstack.html



Kamis, 29 Oktober 2020

CATATAN C/C++

ASSEMBLY C/C++

BASIC ASM

Basic Asm bisa berada diluar fungsi.

Format basic assembly GNU GCC :

asm asm-qualifiers( AssemblerInstructions)

asm-qualifiers bisa :

    volatile. Ini optional. Semua basic asm sudah secara implicit volatile. Volatile maksudnya manipulasi input untuk mengubah output.

    inline. Ini dioptimalkan untuk menghasilkan ukuran statement asm sekecil mungkin.

untuk satu baris :


void main()
{
    __asm__("movl %ecx %eax");
}

kalau banyak baris tambahkan \n\t di akhir baris. Hal ini karena gcc mengirim tiap instruksi sebagai sebuah string ke GNU assembler (GAS). Dengan menggunakan newline/tab kita mengirim format yang sesuai untuk assembler. 
Contoh (bisa pakai __asm__ atau asm saja) :

void main()
{
    __asm__("movl %ecx %eax \n\t"
                       "movl %eax %ebx \n\t"
             "movl %ebx %edx"
                        );
}


EXTENDED ASSEMBLY - DENGAN C EXPRESSION OPERANDS

Extended Asm harus berada dalam fungsi.

Format extended assembly GNU GCC :

asm asm-qualifiers( AssemblerTemplate
    : OutputOperands
    : Input Operands
    : Clobbers);

asm asm-qualifiers( AssemblerTemplate
    : 
    : Input Operands
    : Clobbers
    : GotoLabels);

asm-qualifiers bisa volatile, inline atau goto.

    volatile. Volatile maksudnya manipulsi input untuk mengubah output.

    inline. Ini dioptimalkan untuk menghasilkan ukuran statement asm sekecil mungkin.

    goto. Ini menginformasikan pada compiler bahwa statement asm mungkin akan melakukan jump ke label yang ada di daftar GotoLabels.

contoh copy src ke dst + 1

int src = 1;
int dst;

asm("mov %1, %0\n\t"
    "add $1, %0"
    : "=r" (dst)
    : "r" (src));

printf("%d\n", dst);






CATATAN GDB DEBUGGER

 1. Load program :
        gdb nama-program
2. Set breakpoint :
        break main
        lihat breakpoint : info breakpoint
3. Set disassembly sintaks :
        set disassembly-flavor intel (atau att defaultnya)
4. Tampilkan disassemble instruksi berikutnya :
        set disassemble-next-line on /off
        single step disassembly : nexti dan stepi untuk masuk dalam fungsi
5. Tampilkan variable :
        print nama_variable atau
        display nama_variable
6. Set watchpoint :
        write : watch nama-variable
        read : rwatch nama-variable
        tampilkan watchpoint : info breakpoint
        disable watchpoint : disable nomor-watchpoint
7. Tampilkan memory :
        x/nfu
        n : jumlah data
        f : format ( x = hexadecimal/default, i = mnemonic, lainnya d’, ‘u’, ‘o’, ‘t’, ‘a’, ‘c’, ‘f’, ‘s’)
        u : unit (b = byte, h = half word/2byte, w = word/4byte/default, g = giant word/ 8byte )
        contoh : x/10xw $esp (tampilkan 10 data word format hexadecimal dimulai alamat esp.

Sabtu, 10 Oktober 2020

TUTORIAL SINGKAT DASAR ASSEMBLY x86

Tulisan ini masih dalam tahap pengembangan, jadi akan ada perubahan dari waktu ke waktu.

I. BILANGAN

    1. Bilangan Desimal
        Bilangan Desimal adalah bilangan basis 10 seperti yang dipergunakan sehari-hari. 
        Dalam dunia komputasi biasa ditulis penanda huruf d dibelakan nilainya.
        Contohnya : 10d

    2. Bilangan Hexadecimal 
        Bilangan Hexadecimal adalah bilangan basis 16. Angkanya adalah :
        0 1 2 3 4 5 6 7 8 9 A B C D E F
        Biasanya notasi ditulis dengan penanda 0x didepan nilainya, atau h dibelakang. 
        Contohnya : 0x41 atau biasa ditulis 41h

    3. Bilangan Octal 
        Bilangan Octal adalah bilangan basis 8. Angkanya adalah :
        0 1 2 3 4 5 6 7

    4. Bilangan Biner. 
        Bilangan biner adalah bilangan basis 2. Angkanya adalah :
        0 1

Berikut ini penjelasan singkat bahasa assembly yang saya adaptasi dari tuts4you.com

        Satuan dalam bilangan biner :

BIT - Ukuran terkecil dari data. Dapat berupa 0 atau 1.

BYTE - 1byte terdiri dari 8bit (aristektur processor x86_64). Nilai maksimal yang dapat ditampung adalah 255 (0-255). Untuk memudahkan dalam membaca bilangan biner maka dibuatlah bilangan hexadecimal. 
 
WORD - Satu word berarti 2byte, ukuran masimal datanya 0FFFFh (atau 65535d). 

DOUBLE WORD - DoubelWord adalah 4byte. Nilai Maksimal = 0FFFFFFFF (4294967295d). 

KILOBYTE - Satu kilobyte adalah 1024 byte (32*32byte) 

MEGABYTE - Satu megabyte adalah 1024*1024 atau 1,048,578 byte.



II. REGISTER

Register adalah tempat khusus di komputer untuk menyimpan data. Register dapat diibaratkan kotak kecil, yang mana kita dapat menyimpan nama, nomor atau kalimat. 

Nama register pada processor 8086(16bit) adalah :

AX:          Accumulator Register
BX:          Base Register
CX:          Counter Register
DX:         Data Register
SI:          Source Index
DI:      Destination Index
BP:          Base Pointer
SP:          Stack Pointer
IP:          Instruction Pointer

Nama register pada processor x86(32bit) adalah :

EAX: Extended Accumulator Register
EBX: Extended Base Register
ECX: Extended Counter Register
EDX: Extended Data Register
ESI:          Extended Source Index
EDI: Extended Destination Index
EBP: Extended Base Pointer
ESP: Extended Stack Pointer
EIP:          Extended Instruction Pointer

Nama register pada processor x86_64(64bit) adalah :

RAX:   Accumulator Register 64bit
RBX:   Base Register 64bit
RCX:   Counter Register 64bit
RDX:   Data Register 64bit
RSI:           Source Index 64bit
RDI:   Destination Index 64bit
RBP:   Base Pointer 64bit
RSP:   Stack Pointer 64bit
RIP:           Instruction Pointer 64bit

Sesuai dengan namanya, ukuran register x86 32bit adalah 4 bytes. Dapat menampung data mulai dari  0-FFFFFFFF (unsigned/ tidak bertanda). Pada awalnya setiap register memiliki fungsi khusus sesuai dengan namanya, misalnya ECX = Counter, tapi sekarang kita bisa gunakan register lainnya sebagai counter (kecuali instruksi khusus seperti jcxz yang jump jika ecx == 0). Fungsi dari EAX, EBX, ECX, EDX, ESI dan EDI akan dijelaskan nanti. Selanjutnya mari pelajari register EBP, ESP dan EIP:

EBP: EBP kebanyakan digunakan untuk stack.

ESP: ESP pasangan dari EBP. EBP sebagai Base Pointer dan ESP sebagai Stack Pointer. Stack seperti laci untuk menyimpan data sementara (untuk informasi lebih lanjut, lihat penjelasan instruksi push dan pop)

EIP:        EIP selalu menunjuk pada instruksi selanjutnya yang akan diesksekusi. Bisa diingat dengan nama Instruction Pointer.


Perhatikan gambar berikut :




Jadi, EAX adalah register 32bit,  "Low Word" (16bit) dari kanan (EAX) dapat diakses dengan nama AX. 8bit dari kanan AX dapat diakses dengan AL dan 8bit sisanya dapat diakses dengan AH. Sedangkan 16bit dari kiri EAX tidak dapat diakses secara langsung seperti AX.

Untuk lebih jelasnya, lihat kembali pada gambar diatas.

AH dan AL merupakan bagian dari AX. Ini berarti jika AH diubah, atau AL diubah (atau keduanya diubah), Maka AX akan berubah juga!

EAX         ->  Ingat 'accumulator': biasanya return fungsi disimpan di eax

EBX ->   Ingat 'base': umum

ECX ->   Ingat 'counter' 

EDX ->   Ingat 'data': umum

EDI ->   'destination index': operasi string 

ESI    -> 'source index': operasi string pasangan EDI

EDI dan ESI digunakan oleh instruksi STOSB untuk menyalin data string. Isi EDI dengan alamat memori tujuan
lalu isi ESI dengan alamat memori sumber kemudian panggil instruksi STOSB untuk menyalin data.


Dulu segmen register digunakan oleh DOS 16bit. Sekarang windows 32bit dan linux tidak perlu mengatur register segmen.

     CS -> 'code segment': instructions an application has to execute (see later) 
     DS -> 'data segment': the data your application needs (see later) 
     ES -> 'extra segment': duh! (see later) 
     SS -> 'stack segment': here we'll find the stack (see later) 



III. FLAG

Flag adalah bit tunggal yang mengidentifikasikan status dari suatu operasi. Misalnya operasi test akan mempengaruhi zero flag. Zero flag akan bernilai 1 jika hasil operasi test bernilai nol. Beberapa instruksi akan merujuk pada kondisi flag. Misalnya JNZ (jump if not zero) , jika instruksi sebelum JNZ menyebabkan zero flag bernilai 0, maka instruksi JNZ akan di eksekusi. Alur program akan melompat ke alamat yang ditunjuk JNZ. Ada banyak flag, namun tidak semuanya akan dijelaskan, hanya beberapa saja yang dianggap penting.

Z-Flag:
o Z-Flag (zero flag) seperti sudah dijelaskan sebelumnya.

O-Flag:
o O-Flag (overflow flag) . OF akan di set (status: 1) jika operasi terakhir mengubah bit tertinggi dari register. Contoh: EAX bernilai 7FFF FFFF. Jika kita melakukan instruksi yang menyebabkan EAX bertambah 1, maka OF akan di set, sebab operasi mengubah bit tertinggi dari EAX (yang mana akan menjadi 8000 0000). Hal lain yang akan menyebabkan OF bernilai 1 yaitu jika register tujuan tidak bernilai 0 sebelum dan sesudah operasi.

C-Flag:
o C-Flag (Carry flag) CF akan diset jika kita menambahkan sesuatu ke register sehingga nilainya lebih dari FFFF FFFF atau kita mengurangi nilai register yang bernilai 0.


IV. STACK

Stack adalah bagian dari memori yang dapat digunakan untuk menyimpan data sementara untuk digunakan nantinya. Bedanya dengan memori biasa adalah stack berurutan dari alamat memori tinggi ke alamat memori rendah dengan lebar data sesuai processor (32bit untuk processor 32bit). Untuk menyimpan data ke stack menggunakan instruksi PUSH dan untuk mengambil data dari stack digunakan instruksi POP.



V. INSTRUCTION

Please note, that all values in ASM mnemonics (instructions) are *always* hexadecimal.


Most instructions have two operators (like "add EAX, EBX"), but some have one ("not EAX") or even three ("IMUL EAX, EDX, 64"). When you have an instruction that says something with "DWORD PTR [XXX]" then the DWORD (4 byte) value at memory offset [XXX] is meant. Note that the bytes are saved in reverse order in the memory (WinTel CPUs use the so called “Little Endian” format. The same is for "WORD PTR [XXX]" (2 byte) and "BYTE PTR [XXX]" (1 byte).

Most instructions with 2 operators can be used in the following ways (example: add):

add eax,ebx ;; Register, Register
add eax,123 ;; Register, Value
add eax,dword ptr [404000] ;; Register, Dword Pointer [value]
add eax,dword ptr [eax] ;; Register, Dword Pointer [register]
add eax,dword ptr [eax+00404000] ;; Register, Dword Pointer [register+value]
add dword ptr [404000],eax ;; Dword Pointer [value], Register
add dword ptr [404000],123 ;; Dword Pointer [value], Value
add dword ptr [eax],eax ;; Dword Pointer [register], Register
add dword ptr [eax],123 ;; Dword Pointer [register], Value
add dword ptr [eax+404000],eax ;; Dword Pointer [register+value], Register
add dword ptr [eax+404000],123 ;; Dword Pointer [register+value], value



     ADD (Addition)
     Syntax: ADD destination, source

     The ADD instruction adds a value to a register or a memory address. It can be used in
     these ways:

     These instruction can set the Z-Flag, the O-Flag and the C-Flag (and some others, which
     are not needed for cracking).


     AND (Logical And)
     Syntax: AND destination, source     

     The AND instruction uses a logical AND on two values.
     This instruction *will* clear the O-Flag and the C-Flag and can set the Z-Flag.
     To understand AND better, consider those two binary values:

                                    1001010110
                                    0101001101

     If you AND them, the result is 0001000100
     When two 1 stand below each other, the result is of this bit is 1, if not: The result
     is 0. You can use calc.exe to calculate AND easily. 


     CALL (Call)
     Syntax: CALL something

     The instruction CALL pushes the RVA (Relative Virtual Address) of the instruction that
     follows the CALL to the stack and calls a sub program/procedure.

     CALL can be used in the following ways:

     CALL 404000 ;; MOST COMMON: CALL ADDRESS
     CALL EAX ;; CALL REGISTER - IF EAX WOULD BE 404000 IT WOULD BE SAME AS THE ONE ABOVE
     CALL DWORD PTR [EAX] ;; CALLS THE ADDRESS THAT IS STORED AT [EAX]
     CALL DWORD PTR [EAX+5] ;; CALLS THE ADDRESS THAT IS STORED AT [EAX+5]


     CDQ (Convert DWord (4Byte) to QWord (8 Byte))
     Syntax: CQD

     CDQ is an instruction that always confuses newbies when it appears first time. It is
     mostly used in front of divisions and does nothing else then setting all bytes of EDX
     to the value of the highest bit of EAX. (That is: if EAX <80000000, then EDX will be
     00000000; if EAX >= 80000000, EDX will be FFFFFFFF).


     CMP (Compare)
     Syntax: CMP dest, source

     The CMP instruction compares two things and can set the C/O/Z flags if the result fits.

     CMP EAX, EBX ;; compares eax and ebx and sets z-flag if they are equal
     CMP EAX,[404000] ;; compares eax with the dword at 404000
     CMP [404000],EAX ;; compares eax with the dword at 404000



     DEC (Decrement)
     Syntax: DEC something

     dec is used to decrease a value (that is: value=value-1)

     dec can be used in the following ways:
     dec eax ;; decrease eax
     dec [eax] ;; decrease the dword that is stored at [eax]
     dec [401000] ;; decrease the dword that is stored at [401000]
     dec [eax+401000] ;; decrease the dword that is stored at [eax+401000]

     The dec instruction can set the Z/O flags if the result fits.
    


     DIV (Division)
     Syntax: DIV divisor

     DIV is used to divide EAX through divisor (unsigned division). The dividend is always
     EAX, the result is stored in EAX, the modulo-value in EDX.

     An example:
     mov eax,64 ;; EAX = 64h = 100
     mov ecx,9 ;; ECX = 9
     div ecx ;; DIVIDE EAX THROUGH ECX

     After the division EAX = 100/9 = 0B and ECX = 100 MOD 9 = 1

     The div instruction can set the C/O/Z flags if the result fits.


     IDIV (Integer Division)
     Syntax: IDIV divisor

     The IDIV works in the same way as DIV, but IDIV is a signed division.
     The idiv instruction can set the C/O/Z flags if the result fits.


     IMUL (Integer Multiplication)
     Syntax: IMUL value 
              IMUL dest,value,value
              IMUL dest,value

     IMUL multiplies either EAX with value (IMUL value) or it multiplies two values and puts
     them into a destination register (IMUL dest, value, value) or it multiplies a register
     with a value (IMUL dest, value).

     If the multiplication result is too big to fit into the destination register, the
     O/C flags are set. The Z flag can be set, too.


     INC (Increment)
     Syntax: INC register

     INC is the opposite of the DEC instruction; it increases values by 1.
     INC can set the Z/O flags.


      INT 
     Syntax: int dest 

     Generates a call to an interrupt handler. The dest value must be an integer (e.g., Int 21h). 
     INT3 and INTO are interrupt calls that take no parameters but call the handlers for 
     interrupts 3 and 4, respectively.



     JUMP

     Berikut beberapa instruksi jump yang sering digunakan untuk membuat struktur kondisi dalam assembly. 

JA - Jump if (unsigned) above - CF=0 and ZF=0
JAE - Jump if (unsigned) above or equal - CF=0
JB - Jump if (unsigned) below - CF=1
JBE - Jump if (unsigned) below or equal - CF=1 or ZF=1
JC - Jump if carry flag set                              - CF=1
JCXZ-     Jump if CX is 0         - CX=0
JE - Jump if equal                                          - ZF=1
JECXZ- Jump if ECX is 0         - ECX=0
JG  - Jump if (signed) greater                          - ZF=0 and SF=OF (SF = Sign Flag)
JGE - Jump if (signed) greater or equal - SF=OF
JL - Jump if (signed) less - SF != OF (!= is not)
JLE - Jump if (signed) less or equal                 - ZF=1 and OF != OF
JMP - Jump                                                        - Selalu jump, tidak tergantung pada kondisi
JNA - Jump if (unsigned) not above                  - CF=1 or ZF=1
JNAE- Jump if (unsigned) not above or equal  - CF=1
JNB - Jump if (unsigned) not below                  - CF=0
JNBE -    Jump if (unsigned) not below or equal  - CF=0 and ZF=0
JNC - Jump if carry flag not set  - CF=0
JNE - Jump if not equal                                     - ZF=0
JNG - Jump if (signed) not greater                     - ZF=1 or SF!=OF
JNGE-     Jump if (signed) not greater or equal  - SF!=OF
JNL - Jump if (signed) not less          - SF=OF
JNLE -     Jump if (signed) not less or equal  - ZF=0 and SF=OF
JNO - Jump if overflow flag not set          - OF=0
JNP - Jump if parity flag not set - PF=0
JNS - Jump if sign flag not set         - SF=0
JNZ - Jump if not zero         - ZF=0
JO - Jump if overflow flag is set - OF=1
JP - Jump if parity flag set         - PF=1
JPE - Jump if parity is equal         - PF=1
JPO - Jump if parity is odd - PF=0
JS - Jump if sign flag is set         - SF=1
JZ - Jump if zero         - ZF=1



     LEA (Load Effective Address)
     Syntax: LEA dest,src

     LEA can be treated the same way as the MOV instruction. It isn't used too much for its
     original function, but more for quick multiplications like this:

     lea eax, dword ptr [4*ecx+ebx]
     which gives eax the value of 4*ecx+ebx



     MOV (Move)
     Syntax: MOV dest,src

     This is an easy to understand instruction. MOV copies the value from src to dest and src 
     stays what it was before.

     There are some variants of MOV:

     MOVS/MOVSB/MOVSW/MOVSD EDI, ESI: Those variants copy the byte/word/dword ESI points to, 
to the space EDI points to.

     MOVSX: MOVSX expands Byte or Word operands to Word or Dword size and keeps the sign of the
value.

     MOVZX: MOVZX expands Byte or Word operands to Word or Dword size and fills the rest of the
space with 0.



     MUL (Multiplication)
     Syntax: MUL value 

     This instruction is the same as IMUL, except that it multiplies unsigned. It can set the
     O/Z/F flags.



     NOP (No Operation)
     Syntax: NOP

     This instruction does absolutely nothing 
     That's the reason why it is used so often in reversing ;)



     OR (Logical Inclusive Or)
     Syntax: OR dest,src

     The OR instruction connects two values using the logical inclusive or.
     This instruction clears the O-Flag and the C-Flag and can set the Z-Flag.

     To understand OR better, consider those two binary values:

                                    1001010110
                                    0101001101
     
     If you OR them, the result is 1101011111

     Only when there are two 0 on top of each other, the resulting bit is 0. Else the resulting
     bit is 1. You can use calc.exe to calculate OR. I hope you understand why, else
     write down a value on paper and try ;)



     POP
     Syntax: POP dest

     POP loads the value of byte/word/dword ptr [esp] and puts it into dest. Additionally it
     increases the stack by the size of the value that was popped of the stack, so that the next
     POP would get the next value.



     PUSH
     Syntax: PUSH operand

     PUSH is the opposite of POP. It stores a value on the stack and decreases it by the size
     of the operand that was pushed, so that ESP points to the value that was PUSHed.



    REP/REPE/REPZ/REPNE/REPNZ 
     Syntax: REP/REPE/REPZ/REPNE/REPNZ ins 

     Repeat Following String Instruction: Repeats ins until CX=0 or until indicated condition 
     (ZF=1, ZF=1, ZF=0, ZF=0) is met. The ins value must be a string operation such as CMPS, INS, 
     LODS, MOVS, OUTS, SCAS, or STOS.



     RET (Return)
     Syntax: RET
             RET digit

     RET does nothing but return from a part of code that was reached using a CALL instruction.
     RET digit cleans the stack before it returns.


     SUB (Subtraction)
     Syntax: SUB dest,src

     SUB is the opposite of the ADD command. It subtracts the value of src from the value of
     dest and stores the result in dest.

     SUB can set the Z/O/C flags.


VII.  Operasi Logical


Berikut ini truth tabel logical :




AND

0 and 0 = 0
0 and 1 = 0
1 and 0 = 0
1 and 1 = 1

Jadi, operator and hanya menghasilkan 1 jika kedua operand bernilai 1.
contoh :

mov al, 0000 0001
mov bl, 0000 1111
and al, bl

maka hasilnya disimpan pada register al.
al bernilai 0000 0001.

Salah satu kegunaan and adalah untuk mengidentifikasi nilai low nible atau high nible. Kata low nible digunakan untuk merujuk pada 4bit dari kanan dari satu byte dan high nible adalah 4bit dari kiri. Mengapa tidak diakses langsung?jawabnya tidak bisa. Satuan terkecil dari register yang dapat diakses adalah 1byte. Misalnya al berisi 0xCA. Untuk mengetahui bilangan kiri maka digunakan operasi berikut :

and al, 0x0F

atau bisa ditulis :

and 1100 1010, 0000 1111,

karena high nible dari al di and dengan 0, maka hasilnya high nible al bernilai 0000 dan low nible tidak berubah. Hasilnya setelah operasi itu adalah al = 0x0A atau 0000 1010


  TEST
     
    Test mirip dengan and, bedanya adalah test tidak menyimpan hasilnya pada register. Tapi test akan         membuat zero flag bernilai 1 jika hasil operasinya nol.
    Syntax: TEST operand1, operand2

     This instruction is in 99% of all cases used for "TEST EAX, EAX". It performs a Logical
     AND(AND instruction) but does not save the values. It only sets the Z-Flag, when EAX is 0
     or clears it, when EAX is not 0. The O/C flags are always cleared.



  XOR
     Syntax: XOR dest,src

     The XOR instruction connects two values using logical exclusive OR (remember OR uses 
     inclusive OR). 

     This instruction clears the O-Flag and the C-Flag and can set the Z-Flag.
     To understand XOR better, consider those two binary values:

                                    1001010110
                                    0101001101

     If you OR them, the result is 1100011011

     When two bits on top of each other are equal, the resulting bit is 0. Else the resulting
     bit is 1. You can use calc.exe to calculate XOR. 
     The most often seen use of XOR is “XOR, EAX, EAX”. This will set EAX to 0, because when
     you XOR a value with itself, the result is always 0. I hope you understand why, else
     write down a value on paper and try ;)






STRING CHAR UNICODE UTF-32 UTF-16 DAN UTF-8

Dalam bahasa tingkat tinggi seperti java dan python, string mungkin tidak memerlukan perhatian khusus. Tapi pada bahasa c/c++ string perlu ditangani secara kusus. Terlebih lagi dalam bahasa assembly. Dulu ketika komputer hanya dijual di Amerika, tidak ada masalah encoding karena ASCII sudah cukup untuk  digunakan sebagai pengkodean karakter yang ukurannya cuma memerlukan 1byte. 

Seiring perkembangan komputer dan internet, hal tersebut menjadi masalah karena tidak dapat menampilkan karakter tertentu seperti huruf korea dan arab. Masalah tersebut diatasi dengan teknologi code page. Jadi setiap negara memiliki code page masing-masing dalam pengkodean karakternya. Tapi hal tersebut menimbulkan masalah baru lagi, karakter untuk bahasa berbeda tidak dapat ditampilkan dalam satu mesin yang sama. Maka lahirlah utf-16 dan utf-32. UTF-16 menyimpan karakter dengan ukuran 2byte sedangkan UTF-32 menyimpan karakter dalam ukuran 4byte. Masalah lain yang muncul adalah adanya pemborosan storage penyimpanan ketika karakter yang disimpan hanya ASCII dan harus disimpan dengan menghabiskan 2byte atau 4byte, ini dirasa overkill.

Sederet masalah diatas akhirnya terpecahkan dengan lahirnya UTF-8. UTF-8 menyimpan karakter dengan ukuran 1byte, 2byte, 3byte sampai 4byte tergantung kebutuhan. ASCII disimpan hanya menggunakan 1byte. Masalah dari sistem utf-8 ini adalah perlu identifikasi khusus untuk menentukan karakter. Misalnya "Aݔ" dalam hexadecimal "410754", bagaimana cara komputer membedakan karakter pertama adalah 0x41 dan karakter kedua dua byte 0x0754. Mengapa tidak 0x4107 dan 0x54 ? Nah, memang sulit dari sudut pandang bahasa tingkat tinggi. Tapi dari sudut pandang assembly semuanya menjadi jelas. Berikut tabel dari wikipedia bahasa indonesia :

Bit
code point
Code point
pertama
Code point
terakhir
Byte dalam
sequence
Byte 1Byte 2Byte 3Byte 4Byte 5Byte 6
  7U+0000U+007F10xxxxxxx
11U+0080U+07FF2110xxxxx10xxxxxx
16U+0800U+FFFF31110xxxx10xxxxxx10xxxxxx
21U+10000U+1FFFFF411110xxx10xxxxxx10xxxxxx10xxxxxx
26U+200000U+3FFFFFF5111110xx10xxxxxx10xxxxxx10xxxxxx10xxxxxx
31U+4000000U+7FFFFFFF61111110x10xxxxxx10xxxxxx10xxxxxx10xxxxxx10xxxxxx

Jadi, cara membedakan apakah karakter tersebut 1byte, 2byte, 3byte atau 4byte adalah dari bilangan biner yang digunakan. Jika awalnya adalah 0, berarti jumlah byte merepresentasikan karakter tersebut adalah 1byte. Jika awalnya adalah 110, berarti ukuran karakter tersebut 2byte, 1110 untuk 3byte, 11110 untuk 4byte dan seterusnya. Byte selanjutnya jika berisi 10 berarti sambungan dari byte sebelumnya. Maka dapat diidentifikasi 0100 0001 adalah karakter yang hanya 1byte (A) , dan 11011101 10010100 adalah karakter 2byte (ݔ). 

Minggu, 20 September 2020

TUTORIAL 1 : BELAJAR ASSEMBLY UNTUK ANDROID ARM

Ass. Wr. Wb.

Pada kesempatan ini kita akan belajar membuat code assembly untuk android ARM. Disini akan dipraktekkan bagaimana cara membuat aplikasi android tanpa gradle atau tanpa build system. Jadi kita akan membuat aplikasi android tanpa Android Studio. Mengapa kita perlu untuk belajar membuat aplikasi android tanpa Android studio dan gradle, ya karena koneksi internet di indonesia belum merata dan masih mahal. Jadi kita perlu cara untuk mendevelop aplikasi android secara offline. Peralatan yang diperlukan adalah  Android NDK r21 dari situs https://android-developers.googleblog.com cukup download sekali, sudah bisa dipakai di banyak komputer secara offline. System yang saya pakai adalah windows 64bit, jadi download NDKnya ya untuk windows64bit juga. 




Untuk teman-teman bisa sesuaikan dengan system yang dipakai, apakah linux atau mac. Adapun device yang digunakan untuk demo programnya adalah android ice cream sandwich atau ICS, bukan emulator. Alasannya adalah untuk memastikan apakah aplikasi benar-benar akan berjalan di android device smartphone. Perlu diperhatikan bahwa bahasa assembly ini harus dijalankan pada emulator yang bisa mengemulasi processor ARM. Bluestack, NOX, PrimeOS, PhoenixOS semuanya x86_64 tidak bisa ARM. Pada kesempatan lain kita akan menggunakan QEMU, karena qemu ini bisa mengemulasi perangkat keras seperti processor arm 32bit, arm 64bit, x86_64 dan lain-lain. Untuk mengetahui lebih dalam silahkan merujuk ke www.qemu.org.


; Code program sebaiknya diberi keterangan lengkap agar mudah di fahami

;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
; Nama Program : Halo Dunia Robot
; Nama file : halo.s
; Dibuat oleh : Gunawan Jinnu
; Tanggal pembuatan : 20 September 2020
; Tanggal update : 20 September 2020
; Deskripsi
- Menampilkan pesan Halo Dunia Robot

; Compiler : NDK r21 x86_64 https://android-developers.googleblog.com
; Compile dengan cara :
; arm-linux-androideabi/bin/as -o halo.o halo.s
; Link dengan cara :
;
arm-linux-androideabi/bin/ld -o halo halo.o
;Link source code : https://re-pinrang.blogspot.com
;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;

.data
    pesan: .ascii "Halo Dunia Robot.\n"
    len_pesan = . - pesan

.bss

.text
.globl _start
_start:
mov %r0, $1 // file descriptor 1 (stdout)
ldr %r1, =pesan
mov %r2, $len_pesan
mov %r7, $4 // syscall 4 (syswrite)
swi $0                  //linux kernel syscall
mov %r0, $0         // exit status return 0
mov %r7, $1 // syscall 1 (exit)
swi $0                  //linux kernel syscall


Kalau kita perhatikan, kode assembly ini sama saja dengan assembly di linux. Mulai dari file descriptor yang digunakan 1 sebagai standard output di linux. Kemudian syscall 4 syswrite linux. Syscall 1 untuk exit. Yang agak beda adalah swi $0, kalau di linux itu intrupsi 0x80h, tapi artinya sama saja linux kernel syscall.

Cara compile :
Extrack NDK r21 yang sudah di download, cari lokasi folder android-ndk-r21b\toolchains\arm-linux-androideabi-4.9\prebuilt\windows-x86_64\arm-linux-androideabi\bin

as -o halo.o halo.s

menghasilkan file objek halo.o

link dengan cara:

ld -o halo halo.o

Cara running di android :
1. Hubungkan dengan kabel usb, buka cmd, ketik : adb push halo /sdcard/
2. Ketik : adb shell
3. mkdir /data/user/temp
4. mv /sdcard/halo /data/user/temp/
5. cd /data/user/temp
6. ./halo

Setelah itu harusnya muncul pesan Halo Dunia Robot.

Ok. Semoga bermanfaat. Terimakasih.

Dasar

TUTORIAL 1 : DASAR BAHASA ASSEMBLY WINDOWS 32bit MASM32 CONSOLE UNTUK PEMULA

Alhamdulillahirabbilalamin, pada saat ini kita semua masih diberi kesempatan untuk terus belajar, kita niatkan mudah-mudahan apa yang kita l...

Postingan Populer