Unit-5 - CO
Unit-5 - CO
Contents
• Assembler directives
• Simple programs
• Procedures
• Macros
• Assembly language programs involving:
• Logical
• Branch
• CALL instructions
• Sorting
• Evaluation of Arithmetic expressions
• String Manipulation
Assembler Directives
• Types of hints are given to the assembler using
some predefined alphabetical strings called
Assembler Directives.
• Which helps the assembler to correctly
understand the assembly language programs.
Assembler Directives
Assume
Used to tell the assembler the names of the logical
segments to be assumed in the program.
Example, ASSUME CS:CODE ,DS:DATA
DB – Defined Byte
Used to declare a byte type variable in memory.
Example, n1 DB 49h
DW – Define Word
Used to tell the assembler to define a word type
variable in memory. ex: n1 DW 1234h
Assembler Directives
LABEL
•Used to give the name to the current value in the
location counter.
•The LABEL directive must be followed by a term which
specifies the type you want associated with that name.
LENGTH
•Used to determine the number of items in some data
such as string or array.
•EX:MOV CX,LENGTH ARRAY
Assembler Directives
OFFSET
•It is an operator which tells the assembler to determine
the offset or displacement of named data item or
procedure from the start of the segment which contains
it.
•EX: MOV SI,OFFSET LIST
ORG – Originate
•Tells the assembler to set the location value.
•Example, ORG 7000H sets the location counter value to
point to 7000H location in memory.
Assembler Directives
PROC – Procedure
•Used to identify the start of the procedure.
PTR – Pointer (BYTE OR WORD)
•Used to assign a specific type to a variable or a
label.
•EX:MOV AL, BYTE PTR [SI]
MOV AX,WORD PTR [SI]
Assembler Directives
• + & - operators:
• These operators represents arithmetic
addition & subtraction.
• EX:MOV AX,[SI+2]
MOV DX,[BX-3]
• SEGMENT: logical segment
• Ex:CODE SEGMENT
Assembler Directives
SHORT
•Used to tell the assembler that only a 1-byte displacement is
needed to code a jump instruction.
•If the jump destination is after the jump instruction in the
program, the assembler will automatically reserve 2 bytes for
the displacement.
TYPE
•Tells the assembler to determine the type of a specified
variable.
•The TYPE operator can be used in instruction such as ADD BX,
TYPE WORD_ARRAY, where we want to increment BX to point
to the next word in an array of words.
Procedures
• The procedure is a group of repetitive
instructions stored as a separate program in the
memory and it is called from the main program
whenever required.
• Type of procedure depends on where the
procedure is stored in the memory.
• If it is in the same code segment where the main
program is stored then It is called near procedure
otherwise it is referred to as far procedure.
• This procedures are used by CALL and RET
instructions.
Procedures
• The CALL instruction is used to transfer execution
to procedure or subprogram. there are two types
of CALLs, near & far.
• Near call is a call to a procedure which is in the
same code segment as the call instruction.
• Far call is a call to a procedure which is in the
different code segment from that which contains
the call instruction.
• RET instruction will return the execution from a
procedure to the next instruction after call
instruction.
Macros
• Macro is a group of repetitive instructions.
• The macro assembler generates the code in
the program each time where the macro is
called, such that it takes more memory.
• Macros can be defined by MACRO and ENDM.
Differences b/w Procedures & Macros
Procedures Macros
DEC CL
JNZ again
MOV od, DX
MOV eve , BX
INT 3H
code ends
end start
Greatest number in list
assume cs: code ,ds: data
data segment
list db 10h,20h,30h,40h,50h
res db ?
data ends
code segment
start:
MOV AX, data
MOV DS,AX
XOR AX,AX
MOV Cl,04H
MOV SI, offset list
MOV AL,[SI]
again: CMP AL,[SI+1]
JNC next
MOV AL, [SI+1]
next: INC SI
DEC CL
JNZ again
MOV res,AL
INT 3h
code ends
end start
Smallest number in list
assume cs: code ,ds: data
data segment
list db 10h,20h,30h,40h,50h
res db ?
data ends
code segment
start:
MOV AX, data
MOV DS,AX
XOR AX,AX
MOV Cl,04H
MOV SI, offset list
MOV AL,[SI]
again: CMP AL,[SI+1]
JC next
MOV AL, [SI+1]
next: INC SI
DEC CL
JNZ again
MOV res,AL
INT 3h
code ends
end start
Factorial of a given number
assume cs: code, ds: data
data segment
n1 db 04H
Fact dw ?
data ends
code segment
start:
MOV AX,data
MOVDS,AX
MOV AL,0000H
MOV AL,n1
MOV CL,03H
L1:
MUL CL
DEC CL
JNZ L1
MOV fact,AX
INT 3H
Code ends
end start