Chapter Four
Chapter Four
Assembly
language
programming
Types of computer programs
3
Why Assembly Language
Programming
Educational value
Learning assembly language programming will help
understanding the operations of the microprocessor
Faster and shorter programs.
Compilers do not always generate optimum code.
Small controllers embedded in many
products
Have specialized functions,
Rely so heavily on input/output functionality,
HLLs inappropriate for these types of product
development.
E.g.Game developers, virus/untivirus etc.
4
How assembler works
Link
Library
Step 2: Step 3: Step 4:
Source assembler Object linker Executable OS loader
Output
File File File
Listing Map
Step 1: text editor File File
5
Examples of Assembler
MASM
Microsoft : Macro Assembler
TASM
Borland : Turbo Assembler
NASM
Library General Public License (LGPL) [Free]
: Netwide Assembler
etc, Flat Assembler, SpAssembler
Programmer‟s Model:
constant 96
constant expression 2+4
register ax
memory (data label) count
Constants and constant expressions are often called immediate
values
Comments
revision information
application-specific explanations
DUP Operator
Used to define arrays whose elements share
common initial value.
It has the form: repeat_count DUP (value)
E.g. Numbers DB 100 DUP(0)
Allocates an array of 100 bytes, each initialized to 0.
Names DW 200 DUP(?)
Allocates an array of 200 uninitialized words.
Two equivalent definitions
Line DB 5, 4, 3 DUP(2, 3 DUP(0), 1)
Line DB 5, 4, 2, 0, 0, 0, 1, 2, 0, 0, 0, 1, 2, 0, 0, 0, 1
Summary of Assembler Directives
Summary of Assembler Directives
Variable declaration
A data definition statement sets aside storage in memory
for a variable.
Each variable has a type and assigned a memory
address.
May optionally assign a name (label) to the data
Syntax:
[name] directive initializer [,initializer] . . .
value1 BYTE 10
K DB 5, 3, -1 allocates 3 bytes 03
FF
Word Variables
Assembler directive format defining a word
variable
name DW initial value I 04
00
I DW 4
J FE
FF
J DW -2
K BC
1A
K DW 1ABCH
L 31
30
L DW “01”
Defining Strings
Examples:
str1 BYTE "Enter your name",0
str2 BYTE 'Error: halting program',0
str3 BYTE 'A','E','I','O','U'
greeting BYTE "Welcome to the Encryption Demo program "
Defining Strings
Idea: Define all strings used by your program in the same area of
the data segment.
26
Program‟s code, data, and stack are loaded into different
memory segments, namely code segment, data segment
and stack segment.
Data Segment
contains variable definitions
declared by .DATA
Stack segment
used to store the stack
declared by .STACK size
default stack size is 1Kbyte.
Code segment
contains program‟s instructions
declared by .CODE
.MODEL
Provides short cuts in defining segments
It initialize memory model before defining any segment
Input and Output in AL
CPU communicates with peripherals through I/O
registers called I/O ports.
Two instructions access I/O ports directly: IN and OUT.
Used when fast I/O is essential, e.g. games.
Most programs do not use IN/OUT instructions
port addresses vary among computer models
much easier to program I/O with service routines provided by
manufacturer
Two categories of I/O service routines
Basic input/output system (BIOS) routines
Disk operating system (DOS) routines
DOS and BIOS routines invoked by INT (interrupt)
instruction.
An interrupt is the method of processing the microprocessor by
peripheral device.
An interrupt is used to cause a temporary halt in the execution of
program.
Microprocessor responds to the interrupt with an interrupt service
routine, which is short program or subroutine that instructs the
microprocessor on how to handle the interrupt.
Software Layers
System Hardware
Non-standard interface
BIOS
Standard interface
Operating System
Standard interface
Application Program
DOS and BIOS Interrupts
DOS and BIOS interrupts are used to perform
some very useful functions such as:
displaying data to the monitor
reading data from keyboard, etc . BIOS
supplied by computer manufacturer and
resides in ROM
They are used by identifying the interrupt option
type, which is the value stored in register AH
providing, whatever extra information that the
specific option requires.
INT 21H used to invoke a large number of
DOS function.
Type of called function specified by putting
a number in AH register.
AH=1 single-key input with echo
AH=2 single-character output
AH=9 character string output
AH=8 single-key input without echo
AH=0Ah character string input
Hello world program in c++
#include<iostream>
using namespace std
int main()
{
int x,y;
cout<<“hello world\n”;
return 0;
}
35
Hello world in assembly
language
.model small
.stack 100h
.data
message db 'Hello World', 13, 10, '$'
.code
start:
mov ax, @data
mov ds, ax
mov dx, offset message ; copy address of message to dx
mov ah, 9h ; string output
int 21h ; display string
mov ax, 4c00h
int 21h
end start
Program Structure: An
Example
TITLE PRGM1
.MODEL SMALL
.STACK 100H
.DATA
A DW 2
B DW 5
SUM DW ?
.CODE
MAIN PROC
; initialize DS
MOV AX, @DATA
MOV DS, AX
Program Structure: An
Example
; add the numbers
MOV AX, A
ADD AX, B
MOV SUM, AX
; exit to DOS
MOV AX, 4C00H
INT 21H
MAIN ENDP
END MAIN
Input and output
43
Displaying a single character
44
Outputting String a String
Input: AH=9, DX= offset address of a string.
String must end with a „$‟ character.
To display the message Hello!
MSG DB “Hello!$”
MOV AH, 9
MOV DX, offset MSG
INT 21H
OFFSET operator returns the address of a
variable
The instruction LEA (load effective address)
loads destination with address of source
LEA DX, MSG
Our first hello world program
.model small
.stack 100h
.data
message db 'Hello World', 13, 10, '$'
.code
start:
mov ax, @data
mov ds, ax
mov dx, offset message ; copy address of message to dx
mov ah, 9h ; string output
int 21h ; display string
mov ax, 4c00h
int 21h
end start
46
Inputting a String
Input: AH=10, DX= offset address of a buffer to store
read string.
First byte of buffer should contain maximum string size+1
Second byte of buffer reserved for storing size of read
string.
To read a Name of maximum size of 20B display it
Name DB 21,0,22 dup(“$”)
MOV AH, 10
LEA DX, Name
INT 21H
MOV AH, 9
LEA DX, Name+2
INT 21H
A Case Conversion Program
.DATA
String1 DB “Hello”
String2 DB 5 dup(?)
.CODE
MOV AX, @DATA
MOV DS, AX
MOV ES, AX
CLD
MOV CX, 5
LEA SI, String1
LEA DI, String2
REP MOVSB
Reversing a String
String DB “COE-205”
.DATA
String1 DB “Hello”
String2 DB 5 dup(?)
.CODE
MOV AX, @DATA
MOV DS, AX
MOV ES, AX
CLD
MOV CX, 5
LEA SI, String1
LEA DI, String2
REP MOVSB
Time Delay in 8086
TIME delay using NOP operation
Delay using counter
Counting can create time delays. Since the execution
times of the instructions used in a counting routine are
known, the initial value of the counter, required to get
specific time delay can be determined.
Delay using counter
Example
Time delay when delay time is known
Conti…..
Time delay using nested loop
Delay in 8086
microprocessors
Continued
66
Procedure
RET
Name ENDP
Procedure type
NEAR (statement that calls procedure in same segment
with procedure)
FAR (statement that calls procedure in different segment)
Default type is near
Procedure Invocation
CALL Name
Procedures – Cont.
Executing a CALL instruction causes
Save return address on the stack
Near procedure: PUSH IP
Far procedure: PUSH CS; PUSH IP
IP gets the offset address of the first instruction of the
procedure
CS gets new segment number if procedure is far
• Executing a RET instruction causes
Transfer control back to calling procedure
Near procedure: POP IP
Far procedure: POP IP; POP CS
RET n
IP [SP+1:SP]
SP SP + 2 + n
70
continued
71
continued
72
MACRO definition directive
75
76