You are on page 1of 36

MICROPROCESSOR AND

ASSEMBLY LANGUAGE
LECTURE-5-PROGRAM STRUCTURE OF
ASSEMBLY LANGUAGE

MUHAMMAD HAFEEZ
DEPARTMENT OF COMPUTER SCIENCE
GC UNIVERSITY LAHORE
TODAY’S AGENDA

 Program Structure
 Assembler Directives
MEMORY MODELS
 Memory Models defines how much memory
we need for a program

 TINY
 SMALL
 MEDIUM
 COMPACT
 LARGE
 HUGE
MEMORY MODELS
 TINY
 64K for both code and data segment
 SMALL
 64K for code segment
 64K for data segment
 MEDIUM
 64K data segment
 Code can exceed 64K
 COMPACT
 data segment can exceed 64K
 Code segment 64K
 LARGE
 Code can exceed 64K
 Data can exceed 64K but Array declared in one data segment
can not exceed 64K
 HUGE
 Code can exceed 64K
 Data can exceed 64K, Array declared in one data segment can
exceed 64K
MEMORY MODELS
ASSEMBLY LANGUAGE
STATEMENTS
 Assembly Language Statements are either,
 Instructions
 Directive (also called Pseudo-Ops)

 Both Instruction and Directive can have up to 4


Fields
 [Label]mnemonic [operand] [;comment]

 Brackets indicate the fields are optional


ASSEMBLY LANGUAGE
STATEMENTS
 Label:
 Label composed of A-Z, a-z, 0-9 and special
character ?, ., @,$
 Must begin with an alphabetic and special
character
 Cannot exceed 31 characters
 . Can only be used as first character
 No Case Sensitivity
 Label for directive do not need to end with Colon
(:)
 Label for Instruction needs to end with (:) colon
 As it tells the assembler to refer the code
ASSEMBLY LANGUAGE
STATEMENTS
ASSEMBLY LANGUAGE
STATEMENTS
 Mnemonic:
 Instructions and Operands for Processor
 Real work of program
 MOV AX,5
 ADD AX,BX
 Pseudo-Ops/ Directive for Assembler
 MAIN
 ENDP
 DB
 Comment:
 At end of each line
 Optional BUT Highly important for readable/
understandable Assembly Language Programs
ASSEMBLY LANGUAGE
PROGRAM
DATA MOVEMENT
INSTRUCTIONS
simplified

Assigns Stack of
a Title program
to your Data of
Source Label program
File
Assembler
Directive

Comment
DATA DEFINING DIRECTIVES
 Storage is defined using data definition
directives
 Storage is created at assembly time.
Variables can be initialized to character
string

 Syntax

Name Data Directive Initial value, [values]


DATA DEFINING DIRECTIVES
DECLARATION OF DATA

 DATA1 DB 15H
 DATA2 DB 15H, 1FH, 4AH
 DATA3 DB ? ;SET ASIDE ONE BYTE
 DATA4 DB ‘STRING OF CHARACTERS’,’$’

 DB could be replaced with DW, DD, DQ or


DT depending upon requirement
NAMED CONSTANTS WITH
DIRECTIVE “EQU”
 To assign a name to constant, use EQU
pseudo-op

 EQU Syntax
 Name EQU Constant

 Example:

 LF EQU 0AH
 CR EQU 0DH

 PROMPT EQU ‘Enter Number’

 No Memory is allocated for EQU pseudo-op


MORE DATA DEFINITION
DIRECTIVES
 To assign a name to constant, use EQU
pseudo-op

 EQU Syntax
 Name EQU Constant

 Example:

 LF EQU 0AH
 CR EQU 0DH

 PROMPT EQU ‘Enter Number’

 No Memory is allocated for EQU pseudo-op


ASSEMBLE, LINK AND RUN
A PROGRAM
 Software you need can be downloaded from blog,
along with instructions to set up your assembly
language programming environment.

 Three Steps are required to create, assemble and


link an assembly language program
 Step 1: A simple Text Editor to create assembly language
program file and save it with Extension (.ASM)
 An Assembler MASM (Microsoft Macro Assembler) that takes
(.ASM) file and create an Object File with Extension (.OBJ)
 A Linker Program (LINK) that takes an (.OBJ) file and create
Executable file with Extension (.EXE)
 Run Executable File

 Execute under Debug, DEBUG PRG1.EXE (Enter) .. U CS:0 1


to find data segment address, D Datasegment: 0 F , G, Then
again D DataSegment: 0 F
 Note: Other assembler and linker program also exist, like
Borland’s TASM and TLINK etc.
ASSEMBLE, LINK AND RUN
A PROGRAM
FILES CREATED BY
ASSEMBLER
 The Source Listing File (.LST)
 Display line number and corresponding machine code side-by-
