Top Banner
Assembly Language By Mohammed Imran Get your hands dirty with PART- I @imran_naseem
70

Assembly language part I

Nov 01, 2014

Download

Technology

This presentation covers very basics of assembly language with some computer organization concept. I took this session as part of on going series on assembly at NULL Hyderabad meets. PART II will cover instruction sets and more in detail.
Welcome message from author
This document is posted to help you gain knowledge. Please leave a comment to let me know what you think about it! Share it to your friends and learn new things together.
Transcript
Page 1: Assembly language part I

Assembly Language

By Mohammed Imran

Get your hands dirty with

PART- I

@imran_naseem

Page 2: Assembly language part I

If I say Assembly is cool !

Page 3: Assembly language part I

Seriously ?

People say

Page 4: Assembly language part I

And some go even further..

Page 5: Assembly language part I
Page 6: Assembly language part I

But you could do some Amazing things

Page 7: Assembly language part I
Page 8: Assembly language part I
Page 9: Assembly language part I
Page 10: Assembly language part I

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

Page 11: Assembly language part I

Fly these

Page 12: Assembly language part I
Page 13: Assembly language part I

Lets start

Page 14: Assembly language part I
Page 15: Assembly language part I

CPU can't understand c, java or assembly.

Page 16: Assembly language part I

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

Page 17: Assembly language part I
Page 18: Assembly language part I
Page 19: Assembly language part I

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

Page 20: Assembly language part I

You can see these intermediate stages using gcc command

Page 21: Assembly language part I

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

Page 22: Assembly language part I

ls hello.* hello.i ; Preprocessed file

hello.s ; assembly file

hello.o ; object file

hello ; binary file

Page 23: Assembly language part I

Demo

Page 24: Assembly language part I

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 ?

Page 25: Assembly language part I

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

Page 26: Assembly language part I

Assembly acts as bridge

Machine Language High level language

Assembly Language

Page 27: Assembly language part I

Machine code

10110000 01100001

This is how, an instruction in machine language looks like

Page 28: Assembly language part I

And code is parsed like.

10110000 01100001

Instruction Register Register/Operand

Page 29: Assembly language part I

Machine code in hex

10110000 01100001

B0 61 (in hex)

The above machine code representedIn hexadecimal format for ease.

Page 30: Assembly language part I

Assembly representation

10110000 01100001

B0 61 (in hex)

MOV AL, 61h

The above machine code representedIn assembly language code

Page 31: Assembly language part I

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

What does it mean ?

Opcodes Operands

Page 32: Assembly language part I

Lets see how it all fits together

Page 33: Assembly language part I

System organization

CPU

Memory

IO

Bus

Page 34: Assembly language part I

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

Page 35: Assembly language part I

Typical CPU Contents

Arithmetic and Logical Unit

Registers

flags

Segment registers

CPU

Page 36: Assembly language part I

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

temporarily.

Page 37: Assembly language part I

Registers

SP

SI DI

AX, BX, CX, DX

IP

General-purpose registers for storing numbers.

Page 38: Assembly language part I

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.

Page 39: Assembly language part I

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.

Page 40: Assembly language part I

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.

Page 41: Assembly language part I

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

Page 42: Assembly language part I

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

32 bit CPU ArchitectureEAX, EBX, ECX, EDX

RAX, RBX, RCX, RDX 64 bit CPU Architecture

Page 43: Assembly language part I

For Handling special sections we have segment registers

Page 44: Assembly language part I

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

Page 45: Assembly language part I

Memory management

Page 46: Assembly language part I

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

Page 47: Assembly language part I

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

Page 48: Assembly language part I

Virtual memory model

1234h

4567h

1234h

4567h

Process1

Process2

Process3

Page 49: Assembly language part I

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

Page 50: Assembly language part I

Also we need to understand how stack works

Page 51: Assembly language part I

Also we need to understand how stack works

Page 52: Assembly language part I

Stack (LIFO)

Lower Address

Higher Address

Grows DownwardsESP

0x12345678

0x12345690

Page 53: Assembly language part I

Stack (LIFO)- Push

Lower Address

Higher Address

Push ABCDEF00ESP

0x12345678

0x12345690

0xABCDEF00

Page 54: Assembly language part I

Stack (LIFO)- Push

Lower Address

Higher Address

Push ABCDEF00

ESP

0x12345678

0x12345690

0xABCDEF00ESP = ESP-1

Page 55: Assembly language part I

Stack (LIFO)- POP

Lower Address

Higher Address

POP

ESP

0x12345678

0x12345690

0xABCDEF00

Page 56: Assembly language part I

Stack (LIFO)- POP

Lower Address

Higher Address

POPESP

0x12345678

0x12345690

ESP = ESP+1

Page 57: Assembly language part I

Instruction set● Mov

● Add/sub/multiply/divide

● cmp

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

● int

Page 58: Assembly language part I

Move statement● Move statement moves data from one place

to another

Page 59: Assembly language part I

Before and After MoveBefore Move

After Move

AX=30h BX=10h

AX=10h BX=10hAX=10h BX=10h

INSTRUCTION: MOV AX,BX

Page 60: Assembly language part I

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.

Page 61: Assembly language part I

Add/Sub/Multiply/Divide● Adds, subtracts, multiplies and divides the

numbers and stores it in the AX registers and these instructions can affects flags.

Page 62: Assembly language part I

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

Page 63: Assembly language part I

Compare statement● Cmp CX,05h – Compare CX with 05h, results

will be reflected in special registers called flags.

Page 64: Assembly language part I

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

set.

Page 65: Assembly language part I

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.

Page 66: Assembly language part I

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

Page 67: Assembly language part I

Interrupt instruction● Interrupts the CPU and jumps to the location

given.

Page 68: Assembly language part I

ExamplesInt 21h; calls the 21h OS routine

Page 69: Assembly language part I

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

Page 70: Assembly language part I

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