Lecture #02, Microprocessor Lab

Download as pdf or txt
Download as pdf or txt
You are on page 1of 13

EEE-3104: Microprocessor and Interfacing Lab

Dept. of Electrical and Electronic Engineering


University of Dhaka

Prof. Sazzad M.S. Imran, PhD


sazzadmsi.webnode.com
&
Dr. Sakhawat Hussain
Assembly Language Programming
Procedures:
Repeated group of instructions in program can be organized as subprograms.
Procedures are independent.

Linkage between calling procedure and called procedure need to be established.


Assembler directives PROC and ENDP are used to define a procedure.
General form of the procedure-

ProcedureName PROC [NEAR/FAR]


; save all registers that get modified using PUSH

; procedure code with RET as procedure terminator

; restore all registers that are saved in stack
; return to calling program using RET instruction
ProcedureName ENDP
Assembly Language Programming
Procedures:
CALL instruction is used to transfer control to subprogram or procedure.
General form-
CALL ProcedureName

Instruction RET is used to transfer control from procedure back to calling procedure/
main program instruction following CALL. Example-

a) HEX2ASC PROC NEAR



; procedure code

RET
HEX2ASC ENDP

Call to procedure HEX2ASC is performed using statement-


CALL HEX2ASC
Assembly Language Programming
Procedures:
b) MYCODE SEGMENT
IFACT PROC FAR

; procedure code

RET
IFACT ENDP
MYCODE ENDS

Call to procedure IFACT is made as far call as-


CALL FAR PTR IFACT
Assembly Language Programming
Programming with Macros:
Small sequence of codes of same pattern are repeated frequently at different places.
Block of code is associated with macro name.
Assembler directives MACRO and ENDM are used to define macro.
General form-

MacroName MACRO [Argument1, …, ArgumentN]



; Body of macro: program text to be declared as macro

ENDM

Example-
PrintString MACRO msg
mov ah, 09h ; AH = display string function
mov dx, offset msg ; DX = offset of data item msg
int 21h ; call DOS service
ENDM
Assembly Language Programming
Input-Output Structure:
System calls are invoked using instruction INT.
Each interrupt has associated 4 bytes vector in 8086.
DOS services are accessed through interrupt 21H (INT 21H).
Example-

_DATA segment
msg1 db ‘Welcome to System Call through Software Interrupts$’
_DATA ends

; Display String through DOS call
mov ah, 09h ; display string function
mov dx, offset msg1 ; DX = offset address of msg in DS
int 21h ; call DOS service

Alternately, call to macro PrintString can be performed using statement-


PrintString msg1
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.

5) Linker  combines relocatable object program modules and library functions


produce single executable file.
TLINK, LINK etc.
Program Development Tools
6) Loader  loads executable file into main memory for execution.
MS-DOS shell COMMAND.COM.

7) Debugger  allows execution of program in single step mode.


process of locating and correcting error.
Borland’s TD, Microsoft’s CV etc.

8) EXE to COM Converter  converts executable file (EXE) to command file (COM).
Microsoft’s EXE2BIN, Borland’s Turbo linker (TLINK/T) etc.

9) Library Builder  combines all program modules into single file.


Microsoft’s LIB, Borland’s TLIB etc.
Program Development
Process
Microprocessor Lab Execution Procedure
How to run MASM programming through DOSBox in Windows 10?
STEP-1: Download and install DOSBox on your PC.
STEP-2: Download and copy MASM folder on your PC.
STEP-3: Now open DOSBox and type-
mount d d:\MASM and press enter.
d: and press enter.
STEP-4: Type edit filename.asm and press enter.
Write your program and save the file.
[Alternately, use Notepad to edit program]
STEP-5: Now on DOSBox window type-
masm filename.asm; and press enter.
link filename.obj; and press enter.
filename.exe and press enter.

Emu8086  emulator to edit, assemble, debug and execute ALP.


Problem: Read a single digit number from keyboard and display the number on the screen.
(without macro, result initialized with 0)
Programming Code:
_DATA segment mov ah, 01h
cr equ 0dh int 21h
lf equ 0ah
msg1 db 'Enter a single digit: ', '$' mov si, offset result
msg2 db cr, lf, 'You have entered: $' mov [si], al
result db 3 dup(0) inc si
_DATA ends mov al, ‘$’
mov [si], al
_CODE segment
assume cs: _CODE, ds: _DATA mov ah, 09h
start: mov ax, _DATA mov dx, offset msg2
mov ds, ax int 21h mov ah, 4ch
mov al, 00h
mov ah, 09h mov ah, 09h int 21h
mov dx, offset msg1 mov dx, offset result _CODE ends
int 21h int 21h end start
Problem: Read a single digit number from keyboard and display the number on the screen.
(without macro, result initialized with ‘$’)
Programming Code:
_DATA segment
cr equ 0dh
lf equ 0ah
msg1 db 'Enter a single digit: ', '$' mov ah, 01h
msg2 db cr, lf, 'You have entered: $' int 21h
result db 3 dup(‘$’)
_DATA ends mov si, offset result
mov [si], al
_CODE segment
assume cs: _CODE, ds: _DATA mov ah, 09h
start: mov ax, _DATA mov dx, offset msg2
mov ds, ax int 21h mov ah, 4ch
mov al, 00h
mov ah, 09h mov ah, 09h int 21h
mov dx, offset msg1 mov dx, offset result _CODE ends
int 21h int 21h end start
Problem: Read a single digit number from keyboard and display the number on the screen.
(with macro, result initialized with ‘$’)
Programming Code:
printstring macro msg
mov ah, 09h
mov dx, offset msg
int 21h start: mov ax, _DATA
endm mov ds, ax

_DATA segment printstring msg1


cr equ 0dh
lf equ 0ah mov ah, 01h
msg1 db 'Enter a single digit: ', '$' int 21h
msg2 db cr, lf, 'You have entered: $'
result db 3 dup(‘$’) mov si, offset result mov ah, 4ch
_DATA ends mov [si], al mov al, 00h
int 21h
_CODE segment printstring msg2 _CODE ends
assume cs: _CODE, ds: _DATA printstring result end start

You might also like