Assembly - Fundamentals of ALP
Assembly - Fundamentals of ALP
The processor does not provide such function like Cin and Cout.
Cont’d
So how do we communicate with the user?
It is simple we can use system call (or interrupt calls in DOS).
Mov
al,2h
Mov
dl,’a’
Int 21
<---------
coding
interrupt
explicitl
Assembly language structure/components
The STC instruction, for example, has no operands: stc ; set Carry flag
The INC instruction has one operand: inc eax ; add 1 to EAX
The MOV instruction has two operands: mov count,ebx ; move EBX to count
The imul instruction has Three operands: imul eax,ebx,5 ;In this case, EBX
is
multiplied by 5, and the product is stored in the EAX register.
Assembling, Linking, and Running
Programs
A source program written in assembly language cannot be executed directly
on its target computer. It must be translated, or assembled into
executable code.
The assembler produces a file containing machine language called an object
file. This file isn’t quite ready to execute. It must be passed to another
program called a linker, which in turn produces an executable file.
Defining Data
Types
It describes a set of values that can be assigned to variables and
expressions
of the given type.
The essential characteristic of each type is its size in bits: 8, 16, 32, 48, 64
A variable declared as DWORD, for example, logically holds an unsigned
32- bit integer. In fact, it could hold a signed 32-bit integer, a 32-bit
single precision real, or a 32-bit pointer. The assembler is not case
sensitive, so a directive such as DWORD can be written as dword, Dword,
dWord, and so on.
Instruction Mnemonic
Is a short word that identifies an instruction. Assembly language instruction
mnemonics such as mov, add, and sub provide hints about the type
of operation they perform.
Following are examples of instruction mnemonics:
Data Definition
Statement
Syntax: name directive initializer [, initializer]...
count DWORD 12345
Directive: The directive in a data definition statement can be BYTE,
WORD, DWORD, SBYTE, SWORD, or DB,DW,DD,DQ.
Initializer: At least one initializer is required in a data definition, even if it
is zero. Additional initializers, if any, are separated by commas. For integer
data types, initializer is an integer constant or expression matching the size
of the variable’s type, such as BYTE or WORD. If you prefer to leave the
variable uninitialized (assigned a random value), the ? symbol can be
used as the initializer. All initializers, regardless of their format, are
converted to binary data by the assembler.
Defining BYTE and SBYTE
Data
The BYTE (define byte) and SBYTE (define signed byte) directives
allocate storage for one or more unsigned or signed values. Each initializer
must fit into 8 bits of storage. For example:-
value1 BYTE 'A' ; character constant
value2 BYTE 0 ; smallest unsigned byte
value3 BYTE 255 ; largest unsigned byte
value4 /BYTE −128 ; smallest signed byte
value5 /BYTE +127 ; largest signed byte
A question mark (?) initializer leaves the variable uninitialized, implying it
will
be assigned a value at runtime:
value6 BYTE ?
Cont’
dThe optional name is a label marking the variable’s offset from the
beginning of its enclosing segment. For example, if value1 is located at
offset 0000 in the data segment and consumes 1 byte of storage, value2 is
automatically located at offset 0001:
value1 BYTE 10h
value2 BYTE 20h
The DB directive can also define an 8-bit variable, signed or unsigned:
val1 DB 255 ; unsigned byte
val2 DB -128 ; signed byte
Multiple Initializers
If multiple initializers are used in the same data definition, its label refers
only to the offset of the first initializer. In the following example, assume list is
located at offset 0000. If so, the value 10 is at offset 0000, 20 is at offset
0001, 30 is at offset 0002, and 40 is at offset 0003:
list BYTE 10,20,30,40 list BYTE 10,20,30,40
BYTE 50,60,70,80
BYTE 81,82,83,84;multiple declaration
Within a single data definition, its initializers can use
different radixes. Character and string constants can
be freely mixed.
In the following example, list1 and list2 have the same
contents:
list1 BYTE 10, 32, 41h, 00100010b
list2 BYTE 0Ah, 20h, 'A', 22h
greeting1 BYTE "Good afternoon",0
greeting2 BYTE 'Good night',0
Each character uses a byte of storage. Strings are an exception to the rule
that byte values must be separated by commas.
DUP
Operator
The DUP operator allocates storage for multiple data items, using a
constant expression as a counter. It is particularly useful when allocating space for
a string or array, and can be used with initialized or uninitialized data:
BYTE 20 DUP(0) ; 20 bytes, all equal to zero
BYTE 20 DUP(?) ; 20 bytes, uninitialized
BYTE 4 DUP("STACK") ; 4 bytes: " STACKSTACKSTACKSTACK "
.data Data with instruction
smallArray DWORD 10 DUP(0) ; 40 bytes – .code
.data? – mov eax,ebx
– .data
bigArray DWORD 5000 DUP(?) ; 20,000
– temp
bytes,
DWORD ?
not initialized – .code
.data – mov temp,eax
smallArray DWORD 10 DUP(0) ; 40 bytes
bigArray DWORD 5000 DUP(?) ; 20,000
bytes
Data Transfer
Instructions
Label is optional and can be ignored. Operands can be zero, one, two,
three
mnemonic ;no operands
mnemonic [destination] ;one operands
mnemonic [destination],[ source] ; two operands
mnemonic [destination],[ source-1],[ source-2] ; three operands
x86 assembly language uses different types of instruction operands
Immediate—uses a numeric literal expression
Register—uses a named register in the CPU
Memory—references a memory location
Simple notation for operands
MOV Instruction
The MOV instruction copies data from a source operand to a
destination
operand.
Known as a data transfer instruction, it is used in virtually every program
Syntax:- MOV destination, source
The destination operand’s contents change, but the source operand is
unchanged. The right to left movement of data is similar to the
assignment statement in C++ or Java:dest = source;
(In nearly all assembly language instructions, the left-hand operand is the
destination and the righthand operand is the source.)
MOV is very flexible in its use of operands, as long as the following rules
are
observed:
Cont’
dBoth operands must be the same size.
Both operands cannot be memory operands.
CS, EIP, and IP cannot be destination operands.
An immediate value cannot to a segment register.
Here is a list of the general variants of MOV
Overlapping
MOV reg,reg
Values:-
.data
MOV mem,reg .data oneByte BYTE 78h
MOV reg,mem var1 oneWord WORD
MOV mem,imm WORD ? 1234h
MOV reg,imm var2 oneDword DWORD
MOV reg/mem16,sreg WORD ? 12345678h
.code
MOV sreg,reg/mem16 .code
mov eax,0 ; EAX =
mov 00000000h
ax,var1 mov al,oneByte ; EAX = 00000078h
mov mov ax,oneWord ; EAX =
00001234h mov eax,oneDword ;
Addition & subtraction
There are different types of arithmetic instruction sets. Let’s begin with INC
(increment), DEC (decrement), ADD (add), SUB (subtract), and NEG
(negate).
INC and DEC Instructions
The INC (increment) and DEC (decrement) instructions, respectively, add 1
andsyntax
The subtract
is:1 from a single operand. ADD Instruction
⚫ INC reg/mem Syntax: ADD dest,
⚫ DEC reg/mem source
.data
Following are some examples:
var1 dw 10000h
– .data var2 dw 20000h
– myWord dw 1000h .code
– .code mov eax,var2
add eax,var1 ;; EAX
EAX==
– inc myWord ; myWord =
10000h
30000h
Flags :-The Carry, Zero, Sign, Overflow,
1001h Carry, and Parity flags are changed according
Auxili
– mov bx,myWord t
value that is placed in the destination
– dec bx ; BX = 1000h
operand.
Cont’
d dest, source
SUB
Here is a short code example that subtracts two 32-bit integers:
– org 100h
– .data
– var1 DD 300h
– var2 DD 100h
– .code
– mov ax,var1; AX = 300h
– sub ax,var2 ; AX = 200h
– Ret
– Flags The Carry, Zero, Sign, Overflow, Auxiliary Carry, and Parity flags
are changed according to the value that is placed in the destination
operand.
NEG Instruction:
TheNEG (negate) instruction reverses the sign of a number by converting the
number to its two’s complement. The following operands are permitted:
NEG reg
Div Instruction
It has one destination operand
Syntax: Div S ;if S=8bit then Ax/S8 is possible to operate
Q(Ax/S8)==> AL,R(Ax/S8)==> AH
if S=16bit (DX,AX)/S16 is possible to operate
Q(DX,Ax)/S16==> AX,R(DX,AX)/S16==>
1. .code DX
2. ; 5001d/25d Q=200,R=1
mov
3. ; 1000d/100d Q=10,R=0 Ax,1004h
4. ; 55436d/568d ,Q=217,R=284 mov
dx,1004h
mov Bx,1234h
5. mov Ax,1389h div
6. mov Bl,19h Bx
7. div bl ;10041004h/1234h
8. mov Ax,1000
10. div Bx
9. mov Bx,100
;10
Mult instruction
The default 8 and 16 bit storage while performing multiplication is stored
in accumulative register and 32bit is stored in AX, and DX
(16,16)bit registers
Syntax: Mul S
Mul ;D8*AL==>
D8 AX
;D16*AX==>
mov AL,14h
Dx,Ax mov
mov BL,24h Ax,10
MUL BL ;AH=02 mov
AL=D0 BX,10
mov Ax,1234h MUL
mov BX,45F4h BX ;AX=0
MUL Ah
BX ;AX=5D90,DX=04F9
JMP & LOOP
Instructions
Assembly language programs use conditional instructions to implement
high- level statements such as IF statements and loops.
Unconditional Transfer: Control is transferred to a new location in all
cases; a new address is loaded into the instruction pointer, causing
execution to continue at the new address.The JMP instruction does this. \
Conditional Transfer: The program branches if a certain condition is true.
A wide variety of conditional transfer instructions can be combined to
create conditional logic structures.
The CPU interprets true/false conditions based on the contents of the
ECX
and Flags registers.
Cont’
dJMP destination
top:
jmp top ; repeat the endless loop
JMP is unconditional, so a loop like this will continue endlessly
unless another way is found to exit the loop.
org 100h
.data
var1 DB "Hello World$"
.code
top:
mov ax,@data
mov ds,ax
mov dx, offset
var1
mov ah,09h
int 21h
jmp top
LOOP Instruction
ECX is automatically used as a counter and is decremented each time the loop
repeats. Its syntax is LOOP destination
The execution of the LOOP instruction involves two steps:
First, it subtracts 1 from ECX. Next, it compares ECX to zero.
If ECX is not equal to zero, a jump is taken to the label identified by
destination. if ECX equals zero, no jump takes place, and control passes to
the instruction following the loop.
In the following example, we add 1 to AX each time the loop repeats.When
theloop ends, AX = 5 and ECX = 1. .data
0: 2. sum dw 1
mov ax,0 3. limit dw 5
4. .code
mov cx,5
5. mov ax,0
L1: 6. mov
inc ax cx,limit
loop L1 7. lable1:
8. ADD
10. ax,sum
loop lable1 ;output 0Fh,15d,1111b,17o
Nested
Loops
When creating a loop inside another loop, special consideration must be
given
to the outer loop counter in ECX.You can save it in a variable:
.data 2.
1. .data
count DWORD ? 3. count DW ?
org 100h
4. .code
.code 5. mov cx,5 ; set outer loop count
mov ecx,100 ; set outer loop 6. L1:
7. mov count,cx ; save outer loop count
count L1: 8. mov cx,4 ; set inner loop count
mov count,ecx ; save outer 9. L2:
loop count 10. mov ah,02h
11. mov dl,2Ah
mov ecx,20 ; set inner loop 12. int 21h
count 13. loop L2 ; repeat the inner loop
loop L2 ; repeat the inner
L2:
loop 14. mov cx,count ; restore outer loop count
mov ecx,count ; restore outer loop count 15. mov dl,0Dh ;line feed
loop L1 ; repeat the outer 16. int 21h
loop 17. mov dl,0Ah ;to Disp characters
18. int 21h
19. loop L1 ; repeat the outer loop
Array
sArrays can be seen as chains of variables. A text string is an example of a byte array,
each character is presented as an ASCII code value (0..255).
Here are some array definition examples:
a DB 48h, 65h, 6Ch, 6Ch, 6Fh, 00h
b DB 'Hello$’
When compiler sees a string inside quotes it automatically converts it to set of
bytes.This
chart shows a part of the memory where these arrays are declared:
Cont’
dYou can access the value of any element in array using square brackets, for
example:
MOV AL, a[3]
You can also use any of the memory index registers, SI, DI, BP for
example: MOV SI, 3
MOV AL, a[SI]
If you need to declare a large array you can use DUP operator.
The syntax for DUP:
number DUP (value(s))
number - number of duplicate to make (any constant value).
value - expression that DUP will duplicate.
for example:
c DB 5 DUP(9) is an alternative way of
declaring:
c DB 9, 9, 9, 9, 9
Cont’
d(Load Effective Address) instruction and alternative OFFSET operator are
LEA
used to access the elements of an array. Both OFFSET and LEA can be used to
get the offset address of the variable.
LEA is more powerful because it also allows you to get the address of an
indexed variables. Getting the address of the variable can be very useful in
some situations, for example when you need to pass parameters to a
procedure. 1. org
1. org
2. MOV
100h AL,VAR1 ; check value of VAR1 by moving it to 100hMOV AL,VAR1.
BX,VAR1 ; get address of VAR1 in 3. MOV BX, OFFSET
AL. 2.
BX.
BYTE PTR [BX], 44h ; modify the contents of VAR1
3. LEA
AL,VAR1 ; check value of VAR1 by moving it to AL.4.
VAR1. MOV BYTE PTR
4. MOV [BX], 44
5. MOV 5. MOV AL,VAR1
6.
6. ret
VAR1 DB 22h 6. RET
Line number 3 LEA vs offset same 7. VAR1 DB 22h
functionality
Cont’
dIn the
Isnext example,embedded
a command arrayB contains 3 bytes.
in the source codeAsthat
ESIisisrecognized
incremented,
and it points to
acted
each
byte,
uponin order: If we use an array of 16-bit integers, we add 2 to ESI to
1. .data address each subsequent array element:
2. arrayB DB 1. org 100h
10h,20h,30h 2. .data
3. .code 3. arrayW DW 1000h,2000h,3000h
4. mov si,OFFSET 4. .code
arrayB 5. mov si, offset arrayW
5. mov al,[si] ; AL = 10h 6. mov ax,[si]; AX = 1000h
7. add si,2
6. inc si 8. mov ax,[si]; AX = 2000h
7. mov al,[si] ; AL = 20h 9. add si,2
8. inc si 10. mov ax,[si]; AX = 3000h
9. mov al,[si] ; AL = 30h 11. ret
10. ret
Cont’
dExample: Adding 32-Bit Integers: The following code example adds
three double words. A displacement of 4 must be added to ESI as it
points to each subsequent array value because Double words are 4 bytes
long:
– .data
–– arrayD
.code DWORD 10000h,20000h,30000h Shows the initial value of ESI
– mov esi,OFFSET arrayD in
relation to the array data:
– mov eax,[esi] ; first number
– add esi,4
– mov eax,[esi] ; second
number
– add esi,4
–– mov eax,[esi]
Suppose arrayD; third number
is located at offset
10200h.
System Administration
Thank You…..!!!