Experiment 2
Directives – indicates how an operand or section of a program is to be processed by the
assembler
model tiny – uses 64 KB both for the source code and the data code
model small – uses 64KB each for the source code and data code
code – segment that holds the program
org 100h – origin, indicates the start of a program
start: / end start – indicates the beginning and end of a program or a section of a
program
Interrupts
Display single character
Interrupt 21, service number 2
- dl – register to hold the character to output
- ah=2 – service number to output a single character stored in register ah
ex.
mov ah,2
mov dl,41h
int 21h
Terminate the program properly
Interrupt 20h
or
Interrupt 21h, service number 4ch
ah = 4ch – service number to terminate the program properly stored in register ah
ex.
int 20h / mov ah, 4ch
int 21h
Clear screen
Interrupt 10h, service number 3
ax = 03 – service number to clear the screen stored in register ax
ex.
mov ax,03
int 10h
Carriage return
Interrupt 21h, service number 13
- dl=13 – service number for carriage return stored in register dl
ex.
mov dl,13
int 21h
New Line
Interrupt 21h, service number 10
- dl =10 – service number for new line stored in register dl
ex.
mov dl,10
int 21h
Int 21h
Service 1 Service 7 Service 8
-character input with echo -character input without -character input without
- can recognized CTRL+C echo echo
- can’t recognized CTRL+C - can recognized CTRL+C
Sample program:
.model small
.code
Org 100h
S:
Mov ah,1
Int 21h
Int 20h
End s
Common Instruction
1. Inc
- increment the value of a register by 1
Ex,
Inc dl
2. Dec
- decrement the value of a register by 1
Ex.
Dec dl
3. add
- lets you add a value to your register
Ex.
Add dl,20
Int 21h, service 9
<display string / string manipulation
ah = 9
[ds:dx] = address of the string
ds = address or location of the string
dx = register to hold the string
how to declare a string
var_name db “string$”
db = define byte
$ = string terminator
ex.
mess db “hello$”
mess1 db 10,13,”World$”
mess2 db “good day”,10,13,’$’
Syntax:
mov ax,@data
mov ds,ax
mov ah,9
mov dx, offset mess
int 21h
Common Instruction
LEA
Loads effective address
Use register dx to store the effective address
Format:
lea dx,var_name
Ex. Lea dx,mess
Sample program:
.model small
.stack
.code
mess db "hello$",10,13
mess1 db “hello$
org 100h
s:
main proc
mov ax,@data
mov ds,ax
mov ah,9
lea dx,mess
int 21h
mov ah,9
mov dx, offset mess1
int 21h
mov ah,4ch
int 21h
main endp
end s
JMP instructions
JMP stands for jump
Jump to a level
Allows the programmer to skip sections of a program and branch to any part of
the memory for the next instruction
Format:
jmp label
label – refers to the address of an instruction procedure or segment
ex. jmp start
- this instruction will transfer the flow of control to the label start
LOOP
allows a part of a program to be repeated for several times
the number of loops depends upon the value of the CX register
CX takes the number of loops
Each time the loop is encountered the value of CX is decremented
Looping stops when CX becomes zero
Format:
LOOP label
Ex. LOOP start
Sample program:
.model small
.code
Org 100h
S:
Mov ax,03
Int 10h
Mov cx,3
X:
Mov ah,2
Mov dl,’A’
Int 21h
Loop x
Int 20h
End s
Conditional JUMPs
same as JMP instruction but JMP is only taken if the condition is TRUE.
JA – jump if above = >
JNA – jump if not above = <=
JB – jump if below = <
JNB – jump if not below = >=
JE – jump if equal = =
JNE – jump if not equal = !=
Format:
Conditional_jump level
Ex. je start
CMP instruction
Cmp stands for compare
Compare bytes or words
Used to compare two numeric data fields, one or both of which are contained in a
register
Format: cmp s1,s2
Takes the relation of s1 and s2
Ex.
cmp dx,00 ;is dx=00?
je L10 ;if yes, jump to L10
L10:……… ;jump point if dx = 00
Note: cmp instruction is used with conditional jumps
Ex.
cmp al, bl
jmp x
Sample program:
.model small
.code
Org 100h
S:
Mov ax,03
Int 10h
Mov ah,1
Cmp al,’x’
Int 21h
Int 20h
End s
CALL / RET instruction
Is similar to jump but it allows to return back to the main program.
Syntax :
Call {arguments}
.
.
.
Ret
Ex.
.
.
.
Call down
.
.
.
.
Down proc
.
.
.
ret