Showing posts with label assembly. Show all posts
Showing posts with label assembly. Show all posts

Sunday, May 29, 2011

Intel vs AT&T syntax

There're two popular assembly syntaxes: Intel and AT&T. Intel syntax is popular in Windows world. In Linux, AT&T syntax is more popular though GAS (Gnu Assembler) supports both.

The following lists some major differences of the two syntaxes:

  1. AT&T prefixes register with % sign
    * Intel:
       eax, ebx, ecx,...
    * AT&T:
       prefix by % sign: %eax, %ebx, %ecx,...

  2. AT&T prefixes immediate value with $ sign, Intel is not
    * Intel: 10, 80h
    * AT&T: $10, $0x80

  3. AT&T and Intel syntax use opposite instruction operands
    * Intel:      mnemonic destination, source 
       Ex:         mov eax, 100
    * AT&T:  mnemonic source, destination
       Ex:        movl $100, %eax

  4. AT&T suffixes instruction to specify instruction's operand size (1 byte: b, 2 bytes: w, 4 bytes: l) but Intel uses directive before operands (1 byte: byte ptr, 2 bytes: word ptr, 4 bytes: dword ptr)
    * Intel:
       mov al, byte ptr foo
    * AT&T:
       movb foo, %al

Saturday, May 28, 2011

Assembly Language

Assembly can be seen as machine language but in symbols/mnemonics instead of 0s or 1s. So one can make use of any aspect of computer's power if writing programs in assembly.

Assembly is specific to machine architecture. IA32 (also called x86, i386) is the most popular architecture for PC.
  1. Register
    Can be classified in 4 types:
    - general purpose (eax, ebx, ecx, edx)
    - pointer/index (esp, ebp, esi, edi)
    - instruction pointer (eip)
    - flags (eflags)

    These are all 32 bits. Each register contains 8-bit and 16-bit parts. Ex: eax (32 bits), ax (16 bits), ah (8 bits), al (8bits).

  2. Instruction
    - arithmetic/logic: add, sub, and, or,...
    - control: jmp,..
    - data movement: mov,..

  3. Operand
    - register: operand value is contained in register
    - immediate: operand value is a constant
    - memory: operand value is in memory

  4. Addressing mode
    Addressing mode is the way to specify a memory address.
    - absolute:
      address = a value
    - register:
      address = register content
    - displacement:
      address = register content + a value
    - indexed:
      address = register content + a value + another register content (index) * another value (scale)

  5. Subroutine
    - subroutine is a set of instructions
    - parameters passed to subroutine are usually pushed on stack