assembly language part i

Post on 28-May-2015

1.182 Views

Category:

Education

2 Downloads

Preview:

Click to see full reader

DESCRIPTION

null May 2013 - Hyderabad Chapter Meet

TRANSCRIPT

Assembly Language

By Mohammed Imran

Get your hands dirty with

PART- I

@imran_naseem

If I say Assembly is cool !

Seriously ?

People say

And some go even further..

But you could do some Amazing things

Create faster programsNo, you cant fly cars with assembly :)

Fly these

Lets start

CPU can't understand c, java or assembly.

hence we have compilers, assembers to convert high level code to machine code.

Hello.c#include <stdio.h>#define STRING "Hello World"int main(void){/* Using a macro to print 'Hello World'*/printf(STRING);return 0;}

You can see these intermediate stages using gcc command

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

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.

What is Assembly language ?

Assembly is easy to learn, but hard to master!“ ”

Assembly acts as bridge

Machine Language High level language

Assembly Language

Machine code

10110000 01100001

This is how, an instruction in machine language looks like

And code is parsed like.

10110000 01100001

Instruction Register Register/Operand

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

MOV AL, 61h ; Load AL with 97 decimal (61 hex)

What does it mean ?

Opcodes Operands

Lets see how it all fits together

System organization

CPU

Memory

IO

Bus

CPU contains registers, flags and ALU to do math operations.

Typical CPU Contents

Arithmetic and Logical Unit

Registers

flags

Segment registers

CPU

Registers are like variables in C, used to store and compute data

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.

Also depending on the cpu arch, the register name and size varies.

Registers sizesAX, BX, CX, DX 16 bit CPU Architecture

32 bit CPU ArchitectureEAX, EBX, ECX, EDX

RAX, RBX, RCX, RDX 64 bit CPU Architecture

For Handling special sections we have segment registers

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

Memory management

Every process in memory thinks its the only process in the system

Memory is laid out in physical ram according to virtual memory.

Virtual memory model

1234h

4567h

1234h

4567h

Process1

Process2

Process3

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

Also we need to understand how stack works

Also we need to understand how stack works

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

0xABCDEF00ESP = 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

Instruction set● Mov

● Add/sub/multiply/divide

● cmp

● Jmp/jne/jz/je/jnz/jg/jl

● int

Move statement● Move statement moves data from one place

to another

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.

Examples● Cmp CX, 05h; if cx=2, then Negative flag is

set.

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

Interrupt instruction● Interrupts the CPU and jumps to the location

given.

ExamplesInt 21h; calls the 21h OS routine

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

top related