"Siapa tidak mendekat kepada Allah gara-gara halusnya kebaikan yang Dia berikan, maka ia akan diseret (supaya mendekat) dengan rantai cobaan."

Simple Use GCC with inline asm

by danang.wijanarko@gmail.com

 

Preliminare

AT&T vs Intel sytax

  AT&T Intel
Register naming %eax eax
Src/dst order movl %eax, %ebx mov ebx, eax
Constant/immediate value movl $1, %eax mov eax, 1
Operator size spec. movw %ax, %bx mov bx, ax

And again about memory reference

  AT&T Intel
Format immed32(base_pointer, index_pointer, index_scale) [base_pointer + index_pointer * index_scale + immed32]
Access memory address label label [label]
Addressin what a register point to (%eax) [eax]
Addressing a variable offset by a value in a register variable(%eax) [eax + variable]
Addressing a value in an array of integers (scaling up by 4) array(, %eax, 4) [eax * 4 + array]
Simple math on the immediate value _struct_pointer+8 -

ALL

The propose syntax is:

        __asm__ __volatile__ (
"statement" : "output_regs" (passed_var_from_c_code) : "input_regs" (passed_var_from_c_code) : "clobbered_regs"
);

And here are the list of registers and the symbols:

        a	: e(a)x
b : e(b)x
c : e(c)x
d : e(d)x
S : e(S)i
D : e(D)i

i : (i)mmediate that would allow an address known only at link time
I : (I)mmediate 0 to 31
J : immediate 0 to 63
K : immediate 255
L : immediate 65535
M : immediate 0 to 3 (shift that can be done with lea)
N : immediate 0 to 255 (one-byte immediate value)
O : immediate 0 to 32

q : byte-addressable register or dynamically allocated register from eax, ebx, ecx, and edx
r : (r)egister same as q but also consider esi and edi
g : (g)eneral effective address register eax, ebx, ecx, edx, or variable in memory
m : (m)emory effective address
A : eax and edx combined into 64-bit integer (for long longs type)

Some sample descriptive usage:

      1. Using dynamically allocated register
long int a, b;

a = 1; b = 2;
__asm__ __volatile__ (
"movl %1, %0" : "=r" (b) : "rI" (a)
);

or

__asm__ __volatile__ (
"movl %0, %0" : "=r" (b) : "0" (a)
);

Note:
- Above code will operate in similiar way as b = a.
- The "rI" from first code meas a could use r or I register.
- The "0" from second code make %0 = %1, so need not use %1 because %1 is the same as %0, this need some explanation.
we see this code:
movl %1, %0 means %1 as source and %0 as destination.
when we use:
movl %0, %0 means the first %0 is source and the later %0 is destination.
from above, the input is a and output is b.
then the %0 will be interpreted as:
a : when act as input or source
b : when act as output or destination

Q : Is it legal of using the same register for one instruction simultaneously ?
A : well actually gcc use temporary register to do that

2. Clobbered exmaple
__asm__ __volatile__ (
"movl %0, $1" : : "r" (a) : "r0"
);

3. The very special cases like K are mostly used inside GCC in alternative code sequences,
providing a special-case way to do something like ANDing with 255.

4. Using I
For x86 rotate left

__asm__ __volatile__ (
"roll %1, %0" : "=g" (result) : "cI" (rotate), "0" (input)
)

The constraint

well see the gcc doc about constraint, AKU MALES...

Some basic that i often forget

        -- JMP INSTRUCTION
Purpose : Unconditional jump.
Syntax :
JMP destiny
Note :
This instruction is used to deviate the flow of a program without taking into account the actual conditions of the flags or of the data.

-- JA (JNBE) INSTRUCTION
Purpose : Conditional jump.
Syntax :
JA Label
Note :
After a comparison this command jumps if it is or jumps if it is not down or if not it is the equal.
This means that the jump is only done if the CF flag is deactivated or if the ZF flag is deactivated, that is that one of the two be equal to zero.

-- JAE (JNB) INSTRUCTION
Purpose : Conditional jump.
Syntax :
JAE label
Note :
It jumps if it is or it is the equal or if it is not down.
The jump is done if CF is deactivated.

-- JB (JNAE) INSTRUCTION
Purpose : Conditional jump.
Syntax :
JB label
Note :
It jumps if it is down, if it is not , or if it is the equal.
The jump is done if CF is activated.

-- JBE (JNA) INSTRUCTION
Purpose : Conditional jump.
Syntax :
JBE label
Note :
It jumps if it is down, the equal, or if it is not .
The jump is done if CF is activated or if ZF is activated, that any of them be equal to 1.

