Assembly Language Fundamentals: Basic Elements of Assembly Language Assembling, Linking, and Debugging
Assembly Language Fundamentals: Basic Elements of Assembly Language Assembling, Linking, and Debugging
Assembly Language Fundamentals: Basic Elements of Assembly Language Assembling, Linking, and Debugging
Chapter 3
Basic Elements of Assembly Language
1
Numeric Constants
Numeric constants are made of numerical
digits with, possibly, a sign and a suffix.
Ex:
-23(a negative integer, base 10 is default)
1011b (a binary number)
1011 (a decimal number)
0A7Ch (an hexadecimal number)
A7Ch (this is the name of a variable, an
hexadecimal number must start with a
decimal digit)
We shall not discuss floating point
2
numbers in this short course
Character and String Constants
3
Statements
The general format is:
[name] mnemonic [operands] [;comment]
Statements are either:
Instructions: executable statements -- translated
into machine instructions. Ex:
call MySub ;transfer of control
mov ax,5 ;data transfer
Directives:
tells the assembler how to generate
machine code and allocate storage. Ex:
count db 50 ;creates 1 byte of storage
;initialized to 50
4
Names
A name identifies either:
a label
a variable
a symbolic constant (name given to a constant)
a keyword (assembler-reserved word).
5
Names (cont.)
A variable is a symbolic name for a location in
memory that was allocated by a data allocation
directive. Ex:
count db 50 ; allocates 1 byte to variable count
6
Names (cont.)
7
Segment Directives
A program normally consist of a:
code segment that holds the executable code
data segment that holds the variables
stack segment that holds the stack (used for
calling and returning from procedures)
Directives .code, .data, and .stack mark
the beginning of the corresponding
segments
The .model small directive indicates that
the program uses 1 code segment and one
data segment (64KB/segment)
8
A Sample Program
The proc and endp directives denote the
beginning and end of a procedure
To return the control to DOS we use a
software interrupt
mov ah,4Ch
int 21h
The end directive marks the end of the
program and specify the pgm’s entry point
hello.asm
9
Standard Assembler Directives
proc, endp
.code, .data, .stack
.model
end
title
page
10
The Program Segment Prefix (PSP)
When DOS loads a program in memory, it prefaces
the program with a PSP of 256 bytes
the PSP contains info (about the pgm) used by DOS
DS (and ES) gets loaded by DOS with the segment
address of the PSP. To load DS with the segment
address of the data we do:
mov ax,@data
mov ds,ax ;cannot move a constant into ds
@data is the name of the data segment defined by
.data (and gets translated by the assembler into the
data’s segment number)
CS and SS are correctly loaded by DOS with the
segment number of code and stack respectively
11
Assembling, Linking, and Loading
Directives
Data Allocation
Directives
Symbolic Constants
Instructions
Data Transfer Instructions
Arithmetic Instructions
13
Simple Data Allocation Directives
The DB (define byte) directive allocates
storage for one or more byte values
[name] DB initval [,initval]
Each initializer can be any constant. Ex:
a db 10, 32, 41h ;allocate 3 bytes
b db 0Ah, 20h, ‘A’ ;same values as above
A question mark (?) in the initializer leaves
the initial value of the variable undefined. Ex:
c db ? ;the initial value for c is undefined
14
Simple Data Allocation Directives (cont.)
A string is stored as a sequence of characters. Ex:
aString db “ABCD”
The offset of a variable is the distance from the
beginning of the segment to the first byte of the
variable. Ex. If Var1 is at the beginning of the data
segment:
.data
Var1 db “ABC” offset cont
Var2 db “DEFG” 0000 ‘A’
0001 ‘B’
0002 ‘C’
0003 ‘D’
15
Simple Data Allocation Directives (cont.)
Define Word (DW) allocates a sequence of
words. Ex:
A dw 1234h, 5678h ; allocates 2 words
Intel’s x86 are little endian processors: the
lowest order byte (of a word or double word)
is always stored at the lowest address. Ex: if
the offset of variable A (above) is 0, we have:
offset:0 1 2 3
value: 34h 12h 78h 56h
16
Simple Data Allocation Directives (cont.)
Define Double Word (DD) allocates a
sequence of double words. Ex:
B dd 12345678h ; allocates one double word
If this variable has an offset of 0, we have:
offset:0 1 2 3
value: 78h 56h 34h 12h
17
Simple Data Allocation Directives (cont.)
If a value fits into a byte, it will be stored in
the lowest ordered one available. Ex:
V dw ‘A’
the value will be stored as:
offset: 0 1
value: 41h 00h
The value of a variable B will be the address
of a variable A whenever B’s initializer is the
name of variable A. Ex:
A dw ‘This is a string’
B dw A ; B points to A
18
Simple Data Allocation Directives (cont.)
19
Symbolic constants
We can use the equal-sign (=) directive to
give a name to a constant. Ex:
this is a (numeric) symbolic constant
The assembler does not allocate storage to
a symbolic constant (in contrast with data
allocation directives)
itmerely substitutes, at assembly time, the
value of the constant at each occurrence of the
symbolic constant
20
Symbolic constants (cont.)
In place of a constant, we can use a
constant expression involving the
standard operators used in HLLs: +, -, *, /
Ex: the following constant expression is
evaluated at assembly time and given a
name at assembly time:
A = (-3 * 8) + 2
A symbolic constant can be defined in
terms of another symbolic constant:
B = (A+2)/2
21
Symbolic constants (cont.)
To make use of it, a symbolic constant
must evaluate to a numerical value that
can fit into 16 bits or 32 bits (when the .386
directive is used...) Ex:
prod = 5 * 10 ; fits into 16 bits
string = ‘xy’ ; fits into 16 bits
string2 = ‘xyxy’ ; when using the .386
23
Assembly Language Components
Directives
Data Allocation
Directives
Symbolic Constants
Instructions
Data Transfer Instructions
Arithmetic Instructions
I/O Instructions
24
Data Transfer Instructions
The MOV instruction transfers the content of the
source operand to the destination operand
mov destination, source
Both operands must be of the same size.
An operand can be either direct or indirect
Direct operands (this chapter):
immediate (imm) (constant or constant expression)
register (reg)
memory variable (mem) (with displacement)
Indirect operands are used for indirect addressing
(next chapter)
25
Data Transfer Instructions (cont.)
Some restrictions on MOV:
imm cannot be the destination operand...
IP cannot be an operand
the source operand cannot be imm when the
destination is a segment register (segreg)
mov ds, @data ; illegal
mov ax, @data ; legal
mov ds, ax ; legal
source and destination cannot both be mem
(direct memory-to-memory data transfer is
forbidden!)
mov wordVar1,wordVar2; illegal
26
Data Transfer Instructions --
type checking
The type of an operand is given by its size
(byte, word, doubleword…)
both operands of MOV must be of the same
type
type check is done by the assembler
the type assigned to a mem operand is given
by its data allocation directive (DB, DW…)
the type assigned to a register is given by its
size
an imm source operand of MOV must fit into
the size of the destination operand
27
Data Transfer Instructions (cont.)
28
Data Transfer Instructions (cont.)
We can add a displacement to a memory operand to
access a memory value without a name Ex:
.data
arrB db 10h, 20h
arrW dw 1234h, 5678h
arrB+1 refers to the location one byte beyond the
beginning of arrB and arrW+2 refers to the location two
bytes beyond the beginning of arrW.
mov al,arrB ; AL = 10h
mov al,arrB+1 ; AL = 20h (mem with displacement)
mov ax,arrW+2 ; AX = 5678h
mov ax,arrW+1 ; AX = 7812h (little endian convention!!)
29
Data Transfer Instructions --
XCHG instruction
The XCHG instruction exchanges the content of
the source and destination operands:
XCHG destination, source
Only mem and reg operands are permitted (and
must be of the same size)
both operands cannot be mem (direct mem-to-
mem exchange is forbidden).
To exchange the content of word1 and word2, we
have to do:
mov ax,word1
xchg word2,ax
mov word1,ax
30
Assembly Language Components
Directives
Data Allocation
Directives
Symbolic Constants
Instructions
Data Transfer Instructions
Arithmetic Instructions
31
Simple arithmetic instructions
The ADD instruction adds the source to the
destination and stores the result in the
destination (source remains unchanged)
ADD destination,source
The SUB instruction subtracts the source from
the destination and stores the result in the
destination (source remains unchanged)
SUB destination,source
Both operands must be of the same size and
they cannot be both mem operands
Recall that to perform A - B the CPU in fact
performs A + NEG(B)
32
Simple arithmetic instructions (cont.)
ADD and SUB affect all the status flags according
to the result of the operation
ZF (zero flag) = 1 iff the result is zero
SF (sign flag) = 1 iff the msb of the result is one
OF (overflow flag) = 1 iff there is a signed overflow
CF (carry flag) = 1 iff there is an unsigned overflow
Signed overflow: when the operation generates an
out-of-range (erroneous) signed value
Unsigned overflow: when the operation generates
an out-of-range (erroneous) unsigned value
33
Simple arithmetic instructions (cont.)
34
Simple arithmetic instructions (cont.)
The INC (increment) and DEC (decrement)
instructions add 1 or subtracts 1 from a
single operand (mem or reg operand)
INC destination
DEC destination
They affect all status flags, except CF. Say
that initially we have, CF=OF=0
mov bh,0FFh ; CF=0, OF=0
inc bh ; bh=00h, CF=0, OF=0
mov bh,7Fh ; CF=0, OF=0
inc bh ; bh=80h, CF=0, OF=1
35
Simple I/O Instructions
We can perform simple I/O by calling DOS functions
with the INT 21h instruction
The I/O operation performed (on execution of INT 21h)
depends on the content of AH
When AH=2: the ASCII code contained in DL will be
displayed on the screen. Ex:
mov dl, ‘A’
int 21h ; displays ‘A’ on screen at cursor position
Also, just after displaying the character:
the cursor advance one position
AL is loaded with the ASCII code
When the ASCII code is a control code like 0Dh (CR),
or 0Ah (LF): the corresponding function is performed
36
Reading a single char from the keyboard
When we strike a key, a word is sent to the
keyboard buffer (in the BIOS data area)
low byte = ASCII code of the char
high byte = Scan Code of key (more in chap 5)
Fibonacci Numbers
1, 1, 2, 3, 5, 8, 13, 21, 34, 55, …
Write a program that generates and displays
the first 24 numbers in the Fibonacci series,
beginning with 1 and ending with 46,368.
39