Professional Documents
Culture Documents
Module-4 1
Module-4 1
Different
-
MOV options
location R -
Register
E Swap the word at memory 24000N R ← M
with 25000 n
M -
Memory
M R
←
I -
Immediate data
24000h 2 Memory is
Ag
]
:
organised R ← R
byte
24001 n
but we need M ← I
to swap word
, , , , g, , any
µ ← ,
,
M←M①
25000h
so we swap
2500 In 2 bytes
M0VDsT,sRC_
① Initialize segment register
contents of source to destination
→ Copies tire
MOV AX , 2000h → No
flags affected
Size
→
MOV DS , AX Serc ⑧ dat m u s t be some
of
.
Source can be R M @ I
② Initialize offset resisters
→ ,
be
# Can't transfer → Destination can R
@ M
MOV SI 4000
directly form
n
,
Mov BX § I ] 24
diff ways )
,
MOV DX [ DI ]
, →
( AF ,
of ,PF ,
Sf ,zF affected ,
CF not affected )
INC BX
Mov [SI ] ,
DX
I]
MOV
④ ,
✗ →
Similarly DEC Destination
E¥ :
MOV BX ,
2000h E± Bx has 56 ,
after INC ,
it has 57 n ,
MOV DI ,
10h →
DEC decrements tire contents of destination by I.
B- ✗ + DI ]
MOV AL ,
[
/
Inc Dec
the contents of a memory location
MOV DI , Lon
ani→mtim%""t①*
data
→
specify the
size in
memory
MOV [ BX + DI ] AL
, the
→
use assembler directives →
assembler and n ot to Mp
fbit
Keeps
put
PTR PTR
AI Copied , data from DS : 2010 to DS : 2020 ☒ BYTE PTR * WORD * DWORD
d- d- T '
← 2010 @
DS : 2020 DS :
going to
assembler
towards a byte 18 bit ) knows tie size
But for M
, you
need
1000m DS -2000h
SI
]⑥
,
INC SI -
2001
INC DI DI -
4001
21000 FF
21001 00
'
]
JNZ ✗1
transferred
NXT :
" " ""
① " "" " ""
✗ 1 is the branch
Bit
210004
lover
00
Bit
21001 01 higher
Machine Code
For instruction ②
10111000 40 D8 40
(
00 00
②[
00 zo
21001 00
40
to 111111 ooo yo 8f 00
③( 10 111001 32 00
B. 9 32 00
Flags
]
④ ( 8A I C
100010110 00011 too
±" " ° " " ^ "" ±" B" " " " " "" °&
③[
" " "° " ° " " " " "
is also
Cf 46
Of → of 0 →
but 01000110
⑥[
-
→ 0
affected
-
47
S F
internally
O 0 1000111
SF →
-
→ -
O
→ zf -
0 → ZF -
I it is
not
( 01001001 49
→ Pf -
1 ( follows only low byte) → PF -
I reflected
⑧[ 01110101 75 displacement
→ Af -
I
→
Af -
I displacement
Branch Instructions ( conditional taraxch instruction ) * Mp will always point towards the next instruction that is
same to be executed
→ going
[ Jump
.
°°°÷
"
→ Address
current
branch instructions are
going
to ex a m i n e some flag -
→ which is -
g
E JZ examines ze ro flag
so
,
it branches to a particular address provided the zf is set
Destination Source
,
m ove forward .
→ Negative displacement
is
you more backward .
* ( source ) + ( Destination ) → ( Destination)
be I
/ RIM specified by of
8bitdisplaame@
p source may any one
of the
flag .
E± 12346 .
If JZ 6
- of the
123461-6 = 1234 Cn
24
addressing methods .
be memory locations
to another memory a re a -
bytes 32h
to 44000h * gonna ⑧ Destination must be of same
size .
-
resister
MOV DS , AX normally
(labels
Ex=ple
:
MOV 2000
can have
MOV CL 73h 0111 0011
SI
]
,
, ② value )
any
MOV DI 4000
, MOV BL
,
Yfn 0100 I 111
] ③ Store count so
CX 0032,1
Labels MOV
,
ADD CL BL
XT ⑦
,
MOV CDI ] ,
BL ]⑤ 4000 : 4000 = BL
?⃝
AI 0010g ✗1 [ SI ]
:
Result in CL = C2 1100 : MOV AL
n >
(
,
Plinth
( 19yd if unsigned -62A if signed )
^" ^ "
Increment
any
g)
the
In , g,
affect gag
]
won't
want to of DI
* If you
use
unsigned form , ignore
the
INC → They
cc
else DEC
for signed ,
consider it .
JNZ ✗1
CF = 0
,
Pf -0
-
,
AF = 1
,
zf = 0
,
SF = 1
,
of =L JNE
INC BL
✗ 2 →
[Jump if no
carry ]
✗ 2 : MOV ④ I ] ,
BL →
Carry is moved to next byte
( ie
7th byte after 6 byte
ADC Destination Source , ( Acted with Carny instruction calculation )
P ( Sowa) -1 ( Destination) +
( ( f) →
( Destination)
source →
98 34 AB Ef FA ①
AI 56
71 f- 3 67 90 10 21
① SBB Destination ,
Source (Subtract with borrow )
3C 1B ( Sorce ) ✗ f) ( Destination )
1 0 A 27 BE 00
(Destination) -
-
Source Destination
SUB BX , 8190h
/
40001 Ef 41001 10
40002 AB 41002 90
40004 34 41004 f- 3
7
higher byte
40005 98 41005 71
f
☒ to 12 I
⑨ 80 2 I
-
8 1 90 1- 7 E 7 0
zf -0 CF -1
zf 0 (f O AF -0
-0J
-
-
, ,
, , ,
Pf -
✗ SF -1
OF pf × sf -
1. of - 0
CLC flag )
-
( clear
, , ,
* carry -
I
ignore matters
bits
bye 16
with 8 bit
Add 2- 6 byte nos stored in location 20000h
nos .
location add of
21000h
Q write a
program
to an
array bytes stored
the result
Ans : MOV AX 2000N in memory starting at 40000h and store
,
-
in 41000h
MOV DS, AX
.
No of elements in the
.
array sod
MOV SI 0000N
,
AI MOV AX ,
4000 n D8
0040
5od= 32h
Mov CL ,
06 n - 6 byte data MOV SI
,
0000h BE 00 ① o
BF 00 lo
MOV DI 100011
MOV BL 00 → Clear BL to have
carry
,
, B. 932 00
CX
of 6M byte MOV , 0032m
( ( ( B] 00
Mov BL , ooh
→
BL -
O for adding
Mov Bn
,
BL →
Bn
-
o
for Larry 8A FB
✗1 : ADD BL
,
[ SI ] 02 Ice
yyiseve E ORG 0000h
¥?? ??
73 yy
✗✗ " " e
g)
DATAI 25
'
NC BH DB
✗ z :
fyrg DATAZ DB 1000100lb
DATA } DB 12h
DEC CX 49
ynz , , , , on , o , , ,
data
→
string of
89 ID DATAY DB czsq , ,
MOV [DID BX
ORG 0018h
is
location
that memory
41000 DATAS
DB
? → It
means
So a t the END ,
FD rolled DATAS
4100 1 03
This is how data is initialized in the data segment .
}
is
0001 0011 35m location but n a m e
89N * TAE
←
0012 39h '
18h 12h
000 ' '
ooo ascii equivalents of 2591
-
00016h 0013 31
No idea n
-02N
-
yY
faafdntnis as
data
←
000 12h
directed
an orbs
point per
xx
E ORG 0000h
'
MS 42 DB
'
123456 →
string of data
MSG } DW 6667N
data I db 12,3 - three numbers ( not string )
'
db
'
db 11110000 b
MASM Assembler Directives
data 2 dw 12,13
P Assembler directives a re n ot converted into machine dw ↳ usn
dd 300N
code but rather assembler makes
language use
Dup directive
is no of
bytes ffn
-
with
to machine language .
)
Assembler takes these
.
]
32 45
directives and stores the
33 23
Data Declaration ( Directive ) m oo data directly . It doesn't
)
35 it
03 write for
( Quad) program
a
.
* DB , DW DD DQ
, 36 oo
/
Label ol FF
E
9
03
] FF
ff
taken 61 ] ff
Data , Dw noon ,> n zzzzn as data
magnet
,
,
, , , , ,
DATA } DD 237567A Bn
decimal 0C Ff
|
is
00 Ff
b for FF
* Take
OD
*
Define byte is used for indicating binary
'
'
b "
data * Take
whether a
particular happens
alphabets
to be a byte / word @ double word
"
Adem [ BX ]
Equate directive equates a
symbolic name to a value MOV AL , →
AL=①
value
offset
Inari
AddM[
* Not stored in
memory .
It replaces the value wherever MOV BX , offset DATY →
It means
its name
appears in
program
.
MOV AL ,[ BX ]
of DATY
B✗=o@
COUNT EQU to →
A
CONST F- One 20h
Direct
An
-0A c- MOV Ah
,
COUNT AddM[M0V AL ,
DATY →
AL Direct Addressing
-10 ← MOV AL CONST
AL ,
ADD AH AL
2A c- ,
An
-
-
MASM
Program Models
phynaevs.logicalsegmentat.io#ÉÉ?oÉ]
easels
is labelling
E ORG 0010 n * Eau ,
data segments -
your y
count f- 00
32N is labelled
✓ At " f- QU * DB labels
0030h location
DAT 1 DB 45h ,
67m , 100,1A
'
you
memory
G segments
WRD DW 10h 0910h
,
3500h , "
DD P block called physical
DATI 0902N A of memory of discrete
size
-
a
DAT 3
DW 2 Duplo ) segment .
ORG ALL
physical
✓
DATY DB 56N
processors @
ORG 64K 16 4 bit
VALZ
to locations for bit gigabytes for 32
- -
CÉÉ¥ssors
Resents
res DB ioiupc ? ) →
DWRD DD 01020304N
a.
*
care .
# Generally 64K
¥hs lomax size)
fond
→
✗
what
"
T
✗
RES
#
64
×
data
41
logical
code segment
✗
WRD 10 logical
00
✗ segment
✗
✗
35
✗
10
✗
09
✗
Logical Segments
DATZ 02
① WRD ooze Oy
09
00 3D 03
00
DAT 3 00 → code
00 → data
→ stack
in
*
Mapping of logical segments to actual physical segments memory
sT
.
MOV SI DAT } →
,
(Needs to be done )
→AL=#
"
0010+1 = 0°
→BÉgg±gg
0010-14 0014
AddM
to the same
=
ADD BX , 20h →
B✗=00 ☒ Segments defined in
two
ways ⇐ ( MASM does it in two ways)
Mov AL
@× ] →
µL- simplifiedsegmentdirect.rs#
,
→ we'll use this tmoyhout course
Effective Address
LEA Load
→ full
segment definitions
[
Indirect LEA Bx ,
DAT y →
( It 'll not move data
it
resister Ratner
pddmis @✗ I into
.
Mov AL →
,
will move
displacement Models ( Program Models )
into register)
0030 ) models available MASM assembles ranging
( B- ✗
to
=
* there a re
many
from Tiny to Huge .
offset ± Displacement
Effective Address =
#
E . MODEL TINY
* TINY model requires that all program and data
the is
fit into one 64K segment .
CS
resist
( also
ensures
• CODE defines code segment is updated )
data segment ( and assembler automatically
•
DATA defines tuedata segment
•
STARTUP shows best unis of the updates
the
end of program
f- ✗ IT indicates the
•
Yimou "
Ent
Segnetlstacnsegnct
is fixed into I 64K
3Ffffn
space .
Ex 1 :
•
model tiny
•
data
DATA 1 23
]
DB
• Code
•
startup
MOV BX , DATA 2 B ✗ = 9999
MOV CX
,
DATA }
MOV DATAI , BL
MOV DI
,
0002h DI -
00002 n
Mov AX
,
ARRAY [ DI ] AX -
0002h
• exit
end → to end assembly
• DATA
DATA 1 DB 23h
• CODE
STARTUP
ARRAY DX 0001
•
MOV
-
DX
,
MOV CL DATA 1
cL -
23h
,
BX address of ARRAY
BX , OFFSET ARRAY
-
MOV
AL
CBX ]
MOV AL / -
01
• EXIT
END
From
find the greatest signed no a
write an ALP to .
TILE / JNG Sf ④ Of -
'
④ 2ft Lesser equal / not greater
JE / 52 zf -
l eared / zero
Zf -0
JNEIJNZ not equal / ze ro
compare Instructions
provided signed
is subtraction that changes
Compare Instructions a
*
an
The greatest no .
most
→
Cmp Destination Source location RES
, be stored at .
AI model tiny
CMP AX , 2000h location ⑧
•
→
✓ memory
data
[ DI ] ,Cn immediate data
•
→ CMP SI ,
DI RES DB ?
•
Code
•
startup
CMP CX ,AX LEA BX ARRAY ( BX is n ow
pointer )
,
SF -0 CL
,oAn towards fist data
,
(✗ < BX Cf -
I zf -0 Sf -
I MOV AL
,
[ BX ] → At -91 ie , 9in
DEC CL
(✗ >
BX CF -
o zf -
o
sp -
o
B- ✗
INC
for
unsigned)
PF , Alf data x2 : Cmp Ah [ Bx ] (
Of ,
-
depends on ,
,
,
.ae
THE ✗I ~
MOV AL ,[ BX ] →
Al -02/75
-
→
greatest
To check result of compare
-
Logical ✗ 1 : INC
BX
DEC CL
JA
/ JNBE Cf oadzifo Above / Not Below @ efhal ×,
g- Nz
-
exit
/ equal
•
JBIJCIJNAE CF -
l
Below Carry / Not Above
end
T.BE/JNACF-l@Zf-1 Below efnal / Not above
T.ME/JNZzf -0
Not Equal / Not Ze ro
provided unsigned
Program Models align 2
dat 2
'
db
'
M ov
,
the must
which means program
yal Es add dat
be originated a t location 100N
Keven
⑧ ) Ss
• exit
Contains two segments one DS of 64K
small end
-
bit
* flat Model Special type tiny Model for 32
-
of
MemayM①
-
6,135,55 ,ES→
OBYFN
Psp Mode Tiny
Model Type Description 0100h
Ip →
Code
Contains one DS of 64 kbyte ad and
month 010 Ch
Medium Data
FFFFN b/c
Ex 05 I 1
of for large programs Not
(
CS
-
processor
' '
16 bit
→a
I 1
it 'll
the ahah "
hence
compact one CS contains program and is
" Data fffEn every tis
the data Ex System & assume bits )
contains
gone 16⑨ Ffff )
→
-
of Dg processing gp
.
going f-
( fff
⑧ DS assembler FFFEN b
of done by the
number CS
large allows any
oh only
I "→
so ,
.ve
µµswÉ
can
but the Ds , contain
same large m ay
§
" Ye as -
" " →
"allows
_plvYffiub①
"
" " "" "
^^ "" "
"" "
PS P
oak
m o re
)
tram
I Memory Map
0B6on
:¥÷ 0000h
es
Model
oB6znÑÉ% Data
0020m
• Tiny some
Stack is initialized
onion )
• data
0 B- 62h Ss :sp→ on , on ( sp at
'
'
dat I db a
align 2 →
It
aligns the data to
the even boundy ( even location) * After compiling model tiny ,
an executable file
'
b
'
dat 2 db '
'
at 04 odd
'
EI : a
( skips" , created having .COM extension .
Ab
'
°
dat 2 c -
'
at OEN
b
i at 10M
A Program Segment Prefix : A part of .com file .
code
-
-
startup
→
A set of instructions which a re
given to the
operatives
n, yay ,
gym www.gayn.wm.waygggn.ua
add dat 2
,
al handled ( From 0000N to 0100N)
.
• exit - Mov
21
INT
End * PSP is created for any executable file ( for
asm
@ exe )
•
stack → has to be declared
in small Data started immediately from oB62n .
• data
'
dat 1
'
db a
bit
with 8 bit @ 16
main
j this is a
-
directives
; using simplified segment • of set indicating operation could not be done
-
are
NEH
All flags affected ⑧
pesigleopeod
q.my
MODEL SMALL this statement is need before
; of
of ✗ 86
•
gone
'
inalitd)
'
get directives
; segment NEG REX
.
NEG YTEPTRLSI ]
kilobyte stack
• STACK ; use default 1 -
• CODE
; Begin code segment string Instructions
start-up code
STARTUP ; Generate handle
equipped with
* 80×86 to
special instructions
•
is
here
Place instructions
-
- - -
-
; string operations .
exit code
Generate
• EXIT ; * string
:
A series of data words (@ bytes ) that reside in
* Operations :
m ove ,
scan
,
compare
L d d '
d
for / 6 bitter stirs Shi Shi
word
# Debt Source shisofwods of bytes of words ) of D.
bit )
AND ,
aol.it ( 32
for 32 up,
shisof Dwods
A logical AND , each bit in the source with the
corresponding bit in the destination * Copies byte @ word @ double word from location
-
a a
.
* CF ② OF both become ze ro
(irrespective of insult in the data segment to a location in the extra segment .
Source DS
Zf
affected ( * SI
:
* Pf ,
Sf an d Pf will make sense -
☒ for multiple -
byte @ multiple -
word moves
,
the count to be
in cx register .
# NOT Destination
decrement by 1
* Byte transfer ,
SI @ DI increment @
P
Complements each bit in the destination and stores
- @ -
by
2
p word -
,
4
the result back into the destination
-
.
PD -
- ,
* No Flags Affected
Mnemonic Meaning Format Operation Flags Affected
AL
E NOT
clear DF CLD ( Df ) 0 Df
CLD
←
NOT BX
STD set DF STD ④ f) ← I DF
NOT BYTEPTRISI]
# Df @ D
D= I
NEG Destination auto decrement
→
Direction flag
* resisters
Does 2 's complement the data in the destination for the DI & SI
on
operation
result back into duris string ops
and stores the the destination .
D with stings
* Cannot 's is used only
find 2 -128 ( 8- bit ) @
complement of
* CLD ⑧ STD → No operands sH→sg① * Lois affects no
Flags
with operands the kind of
(If instructions
no ,
addressing is
implied / implicit addressing ) # STOS
CLD implies that I'm working with Direction bbf -
segment memory
at
offset address indexed
memory a re a 50 DATA
jo another *
After store
-
contents if
of
13=01
'
DI
ng ine
AVI •
model tiny decile D= I
to
is going
set architecture
Ting ) E STOSB f- 5. [ DI ] AL DI -
( Ds D I -11
-
• data same as
F- S in
,
2
AMYL db 09h ,
ben den Ofsh STO SW .
_
AX
, ,
- - -
,
- -
array
2 as 50
deep 6) grasp ! -
F- AX 4
Code
affects
•
P Stol no FLAGS .
• startup
M ov CX
, 32h
lea Si
, array I F- write an ALP to
fill a set of 100
memory
tea di array
2
Now locations
'
DESI
starting displacement
'
, at with
MOV ⑤I] BL
Mors B
-
INC SI
AI Modelling
,
rep DI •
IN C SI Ina
INC DI d
#rh rest by
•
DATA
DEC
JNZ
'✗
✗1
Estuary
prefix ( reps
it
he
'll
Motb
DAT 1 DB 100 DUPC ? )
cause * ↳
Code
instruction till
.
Exit executed
•
contralto) .
startup
end
me zero
reaches DAT 1 DI DAH
MOV DI OFFSET LEA
,
; ,
REP STOSB
☒ AX @ EAX with the data
Loads AL
@
exit
stored at the data segment .
•
end
*
offset address indexed by SI register
*
After loading contents of SI inc
if 13=0 # SCAS
*
_
AX - word -
↳ Dsw A- ✗ -2
j
= -
,
*
Memory is Es :D I
EAX =
j Operands not affected ( sashay:)
-
, -
-
*
E SCASB # Repeat until end of string (0--0) @
SCASW
until compared bytes a re not
equal
SCASD
1000 67 67
}
2000
100 / 6 56
5 2001
ALP the displacement
E± :
write an to
find at which
1002 ya zool 4A
no , g, zoo ,
me og
aya , ,,µ ,
present gon an
array gz
1004 AA 2004 AA
data stored
from location DATI the number of
exit
.
<
of the 80
bytes data array
in is
.
1000 67 2000
1004 AA 2004 AA
MOV AL , ODN
Mov cx
,
80
Rotate
Operations
CLD
(
① If
Exit
all iterations ROR Best, Count
②
•
done
END
the next
* If oDn is found ,
DI will point to
* use CL for Count greater than 1 ( if 80386
address .
not ax
,
I
# [ Mps * nor
byteptnf.si ] ,
I
* MOV I 04h
string with
*
compares a
byte in one a
byte in ,
not ax I
*
another word dword
,
string @ a
/ in one string
another string
* nor byteptrf.si ] ,
I
with a
word / dword in .
* not ecx 12
SI with ES :
DI ,
* Ds :
* flags affected
*
P
Direction
Can be used
flag used
with
for
Prefix
auto inc @ dec
ROL
cL←y%<
- → CL
OFFSET STRING ROR
E MOV SI
,
FIRST
CLD
flags affected : Cf
100
MOV CX ,
of -
Ile MSB
changes -
( f JNZ AGA I N
MOV RESL ,
BL
AI
O exit
l l
O I I o l 0 0 •
Cf end
for ROL
, -o
Instructions
Shift
E ROL BL ,oy ( assumed
32 bit Mp
) # SAL
/ 5hL
AI :
Baye : 1001 1100
nf# 0111
Operation
"
0010 .
' ' 0 0 1 0°
bit
^bµ÷n;
'
*
shifts each in the specified some number
I 1001
left
100
of bit positions to he .
*
swap Nibbles using this * As bit shifted out of LSB 0 is put in LSB
,
to Cf
* ROR WORD PTRCBX] ,oy ( same effect) MSB shifted .
E SAL BX , 1
R④
SAL BP , CL
SAL EAX ,
12
writes
1 's in a
byte in location DATE and
→
OF If MSB changes bit rotate
-
-
single
→ pf -
Affected -
but has
meaning only if 8- bit operation
And
:
.
model tins
→
Af -
undefined ( unaffected )
-
data
DATAL DB
%f¥p
0A In
✗or
# SNR ( Shift Logical Right)
RESI ago
DB ?
BL ,0 MSAs
Shift operand bits rights Put zeroes in
✓
.
• code
•
startup tongue * 5hr Best, Count
AL
-
-
ish
Let
8 of "
MOV DL
E Mov BL ,AL BL -43
"
-
, So
,
AGAIN : AOL Al
,
I SMR AL CL
04h
,
Bn=
JNC NEXT Mov Bn , AL
Jo this program separated
INC BL ,
into two bytes -
u ⑨ ↳
?⃝
mode )
# SAR # c.
W/ CWD →
(Implicit Addrexis
0000
100 009
* 80 FF 80
EI
-
:
SAR DI
,
I 1111 1111 1000 0000
e×tendedboh
-
w
27 0027
Mov CL ,
02h *
-
design is
MuLy±µuLsouÉ"Md^
"
"" Md
* 9100 - ffff 9100
always
¥ Ix iobitrp
# is
Destination bitmp
EAT
32
Accumulator
* Source times AL → byte multiplication F-
.
mode tiny
* source times AX →
word -
<
data
→ D-
Some times E- AX -
* Multiplicand DW 2040h
Multiplier
Product 1 DW ?
product 2 DW ?
E
B¥AL
MUL BL AX
;
•
Code
possible .
startup
that the
bit Mov AX , Multiplicand
is 16
result
MVL Multiplier
BX BX DX : AX Mov Products ,A✗
MVL ;
✗ AX
b
b Mov product 2 DX
16 bits ,
lower
Upper •
exit
16 bits
end
Acts microcontroller
EI AL like
-02
:
BL -
03 .
MUL BL
06
AX ⑧ # DIV / IDIV
-
b
bate Ginder bor woeoyiworoy
significant
most A word ÷
Byte @ word → quad word
*
D. word
Qword
÷
÷
word
Dword
④ te ÷ byte ( Not allowed)
* DIV / IDIV Source
P word ÷
-
Byte
Byte location
A word in AX
,
in
Register @ memory
* Ah reminder
quotient
-
AL -
* Dwordiwod
* Dword in DX
:
AX
,
word in Register ( on
) Meng
location .
DX Reminder
* AX quotient
-
-
* Qword-Dwoo
word in f- DX : EAX , D word in Register ( of rang
*
location
p
EAX - Quotient F-DX -
Reminder
All
p flags undefined
B Sign of remainder same as divided -
I n case
of signed ops
# 80×86 is slower in MULIDIV
instructions than other
any
CWD arithmetic instruction
use KBW .
resignations
on
,
# It imposes general / Div
mud