x86 Assembly

BIOS

  • Responsible for the very first bit of code on the system

  • Does initialization of basic hardware

  • Provides an API for hardware access

  • It knows how to find media to boot off of, load code from them into memory and hands control over

CPU

  • A CPU fetches the instruction, decodes it and executes it.

  • Contain some tiny local storage (registers) - takes ~1-3 nanoseconds to access

  • 1 nanosecond is 10āˆ’910^{-9}th of a second

  • It also contains on-chip cache - takes ~2-8 ns to access L1, ~5-12 to access L2

  • RAM is 10-60 ns

  • SSD is 100 microseconds (10āˆ’610^{-6})

  • Disk is 3-20 milliseconds (10āˆ’310^{-3})

Talking to Devices

  • Memory-mapped I/O:

    • Part of address space is mapped to device, rather than to main memory.

  • Port I/O:

    • Special CPU instructions to talk to I/O ports.

    • Only 1024 ports defined.

  • Interrupts:

    • How devices talk back.

    • When devices want to talk back they can raise an interrupt

Modes of Operation

  • Protected Mode:

    • This is what runs when OS is running

    • Native mode (Windows, Linux)

    • Full features

    • Separate memory

    • 32-bit addresses and 4GB address space

  • Virtual 8086 Mode:

    • Machine starts in this mode

    • It imitates a machine from the 80s

    • It changes some registers to become a full 32-bit or 64-bit machine

    • 20-bit addresses (Registers + Segments) and 1 MB address space

  • Real Address Mode:

    • Native MS-DOS

    • 20-bit addresses (Registers + Segments) and 1 MB address space

  • System Management Mode:

    • Power management

    • System security

    • Diagnostics

Assembly x86 (32-bit)

  • General purpose registers:

    • EAX, EBC, ECD, EDX

    • ESI, EDI

    • ECX generally used for counters

    • ESI, EDI used as source and destination for string operations

  • Special-purpose registers

    • EBP (base pointers)

    • ESP (stack pointer which manages actual stack)

  • Flags

    • EFAGS is the flags register

  • Segment registers

    • CS, SS, DS, ES, FS, GS

    • Used to give you more memory when you needed more than 2162^{16} bytes of memory.

  • Instruction pointer / program counter

    • EIP

  • Control registers

    • CR0 to CR4

Accessing Parts of a Register

x86 Instructions

Conventions

  • Instructions are variable length

  • Conventions:

    • regN means register of N bits

    • mem means memory

    • constN means constant of N bits

  • x86 syntax comes in two flavors

    • Intel

    • AT&T

  • xv6 uses AT&T syntax

    • AT&T syntax has SRC first DEST second

    • Example: movl (%ebx), %eax

Data Movement

  • mov <reg>, <reg>

  • mov <reg>, <mem>

  • mov <mem>, <reg>

  • mov <reg>, <const>

  • mov <mem>, <const>

  • movement from memory to memory does not exist

movl %eax, %eax ;edx = eax 
movl $0x123, %ebx ;ebx = 0x123 -- puts that exact value using $ sign
movl 0x123, %ebx ;ebx = *(int*) 0x123 -- fetches from memory using no $ sign
movl (%ebx), %edx ;edx = *(int*) ebx -- dereference register using parantheses
movl 4(%ebx), %edx ;edx = *(int*)(ebx + 4)

Arithmetic

  • add, sub, inc, dec

  • imul, idiv

  • and, or, xor

  • not

  • neg

  • shl, shr

Logical

  • test %x, %y (equivalent to bitwise and %x, %y - but does not modify %y)

  • cmp %x, %y (equivalen to sub %x, %y - but does not modify %y

  • They don't modify destination operand

  • Results go to flags by setting bits in EFLAGS as a side effect.

Unconditional and Conditional Branches

  • jmp: Unconditional jump

    • jmp <const> jumps to constant address

    • jmp <mem> dereferences mem and jump

    • jmp <reg> jumps to address in register

  • Several conditional jumps exist.

  • je/jz: Jump is equal or zero

  • jne/jnz: Jump if not equal or not zero

  • jg: Jump if greater

  • jge: Jump if greater/equal

  • jl: Jump if less

  • jle: Jump if less than or equal

Strings

  • Source is DS:SI

  • Destination is ES:DI

  • lodsb, stosb: Loads or stores a byte from %esi or %edi and increments %esi or %edi

  • movsb: Moves a byte from ESI to EDI

  • scasb: compares %(es:edi) with %al and sets flags, then increments %sdi

  • String operations can be used with repCC prefix, which repeats instructions until CC condition is true

Labels

  • Data label

    • Must be unique

    • Example: myArray BYTE 10

  • Code label (ends with a colon)

    • Target of jump and loop instructions

    • Example: L1: mov

System

  • Control system behavior

  • int executes a software interrupt (gets you into kernel mode)

  • iret returns from an interrupt (gets you back into user mode)

  • sysenter/sysret (newer way to enter/exit kernel mode)

Push/Pop

  • ESP is a stack pointer

  • On x86 the stack grows downward: Which means that putting something on the stack decrements the stack pointer

  • push: Substracts 4 from ESP, then writes to memory at address ESP

Function Calls

  • Supports creation of functions

  • call <f>: Pushes the address of the next instruction on the stack and jumps to f

  • Example:

    • 1000 call 0x1234

    • 1005 movl %eax, %ebx

    • This pushes 1005 to the stack and jumps to f aka 0x1234

  • ret: Pops return address off the stack and jumps to it

Segmentation

  • 8086 had 20 address lines (i.e bits)

  • But only 16-bit registers

  • Segment provides a way to get the extra 4 bits

  • cs: code segment (modifies code fetch)

  • ds: data segment (modifies data access)

  • ss: stack segment (modifies ESP stack and EBP base pointers)

  • es: extra segment (modifies string ops)

// Some code

Last updated