-- JE (JZ) INSTRUCTION
Purpose : Conditional jump.
Syntax :
JE label
Note :
It jumps if it is the equal or if it is zero.
The jump is done if ZF is activated.

-- JNE (JNZ) INSTRUCTION
Purpose : Conditional jump.
Syntax :
JNE label
Note :
It jumps if it is not equal or zero.
The jump will be done if ZF is deactivated.

-- JG (JNLE) INSTRUCTION
Purpose : Conditional jump, and the sign is taken into account.
Syntax :
JG label
Note :
It jumps if it is larger, if it is not larger or equal.
The jump occurs if ZF = 0 or if OF = SF.

-- JGE (JNL) INSTRUCTION
Purpose : Conditional jump, and the sign is taken into account.
Syntax :
JGE label
Note :
It jumps if it is larger or less than, or equal to.
The jump is done if SF = OF

-- JL (JNGE) INSTRUCTION
Purpose : Conditional jump, and the sign is taken into account.
Syntax :
JL label
Note :
It jumps if it is less than or if it is not larger than or equal to.
The jump is done if SF is different than OF.

-- JLE (JNG) INSTRUCTION
Purpose : Conditional jump, and the sign is taken into account.
Syntax :
JLE label
Note :
It jumps if it is less than or equal to, or if it is not larger.
The jump is done if ZF = 1 or if SF is defferent than OF.

-- JC INSTRUCTION
Purpose : Conditional jump, and the flags are taken into account.
Syntax :
JC label
Note :
It jumps if there is cartage.
The jump is done if CF = 1

-- JNC INSTRUCTION
Purpose : Conditional jump, and the state of the flags is taken into account.
Syntax :
NC label
Note :
It jumps if there is no cartage.
The jump is done if CF = 0.

-- JNO INSTRUCTION
Purpose : Conditional jump, and the state of the flags is taken into account.
Syntax :
JNO label
Note :
It jumps if there is no overflow.
The jump is done if OF = 0.

-- JNP (JPO) INSTRUCTION
Purpose : Conditional jump, and the state of the flags is taken into account.
Syntax :
JNP label
Note :
It jumps if there is no parity or if the parity is uneven.
The jump is done if PF = 0.

-- JNS INSTRUCTION
Purpose : Conditional jump, and the state of the flags is taken into account.
Syntax :
JNP label
Note :
It jumps if the sign is deactivated.
The jump is done if SF = 0.

-- JO INSTRUCTION
Purpose : Conditional jump, and the state of the flags is taken into account.
Syntax :
JO label
Note :
It jumps if there is overflow.
The jump is done if OF = 1.

-- JP (JPE) INSTRUCTION
Purpose : Conditional jump, the state of the flags is taken into account.
Syntax :
JP label
Note :
It jumps if there is parity or if the parity is even.
The jump is done if PF = 1.

-- JS INSTRUCTION
Purpose : Conditional jump, and the state of the flags is taken into account.
Syntax :
JS label
Note :
It jumps if the sign is on.
The jump is done if SF = 1.

 

- d

Comment viewing options

Select your preferred way to display the comments and click "Save settings" to activate your changes.

Superp

Keren Hebuat hehehehe ingin sekali rasanya bisa menggunakan bahasa asembly pada PC.

mas/mbak gimana kalau contoh aplikasi (sederhana) nya di perbanyak okeh

Aplikasi Kernel Microkernel (dix)

Mas kenzainn,

Saya membuat satu kernel, saya beri nama dix, kernel ini saya load di sourceforge. Mungkin sampeyan tertarik silahkan di download saja di http://sourceforge.net/projects/dix karena program yang saya buat ini masih dalam domain free.

Kernel ini modelnya "microkernel" yang secara desain, berbeda dengan Linux. Di kernel ini ada beberapa contoh penggunaan assembly coding. Dan kernel ini adalah kernel untuk belajar. Tapi saya desain untuk mesin processor i386. Mungkin sampeyan tertarik untuk mengembangkannya lebih lanjut ?

Catatan di atas adalah catatan saya waktu masih proses membuat kernel ini. Semoga bermanfaat. :)

 

- d

"Bekatul Port" Personal Enterprise Number (PEN) registered @ IANA OID

ASN.1 Notation: {iso(1) identified-organization(3) dod(6) internet(1) private(4) enterprise(1) 30347}

Dot Notation: 1.3.6.1.4.1.1.30347

IRI Notation: oid:/ISO/Identified-Organization/6/1/4/1/30347


View Stat Counter