Sunday, 26 February 2017

Quick look at Intel Assembly

Intel registries 64 x86



Assembly Instructions

MOV
  • mov eax, ESP   ;  Move stack pointer to eax
  • mov ebx, [ESP] ;  Move value at top-of-stack to ebx  ( memory content moved 
  • .DATA
    array DD 20 DUP (0) ; Array of 20 integers initialized to zero
    .CODE
    mov ecx, OFFSET array ;  Move starting address of 'array' to ecx. OFFSET is used to move addresses not memory content
  • mov ecx, array ; copy the first element of the array into ecx.  ( memory content moved )
  • mov ecx, 20[ebp]  ;copy the element at [ebp+20] into ecx ( memory content moved )
  • mov eax, table[ESI*4] ; copy element at [ OFFSET table + esi * 4] into eax ( memory content moved )
  • mov ecx, DWORD PTR ?myArray@@3PAHA[eax]  ;  move a DWORD element from myArray@@3PAHA position EAX  into ECX. myArray@@3PAHA is a pointer to the segment area (memory) where myArray lives.  ( memory content moved )
    PTR gives size directives to the instruction.

LEA (Load Effective address)  
  • lea edi, DWORD PTR [ebp-192] ; copy [ebp-192] address into o edi
ADD

  • add eax,[DI + 20 ] ; eax = eax + memory [ DI +20 ]   ( memory content moved )
  • add BYTE PTR  [var], 10 ; add 10 to the single byt eax = eax + memory [ DI +20 ]   ( memory content moved )





C++ Example

Assembly generated by MS Visual Studio 2015
class FooClass
{
protected:
int id;     //               4 bytes
public:
float vel[2]; // 4 bytes x 2   8 bytes
float acc[2]; // 4 bytes x 2   8 bytes
// total 20 bytes
void SetId(int _id)
{
id = _id;
}
};

FooClass * f = new FooClass();
mov  DWORD PTR $T1[ebp], 20 ;20 bytes size of the class
mov  eax, DWORD PTR $T1[ebp] ;  move base pointer into eax
push eax ;  save eax
call ??2@YAPAXI@Z ;  extern operator new
add esp, 4 ;  add 4 bytes to the stack pointer  ( pointer  size? )
mov DWORD PTR $T2[ebp], eax ;  move eax address where  base pointer  is pointing at
cmp DWORD PTR $T2[ebp], 0 ;  if the address of  base pointer  is 0
je SHORT $LN3@main ;
mov ecx, DWORD PTR $T1[ebp] ; move  base pointer into ecx
push ecx ; save ecx
push 0  push 0
mov edx, DWORD PTR $T2[ebp] ;
push edx
call _memset ; initiaize to 0 class member
add esp, 12 ; 0000000cH ; add 12 to to the stack pointer
mov eax, DWORD PTR $T2[ebp]
mov DWORD PTR tv67[ebp], eax
jmp SHORT $LN4@main ; continue the program 
$LN3@main:
mov DWORD PTR tv67[ebp], 0 
$LN4@main:
mov ecx, DWORD PTR tv67[ebp] mov DWORD PTR _d$[ebp], ecx 















No comments:

Post a Comment