side, helpful in debugging

 The Cross Reference File (.CRF)


 List of variables and labels in the programs

 The Cross Reference File (.CRF)


 List of variables and labels in the programs
MORE DIRECTIVE OF MASM
FOR HANDLING DATA
 ORG [Number]
 Is used to mark the beginning of offset address
 ORG 10H
 OFFSET
 Returns the offset of a variable from the start of
its segment address
 The destination must be 16-bit
 MOV BX, OFFSET DATA2
[More on Data Definitions, PTR, SEG later]
FLAG REGISTER
 Flag Register defines processor status and
help it to make decisions

 Decision making is done by looking at


current state of processor

 Nine individual bits called FLAGS in flag


register represents a particular state of
processor

 Flags are classified as control flags and


status flags
 Status flags are affected by result of
computations, also called conditional flags
FLAG REGISTER
CONDITIONAL FLAGS
 The processor uses conditional flags to
make decision
 SUB AX, AX set Zero Flag = 1

 CARRY FLAG (CF):


 CF=1, If carry out from MSB (D7) for Byte, (D15) for
Word in case of addition, or Borrow in MSB in case of
subtraction, also affected by rotate instructions
 Parity Flag (PF):
 PF=1, If low byte of a result has even number of 1
bits (even parity).
 PF=0, if low byte of a result has odd number of 1 bits
(odd parity)
CONDITIONAL FLAGS
 Auxiliary Carry (AF):
 AF=1, If carry out from D3 bit to D4 bit, otherwise
AF=0
 Zero Flag (ZF):
 ZF=1, If result is zero.
 ZF=0, for non-zero results
 Sign Flag (SF):
 SF=1, If after computations MSB=1, if you are giving
a signed interpretation to result
 SF=0, if MSB = 0
 Overflow Flag (OF):
 OF=1, If Singed overflow occurs
 OF=0, otherwise
CONTROL FLAGS
 Trap Flag (TF):
 TF=1, Allow to program to execute in single step,
helpful in debugging
 TF=0, otherwise
 Interrupt Flag (IF):
 IF=1, Allow external maskable interrupts to the
processor
 IF=0, otherwise
 Direction Flag (DF):
 Used to control the direction of string operations
FLAG REGISTER IN DEBUG
UTILITY
 Command is --- R F
INSTRUCTIONS AFFECT
FLAGS
 MOV/ XCHG
 None
 ADD/ SUB
 All
 INC/ DEC
 All except CF
 NEG
 All, (By default set CF=1, only exception result is
zero)
 OF=1, if Word operand is 8000H and byte
operand is 80H
EXAMPLES:
 AX=FFFFH, BX=FFFFH, ADD AX,BX
 SF=1, CF=1, AF=1
 MOV AL,80H, MOV BL,80H, ADD AL,BL
 PF=1, ZF=1, CF=1, OF=1, SF=0
 MOV CX, 5
 DEC CX, DEC CX, DEC CX, DEC CX, DEC CX
 After execution of these instructions result will be
zero hence, ZF=1
EXAMPLES:
  Addition 0Fh + 08h?
 Addition 0Fh + F8h?
 Addition 4Fh + 40h?

 Addition F8h + 81h?


 Represent in signed and unsigned
numbers
SOME BASIC I/O
OPERATIONS
 INT 21H , an interrupt also called DOS
function call
 87 different interrupts are supported by this
DOS function call
 Each interrupt is identified by a function
number placed in AH register
SOME BASIC I/O
OPERATIONS
 INT 21H , an interrupt also called DOS
function call
 87 different interrupts are supported by this
DOS function call
 Each interrupt is identified by a function
number placed in AH register
SOME BASIC I/O
OPERATIONS

 Read a Character
 Display a Character
 Display a String
SOME BASIC I/O
OPERATIONS
 Read a Character
 INT 21h / AH=1 - read character from
standard input, with echo, result is stored
in AL.
 If there is no character in the keyboard
buffer, the function waits until any key is
pressed.
Example:
mov ah, 1
int 21h
SOME BASIC I/O
OPERATIONS
 Display a Character
 INT 21h / AH=2 - write character to
standard output.
 DL = character to write, after execution AL
= DL.
Example:
 mov ah, 2
 mov dl, ’a’
 int 21h
SOME BASIC I/O
OPERATIONS
 Display a String
 INT 21h / AH=9 - display a string character
string on the console.
 The offset of string must be in DX
 string must be end with $ (24h)

mov ah,9 ;string output function


mov dx,offset msg ;offset address of the
string
int 21 ;call DOS
 obvious disadvantage, a dollar sign can not
be displayed as part of a string
QUESTIONS

 ??????????????????????????

You might also like