Assembly

Behind the wizard's curtain

A talk by Ciro Durán (@chiguire)
<ciro.duran@gmail.com>
December 2015

Press 's' for the speaker notes

Hello!

My name is Ciro Durán, and during the next hour we will be talking about assembly.

Let's honour the fallen from the Ringworld and prevent another disaster like this

Order of the day

  • Processors' generalities
  • 6502 processor assembly
  • x86 processor assembly
  • The gap between assembly and high-level languages

What's a processor?

What's so scary?

Where do I start to learn?

Today's agenda

  • Processors' generalities
  • 6502 processor assembly
  • x86 processor assembly
  • The gap between assembly and high-level languages

The 6502 processor

Your weapons

  • 3 registers: X, Y, and A (accumulator)
  • Stack Pointer (SP)
  • Program Counter (PC)
  • Processor Flags (Zero, Carry, Decimal mode, among others)
  • Did I mention that, except for the PC, this is all 8-bit?

First Program in 6502

A game for the 6502

Load, Store, Memory and Numbers

Addressing Memory

Adding and substracting

Branching

Now you're thinking with p̶o̶r̶t̶a̶l̶s̶  assembly

Today's Agenda

  • Processors' generalities
  • 6502 processor assembly
  • x86 processor assembly
  • The gap between assembly and high-level languages

x86 processor assembly

Your weapons

  • Eight 32-bit registers, 6 for general purpose (yay!), and 2 with special purpose
    • EAX, EBX, ECX, EDX, ESI, EDI
    • ESP (Stack pointer)
    • EBP (Base pointer)
  • EFLAGS register (32 bits for processor state and result operations)
  • EIP - Instruction Pointer
  • 64-bit adds 8 more registers, and the E is exchanged by an R.
  • The CPU has 2 modes of running:
    • Real mode, Protected mode.

Addressing Memory

  • We'll limit ourselves to flat addressing.
    • Let's not forget about segments, though (CS, DS, ES, SS)
  • Static data declarations
    • Precede with .DATA
    • DB, DW, DD (1, 2 or 4 byte size)
    • [name] [size] [initial value] (e.g. foo DD 32567)
    • We can declare some primitive arrays with DUP

Addressing Memory

  • x86 CPUs addresses are 32-bits wide (e.g. 0xdeadbeef)
  • mov eax, [ebx] → gets value from address specified in ebx
  • mov [var], ebx → stores register value in address specified in constant
  • mov eax, [esi-4] → gets value from address specified in ebx ± 4 bytes
  • mov [esi+eax], foo → stores value in memory to address at esi + eax
  • mov edx, [esi+4*ebx] → gets value from address specified in in esi + 4 * ebx

Assembly instructions

  • Data movement instructions
    • mov, push, pop, lea
  • Arithmetic/logic
    • add, sub, inc, dec
    • imul, idiv
    • and, or, xor, not, neg
    • shl, shr
  • Control flow
    • jmp
    • je, jne, jz, jg, jge, jl, jle, jo
    • cmp
    • call, ret

Calling Conventions

cdecl, stdcall.

What about interrupts?

Let's see x86 assembly in action

Here's Microsoft Assembler, MASM32

We can create a project automatically

It generates some boilerplate code

With a convenient batch file that assembles and links the code

Here's the build output

And here's the code in action

Today's Agenda

  • Processors' generalities
  • 6502 processor assembly
  • x86 processor assembly
  • The gap between assembly and high-level languages

The gap between assembly and high-level languages

The disassembly

The combination of the symbols and disassembly might assist you when debugging.

Let's compile a simple Hello World console application

Let's set a breakpoint, run it, and go to disassembly

Woah, that's a lot of instructions, what's 945858h?

Turns out we can watch it and see it's the pointer to the string

Closing Remarks

“Although, some people can program in assembly language and understand the intricacy of the spacecraft [Voyager programme], most younger people can't or really don't want to.” - Suzanne Dodd (NASA JPL)

You can also practice here http://challenges.re/

Thanks for being here!

Your questions are welcome!

http://ciroduran.com
@chiguire
ciro.duran@gmail.com

Browse this talk (press 's' for notes) at
http://ciroduran.com/talks/assembly