Programming is an art.

Assembly Language.

So why assembly language????

·  No programmer is complete without mastery over assembly language..

·  Your program ruins slow or there is some deep glitch.

·  You work on it day and night and still the glitch remains a glitch.

·  Here comes the assembly language.

·  You analyse the processes and come at a diagnosis…

·  Assembly language makes you powerful.

·  It is the base on which everything is built.

·  Without assembly language you will always remain a novice.

·  Whatever you build or achieve.

You must have heard that the:

·  Assembly language is hard to learn and understand

·  It’s difficult to debug

·  It’s a messy outfit

·  Why do you want to save a little space using assembly language when you have so much space?

Assembly language has several benefits:

·  Speed. Assembly language programs are generally the fastest programs around.

·  Space. Assembly language programs are often the smallest.

·  Capability. You can do things in assembly which are difficult or impossible in HLLs.

·  Knowledge. Your knowledge of assembly language will help you write better programs,even when using HLLs.

LESSON1 - THE REGISTERS AND SEGMENTS

Unlike other languagesthere is no predefined commands like "writeln", "printf",…

Assembly language does not provide those tools for you

So how does it work>?

Assembly Language has predefined registers:

All of these are the data holders

AX - accumulator index

BX - Base index

CX - Count index

DX - Data index

All of these are the pointing and index registers

SP - Stack pointer

BP - Base pointer

SI - Source index storage

DI - Destination indec

IP - Instruction pointer

All of these are segments holder

CS - Code segment

DS - Data segment

SS - Stack segment

ES - Extra segment

FLAGS - Holds some of the function conditions

Now to be more specific :

Data registers:

They are the basic registers for all the computer calcs, and position

Each of the registers is 16bit and they are divided into two registers

high and low which are 8 bit :

AX - ah (high), al (lo)

BX - bh (high), bl (lo)

CX - ch (high), cl (lo)

DX - dh (high), dl (lo)

High is MSB - most significant byte

Low is LSB - least significant byte

Pointing registers:

Each of these registers has a unique job:

SP - is the offset of the stack (-n-)

BP - a pointer for the stack (-n-)

SI - is the source index, uses as an offset in memory transfers

DI - is the destination index, uses as an offset in memory transfers

IP - is the offset of the current instruction (-n-)

(-n-) means don't change unless you know what your'e doing

Segment registers:

CS - is the segment of the code (-n-)

DS - is the segment (usually) of the data

SS - is the segment for the stack (-n-)

ES - is an extra segment, uses for memory transfers

Flags, will be discussed later

Assembly language works with segments .each segment has a maximum limit which is 64K,

Now we create a segment.

when we have a segment we have to give it a definition,

For this we need the command "Assume" which gives each one of the segments

registers it's default segment,

Here is a typical segment—-

Sseg segment ; a semicolon (;) is a remark and will not be compiled

db 10 dup (?)

ends ; each segment has a name and the "segment" after it

; when we finished to define stuff in the segment

; we close it with ends (end segment)

Dseg segment

ends

Cseg segment

assume cs:cseg,ds:dseg,ss:sseg

ends

end

LESSON 2 Basic operations!!!!!

In the last chapter we learned how an assembly language program is built.

Now we move on to a higher level.

The operators.

MOV destination,source : lets say "mov ax,bx" ax will become the value

which bx stores.

ADD destination,count : lets say "add ax,bx" ax will be increased

according to the value in bx - lets say

ax = 10, bx = 2 then ax will become 12 (10+2)

SUB destination,count : lets say "add ax,bx" ax will be decreased

according to the value in bx - lets say

ax = 10, bx = 2 then ax will become 8 (10-2)

INC destination : lets say "inc ax" then ax will be ax+1

DEC destination : lets say "dec ax" then ax will be ax-1

AND destination,count : lets say "and ax,bx" then ax will be anded

with bx, lets say ax = 1, bx = 0 then ax will

be 0.

OR destination,count : lets say "or ax,bx" then ax will be ored

with bx, lets say ax = 1, bx = 0 then ax will

be 1.

know after we learnt the basic command, what happened about the size of

those registers well, you can't do one of those between registers

which aren't not the same size : "mov ax,bl", "mov al,bx", "add cx,cl"

and so on.

simple example, if we want to preform subtraction, what will do,

(before each example check if you can do it your self) :

mov ax,20 ; ax will be 20

mov cx,10 ; cx will be 10

sub ax,cx ; ax will be 10 (ax-cx,20-10)

know if we have a number in hex or bin and we want to put it as it is so

we will have to put "h" after the number for hex and "b" for bin :

mov ax,0a000h ; you must have a zero before a letter in hex

mov ax,010010b ; a simple number in binary

LESSON3 - INTERRUPTS LABELS AND DB

The example I showed above will run,but it will not exit.

For a program to be a program,it must be complete.ie it must exit.

we must tell dos we want to exit :It is done simply by using the operator "int" which calls to interrupt.

(0..0FFH) and instruct him what to do, so lets discuss some int's :

009h - the keyboard interrupt

010h - the screen interrupt

013h - the sectors, (format, read, write) interrupt

016h - the keyboard interrupt

017h - the printer interrupt

021h - the main dos command

each interrupt got many command and each one needs difrrent parameters

(if you want to know get the complete int list from your local BBS)

INT 21 - TERMINATE

ah - 4ch

al - errorlevel

so in order to initiate this interrupt we will do :

mov ah,4ch

mov al,0 ; if you want it can every value

int 21h ; and then the program will exit to dos

but how will the compiler know where to start, well we have to tell

him, the compiler will know to start excatly in the first, label -

label is a name which you can jump to him, the label will be like :

Name : - "start :", "now :", "rt :" all of those are labels, so this is

a basic (working) assembly program :

