# assembly broken down session 2

0101010101010101010101010101010101010101010
1 Assembly broken down 1
0 session 2 instruction sets 0
1 by rustymemory of uk2duohet 1
0 .we share thus we are one. 0
1 www.uk2duohet.co.uk 1
0101010101010101010101010101010101010101010
Session 2 - instruction sets

This session i will provide most commonly used instruction sets and in later sessions i will try to work on
an updated instruction set, i will not just bash in instructions i will state the name of the instruction
and its function and examples on how to use it.

before we start.......

Read session 1 before you start here.......

We start.....

[1] MOV:

This instruction is used when you need to move a value from a register to a register or move a value to a register,
it also can help in setting the segment number and offset... and and and i can go on for ever. lets see how..

example:
mov ax,5 ;put value 5 in AX
mov bx,1000 ;set the offset to 1000
mov ds,ax ; set the data segment number to 5

[2] CMP:

This instruction is used to compare between values and the result isnt stored anywhere it just affects the flags
in other words Subtracts source from destination and updates the flags but does not save a result. it will be abit clear
when you get to the jump instruction ill put it in the last peice in this paper because they are quite alot ..

cmp ax,bx ; compare bx with ax not ax with bx so its "dest minus src"

[3] INC / DEC

Increment / Decrement by 1

lets see,

mov ax,5
mov dx,5
inc ax ; AX now = 6
dec dx ; DX now = 4

[4] ADD / ADC / SUB / SBB / MUL / IMUL / DIV / IDIV

Before we start on this section i wanted to make you put some points into consideration..
lets see how to define if the number is "+ve or -ve" number "unsigned or signed"

DECIMAL BINARY HEX
0 0000 0
+1 0001 1
+2 0010 2
+3 0011 3
+4 0100 4
+5 0101 5
+6 0110 6
+7 0111 7
------------------------------ i hope you see this but its a 1 or a 0 signed or unsigned have a look at the binary
-8 1000 8
-7 1001 9
-6 1010 A
-5 1011 B
-4 1100 C
-3 1101 D
-2 1110 E
-1 1111 F
example:
mov ax,1
add ax,5 ; ax = 6
add ax,bx ; ax + bx then put the addition value in AX lets assume ax=5 bx=1 ax = 6

some wrong examples:

add DS,BX Segment registers are not able to preform arithmetic operations

add ax,cl ; wrong wrong 16 bit and 8 bits dont mix

this operation adds the two registers plus the content of the carry flag

examples:
lets assume CF = 1 carry flag set "1"
mov ax,5
mov si,10
adc ax,si ;AX[00000000-00000101]+si[00000000-00001010]+CF[1] the sum will be put in AX

[iii]SUB = subtraction

mov ax,4
mov dx,12
sub ax,dx ; 12 - 4

[iv]SBB = subtract with borrow

this operation subtracts the registers from eachother and if the CF"carry flag" is set "1" its subtracts

examples:
lets assume CF = 1 carry flag set "1"
mov ax,10
mov si,5
sbb ax,si ;(AX[00000000-00010010]-si[00000000-00000101])-CF[1] the sum will be put in AX

[v] MUL = multiply / IMUL = whole numbers multipiled and thier sings are taken into consideration
points to put into consideration
*any BYTE multiplication result is stored in AX
*any WORD multiplication result is stored in DX:AX
in other words :"When a multiplication is done with 8 bit values, the result is stored on the AX
register and when the multiplication is with 16 bit values the result is stored on the even DX:AX register"-laynetworks

example:
mul bl ; what is does here is BL * AL and the result is stored in ax

mul cx ; CX * AX and result stored in AX

[vi] DIV = divide / IDIV = divide and thier sings are taken into consideration
put into consideration, we may have overflows when the result is unable to be stored in AL|AX ,or division by zero

example:

mov ax,10
mov bx,2 ; no comment i assume 5 5 5 5 5 5 5 5 5 5 5 5 5 5 5 5 5 5 5 5 5 5 heh
div bx

an overflow could be like:
mov ax,1234
mov bx,1
div bl

[5] XCHG = exchange

heh.. no comment stright forward to code

mov ax,5
mov bx,2
xchg ax,bx ; ax =2 & bx =5
how is this step done manually without calling xchg
mov ax,5
mov bx,2
push dx ; well i pushed the value of dx into the stack"well cover that in another session" so if i wanted to use dx later
; with the original value it used to hold
mov dx,ax
mov ax,bx
mov bx,dx
pop dx ; get the value i pushed into the stack by poping it back into the original place "back to normal"
;for this push/pop instruction i didnt mess with my "DX" if i used to have an important content in
;it, which i might use later.

