Assembly Language
By Mohammed Imran
Get your hands dirty with
PART- I
@imran_naseem
If IsayAssemblyiscool!
Seriously ?
Peoplesay
Andsomegoevenfurther..
Butyoucoulddosome
Amazingthings
CreatefasterprogramsNo, you cant fly cars with assembly :)
Flythese
Letsstart
CPUcan'tunderstandc,javaor
assembly.
hencewehavecompilers,assembersto
converthighlevelcodetomachinecode.
Hello.c
#include <stdio.h>
#define STRING "Hello World"
int main(void)
{
/* Using a macro to print 'Hello World'*/
printf(STRING);
return 0;
}
Youcanseetheseintermediate
stagesusinggcccommand
gcccommands
gcc -Wall -save-temps hello.c -o hello
The above command saves temporary files generated during
creation of binary file hello in the current directory
ls hello.*
hello.i ;Preprocessedfile
hello.s ;assemblyfile
hello.o ;objectfile
hello ;binaryfile
Demo
An assembly language is a low-level
programming language for a computer, or
other programmable device, in which there is
a very strong (generally one-to-one)
correspondence between the language and
the architecture's machine code instructions.
“
”
WhatisAssemblylanguage?
Assembly is easy to learn, but hard to master!
“ ”
Assemblyactsasbridge
Machine Language High level language
Assembly Language
Machinecode
10110000 01100001
This is how, an instruction
in machine language looks like
Andcodeisparsedlike.
10110000 01100001
Instruction Register Register/Operand
Machinecodeinhex
10110000 01100001
B0 61 (in hex)
The above machine code represented
In hexadecimal format for ease.
Assemblyrepresentation
10110000 01100001
B0 61 (in hex)
MOV AL, 61h
The above machine code represented
In assembly language code
MOV AL, 61h ; Load AL with 97 decimal (61 hex)
Whatdoesitmean?
Opcodes Operands
Letsseehowitallfitstogether
Systemorganization
CPU
Memory
IO
Bus
CPUcontainsregisters,flagsand
ALUtodomathoperations.
TypicalCPUContents
Arithmetic and
Logical Unit
Registers
flags
Segment registers
CPU
RegistersarelikevariablesinC,used
tostoreandcomputedata
temporarily.
Registers
SP
SI DI
AX, BX, CX, DX
IP
General-purpose registers for storing numbers.
Registers
SP
SI DI
AX, BX, CX, DX
IP
General-purpose registers for storing numbers.
Source and destination data index registers.
memory pointers for retrieving and storing
data.
Registers
SP
SI DI
AX, BX, CX, DX
IP
General-purpose registers for storing numbers.
Source and destination data index registers.
memory pointers for retrieving and storing
data.
Stack pointer, used to store parameters and
variables on the stack.
Registers
SP
SI DI
AX, BX, CX, DX
IP
General-purpose registers for storing numbers.
Source and destination data index registers.
memory pointers for retrieving and storing
data.
Stack pointer, used to store parameters and
variables on the stack.
Instruction pointer, points to next instruction
to execute.
Alsodependingonthecpuarch,the
registernameandsizevaries.
Registerssizes
AX, BX, CX, DX 16 bit CPU Architecture
32 bit CPU ArchitectureEAX, EBX, ECX, EDX
RAX, RBX, RCX, RDX 64 bit CPU Architecture
ForHandlingspecialsectionswe
havesegmentregisters
Segments
Code Segment
(CS)
Place where assembly code is stored
Data Segment
(DS)
Stack Segment
(SS)
Extra Segment
(ES)
Place where initialized data is stored
Place where stack data is stored
Place kept for extra data handling
Memorymanagement
Everyprocessinmemorythinks
itstheonlyprocessinthesystem
Memoryislaidoutinphysicalram
accordingtovirtualmemory.
Virtualmemorymodel
1234h
4567h
1234h
4567h
Process
1
Process
2
Process
3
Processinmemory
unused
heap
.bss
.data
.text
Stack
Place to store code
Place to store initialized data
Place to store un initialized data
Place to store dynamic data
Place to store func variables & params
Alsoweneedtounderstandhow
stackworks
Alsoweneedtounderstandhow
stackworks
Stack(LIFO)
Lower Address
Higher Address
Grows DownwardsESP
0x12345678
0x12345690
Stack(LIFO)-Push
Lower Address
Higher Address
Push ABCDEF00ESP
0x12345678
0x12345690
0xABCDEF00
Stack(LIFO)-Push
Lower Address
Higher Address
Push ABCDEF00
ESP
0x12345678
0x12345690
0xABCDEF00
ESP = ESP-1
Stack(LIFO)-POP
Lower Address
Higher Address
POP
ESP
0x12345678
0x12345690
0xABCDEF00
Stack(LIFO)-POP
Lower Address
Higher Address
POPESP
0x12345678
0x12345690
ESP = ESP+1
Instructionset
● Mov
● Add/sub/multiply/divide
● cmp
● Jmp/jne/jz/je/jnz/jg/jl
● int
Movestatement
● Move statement moves data from one place
to another
BeforeandAfterMove
Before Move
After Move
AX=30h BX=10h
AX=10h BX=10hAX=10h BX=10h
INSTRUCTION: MOV AX,BX
Examples
Move AX, BX ; move bx content to AX
Move AL, 06h ; move 06h into AL
Move AX, [BX]; If BX=90, move content
present in memory 90 to AX.
Add/Sub/Multiply/Divide
● Adds, subtracts, multiplies and divides the
numbers and stores it in the AX registers and
these instructions can affects flags.
Examples
● Add AX,05h – Add 05h to AX and stores result
back in AX
● Add AX,BX - Add contents of BX and AX, store
result in AX. Affects flags
● Sub AX,05h – Subtract 05h from AX, store
result in AX. Affects flags
Comparestatement
● Cmp CX,05h – Compare CX with 05h, results
will be reflected in special registers called
flags.
Examples
● Cmp CX, 05h; if cx=2, then Negative flag is
set.
Jumpinstructions
● Jump to a different part of the code.
● If label is given then jumps to label section
● Conditional jump happens based on flags.
Examples
● Jnz loop; jump to label loop if zero flag is not
zero
● Jmp loop; jump to label loop part of the code
● Jz loop ; jump to label loop if zero flag is set
Interruptinstruction
● Interrupts the CPU and jumps to the location
given.
Examples
Int 21h; calls the 21h OS routine
TobecontinuedinpartII...
Part- II will cover Instruction set and other concepts in depth.
This presentation is/was a teaser for the part II
Credits
● http://www.flickr.com/photos/yacknonchalant/541
● http://www.flickr.com/photos/15923063@N00/49
● All the icons are from The noun project
● Assembly language primer for hackers
securitytube.net

Assembly language part I