Chapter 4
Chapter 4
Chapter 4
2
Assembly Language Syntax
Generally, not case sensitive, but use upper case to
differentiate code from the rest of the text.
Each statement is either an instruction or an assembler
directive
the assembler translates into machine code.
instructs the assembler to perform some specific task,
such as allocating memory space for a variable.
Both instructions and directives have up to four fields:
3
Assembly Language Syntax (2)
An example of an instruction is:
4
Assembly Language Syntax (3)
An example of an assembler directive is
Operation
Name
Directive creates
a procedure called
MAIN
5
Assembly Language Syntax (4)
Name Field
Use for instruction labels, procedure names, and
variable names.
Translates names into memory addresses.
1 to 31 characters long and may consist of letters, digits,
and the special characters ? . @ _ $ %.
If a period is used, it must be the first character.
Not begin with a digit.
6
Assembly Language Syntax (5)
Examples of legal names
COUNTER1
?character
SUM_OF_DIGITS
$1000
DONE?
.TEST
7
Assembly Language Syntax (6)
Operation Field
Contains a symbolic operation code (opcode).
Often describe the operation's function; for example,
MOV, ADD, SUB.
The operation field contains a pseudo-operation code
(pseudo-op).
Not translated into machine code.
PROC pseudo-op is used to create a procedure.
8
Assembly Language Syntax (7)
Operand Field
Specifies the data that are to be acted on by the
operation.
Instruction may have zero, one, or two operands.
The first operand is the destination operand.
register or memory location where the result is stored
The second operand is the source operand.
9
Program Data
Express data as binary, decimal, or hex numbers, and
even as characters.
Numbers:
Number Type
11011 decimal
11011B binary
64223 decimal
-21843D decimal
1, 234 illegal-contains a nondig1t character
1B4BH hex
1B4D illegal hex number doesn't end in "H"
FFFFH illegal hex number-doesn't begin with a decimal
digit
0FFFFH hex 10
Program Data (2)
Characters
must be enclosed in single or double quotes: for
example, "A" or 'hello'.
translated into their ASCII codes by the assembler.
no difference between using "A" and 41h in a program.
11
Variables
Each variable has a data type and is assigned a memory
address by the program.
Data-defining pseudo-ops and their meanings as
below:
12
Variables (2)
Byte Variables
Uninitialized
Word Variables
13
Variables (3)
Arrays
a sequence of memory bytes or words.
14
Variables (4)
how ?
15
A Few Basic Instructions
More than 100 instruction sets
There are some more instructions for more advanced
processors.
Only six of the most useful instructions transferring
data and doing arithmetic.
Uses either word or byte operands.
MOV and XCHG
Use to transfer data between registers, between a
register and a memory location, or to move a number
directly into a register or memory location.
16
A Few Basic Instructions (2)
The syntax is:
MOV AX, BX
17
A Few Basic Instructions (3)
Legal Combinations of Operands for MOV
18
A Few Basic Instructions (4)
XCHG operation is used to exchange the contents of two
registers or a register and a memory location.
The syntax is:
19
A Few Basic Instructions (5)
Legal Combinations of Operands for XCHG
20
Restrictions on MOV and XCHG
21
A Few Basic Instructions (6)
ADD, SUB, INC, and DEC
ADD and SUB instructions are used to add or subtract
Contents of two registers,
A register and a memory location, or
To add (subtract) a number to (from) a register or memory
location.
22
A Few Basic Instructions (6)
SUB AX, DX
INC is used to add 1 to the contents of a register or
memory location and DEC subtracts 1 from a register
or memory location.
23
A Few Basic Instructions (6)
The syntax is
24
A Few Basic Instructions (7)
NEG is used to negate the contents of the destination.
NEG BX
25
Translation of High level Language
to Assembly Language
Only MOV, ADD, SUB, INC, DEC, and NEG are used
to translate assembly language.
26
Translation of High level Language
to Assembly Language (2)
For instance, statement, A = 5 – A
27
Translation of High level Language
to Assembly Language (3)
Example shows how to do multiplication by a
constant.
28
Program Structure
The code, data, and stack are structured as program
segments.
Program segment is translated into a memory segment
by the assembler.
Microsoft Macro Assembler (MASM).
Memory Models
Size of code and data a program can have is determined
by specifying a memory model (.Model directive)
Frequently used memory models are SMALL, MEDIUM,
COMPACT, and LARGE.
29
Program Structure (2)
30
Program Structure (3)
Data segment contains all the variable definition.
.DATA
Stack segment declaration is to set aside a block of
memory (the stack area) to store the stack.
.STACK size
Code segment contains a program's instructions. The
declaration syntax is:
.CODE name
31
Program Structure (4)
32
Input and Output Instructions
There are two instructions, IN and OUT, that access
the ports directly.
Use when fast I/O essential; for example, in a game
program.
the Basic Input/Output System (BIOS) routines and the
DOS routines.
The INT Instruction
To invoke a DOS or BIOS routine
INT interrupt_number
33
Input and Output Instructions (2)
INT 21h
Invoke a large number of DOS functions
Function is requested by placing a function number in
the AH register and invoking INT 21h
34
Input and Output Instructions (3)
36
First Program
The first program will read a character from the
keyboard and display it at the beginning of the next
line.
start by displaying a question mark:
37
First Program (2)
to display the character on the next line, the character
must be saved in another register.
38
Creating and Running a Program
1. Use a text editor or word processor to create a source
program file.
2. Use an assembler to create a machine language
object file.
3. Use the LINK program (see description later) to link
one or more object files to create a run file.
4. Execute the run file.
39
Programming Steps
40
Displaying Input Character at the
Beginning of the Next Line
41
Displaying a String
INT 21h function that can be used to display a
character string:
MSG DB ‘Hello! $’
42
Displaying a String (2)
The LEA Instruction
Puts a copy of the source offset address into the
destination.
LEA destination, source
LEA DX MSG
Offset address of the variable MSG into DX
43
Displaying a String (3)
44
A Case Conversion Program
45