wrong examples:
xchg [200],[300] ; you cannot exchange a vule in a memory directly pass it to a register

this saves an addess offset into a register
lets assume that in the location [100] "12ab" is its content
mov bx,100
lea ax,[bx+10] ; ax will have the value of 100, run your eyes across CX register ull see "12ab"

[7] LES = Load extra segment / LDS = load data segment

erm....
8 bits = 1 byte [0 1 2 3 4 5 6 7]
16 bits = word [0 1 2 3 4 5 6 7] [9 8 10 11 12 13 14 15]
32 bits = double word [0 1 2 3 4 5 6 7][8 9 10 11 12 13 14 15][16 17 18 19 20 21 22 23][24 25 26 27 28 29 30 31]

The source must be a double word in memory. The word associated
with the largest address is transferred to DS in other words it is taken as

lets say we wanna load a large content in a register, which possibily cant happen. what it does is load a 32-bit

lets take an example:

LDS si,[BX]

LES DI,[BX+si+10]

[8] the fellow ship of the JUMP: "damn they are alot"

ways to use jumps first is after a add/sub/cmp which has a result that might affect our beloved flags
or the second is jump that would take you to another address or etc........you will see..............

ok ill slap in an exampe for each type

********************unconditional***************
jmp ; unconditional jump

lable1:
.
.
lable2:
jmp my_lable1:
*****************look at the flag and jump*******
jz ; jump if zero flag is set"1" ummm last opertation was a zero
lable1:
.
.
lable2:
cmp al,0
jz lable1

js ; jump if sign flag is set"1" which means its a negative result
lable1:
.
.
lable2:
sub ax,4
js lable1

jc ; jump if carry flag is set"1" a operation that produced a carry
jnz ; jump if not zero
jns ; jump if no sign
jnc ; jump if carry
jo ; jump if the overflow flag is set"1"
************odd or even sir**********************
JP ; jump if parity even "as a result of an operation check if the 1's are even [11000011]
jnp ; jump if parity odd "as a result of an operation check if the 1's are odd [11000010]
*************the counter jump********************
jcxz ; jump if cx = 0
label1:
.
.
lable2:
.
.
mov cx,10
jcxz lable1
rep
*******Comparisons of unsigned/signed*************
UNSIGNED "all positive numbers"
ja ; jump if above destination > source
jnbe ; jump if not below or equal
jae ; jump if above or equal destination >= source
jnb ; jump if not below
jb ; jump if below destination < source
jnae ; jump if not above or equal
jbe ; jump if below or equal destination <= source
jna ; jump if not above
SIGNED "negative included "
jg ; jump if greater destination > source
jnle ; jump is not less than or equal
jge ; jump if greater than or equal destination >= source
jnl ; jump if not less
jl ; jump if less destination < source
jnge ; jump if not greater than or equal
jle ; jump is less than or equal destination <= source
jng ;Jump if not greater

gr:
.
.
ab: ; it wont jump here
.
.
wicked:
mov al, 41h ; 41 hex = +65 decimal
cmp al, 80h ; 80 hex or -128 decimal
ja ab ; no jump since 41h is not > 80h
jg gr ; jump since +65 > -128

ENOUGH****************************************JUMPS

[9] push/pop to/from stack

THE STACK:
lets make it sound not hard, imagine the stack as the following:
day1:
i go buy a book , i finish it and i put it on my desk
day2:
i got so bored i bought another book , finished it and put it on top of the previous one
day3: i got so bored and went to buy 2 comic books and finish them and put them onto of the previous ones
ok what do we have now:

[the book from the day before]
[the first book]
ok dont be dumb here heh, like if i wanted to open the first book as a reference i have to remove 3 books to reach to
it so id remove firstly :[latest read comic book] then [before latest comic book read] then [the book from the day before]
then id have the choosen book. heh dont be a smart ass and say "ill snap it from below"

SO thats definatly a LIFO, so last in first out

example:

mov ax,5
mov bx,2
mov cx,1
push ax
push bx
push cx
pushf ;push all the flags
; ok what have we here push 5 push 2 push 1
;[ stack ]
;[ 1 ]
;[ 2 ]
;[ 5 ]
;whats next poping them, note: you can pop anywhere dont have to pop to the same register
pop ax ; 1
pop bx ; 2
pop cx ; 5
popf ; pop all the flags

