Assembly Language for Intel-Based
Computers, 5th Edition
Kip Irvine
Chapter 1: Basic Concepts
(c) Pearson Education, 2006-2007. All rights reserved. You may modify and copy this slide show for your personal use,
or for use in the classroom, as long as this copyright statement, the author's name, and the title are not changed.
Welcome to Assembly Language
What are Assemblers and Linkers?
An assembler is a program that converts source code program
from assembly language into machine language (such as listing
files generated by an assembler).
A linker is program that combines individual files created by an
assembler into a single executable program.
Irvine, Kip R. Assembly Language for Intel-Based Computers 5/e, 2007.
Web site
Examples
Welcome to Assembly Language
What Hardware and Software Do I Need?
For hardware you need computer with Intel386,Intel486, Pentium,
or compatible processor.
For Software you need
1. Editor : use text editor to write an assembly language source file
2. Assembler : MASM version 6.15
Irvine, Kip R. Assembly Language for Intel-Based Computers 5/e, 2007.
Web site
Examples
Welcome to Assembly Language
How Does Assembly Language (AL) Relate to Machine Language
Machine Language: is a numeric language specifically
understood by the computers processor (the CPU).
Assembly Language: consists of statements written with
short mnemonics such as ADD, MOV and SUB. Assembly
language has a one-to-one relationship with machine
language.
Each
assembly
language
instruction
corresponds to single machine-language instruction.
Irvine, Kip R. Assembly Language for Intel-Based Computers 5/e, 2007.
Web site
Examples
Binary Numbers
Digits are 1 and 0
1 = true
0 = false
MSB most significant bit
LSB least significant bit
MSB
Bit numbering:
Irvine, Kip R. Assembly Language for Intel-Based Computers 5/e, 2007.
LSB
1011001010011100
15
Web site
Examples
Binary Numbers
Each digit (bit) is either 1 or 0
Each bit represents a power of 2:
Every binary
number is a
sum of powers
of 2
Irvine, Kip R. Assembly Language for Intel-Based Computers 5/e, 2007.
Web site
Examples
Translating Binary to Decimal
Weighted positional notation shows how to calculate the
decimal value of each binary bit:
dec = (Dn-1 2n-1) (Dn-2 2n-2) ... (D1 21) (D0 20)
D = binary digit
binary 00001001 = decimal 9:
(1 23) + (1 20) = 9
Irvine, Kip R. Assembly Language for Intel-Based Computers 5/e, 2007.
Web site
Examples
Translating Unsigned Decimal to Binary
Repeatedly divide the decimal integer by 2. Each
remainder is a binary digit in the translated value:
37 = 100101
Irvine, Kip R. Assembly Language for Intel-Based Computers 5/e, 2007.
Web site
Examples
Binary Addition
Starting with the LSB, add each pair of digits, include
the carry if present.
Irvine, Kip R. Assembly Language for Intel-Based Computers 5/e, 2007.
Web site
Examples
Integer Storage Sizes
Standard sizes:
Note byte = 8 bits
What is the largest unsigned integer that may be stored in 20 bits?
Irvine, Kip R. Assembly Language for Intel-Based Computers 5/e, 2007.
Web site
Examples
10
Hexadecimal Integers
Binary values are represented in hexadecimal.
Irvine, Kip R. Assembly Language for Intel-Based Computers 5/e, 2007.
Web site
Examples
11
Translating Binary to Hexadecimal
Each hexadecimal digit corresponds to 4 binary bits.
Example: Translate the binary integer
000101101010011110010100 to hexadecimal:
And this equal to 16A794
Irvine, Kip R. Assembly Language for Intel-Based Computers 5/e, 2007.
Web site
Examples
12
Converting Hexadecimal to Decimal
Multiply each digit by its corresponding power of 16:
dec=(D3163)+(D2162)+(D1161)+(D0160)
Hex1234equals(1163)+(2162)+(3161)+(4160),or
decimal4,660.
Hex3BA4equals(3163)+(11162)+(10161)+(4160),or
decimal15,268.
Irvine, Kip R. Assembly Language for Intel-Based Computers 5/e, 2007.
Web site
Examples
13
Converting Decimal to Hexadecimal
decimal 422 = 1A6 hexadecimal
Irvine, Kip R. Assembly Language for Intel-Based Computers 5/e, 2007.
Web site
Examples
14
Hexadecimal Addition
Divide the sum of two digits by the number base (16). The quotient becomes the
carry value, and the remainder is the sum digit.
36
42
78
28
45
6D
28
58
80
6A
4B
B5
21 / 16 = 1, rem 5
Important skill: Programmers frequently add and subtract the
addresses of variables and instructions.
Irvine, Kip R. Assembly Language for Intel-Based Computers 5/e, 2007.
Web site
Examples
15
Hexadecimal Subtraction
When a borrow is required from the digit to the left, add 16
(decimal) to the current digit's value:
16 + 5 = 21
C6
A2
24
75
47
2E
Practice: The address of var1 is 00400020. The address of the next
variable after var1 is 0040006A. How many bytes are used by var1?
Irvine, Kip R. Assembly Language for Intel-Based Computers 5/e, 2007.
Web site
Examples
16
Signed Integers
The highest bit (MSB) indicates the sign. 1 = negative,
0 = positive
If the highest digit of a hexadecimal integer is > 7, the value is
negative. Examples: 8A, C5, A2, 9D
Irvine, Kip R. Assembly Language for Intel-Based Computers 5/e, 2007.
Web site
Examples
17
Forming the Two's Complement
Negative numbers are stored in two's complement
notation
11111111 is the twos complement representation
of -1
Irvine, Kip R. Assembly Language for Intel-Based Computers 5/e, 2007.
Web site
Examples
18
Binary Subtraction
When subtracting A B, convert B to its two's
complement
Add A to (B)
00001100
00000011
00001100
11111101
00001001
Practice: Subtract 0101 from 1001.
Irvine, Kip R. Assembly Language for Intel-Based Computers 5/e, 2007.
Web site
Examples
19
Two's Complement of Hexadecimal
Form the two's complement of a hexadecimal
integer
1. Subtract each digit from F
2. Add 1 to the result
Ex. Twos comp of
6A3D = 95C2 + 1 = 95C3
95C3 = 6A3C + 1 = 6A3D
21F0 = DE0F + 1 = DE10
DE10 = 21EF + 1 = 21F0
Irvine, Kip R. Assembly Language for Intel-Based Computers 5/e, 2007.
Web site
Examples
20
Convert Signed Binary to Decimal
If the highest bit is 1 then the
number is stored in twoscomplement so take the twos
complement a second time to
get its positive equivalent. Then
convert this new number to
decimal as if it were unsigned
binary integer.
If the highest bit is 0 convert it
to decimal as if it were unsigned
binary integer.
Ex. Signed binary 11110000
represent -16 because it has 1
in the highest bit, indicating that
it is a negative integer.
Irvine, Kip R. Assembly Language for Intel-Based Computers 5/e, 2007.
Twos of 11110000
flip all bit 00001111
Add 1
00001111 +
1
----------------00010000
Convert to decimal 16
Because the original integer
(11110000) was negative, we
infer its decimal value was -16.
Web site
Examples
21
Convert Signed Decimal to Binary
1. Convert the absolute value
of the decimal integer to
binary.
2. If the original decimal
integer was negative, form
the twos complement of the
binary number from the
previous step.
Irvine, Kip R. Assembly Language for Intel-Based Computers 5/e, 2007.
Ex -43 decimal to binary
1. The binary representation of
unsigned 43 is 00101011
2. Take the twos comp
because the original value
was negative
11010101 and this is the
representation of -43
Web site
Examples
22
Convert Signed Decimal to Hexadecimal
1. Convert the absolute value of the decimal integer to
hexadecimal.
2. If the decimal integer was negative, form the twos
complement of the Hexadecimal number from the previous
step.
Irvine, Kip R. Assembly Language for Intel-Based Computers 5/e, 2007.
Web site
Examples
23
Convert signed hexadecimal to decimal
1. If the hexadecimal integer is negative, form its twos
complement ; otherwise , retain the integer as is.
2. Using the integer from the previous step , convert it
to decimal. If the original value was negative, attach
a minus sign to the beginning of the decimal integer.
Irvine, Kip R. Assembly Language for Intel-Based Computers 5/e, 2007.
Web site
Examples
24
Ranges of Signed Integers
The highest bit is reserved for the sign. This limits the range:
A signed integer of n bits uses only n-1 bits to represent the
numbers magnitude.
Practice: What is the largest positive value that may be stored in 20 bits?
Irvine, Kip R. Assembly Language for Intel-Based Computers 5/e, 2007.
Web site
Examples
25
Character Storage
Character sets
Standard ASCII (0 127)
Extended ASCII (0 255)
ANSI (0 255)
Unicode (0 65,535)
Null-terminated String
Array of characters followed by a null byte
Using the ASCII table
Irvine, Kip R. Assembly Language for Intel-Based Computers 5/e, 2007.
Web site
Examples
26
Character Storage
Using the ASCII table
Irvine, Kip R. Assembly Language for Intel-Based Computers 5/e, 2007.
Web site
Examples
27
General-Purpose Registers
Registers are high-speed storage locations directly
inside the CPU, designed to be accessed at much
higher speed than conventional memory.
The general-purpose registers are primarily used for
arithmetic and data movement.
32-bit General-Purpose Registers
EAX
EBP
EBX
ESP
ECX
ESI
EDX
EDI
16-bit Segment Registers
EFLAGS
EIP
Irvine, Kip R. Assembly Language for Intel-Based Computers 5/e, 2007.
CS
ES
SS
FS
DS
GS
Web site
Examples
28
Accessing Parts of Registers
Use 8-bit name, 16-bit name, or 32-bit name
Applies to EAX, EBX, ECX, and EDX
Irvine, Kip R. Assembly Language for Intel-Based Computers 5/e, 2007.
Web site
Examples
29
Index and Base Registers
Some registers have only a 16-bit name for their
:lower half
Irvine, Kip R. Assembly Language for Intel-Based Computers 5/e, 2007.
Web site
Examples
30
Some Specialized Register Uses (1 of 2)
General-Purpose
EAX accumulator
ECX loop counter
ESP stack pointer
ESI, EDI index registers
Segment
CS code segment
DS data segment
SS stack segment
ES, FS, GS - additional segments
Irvine, Kip R. Assembly Language for Intel-Based Computers 5/e, 2007.
Web site
Examples
31
Some Specialized Register Uses (2 of 2)
EIP instruction pointer (contains the address of the next
instruction to be executed)
EFLAGS
The EFLAGS register consists of individual binary bits that
.reflect the outcome of some CPU operation
Some instructions test and manipulate individual processor
.flags
Each flag is a single binary bit
Irvine, Kip R. Assembly Language for Intel-Based Computers 5/e, 2007.
Web site
Examples
32
Status Flags
Carry Flag
is set when the result of an unsigned arithmetic operation
.is too large to fit into the destination
Overflow Flag
signed arithmetic out of range
Sign Flag
Set when the result is negative
Zero Flag
Ser when the result is zero
Parity
is set if the least-significant byte in the result contains an
even number of 1 bits. Otherwise, PF is clear. In general,
it is used for error checking when there is a possibility that
data might be altered or corrupted
Irvine, Kip R. Assembly Language for Intel-Based Computers 5/e, 2007.
Web site
Examples
33