Lecture #02, Microprocessor Lab 2
Lecture #02, Microprocessor Lab 2
Lab
Dept. of Electrical and Electronic Engineering
University of Dhaka
Prof. Sazzad M.S. Imran, PhD
sazzadmsi.webnode.com
&
Dr. Sakhawat Hussain
Procedure
Set of program statements that s can be processed independently.
Variable and labels defined in procedure are local.
General Form:
ProcedureName PROC [NEAR/FAR]
; save all registers that get modified using PUSH
…
; procedure codes
…
; Restore all registers that were saved in stack
; Return to calling program using RET instruction
ProcedureName ENDP
PROC directive that indicates beginning of procedure.
ENDP directive that indicates end of procedure.
Procedures must be defined within code segment only.
Procedure
Procedure Call:
CALL
s
transfer control to subprogram or procedure.
saves return address on stack.
two types Intra-Segment or near call.
Inter-Segment or far call.
General Form:
CALL ProcedureName
Procedure
Procedure Return: s
RET transfer control from procedure back to calling procedure.
returns to main program instruction following CALL.
two types
1) Near RET:
Transfer of control is within segment.
RET replaces IP with a word from top of stack.
IP = offset address of instruction following CALL.
SP = SP+2
2) Far RET:
RET instruction pops 2 words from stack
IP = offset address of instruction following CALL
CS = segment address of calling program.
Procedure
Procedure Examples:
a) HEX2ASC PROC NEAR
s
b) MYCODE SEGMENT
… IFACT PROC FAR
; procedure code …
… ; procedure code
RET …
HEX2ASC ENDP RET
Call to procedure: IFACT ENDP
CALL HEX2ASC MYCODE ENDS
Call to procedure:
CALL FAR PTR IFACT
Programming with
General Form: Macros
MacroName MACRO [Argument1, …, ArgumentN]
…
; Body of macro: Program text to be declared as macro
…
ENDM
MACRO = beginning of macro.
ENDM = end of macro.
Body = definitions, declarations, or small part of codes.
Codes are substituted while translating them to machine code.
Invoking Macro:
MacroName Arguments
Programming with
MACRO Example:
PrintString
Macros
MACRO msg
mov ah, 09H ; AH=display string function
mov dx, offset msg ; DX=offset of a data item msg
int 21H ; call DOS service
ENDM
Invoking Macro:
msg1 db ‘Hello everyone!$’
PrintString msg1
After assembling
mov ah, 09H
mov dx, offset msg1
int 21H
Program Development
Tools
Program development tools include-
1) Text Editor program created
source program with filename extension ASM.
DOS editor (EDIT), Visual editor (VI) etc.
2) Pre-Processor translates source program to source file
macros resolved and header files included.
TASM, MASM, Borland C, Microsoft C.
3) Assembler translates assembly language program to machine
language program.
source_file.ASM object code with filename extension OBJ.
Borland’s TASM, Microsoft’s MASM etc.
4) Compiler translates high level language program to equivalent object
code.
Borland or Microsoft compiler for C, Turbo Pascal etc.
Program Development
5) Linker
functions
Tools
combines relocatable object program modules and library