Chapter 4.1 Introduction To Assembly Language
Chapter 4.1 Introduction To Assembly Language
Chapter 4.1 Introduction To Assembly Language
CHAPTER 4.1
ASSEMBLY LANGUAGE PROGRAMMING OF 8086
PROCESSOR
MACHINE/ASSEMBLY LANGUAGE
Machine Language:
Setof fundamental instructions the machine can
execute
Expressed as a pattern of 1’s and 0’s
Assembly Language:
Alphanumericequivalent of machine language
Mnemonics more human-oriented than 1’s and 0’s
Assembler:
Computer program that transliterates (one-to-one
mapping) assembly to machine language
Computer’s native language is machine language
WHY ASSEMBLY LANGUAGE
PROGRAMMING
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,
Game developers
3
EXAMPLE ASSEMBLER
MASM
Microsoft : Macro Assembler
TASM
Borland : Turbo Assembler
NASM
Library
General Public License (LGPL) [Free] :
Netwide Assembler
Flat Assembler, SpAssembler
Etc.
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
5
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
ASSEMBLY INSTRUCTIONS SYNTAX
An instruction is a statement that becomes executable
when a program is assembled.
Assembled into machine code by assembler
An instruction contains:
Label (optional)
Mnemonic (required)
Operand (depends on the instruction)
Comment (optional)
Basic syntax
[label:] mnemonic [operands] [ ; comment]
Eg
Start: mov ax,bx ; this instru…
LABELS
Act as place markers
marks the address (offset) of code and data
Follow identifier rules
Data label
mustbe unique
example: myArray (not followed by colon)
count DWORD 100
Code label
target
of jump and loop instructions
example: L1: (followed by colon)
target:
mov ax, bx
…
jmp target
9
MNEMONICS AND OPERANDS
Instruction Mnemonics
memory aid
examples: MOV, ADD, SUB, MUL, INC, DEC
Operands
constant 96
constant expression 2 + 4
register ax
memory (data label) count
11
COMMENTS
Comments are good!
explain the program's purpose
when it was written, and by whom
revision information
tricky coding techniques
application-specific explanations
Single-line comments
begin with semicolon (;)
ELEMENTS OF ASSEMBLY
LANGUAGE PROGRAM
.model small
.stack 100h
.data
message db 'Hello World', 13, 10, '$'
.code
main proc
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
main endp
end main
DIRECTIVES
Commands that are recognized and acted
upon by the assembler
Not part of the instruction set
Used to declare code, data areas, select memory
model, declare procedures, etc.
not case sensitive
Different assemblers have different directives
NASM not the same as MASM, for example
DIRECTIVES: NAMED CONSTANTS
EQU pseudo-op used to assign a name to
constant.
Makes assembly language easier to
understand.
No memory allocated for EQU names.
LF EQU 0AH
MOV DL, 0AH
MOV DL, LF
PROMPT EQU “Type your name”
MSG DB “Type your name”
MDG DB PROMPT
DUP OPERATOR
Used to define arrays whose elements share
common initial value.
It has the form: repeat_count DUP (value)
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
USING THE DUP OPERATOR
Use DUP to allocate (create space for) an array
or string. Syntax: counter DUP ( argument )
Counter and argument must be constants or
constant expressions
var1 BYTE 20 DUP(0) ; 20 bytes, all equal to zero
var2 BYTE 20 DUP(?) ; 20 bytes, uninitialized
var3 BYTE 4 DUP("STACK") ; 20 bytes: "STACKSTACKSTACKSTACK"
var4 BYTE 10,3 DUP(0),20 ; 5 bytes
var4 10
0
0
0
20
PTR OPERATOR
Used to override declared type of an address
expression.
Examples:
MOV [BX], 1 illegal, there is
ambiguity
MOV Bye PTR [BX], 1 legal
MOV WORD PTR [BX], 1 legal
Let j be defined as follows
j DW 10
MOV AL, j illegal
MOV AL, Byte PTR J legal
IDENTIFIERS
Identifiers
Programmer-chosen name to identify a variable, constant,
procedure, or code label
1-247 characters, including digits
not case sensitive
first character must be a letter, _, @, ?, or $
Subsequent characters may also be digits
Cannot be the same as a reserved word
@ is used by assembler as a prefix for predefined symbols,
so avoid it identifiers
Examples
Var1,
Count, $first, _main, MAX, open_file, myFile, xVal,
_12345
RESERVED WORDS
Reserved words cannot be used as identifiers
Instruction mnemonics
MOV, ADD, MUL,, …
Register names
Directives – tells MASM how to assemble programs
type attributes – provides size and usage information
BYTE, WORD
Operators – used in constant expressions
predefined symbols – @data
DATA DEFINITION STATEMENT
A data definition statement sets aside storage in memory for a
variable.
May optionally assign a name (label) to the data
Syntax:
[name] directive initializer [,initializer] . . .
value1 BYTE 10
Examples:
Idea: Define all strings used by your program in the same area of
the data segment.
30
INPUT AND OUTPUT
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
portaddresses 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.
SYSTEM BIOS
A set of programs always present in system
BIOS routines most primitive in a computer
Talksdirectly to system hardware
Hardware specific - must know exact port
address and control bit configuration for I/O
devices
BIOS supplied by computer manufacturer and
resides in ROM
Provides services to O.S. or application
Enables O.S. to be written to a standard
interface
SOFTWARE LAYERS
System Hardware
Non-standard interface
BIOS
Standard interface
Operating System
Standard interface
Application Program
INPUT/OUTPUT - CONT.
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
DISPLAYING A SINGLE
CHARACTER
35
SINGLE-CHARACTER OUTPUT
Input: AH=2, DL= ASCII code of character to be
output
Output: AL=ASCII code of character
To display a character
MOV AH, 2
MOV DL, ‘?’ ; displaying character ‘?’
INT 21H
To read a character and display it
MOV AH, 1
INT 21H
MOV AH, 2
MOV DL, AL
INT 21H
37
SINGLE-KEY INPUT
Input: AH=1
Output: AL= ASCII code if character key is
pressed, otherwise 0.
To input character with echo:
MOV AH, 1
INT 21H ; read character will be in AL register
To input a character without echo:
MOV AH, 8
INT 21H ; read character will be in AL register
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
39
DISPLAYING 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
A CASE CONVERSION PROGRAM
Prompt the user to enter a lowercase letter, and
on next line displays another message with letter
in uppercase.
Enter a lowercase letter: a
In upper case it is: A
.model small
.DATA
CR EQU 0DH
LF EQU 0AH
MSG1 DB "Enter a lower case letter: $"
MSG2 DB CR, LF, "In upper case it is: "
Char DB ?, '$'
A CASE CONVERSION PROGRAM -
CONT.
.CODE
; ; initialize data segment
LEA DX, MSG1 ; display first message
MOV AH, 9
INT 21H
MOV AH, 1 ; read character
INT 21H
SUB AL, 20H; convert it to upper case
MOV CHAR, AL ; and store it
LEA DX, MSG2 ; display second message and
MOV AH, 9 ; uppercase letter
INT 21H
; return to DOS
COPYING A STRING TO
ANOTHER
.model small
.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
End!
44
ASSIGNMENT
Microcomputer and Interfacing assignment two(10%)
Dead line: It will be noticed in the future
Implement the following program using Emu886 and
demonstrate it in the lab in peer groups
1. Write an assembly language that prompt and accept a
string from a keyboard of maximum size of 20 character,
reverse the string and display the reversed string using
appropriate int 21h dos interrupt service routine.
2. Write an assembly language that accept a maximum
number of 10 one digit numbers and do the following things
A. Find the maximum and average numbers
B. Sort it in ascending and descending order using bubble sort whose
pseudo code is shown bellow
45
BUBBLE SORT PSEUDO CODE
46