example:
push [200] ; you can yes yes yes
wrong example:
push 300 ; you cant do an immidiate push :P
solution:
mov ax,300
push ax

[10]BITWISE "Discovery by trial and error is a great feeling": and/or/not/xor/neg

[*]Truth tables:
----------------------------------------------------------
-AND-|0 1 | -OR-|0 1 | -NOT-|0 1 | -XOR-|0 1|
------------|----------------|---------------|-----------|
0 |0 0 | 0 |0 1 | |1 0 | 0 |0 1|
1 |0 1 | 1 |1 1 | | 1 |1 0|
---------you will use these in bitwise operations---------

you use AND , OR , NOT , XOR and NEG like anyother instruction "MOV , CMP , XCHG, ...etc"

YOU WILL LEARN TO TOUCH THE FIRE.....
lets assume:
XOR:
----> binary : 01100110 , decimal: signed= 102 |unsigned=also 102 "refere to conversion table in the beginning of the
tutorial to know why 0 or 1, + or - ", hex: 66 , ascii char: f , oct: 146.

example :

mov dx,66h ; h means hex :P
xor dx,F5h ; F5 = decimal: signed = -11 | unsigned = 245, ascii char: w, oct: 365, binary: 11110101

the output:

when we come to watch/trace the register ull see:
DX= DH DL
hex 00 93
bin 00000000 10010011
oct 000 223
signed 0 -109
unsigned 0 147
ascii it wont be seen here so i cant write just anything heh

...................................................
AND: or TEST
difference between and/test:
"and" changes the value but "test" just tests:D heh
example:
mov al,53
and al,4f

wrong example:
and al,bx ; size

and [200],[300] ; too direct man

.........................................................
NOT 1 = 0 , not 0 = 1 "First complement"
erm....example:

mov cl,93h ; look up in the previous example "DX= DH DL" look at the table
mov ch,0
not cx

*CH WAS empty "0's everywhere"

nice output this time :
CX= CH CL
hex FF 6C
bin 11111111 01101100
oct 377 154
signed -1 108 ; check the binary
unsigned 255 108
ascii l

.........................................................

NEG "2's complement"

example:
;assuming cx holds a binary of 01101101 = ascii :m , hex: 6D.
Neg cx
what happen is the following:
cx = 01101101
then
first complement = 10010010
then
+ 1
--------------------------------------------------------
gives cx = 10010011 = hex: 93 , ascii = cant show here
--------------------------------------------------------
...........................................................

OR used for BIT set operations

mov ax,22h ; 00100010
or ax,1h ; 00000001
; gives
; 0010001[1]

.............................................................................
[11] Shift left|right

lets see:

00001000 | 8
00000100 | 4
00000010 | 2
00000001 | 1

SHR = shift RIGHT means that your dividing by 2
SHL = shift LEFT means you are multiplying by 2

example:

mov dx,10
shr dx,1 ; shr/shl usage : shr|shl op,number of multiplications|divisions
mov ax,4c00h
int 21h

note: shifts arent used in encryption since when u shift it adds a zero at the end so when u reshift back you dont
get the same value XOR or ROR
..............................................................................

[12] Rotate left|right:

ok this is quite easy lets see an example and we will understand it :

|0|1|0|0|1|1|0|1| = ascii M , HEX = 4D
ok lets ror it
what happens is the followig

CF
|0|1|0|0|1|1|0|1| --->[1] <---this is the carry flag , when ror it creates a copy of the last bit and puts it in the CF
and then rotates
|1|0|1|0|0|1|1|0| [1]

rol is the exact of ror but CF is on the left and the motion is clockwise ror is anti-clockwise

cutting down cuz im getting tierd here

-----------------------from wwc.edu-----------------------|
Logical Shift |
shr count, dest - shift dest count bits to the right |
shl count, dest - shift dest count bits to the left |
Arithmetic Shift(preserves sign) |
sar count, dest - shift dest count bits to the right |
sal count, dest - shift dest count bits to the left |
Rotate without carry flag |
ror count, dest - rotate dest count bits to the right |
rol count, dest - rotate dest count bits to the left |
Rotate With carry flag |
rcr count, dest - rotate dest count bits to the right |
rcl count, dest - rotate dest count bits to the left |
-----------------------------------------------------------

[13] LOOP:
loop depends on the value in the CX to know how many times it will loop

mov dx,1
mov cx,5
start:
inc dx
loop start
mov ax,4c00h

i think i covered most of the instructions you'll need until you GO FORTH to the right path and read more deeper

Next session will be on interrupts and some applications

"we share thus we are one"-uk2duohet