assembly language part i
TRANSCRIPT
Hello.c#include <stdio.h>#define STRING "Hello World"int main(void){/* Using a macro to print 'Hello World'*/printf(STRING);return 0;}
gcc commands 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 ; Preprocessed file
hello.s ; assembly file
hello.o ; object file
hello ; binary file
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.
“
”
What is Assembly language ?
Machine code in hex
10110000 01100001
B0 61 (in hex)
The above machine code representedIn hexadecimal format for ease.
Assembly representation
10110000 01100001
B0 61 (in hex)
MOV AL, 61h
The above machine code representedIn assembly language code
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.
Registers sizesAX, BX, CX, DX 16 bit CPU Architecture
32 bit CPU ArchitectureEAX, EBX, ECX, EDX
RAX, RBX, RCX, RDX 64 bit CPU Architecture
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
Process in memory
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
Stack (LIFO)- Push
Lower Address
Higher Address
Push ABCDEF00
ESP
0x12345678
0x12345690
0xABCDEF00ESP = ESP-1
Before and After MoveBefore Move
After Move
AX=30h BX=10h
AX=10h BX=10hAX=10h BX=10h
INSTRUCTION: MOV AX,BX
ExamplesMove 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
Compare statement● Cmp CX,05h – Compare CX with 05h, results
will be reflected in special registers called flags.
Jump instructions● 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
To be continued in part II ...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/5411017937/sizes/o/in/photostream/● http://www.flickr.com/photos/15923063@N00/496721450● All the icons are from The noun project● Assembly language primer for hackers
securitytube.net