Sseg segment

db 10 dup (?) ; will be disscused late

ends

Dseg segment

ends

Cseg segment

assume cs:cseg,ds:dseg,ss:sseg

start : ; the compiler will start here

mov ah,4ch

mov al,0

int 21h ; terminate

ends ; the segment must finish before the label

end start ; close the label

end ; tell compiler script ends here

That was easy.I hope.If you could not get it fair and square, I have some eBooks as links in the home page. Just check out them and come back.

It is the main assembly struc, but it can be much more complex.

now in the sseg (stack) there was "db". what is that?

well if (most of the times) assembly registers are not enough so you (like pascal,c) can define variables, how ?

well you have the operators - db,dw,dd,dt :

DB - define byte, the var will be 1 byte (8 bit)

DW - define word, the var will be 2 bytes (word, 16 bit)

DD - define double, the var will be 4 bytes (double word,32 bit)

DT - define ten, then var will be 10 bytes (not used)

the decleration goes like this :

name type [number] value.

examples : try db 1 - it will define "try" as 1 byte with starting value 1

tru dw 4 - it will define "tru" as 2 byte with starting value 4

ytu dw ? - it will define "ytu" as 2 byte with unknown starting

value

now lets say that you want to define an array (like pascal : a:array[1..10]of

like c : int c[100])

you do as follow : name type number dup(value).

lets say : gvr db 10 dup(2) - it will define "gvr" as 10 byte which each one

has value of 2.

but what if I want to define a messege or multiple values then what ??

then it should be like :

messege - mes db "this is a messege",10,13,"$"

now the numbers after the second """ are optional, it's needed only

for some interrupt.

multiple values (vectors, etc.) then :

vec db 23,54,76,34,234,14,23,123,245

db 124,34,245,23,52,34,52,43,13

db 213,213,123,123

the db can be dw,dd or dt, and you can do it for how much you want

but you must not pass the 64k limit.

but what if you want to write a messege then ???

you will search the int which do that :

int 21h - write string

ah - 9

ds:dx points to string (in the end "$" for termination)

so how will we do it ???

Sseg segment

db 10 dup (?)

ends

Dseg segment

msg db "dr. encryption",10,13,"$" ; for termination

ends

Cseg segment

assume cs:cseg,ds:dseg,ss:sseg

start : ; the compiler will start here

mov dx,offset msg ; ds is already pointing to dseg

mov ah,9

int 21h ; whop there he is

mov ah,4ch

mov al,0

int 21h ; terminate

ends ; the segment must finish before the label

end start ; close the label

end ; tell compiler script ends here

now what offset is, well offset will return the offset of the argument

so if you write :

mov dx,offset msg

then dx will be the offset of the msg

(offset is 16bit and so is segment ,so "mov al,offset ..." will be wrong)

the operator seg will return the segment of the argument, but if you change

ds and not return his original value the system will hang.

so then the stack comes to buisness.

LESSON4 - STACK (PUSH,POP) AND MORE COMMANDS

stack, how does it work ?

the law is last in first out,

the principle is if I enter a series of numbers like : 1 34 54 54 65

and then I take them out I will get : 65 54 54 34 1.

in assembly the command is push (it can push only 16bit) and

the reverse is pop.

so lets say I want to save the ds and later get it back :

push ds

.

.

.

.

pop ds

mov ax,4c00h ; short cut for : mov ah,4ch mov al,0

int 21h

ok now you've got the basic of assembly (I hope).

in order to pass to the next level we have to explore the structure of the 8

bit register !!!

now bit is a condition that can be 0 or 1 so if we have 8 bit so the

number of combinations can be 2^8 which is 256 (0..255) the limit

of one byte (or word 2^16 65535)

so assembly gives us the tools to modify and alter those bits.

SHR destination,count : Shift arithmic right divide the number by

count^2 so 1 is 2 and 2 is 4 and so on

"shr ax,4" will divide ax by 16 (4^2)

SHL destination,count : Shift arithmic left multiply the number by

count^2 so 1 is 2 and 2 is 4 and so on

"shr ax,4" will multiply ax by 16 (4^2)

ROR destination,count : Rotate right rotates the bits right so

bit 0 is now bit 7 (16 bit, bit 0 is bit 15)

and does is by count.

"ror al,8" will not affect cause there are

only 8 bits so a whole round will be preformed.

ROL destination,count : Rotate left rotates the bits right so

bit 7 is now bit 0 (16 bit, bit 15 is bit 0)

and does is by count.

"ror al,8" will not affect cause there are

only 8 bits so a whole round will be preformed.

DIV factor : Divide will divide ax (or al) by the factor

("dx" must be set to 0), "div cl" will divide

al by cl, "div cx" will divide ax by cx.

MUL factor : Multiply will multiply ax (or al) by the factor

(dx will be altered), if the operation is 8 bit

"div cl" then the sum will be stored in ax

(if it passes 8 bit) "mul cl" will mul ax by cl,

"mul cx" will multiply ax by cx.

NEG destination : Negation what it does he reverses negativity

lets say we have 4 so it will become -4 and

vise verse (n*-1)

as you might have noticed no floating points nor integers has been discussed

well assembly has no floating points (don't be shocked) because you can

still use the precision well how ?there are fixed points.

fixed points are precision that was shifted before and after the calc

been shifted back. fixed points example :

mov ax,100 ; our circle radios

mov cx,181 ; 181/256 is 0.707 (sin(45))

mul cx ; multiply (result : 18,100)

shr ax,8 ; now the number is shifted back (n / 256 = shr n,8 cause

; 2^8 is 256) so the result is 70.7 (the point disapears)

; number is trunced to 70 (if you check 100*0.707 is 70.7)

the fixed points are much faster then real (double) and usually the