Download EEL 3801

Document related concepts
no text concepts found
Transcript
EEL 3801
Part I
Computing Basics
EEL 3801C
Data Representation
Digital computers are binary in nature.
They operate only on 0’s and 1’s.
• Everything must be expressed in terms of 0’s
& 1’s - Instructions, data, memory locs.
• 1 = “on”  voltage at output of electronic
device is high (saturated).
• 0 = “off”  voltage at output of electronic
device is zero.
EEL 3801C
Data Representation
• The smallest element of information is a “bit”
– 0 or 1. But by itself a bit does not convey
much information. Therefore:
• 8 bits in succession make a “byte”, the
smallest addressable binary piece of
information
• 2 bytes (16 bits in succession) make a
“word”
EEL 3801C
Data Representation
• 2 words (32 bits in succession) make a
“double word”.
• We can easily understand base 10 numbers.
So we need to learn how to convert between
binary and decimal numbers.
• Decimal numbers are not useful to the
computer: a compromise – base 16 numbers
(hexadecimal) and base 8 nos., or “octal”.
EEL 3801C
Binary Numbers
• Each position in a binary number, starting
from the right and going left, stands for the
power of the number 2 (the base).
• The rightmost position represents 20, which = 1.
• The second position from the right represents 21= 2.
• The third position from the right represents 22 = 4.
• The fourth position from the right represents 23 = 8.
EEL 3801C
Binary Numbers
• The value of an individual binary bit is
multiplied by the corresponding base and
power of 2, and all the resulting values for all
the digits are added together. For ex.
00101001
= 0*27 + 0*26 + 1*25 + 0*24 + 1*23 + 0*22 +
0*21 + 1*20
= 0 + 0 + 32 + 0 + 8 + 0 + 0 +1 = 41
EEL 3801C
Binary Numbers
• Conversion from decimal to binary - Two
methods:
– Division by power of 2:
• Find the value of the largest power of 2 that fits into
decimal number.
• Set 1 for position bit corresponding to that power.
• Subtract that value from the decimal number.
• Go to the first step, and re-do procedure until
remainder is 0
EEL 3801C
Binary Numbers
• Example:
• decimal number = 146
• Largest value of power of 2 that fits into 146 is 128,
or 27
• Set 8th bit (power of 7) to 1
• Subtract 128 from 146 = 18
• Largest value that fits into 18 is 16 or 24
• Set 5th bit (power of 4) to 1
• 18 - 16 = 2
EEL 3801C
Binary Numbers
• Example (continued):
• Largest power of 2 that fits into 2 is 2, or 21
• Set second bit (power of 1) to 1
• Remainder is now 0
• Set all other bits to zero
– The binary equivalent = 1 0 0 1 0 0 1 0
EEL 3801C
Binary Numbers
• Second Method
– Division by 2
• Integer divide the decimal number by 2.
• Note the remainder (if even, 0; if odd, 1)
• The remainder represents the bit
• Integer divide the quotient again by 2 and note
remainder.
• Continue until quotient = 0
EEL 3801C
Binary Numbers
• Example
• Decimal number = 146.
• 146/2 = 73, remainder = 0
• 73/2 = 36, remainder = 1
• 36/2 = 18, remainder =0
• 18/2 = 9, remainder = 0
• 9/2 = 4, remainder = 1
• 4/2 = 2, remainder = 0
• 2/2 = 1, remainder = 0
EEL 3801C
Binary Numbers
• 1/2 = 0, remainder = 1
• 0/2 = 0, remainder = 0
• Starting from the last one, the binary number is now
the string of remainders:
010010010
Since the 0 to the left does not count, we can lop
it off
10010010
EEL 3801C
Hexadecimal Numbers
• Large binary numbers are cumbersome
to read.
• ==> hexadecimal numbers are used to
represent computer memory and
instructions.
• Hexadecimal numbers range from 0 to
15 (total of sixteen).
• Octal numbers range from 0 to 7 (total
of 8)
EEL 3801C
Hexadecimal Numbers
• The letters of the alphabet are used to
the numbers represent 10 through 15.
– where A=10, B=11, C=12, D=13, E=14,
and F=15
• But why use hexadecimal numbers?
• 4 binary digits (half a byte) can have a
maximum value of 15 (1111), and a
minimum value of 0 (0000).
EEL 3801C
Hexadecimal Numbers
• If we can break up a byte into halves, the
upper and lower halves, each half would
have 4 bits.
• A single hexadecimal digit between 0 and F
could more concisely represent the binary
number represented by those half-bytes.
• A byte could then be represented by two
hexadecimal digits, rather than 8 bits
EEL 3801C
Hexadecimal Numbers
• The advantage becomes more evident for
larger binary numbers:
• 00010110 00000111 10010100 11101010
•
A
1
6
0
7
•  160794DAh
EEL 3801C
9
4
D
Numbers
• A radix is placed after the number to indicate
the base of the number.
• These are always in lower case.
• If binary, the radix is a “b”;
• if hexadecimal, “h”,
• if octal, “o” or “q”.
• Decimal is the default, so if it has no indication, it is
assumed to be decimal. A “d” can also be used.
EEL 3801C
Hexadecimal to Decimal
Conversion
• Similarly to binary-to-decimal conversion,
each digit (position from right to left) of the
hex number represents a power of the base
(16), starting with power of 0.
– 2 F 5 B  2*163 + 15*162 + 5*161 + 11*160 =
2*4096 + 15*256 + 5*16 + 11*1
–
= 8192 + 3840 + 80 + 11 = 12,123
EEL 3801C
Binary Conversion into
Hexadecimal
• Binary to hex is somewhat different, because
we in reality, take each 4 bits starting from
the right, and convert it to a decimal number.
• We then take the hexadecimal equivalent of
the decimal number (i.e., 10 = A, 11 = B,
etc.) and assign it to each 4 bit sequence.
• Each digit in a hex number = “hexadized”
decimal equivalent of 4 binary bits.
EEL 3801C
Hexadecimal Conversion into
Binary
• This conversion is also rather simple.
• Each hex digit represents 4 bits. The
corresponding 4-bit binary sequence replaces
the hex digit. For example:
•
26AF  0010 0110 1010 1111
EEL 3801C
Signed and Unsigned Integers
• Integers are typically represented by one
byte (8 bits) or by one word (16 bits).
• There exist two types of binary integers:
signed and unsigned
EEL 3801C
Unsigned Integers
• Unsigned integers are easy – they use all 8
or 16 bits in the byte or word to represent
the number.
• If a byte, the total range is 0 to 255
(00000000 to 11111111).
• If a word, the total range is 0 to 65,535
(0000000000000000 to 1111111111111111).
EEL 3801C
Signed Integers
• Are slightly more complicated, as they can
only use 7 or 15 of the bits to represent the
number. The highest bit is used to indicate
the sign.
• A high bit of 0  positive number
• A high bit of 1  negative number counter intuitive, but more efficient.
EEL 3801C
Signed numbers (cont.)
• The range of a signed integer in a byte is
therefore, -128 to127, remembering that the
high bit does not count.
• The range of a signed integer in a word is –
32,768 to 32,767.
EEL 3801C
The One’s Complement
• The one’s complement of a binary number is
when all digits are reversed in value.
• For example,
00011011
has a one’s complement of
11100100
EEL 3801C
Storage of Numbers
• Unsigned numbers and positive signed
numbers are stored as described above.
• Negatively signed numbers, however, are
stored in a format called the “Two’s
complement” which allows it to be added to
another number as if it was positive.
EEL 3801C
Storage of Numbers (cont.)
• The Two’s Complement of a number is
obtained by adding 1 to the lowest bit of the
one’s complement of the number.
• The Two’s Complement is perfectly reversible
– TC (TC (number)) = number.
EEL 3801C
Storage of Numbers (cont.)
• Therefore, if the high bit is set (to 1), the
number is a negatively signed integer.
• But, its decimal value can only be obtained
by taking the two’s complement, and then
converting to decimal.
• If the high bit is not set (= 0), then the
number can be directly converted into
decimal.
EEL 3801C
Example
• 0 0 0 0 1 0 1 0 is a positive number, as the
high bit is 0.
• 0 0 0 0 1 0 1 0 can be easily converted to 10
decimal in a straightforward fashion.
• 0 0 0 0 1 0 1 0 = 10 decimal
EEL 3801C
Example (cont.)
• 1 0 0 0 1 0 1 0 is a negative number
because of the high bit being set.
• 1 0 0 0 1 0 1 0, however, is not –10, as we
first have to determine its two’s complement.
EEL 3801C
Example (cont.)
• One’s Complement of (1 0 0 0 1 0 1 0)  (0
1 1 1 0 1 0 1),
• add 1  (0 1 1 1 0 1 1 0)
•  64 + 32 + 16 + 4 + 2 = -118
EEL 3801C
Character Representation – ASCII
• How do we represent non-numeric
characters as well as the symbols for the
decimal digits themselves if we want to get
an alphanumeric combination?
• Typically, characters are represented using
only one byte minus the high bit (7-bit
code).
EEL 3801C
Character Representation – ASCII
(cont.)
• Bits 00h to 7Fh represent the possible
values. The ASCII table maps the binary
number designated to be a character with a
specific character. The back inside cover of
the textbook contains that mapping.
• If the eighth bit is used (as is done in the
IBM PC to extend the mapping to Greek and
graphics symbols), then the hex numbers
used are 80h to FFh.
EEL 3801C
Character Representation – ASCII
(cont.)
• The programmer has to keep track of what a
binary number in a program stands for.
– It is not inherent in the hardware or the
operating system.
• High level languages do this by forcing you
to declare a variable as being of a certain
type.
– Different data types have different lengths
EEL 3801C
EEL 3801
Part II
System Architecture
EEL 3801C
Components
•
•
•
•
Video Display Terminal – self explanatory
Keyboard – self-explanatory
Disk Drives – self-explanatory
System Unit – contains the motherboard or
the system board. Otherwise selfexplanatory
EEL 3801C
Components (cont.)
• Random Access Memory (RAM) – Electronic
memory where the program and the data are
kept while the program is running. It is
volatile since the contents are lost if there is
loss of power. Additionally, it is also called
dynamic since its contents must be
continuously refreshed.
EEL 3801C
Components (cont.)
• Read-Only Memory (ROM) BIOS – Contains
the information on the input output
peripherals.
• CMOS RAM – Keeps system setup
information.
• Expansion slots – Permit expansion of the
system by adding special purpose boards
such as modems, communication cards, etc.
EEL 3801C
Components (cont.)
• Power Supply – Self-explanatory
• Parallel Port – Output port that transfers a
set of bits simultaneously. Typically used for
printers. Allow for quick transfer of data but
only for short distances.
• Serial port – Output port where single bits
are produced one by one. Slower, but useful
for longer distances.
EEL 3801C
Components (cont.)
• Microprocessor – Intel microprocessors are
downwardly compatible with each othe.
– Programs written on older versions will run on
the newer ones, but programs written for the
newer versions will not run on the older ones.
• Read Section 2.1 of the textbook for more
details.
EEL 3801C
System Architecture
• The Central Processing Unit (CPU) is the
most important part of the computer. It
consists of the Arithmetic logic Unit (ALU)
and the Control Unit (CU).
• The ALU carries out arithmetic, logic and
shifting operations.
• The CU fetches data and instructions and
decodes addresses for the ALU.
EEL 3801C
System Architecture (cont.)
• Additionally, there may be a math
coprocessor, which speeds up mathematical
calculations, as well as many other support
chips. However, they are all coordinated by
the CPU.
EEL 3801C
The CPU
• The most basic tasks of the CPU are:
• Find and load the next instruction from memory.
• Execute the instruction. This is composed of several
sub-instructions that we will discuss later.
EEL 3801C
The CPU (cont.)
• The CPU, besides the ALU and CU, is
composed of several other components:
• Data bus: Wires that move data within the CPU itself.
• Registers: High-speed memory elements within the
CPU itself on which can significantly speed up the
performance of the computer.
• Clock: A timing device whose ticks coordinate all
individual operations that take place in the computer.
These ticks are called machine cycles.
EEL 3801C
Registers
• Registers are special work areas inside the
CPU that can store data and/or instructions.
• These memory elements are very fast.
• There are several registers on the Intel 8088
family of microprocessors:
–
–
–
–
–
Data registers
Segment registers
Index registers
Special registers
Flag register
EEL 3801C
Data Registers
• Also called general purpose registers.
• Are used for arithmetic and data
manipulation operations.
• Can be addressed as either 8 or 16 bit
values, or as both.
• The 80386 and newer CPU’s use 32-bit
registers addressable as 16-bit ones.
EEL 3801C
Data Registers (cont.)
• There are several of these.
– The AX Register: the accumulator register is
used by the CPU for arithmetic operations.
– It is a 16-bit register, but can be addressed as
two independent 8-bit registers called AH (for
high) and AL (for low).
EEL 3801C
Data Registers (cont.)
– The BX Register: the base register is also general
purpose (like the AX).
– Has the ability to hold addresses for other
variables (pointers).
– Also 16-bit that can be independently addressed
as two 8-bit bytes (BH and BL).
EEL 3801C
Data Registers (cont.)
– The CX register: the counter register best serves
as the counter for repeating looping instructions.
• These instructions automatically repeat and
decrement the CX register, and quit when it equals 0.
– Also 16-bit that can be independently addressed
as two 8-bit bytes (CH and CL).
EEL 3801C
Data Registers (cont.)
– The DX Register: the data register is also general
purpose but has a special role when doing
multiplication or division.
EEL 3801C
Segment Registers
• These registers are used to store memory
locations of either instructions or data in
main memory.
• These registers contain the base segment of
the memory location – where the memory
segment begins.
EEL 3801C
Segment Registers (cont.)
• There are several of these:
– The CS Register: the code segment register
contains the base location of the executable
instructions that make up the program.
– Note that the base location can only address the
initial location where these instructions can be
found, not the entire segment..
EEL 3801C
Segment Registers (cont.)
– The DS Register: the data segment register is
the default base location in memory for variables
– The SS Register: the stack segment register
contains the base location of the run-time stack.
– The ES Register: the extra register is an
additional memory location where additional
base locations can be stored.
EEL 3801C
Index Registers
• Contain the offset (the distance from the
base segment) where a specific variable or
instruction may be found. The base
segment and the offset can uniquely identify
any addressable location of any length in
memory. Base segment + offset = memory
location.
EEL 3801C
Index Registers (cont.)
• There are several of these:
– The SI Register: the source index takes
name from the instruction used to move
strings.
• SI usually contains an offset from the DS
register, but can address any variable.
EEL 3801C
Index Registers (cont.)
– The DI Register: generally acts as a destination
for string movement instructions. Typically
contains an offset for the ES register, but not
necessarily so.
– The BP Register: the base pointer register
contains an offset from the stack register (SS).
• Used to locate variables in the stack.
EEL 3801C
Special Registers
• Do not fit into any other categories.
– The IP Register: the instruction pointer register
contains the offset of the next instruction to be
executed.
• Combines with CS to form the complete address of
the next executable instruction.
EEL 3801C
Special Registers (cont.)
– The SP Register: the stack pointer register
contains the offset from the beginning of the
stack segment to the top of the stack.
– SS and SP combine to form the complete
address for the top of the stack.
EEL 3801C
Flags Register
• One single 16-bit register whose individual
bit positions serve as flags to indicate the
status of the CPU or the result of some
arithmetic operation.
• The individual positions are predefined,
although not all 16 are defined.
EEL 3801C
Flags Register (cont.)
• Bit positions and flags:
– 0  Carry flag: Set when result of unsigned
arithmetic operation is too large to fit into
destination. Values are 1=carry; 0=no carry.
– 1  undefined
– 2  Parity flag: reflects the number of bits that
are set in the result of an operation. Can be
even or odd.
EEL 3801C
Flags Register (cont.)
– 3  undefined
– 4  Auxiliary carry: set when operation causes a
carry from bit 3 to bit 4. Rarely ever used.
– 5  undefined.
– 6  Zero flag: Set when result of an operation
results in zero. Used in jumping to other
instructions based on comparison of two values. Has
a value of 1when 0; 0 when ~0.
EEL 3801C
Flags Register (cont.)
– 7  Sign flag: Set when result of an operation
results in negative number. Value is 1 when
negative; 0 when positive.
– 8  Trap flag: Determines whether or not the
CPU will be halted after each instruction is
executed. Allows Trace or stepping through a
program’s execution. Allows the programmer to
control the CPU in this way through the INT 3
instruction.
EEL 3801C
Flags Register (cont.)
– 9  Interrupt flag: Makes it possible for external
interrupts to occur. Interrupts can be disabled
by setting this flag to 0. Controlled by the
programmer through the CLI and STI
instructions.
– A  Direction flag: controls the assumed
direction used by the string processing
instruction. Values are 1=up; 0=down.
Programmer can control this flag through the
STD and CLD instructions.
EEL 3801C
Flags Register (cont.)
– B  Overflow flag: Like the Carry flag, but for
signed arithmetic operations. Value is
1=overflow; 0=no overflow.
– C, D, E and F  undefined
EEL 3801C
The Run-Time Stack
• The run-time stack is an important element
in the execution of a stored program.
• It is a temporary holding area for addresses
and data.
• It resides in the stack segment identified in
the SS and SP registers.
• Each “cell” in the stack is 16 bits.
EEL 3801C
Run-Time Stack (cont.)
• The stack pointer holds the last element to
be added or pushed into the stack.
• This is also the first element to be taken off
the stack, or popped.
• This is referred to as Last-In-First-Out
(LIFO).
EEL 3801C
The Run-Time Stack (cont.)
• There are three typical uses for the run-time
stack:
– If we want to save the contents of a register, the
stack makes a great place to store their values
temporarily.
EEL 3801C
The Run-Time Stack (cont.)
– When a subroutine is called from another part of
the program, it is important that the processor
return to the place where the function was called
after it exits. The address of the instruction that
called the subroutine is saved on the stack so as
to be able to return to it later.
EEL 3801C
The Run-Time Stack (cont.)
– Local variables can be created when a subroutine
is active and then popped off the stack when the
subroutine returns to the calling instruction. This
is done in an area inside the run-time stack
called the stack frame.
EEL 3801C
The Run-Time Stack (cont.)
• Operations:
– The push operation: Used to put values of data
or instructions onto the stack. There is only on
place in the stack into which things can be
inputted – the top of the stack.
mov ax,00A5
; move 00A5 into AX
push ax
; pushes content of ax into stack
push bx
; assume BX has a value of 0001
push cx
; assume cx has a value of 0002
EEL 3801C
The Run-Time Stack (cont.)
• The push instruction
does not change the
value of the source
register (typically the
ax register, but could
be others). Rather it
simply copies its value
to the top of the stack.
High memory
0006
00A5
0001
0002
Low memory
EEL 3801C
SP
The Run-Time Stack (cont.)
– The pop operation: Used to remove the value in
the stack pointed to by the stack pointer and
places it in a register or memory location
(variable). Immediately upon removing the
element popped, the SP moves to the
immediately previous element in the stack.
pop ax
; pops stack and puts value into AX
EEL 3801C
The Run-Time Stack (cont.)
• Note that the value
remains in the stack,
but not being
pointed by the stack
pointer, it is subject
to be overwritten by
the next push
operation.
High memory
0006
00A5
0001
0002
Low memory
EEL 3801C
SP
Microinstructions
• Machine level instructions are not the lowest
level instructions in the computer.
Microinstructions are. These are very lowlevel operations that carry out the machinelevel instructions.
EEL 3801C
Microinstructions (cont.)
• There are three basic ones:
– fetch: the control unit fetches the instruction,
copies it into the CPU (register).
– decode: this operation decodes the instruction
as well as any operands specified by the
instruction. If any operands, the control unit
fetches the operand from main memory.
EEL 3801C
Microinstructions (cont.)
– execute: the ALU executes the operation and
passes the result operands to the CU, where
they are returned to the registers and/or to main
memory.
– Get next instruction
– Go back to step 1
• Microcode is the interface between the
binary code level and the electronic level.
EEL 3801C
Memory organization of DOS
• The Intel 8086 processor can access 1 Mb of
memory (actually, 1,048,576 bytes, which is
FFFFF in a 20-bit address). This is called the
Real Mode.
• The main memory is divided into RAM and
ROM.
– RAM occupies low memory, and starts at 00000h
and continues up to BFFFFh.
EEL 3801C
Memory organization of DOS
(cont’d)
– ROM occupies high memory and begins at C0000
and continues to FFFFF.
– This is mostly used for the ROM BIOS (the hard
disk controller).
– The BIOS contains diagnostic and configuration
software, as well as input-output subroutines.
EEL 3801C
Memory organization of DOS
(cont’d)
– Addresses begin with a hex address of
00000 and continue incrementally until
FFFFF.
– DOS allows only the first 640kB of RAM to
be used for programs.
– This is misleading because DOS (74kB) itself
has to occupy this area as well.
– Remaining RAM used by video display and
hard disk controller.
EEL 3801C
System Memory (cont.)
• The 80286 and more notably, the 80386 and
80486 processors can run in Protected Mode.
– This means that they can radically increase the
amount of memory they can address (16MB).
EEL 3801C
System Memory (cont.)
– The Pentium can address significantly more than
that.
– Unfortunately, DOS can only run in real mode.
– However, Windows runs in protected mode and
liberates the programmer from the 1MB memory
limit.
EEL 3801C
System Memory (cont.)
• The 80386 and beyond processor also has
the virtual 8086 mode, which allows
concurrent real mode processes to be
executed in by a single CPU.
• The total memory being used can total more
than the available RAM. The processor uses
external memory (hard disk drive or floppy)
to page currently unused portions of the
program to these devices.
EEL 3801C
Address Calculations
• An address is a number that refers to an 8bit (byte) memory location.
• The addresses are numbered consecutively,
starting at 00000h and going up to the
highest location in memory, depending on
the amount of memory available.
EEL 3801C
Address Calculations (cont.)
• Addresses can be expressed in one of two
ways:
– A 32-bit (16 + 16) segment-offset address. This
combines a base location (the base segment)
with the offset to represent the actual address.
For example, 08F1:0100, where 08F1 is the base
location (segment) from which to start counting,
and 0100 is the offset, or how much to count.
The address points to the first byte in the
address.
EEL 3801C
Address Calculations (cont.)
– A 20 bit absolute address, which refers to an
exact memory location. For example, F405Bh.
• Using 20 bits, the processor can only address
1 Mb (actually, 1,048,576 bytes) of memory.
EEL 3801C
Address Calculations (cont.)
• But address registers are only 16 bits wide,
limiting the addressable memory to 65,535.
• Thus, the segment-offset technique is used
to expand the range of accessible memory
beyond the 65,535 limit.
• Thus, when addressing memory locations,
the registers combine the values of two
registers, the base segment and the offset.
EEL 3801C
Address Calculations (cont.)
• The CPU uses the segment and offset value
to generate an absolute address. It adds the
segment and the offset to create the
absolute address. The segment value is
always known to have an implied half-byte at
the right (0000).
• Example: Given an address such as
08F1:0100. The absolute address (20 bit)
would be calculated as follows:
EEL 3801C
Address Calculations (cont.)
Segment value plus implied byte: 0 8 F 1 0 h
Add
the offset value:
0 1 0 0h
__________________________________________
Absolute Address:
0 9 0 1 0h
• The advantages to the segment offset method
is that it allows the program to be loaded into
any segment address in memory without
having to recalculate the addresses of all
variables.
EEL 3801C
Address Calculations (cont’d)
• Furthermore, large data structures that
occupy a large block of memory can be
easily accessed by knowing their base
segment and offset.
EEL 3801C
EEL 3801
Part III
Assembly Language Programming
EEL 3801C
Assembly Language Programming
• The basic element of an assembly program
is the statement.
• Two types of statements:
• Instructions: executable statements that actually
do something.
• Directive: provide information to assist the
assembler in producing executable code. For
example, create storage for a variable and initialize it.
EEL 3801C
Assembly Programming (cont’d)
– Assembly language instructions equate one-toone to machine-level instructions, except they
use a mnemonic to assist the memory.
• Program control: Control the flow of the program and
what instructions to execute next (jump, goto).
• Data transfer: Transfer data to a register or to main
memory.
• Arithmetic: add, subtract, multiply, divide, etc.
EEL 3801C
Assembly Programming (cont’d)
• Logical: > < = etc.
• Input-output: read, print etc.
EEL 3801C
Statements
• A statement is composed of a name, a
mnemonic, operands and an optional
comment. Their general format is as
follows:
[name] [mnemonic] [operand(s)] [;comments]
EEL 3801C
Names, labels
– Name: Identifies a label for a statement or for
a variable or constant.
• Can contain one or more of several characters (see
page 56 of new textbook).
• Only first 31 characters are recognized
• Case insensitive.
• First character may not be a digit.
EEL 3801C
Names, labels
• The period “.” may only be used as the first character.
• Cannot use reserved names.
• Can be used to name variables. Such when put in
front of a memory allocation directive. Can also be
used to define a constant. For example:
count1
db
50
; a variable (memory
allocation directive db)
count2
equ
100
; a constant
EEL 3801C
Names, labels
• Can be used as labels for statements to act as place
markers to indicate where to jump to. Can identify a
blank line. For example:
label1:
mov
ax,10
mov
bx,0
jmp
label1
.
.
.
label2:
EEL 3801C
; jump to label1
Mnemonics
– Mnemonic: identifies an instruction or
directive. These were described above.
• The mnemonics are standard keywords of the
assembly language for a particular processor.
• You will become familiar with them as time goes on
this semester.
EEL 3801C
Operands
– Operands are various pieces of information that
tells the CPU what to take the action on.
Operands may be a register, a variable, a
memory location or an immediate value.
10  immediate value
count  variable
AX  register
[0200]  memory location
– Comments: Any text can be written after the
statement as long as it is preceded by the “;”.
EEL 3801C
Elements of Assembly Language
for the 8086 Processor
• Assembler Character Set These are used
to form the names, mnemonics, operands,
variables, constants, numbers etc. which are
legal in 8086 assembly.
• Constant: A value that is either known or
calculated at assembly time. May be a
number or a string of characters. Cannot be
changed at run time.
EEL 3801C
Elements of Assembly Language…
(cont.)
• Variable: A storage location that is
referenced by name. A directive must
be executed identifying the variable
name with the location in memory.
• Integers: Numeric digits with no
decimal point, followed by the radix
mentioned before (e.g., d, h, o, or b).
Can be signed or unsigned.
EEL 3801C
Elements of Assembly Language…
(cont.)
• Real numbers: floating point number made
up of digits, a decimal point, an optional
exponent, and an optional leading sign.
(+ or -) digits.digits [exponential (+ or -)] digits
EEL 3801C
Elements of Assembly Language…
(cont.)
• Characters and strings:
• A character is one byte long.
• Can be mapped into the binary code equivalent
through the ASCII table, and vice-versa.
• May be enclosed within single or double quotation
marks.
• Length of string determined by number of characters
in string, each of which is 1 byte. For example:
EEL 3801C
Elements of Assembly Language…
(cont.)
“a” – 1 byte long
‘b’ – 1 byte long
“stack overflow” – 14 bytes long
‘abc#?%%A’ – 8 bytes long
EEL 3801C
Example of Simple Assembly
Program
• The following simple program will be
demonstrated and explained:
mov
add
add
mov
int
ax,5
ax,10
ax,20
sum,ax
20
;
;
;
;
move 5h into the AX register
add 10h to the AX register
add 20h to the AX register
store value of AX in variable
; end program
EEL 3801C
Example (cont.)
• The result is that at the end of the program,
the variable sum, which exists somewhere in
memory (declaration not shown), now
accepts the value accumulated in AX,
namely, 35h.
• Explain program.
EEL 3801C
Example (cont.)
• Note that several things have been left off,
such as directives. However, this program
captures the essence of assembly language
programming at its simplest.
• It still needs a directive to start the program.
This is the A directive. It tells the processor
to assemble this program at a particular
memory location.
A
EEL 3801C
100h
More Complex Assembly
Language Example Program
• The following is a more complex program,
used to advance an old dot matrix printer
equivalently to the formfeed command on
the printer control panel.
EEL 3801C
More Complex Assembly
Language Program (cont.)
a
mov ah,5
mov
dl,C
; begin assembly
; moves 5 to the AH register
; moves 0Ch to the DL register
int 21
int 20
n page.com
r cx
;
;
;
;
;
;
;
w
q
Calls DOS function 5
terminate program
names the program “page.com”
sets CX to the program’s length
the length is 8 bytes
writes program to disk
quit debug
EEL 3801C
More Complex Example (cont.)
• The value of 5 in AH is the name of the
function to be called by the DOS subroutine.
This particular one, the DOS function 5,
sends the content of the DL register to the
printer.
• The w command in DEBUGGER starts writing
to memory from offset 100, up to the length
of the program. Therefore, it needs to know
the length of the program being written to
disk. It counts 8 bytes (mov,
ah,5,mov,dl,C,int 21, and int 20).
EEL 3801C
More Complex Example (cont.)
• A more complex assembly program is shown
below.
• This is similar to the program that you will do
in the first lab session. It is the famous C
program “hello world”.
EEL 3801C
More Complex Example (cont.)
1:title Hello World Program (hello.asm)
2:
3:; this program displays “Hello, World”
4:
5:dosseg
6:.model small
7:.stack 100h
8:
9:.data
10: hello_message db ‘Hello, World!’,0dh,0ah,’$’
11:
EEL 3801C
More Complex Example (cont.)
12:
.code
13: main
proc
14:
15:
16:
17:
18:
19:
20:
21:
22:
23:
main
24:
end
mov ax,@data
mov ds,ax
mov ah,9
mov dx,offset hello_message
int 21h
mov ax,4C00h
int 21h
endp
main
EEL 3801C
More Complex Example (cont.)
The program is explained as follows:
• Line 1: Contains the title directive. All characters
located after the title directive are considered as
comments, even though the ; symbol is not used.
• Line 3: Comment line with the ; symbol
• Line 5: The dosseg directive specifies a standard
segment order for the code, data and stack segments.
The code segment is where the program instructions
are stored. The data segment is where the data
(variables) are stored. The stack segment is where
the stack is maintained.
EEL 3801C
More Complex Example (cont.)
• Line 6: The .model directive indicates the memory
architecture to be used. In this case, it uses the
Microsoft small memory architecture. It indicates this
by the word small after .model.
• Line 7: This directive sets aside 100h of memory for
the stack. This is equivalent to 256 bytes of memory
(162 = 256).
• Line 9: The .data directive marks the beginning of
the data segment, where the variables are defined
and memory allocated to them.
EEL 3801C
More Complex Example (cont.)
• Line 10: The db directive stands for “define byte”,
which tells the assembler to allocate a sequence of
memory bytes to the data that follow. 0dh is a
carriage return and 0ah is the linefeed symbol. The $
is the required terminator character. The number of
memory bytes is determined by the data themselves.
Hello_message is the name of the variable to be
stored in memory, and the db allocates memory to it
in the size defined by the data following it.
• Line 12: The directive .code is the indication of the
beginning of the code segment. The next few lines
represent instructions.
EEL 3801C
More Complex Example (cont.)
• Line 13: The proc directive is used to declare the
main procedure called “main”. Any name could have
been used, but this is in keeping with the C/C++
programming requirement that the main procedure be
called the main function. The first executable
instruction following this directive is called the
program entry point - the point at which the program
begins to execute.
EEL 3801C
More Complex Example (cont.)
• Line 14: The instruction mov is used to copy the
contents of one address into the other one. The first
operand is called the destination address, while the
second one is called the source address. In this
particular use, we tell the assembler to copy the
address of the data segment (@data) into the AX
register.
• Line 15: Copies the content of AX into DS, which is a
register used to put the data segment, the default
base location for variables.
EEL 3801C
More Complex Example (cont.)
• Line 17: This instruction places the value 9 in the AH
register. Remember that from the page.com program,
this is the register used to store the name of the DOS
subroutine to be called with the int 21 instruction.
• Line 18: This instruction places the address of the
string to be identified in the DX register. Remember
that this is the offset, where the default base segment
is already identified in the DS register as the base
segment for the data segment. Since the address of
the variable hello_message begins at the beginning of
the data segment, identified by DS, we only need to
supply the offset for the variable.
EEL 3801C
More Complex Example (cont.)
• Line 19: The instruction int 21, as we saw before,
takes the name of the function from the DX register,
which in this case is 9. DOS funtion 9, incidentally,
sends the contents of DX register to the VRT output
device. The DX register contains the address of the
string to be sent.
• Line 21 and 22: These instructions represent the
equivalent of an end or stop statement. This is
different from that done for page.com because this
will be an executable program (.exe), rather than a
.com program. More on this later.
• Line 23: Indicates the end of the main procedure.
EEL 3801C
More Complex Example (cont.)
• Line 24: The END directive – the last line to be
assembled. The main next to it indicates the program
entry point.
EEL 3801C
More Complex Example (cont.)
– The program may seem overly complicated for
such a simple program.
– But remember that assembly language
corresponds one-to-one with machine language
instructions.
– Note that it takes only 562 bytes of memory
when assembled and compiled.
EEL 3801C
More Complex Example (cont.)
– The same program written in a high level
language will require several more machine level
instructions to carry out the same thing.
– Written in Turbo C++, the executable program
would take 8772 bytes of memory to store.
EEL 3801C
Specifics of ALP – Data Definition
Directives
• A variable is a symbolic name for a location
in memory. This is done because it is easy
to remember variables, but not memory
locations. It is like an aka, or a pseudonym.
EEL 3801C
Data Definition Directives
• Variables are identified by labels. A label
shows the starting location of a variable’s
memory location. A variable’s offset is the
distance from the beginning of the data
segment to the beginning of the variable.
EEL 3801C
Data Definition Directives (cont.)
• A label does not indicate the length of the
memory that the variable takes up.
• If a string is being defined, the label offset is
the address of the first byte of the string
(the first element of the string).
– The second element is the offset + 1 byte.
– The third element is offset +2 bytes.
EEL 3801C
Data Definition Directives (cont.)
• The amount of
memory to be
allocated is
determined by
the directive
itself.
DB
Define byte
DW
Define word
DF
DP
Define
doubleword
Define far
pointer
DQ
Define quadword
DT
Define tenbytes
DD
EEL 3801C
Define Byte
• Allocates storage for one or more 8-bit
values (bytes). Has the following format:
[name] DB initialvalue [,initialvalue]
• The name is the name of the variable.
Notice that it is optional.
EEL 3801C
Define Byte (cont)
• initialvalue can be one or more 8-bit numeric
values, a string constant, a constant
expression or a question mark.
• If signed, it has a range of 127 to –128, If
unsigned, it has a range of 0 – 255.
EEL 3801C
Define Byte - Example
char
signed1
signed2
unsigned1
unsigned2
db
db
db
db
db
‘A’
-128
127
0
255
;
;
;
;
;
ASCII character
min signed value
max signed value
min unsigned value
max signed value
EEL 3801C
Define Byte (cont)
• Multiple values: A sequence of 8-bit
numbers can be used as initialvalue. They
are then grouped together under a common
label, as in a list.
• The values must be separated by commas.
list db 10,20,30,40
EEL 3801C
Define Byte (cont)
•
•
•
•
•
The 10 would be stored at offset 0000;
20 at offset 0001;
30 at offset 0002; and
40 at offset 0003,
where 0001 represents a single byte.
EEL 3801C
Define Byte (cont)
• A variable’s value may be left undefined.
This can be done by placing a ‘?’ for each
byte to be allocated (as in a list).
count db ?
EEL 3801C
Define Byte (cont)
• A string may be assigned to a variable, each
of whose elements will be allocated a byte.
c_string
db
‘This is a long string’
• The length of a string can be automatically
determined by the assembler by the ‘$’
symbol.
• See page 65 of new book for details.
EEL 3801C
Define Byte Example
• Using DEBUGGER, variable names
cannot be used
A 150
db 10,0
A 100
mov ax,0
mov ah,[150]
add ah,10
mov [151],ah
int 20
;
;
;
;
;
Assemble data at offset 150
define 2 data bytes, 1st=10, 2nd=0
Assemble code at offset 100
clears the AX register
move cont. of 1st mem. addr. to AH
; add 10 to the contents of AH
; move cont. of AH to other variabl
; end program
EEL 3801C
Define Byte Example (cont)
• If we dump the contents of memory
locations at offset 150 and 151, we will find
10 in [150] and 20 in [151].
EEL 3801C
Define Word
• Serves to allocate memory to one or more
variables that are 16 bits long. Has the
following format:
[name] DW initialvalue [,initialvalue]
• The name is the name of the variable.
Notice that it is optional.
• initialvalue can be one or more 16-bit
numeric values, a string constant, a constant
expression or a question mark.
EEL 3801C
Define Word (cont)
• If signed, it has a range of 32,767 to –
32,768,
• If unsigned, it has a range of 0 – 65,535.
EEL 3801C
Define Word (Example)
var
signed1
signed2
unsigned1
unsigned2
var-bin
var-hex
var-mix
dw
dw
dw
dw
dw
dw
dw
dw
1,2,3 ; defines 3 words
-32768 ; smallest signed value
32767 ; largest signed value
0
; smallest unsigned value
65535 ; largest signed value
1111000011110000b
4000h
1000h,4096,’AB’,0
EEL 3801C
Reverse Storage Format
• The assembler reverses the bytes in a word
when storing it in memory.
– The lowest byte is placed in the lowest address.
– It is re-reversed when moved to a 16-bit register.
value
dw
B6
2AB6h
2A
• See example on page 50 of textbook
EEL 3801C
Define Doubleword – DD
• Same as DB and DW, except the memory
allocated is now 4 bytes (2 words, 32 bits).
[name] DD initialvalue [,initialvalue]
• The name is the name of the variable.
Notice that it is optional.
• initialvalue can be one or more 32-bit
numeric values, either in dec., hex or bin.
form, string const., a const. Expression, or ?
EEL 3801C
Multiple Values
• A sequence of 32-bit numbers can be used
as initialvalue.
• They are then grouped together under a
common label, as in a list.
• The values must be separated by commas.
EEL 3801C
Reverse Order Format
• As in define word, the bytes in doubleword
are stored in reverse order as in DW.
• For example,
var
78
dd
12345678h
56 34 12
EEL 3801C
Type Checking
• When a variable is created, the assembler
characterizes it according to its size (i.e., byte,
word, doubleword, etc.).
• When a variable is later referenced, the
assembler checks its size and only allows
values to be stored that come from a register
or other memory that matches in size.
• Mismatched movements of data not allowed.
EEL 3801C
Data Transfer Instructions – mov
• The instruction mov is called the data
transfer instruction.
• A very important one in assembly - much
programming involves moving data around.
• Operands are 8- or 16-bit on the 8086,
80186 and 80286.
• Operands on the 80386 and beyond, they
can also be 32-bits long.
EEL 3801C
Data Transfer Instructions – mov
(cont)
• The format is:
mov
destination,source
– The source and destination operands are selfexplanatory.
• The source can be an immediate value (a constant), a
register, or a memory location (variable). It is not
changed by the operation.
• The destination can be a register or a memory
location (variable).
EEL 3801C
Operands
• Register Operands: Transfer involving only
registers. It is the fastest.
• Source = any register
• Destination = any register except CS and IP
• Immediate Operands
• Immediate value can be moved to a register (all but
IP) or to memory.
• Destination must be of same type as source.
EEL 3801C
Operands (cont.)
• Direct operands
• A variable may be one of the two operands, but not
both. It does not matter which is the variable.
EEL 3801C
Limitations on operands
• There are some limitations on mov:
• CS or IP not destination operand
• Moving immediate data to segment registers.
• Moving from segment register to segment register.
• Source and destination operands of different types.
• Immediate value as destination (obviously!!)
• Memory to memory moves
EEL 3801C
Sequential Memory Allocation
• Memory for variables is allocated sequentially
by the assembler.
• If we call DB several times, such as in:
var1 db 10
var2 db 15
var3 db 20
EEL 3801C
Sequential Memory Allocation
(cont)
• var1 will be the first byte in the data
segment of main memory.
• This segment may be identified by the base
segment and the offset.
• var2 will occupy the next available memory
location, or 1 byte away from the beginning
of the data segment in memory.
EEL 3801C
Sequential Memory Allocation
(cont)
• var 3 will be 2 bytes away from this
starting point.
• This will be the case even if the memory
locations are not labeled, such as in:
db
db
db
10
20
30
EEL 3801C
Offsets
• Many times, memory will be allocated, but
not labeled.
• This is typical of an array, when only the
entire array is labeled, not each cell.
• The address of the array is the address of
the first element (position) of the array.
• All subsequent cells are allocated by adding
an offset to the address of the head element.
EEL 3801C
Offsets
• This is also true when a list of elements is
defined through DB, DW, or DD.
– Example: an array or list of 8-bit numbers whose
memory location is called a-list.
• To access the first element of a-list, we reference the
location in memory corresponding to a-list.
• To access any of the other elements of the array, we
provide an offset to the address of array.
– The second element at array+1, the third at array+2, the
fourth at array+3, etc.
EEL 3801C
Offsets
• To move the value of the 5th element of the
array to register AL:
mov
al
array+4
• The size of the two operands must match.
– Otherwise, an error may result.
• Note that AL is used, not AX - 1 byte.
• See example on page 54 of textbook.
EEL 3801C
PTR Operator
• Used to clarify an operand’s type. NOT a
pointer.
• It can be placed between the mov command
and the operands, as for example: Used for
readability only. It does not change the size
of the operand in any way.
mov word ptr count,10
mov byte
ptr var2,5
EEL 3801C
XCHG Instruction
• Allows the direct exchange of values
between 2 registers or between a register
and a memory location.
– Very fast, used for sorting
– Needs to obey size constraints
– Used in sorting.
EEL 3801C
Stack Operations
• Already discussed what a stack is.
• Each position in the stack is 2 bytes long –
only 16-bit registers can be copied into the
stack.
• The “bottom” of the stack is in high memory,
and it grows downward.
• Typically, 256 bytes are allocated to the
stack, enough for 128 entries.
EEL 3801C
Stack Operations (cont)
• Identified by the SS register (stack
segment), which identifies the address of the
base location of the stack segment.
• The stack pointer (SP register) indicates the
address of the first element of the stack (top
of the stack).
EEL 3801C
Push Operation
• Puts something (a 16-bit element) at
the top position of the stack.
• Decrements stack pointer (it grows
downward).
• Can put the contents of a register or of
memory (a variable)
• In 80286 and later processors, it can
also place an immediate value.
EEL 3801C
Push Operation
• Has the following form:
push
push
push
ax
ar1
1000h
EEL 3801C
Pop Operation
• The opposite of the push operation.
• Removes the top element in the stack.
• Copies value of top element in stack to
destination indicated in the statement.
• Increments stack pointer.
• Registers CS (code segment) and IP
(instruction pointer) cannot be used as
operands.
EEL 3801C
Pop Operation
• Has the following form:
pop
pop
ax
ar2
EEL 3801C
PUSHF and POPF
• Special instructions that move and remove
the contents of the Flags register onto and
out of the stack.
• These are used to preserve the contents of
these registers in case it is changed and the
old values are to be reinstated.
• See page 56.
EEL 3801C
PUSHA (80186+) and PUSHD
(80386+)
• Pushes the contents of the registers AX, CX,
DX, BX, original SP, BP, SI, and DI on the
stack in this exact order.
• PUSHD does the same for 32-bit registers.
EEL 3801C
POPA and POPD
• Pops the same registers in the reverse order.
EEL 3801C
Arithmetic Instructions
• Form the heart of any program, at any level
of abstraction.
• Integer arithmetic done in 8- or 16-bit
operands.
EEL 3801C
Arithmetic Instructions
– Floating point arithmetic done in one of three
ways:
• 80x87 math coprocessor
• software routines that emulate the coprocessor
• software that converts floating point to integer,
executes the instruction and then converts back to
floating point. (not particularly useful in many cases).
– Section concentrates on integer arithmetic
EEL 3801C
INC and DEC Instructions
• Respectively increment and decrement
the operand by one.
• Form is as follows:
inc
inc
inc
1
dec
1
dec
dec
al ; incr. 8-bit register by 1
bx ; incr. 16-bit register by 1
mem
; incr. memory location by
mem
; decr. memory location by
bx ; decr. 16-bit register by 1
al ; decr. 8-bit register by 1
EEL 3801C
INC and DEC Instructions
• Faster than ADD and SUB instructions
• All status flags are affected except the Carry
Flag.
EEL 3801C
ADD Instruction
• Used to add two numbers together.
• Format is as follows:
add
destination,source
• The high level language equivalent of:
destination = destination + source
EEL 3801C
ADD Instruction
• Takes 2 operands, a source and a
destination.
• Adds the contents of the operands.
• Places the result in the destination.
• Only one memory operand may be used.
• Source may be immediate value, but not
destination.
EEL 3801C
ADD Instruction (cont.)
• The contents of the source remain
unchanged.
• A segment register may not be a destination.
• All status flags are affected.
• Sizes of operands must match (same size).
EEL 3801C
SUB Instruction
• Subtracts a source operand from a
destination operand and stores the result in
the destination.
• Format is as follows:
sub destination,source
• The high level language equivalent of:
destination = destination + (source)
EEL 3801C
SUB Instruction (cont.)
• Takes the two’s complement of source and
adds it to the destination.
• Only one of the operands may be a memory
operand.
• Size of operands must match (same size).
• Segment register may not be the destination
operand.
EEL 3801C
SUB Instruction (cont.)
• Only one of the operands may be a memory
operand.
• Immediate values cannot be destinations.
EEL 3801C
Effects of SUB and ADD on Flag
Registers
• Why the flags?
– If an operation such as ADD overflows (i.e.,
number to be put in the destination exceeds the
size of the destination), then overflow.
– The flag may indicate that the value of the
destination is meaningless.
– The ADD instruction, therefore, affects both the
Carry and Overflow flags.
– Only 1 of these flags (if signed or if unsigned).
EEL 3801C
Flag Registers (cont.)
– Zero Flag: Set when the result of the
instructions inc, dec, add or sub = 0.
– Sign Flag: Set when the result of the
instructions inc, dec, add or sub < 0
– Carry Flag: Used with unsigned arithmetic only,
even though the processor updates it even if
operation is signed.
• For example:
EEL 3801C
Flag Example
mov
ax,00FFh
add
al,1
; AX = 0000h, CF=1
00FFh + 0001h = 0100h
– Since the operand of the add instruction is 8-bit
(AL register), this is an 8-bit operation.
– Therefore, the operation looks more like
FFh + 01h = 100h
EEL 3801C
Flag Example (cont.)
– This is an overflow, so the Carry Flag is set to
indicate that the result was larger than the 8-bit
destination could store.
– Could be fixed by using a 16-bit operation (e.g.,)
add
ax,1
; now it’s a 16-bit operation
; AX = 0100h CF = 0
EEL 3801C
Flag Example (cont.)
• If result of operation is too large for the destination
operand, the Carry flag is set.
• Can also occur in a subtraction operation when
subtracting a larger operand from a smaller one
(signed results are not permissible).
mov
sub
ax,5501h
al,2
; AX = 55FFh
CF=1
• We subtracted 2 from 1, resulting in a negative
number in an unsigned operation.
EEL 3801C
Flag Registers (cont.)
• Overflow Flag: Set by the processor
regardless of the type of operation, but
important only when operation is signed.
– Signed results are useless when this flag is set
– Can result when using add or sub
– But not inc or dec.
EEL 3801C
Flag Registers - Example
• If we add 126 + 2 = 128, in an 8-bit
operation, this will represent an overflow.
• This operation in binary numbers:
01111111 + 00000010 = 10000000
• Equivalent to the two’s complement of –128,
which is completely incorrect, therefore
meaningless in signed integers.
• Therefore, OF = 1.
EEL 3801C
Flag Registers - Example (cont.)
• In the case of subtraction, it gets a little
more complicated.
– If we subtract 2 from –128:
– -128 – 2 = -130 which is an overflow, as it
does not fit in an 8-bit destination.
– In binary, this looks like this:
10000000 (-128 in two’s complement) +
11111110 (-2 in twos’ comp)
= 1 01111110
EEL 3801C
Flag Registers - Example (cont.)
• The first 1, of course, is a ninth digit, which
does not fit into the 8-bit register.
• Thus, the operation looks like it resulted in
01111110
• which is –126.
• So, the OF = 1.
EEL 3801C
Addressing Modes - the OFFSET
• Five (5) different addressing modes in the
intel 8086 family of processors.
• Introduce an operator called offset, which
places the address (actually, the offset) of a
variable in a register.
• To move the address of a variable into a
register, but do not know it directly, the
OFFSET operator returns variable’s address
EEL 3801C
Addressing Modes - Example
• The assembler always knows the address of
each variable (i.e., label).
• For example,
mov
ax,offset
EEL 3801C
avariable
Direct Addressing Mode
• Returns the contents of memory at the offset
of a variable.
• The assembler keeps track of the offset of
every label (variable).
• Thus, by simply referencing the label, the
contents of the memory location assigned to
that label can be accessed.
EEL 3801C
Direct Addressing Mode (cont.)
• The effective address (EA) is the offset (i.e.,
distance) from the beginning of a segment.
• A displacement is either the absolute
address, or the offset of a variable.
EEL 3801C
Direct Addressing Mode Example
count db 20
mov al,count ; AL = 20 moved the
;contents of the label
;count into AL
• Can also be done by referencing the memory
address within brackets,
– for example, [200]
EEL 3801C
Indirect Addressing Mode
• Placing the address of the label in a base or
index register can create a pointer to a label.
• Typically done to represent a complex data
structure, such as an array or a structure.
• We can increment the pointer to point to
elements of the data structure.
EEL 3801C
Indirect Addressing Mode Example
• An array such as shown below.
A B C D E F G H I J K L M N
• Let us define this array as a string in the
following manner:
astring db “ABCDEFGHIJKLMN”
EEL 3801C
Indirect Addressing Mode Example
• To access the 6th element (F), set the
value of a register to the first element of
the array (string).
mov
bx,offset
astring
; moved the
;offset
• Then increment the memory address or
offset, by 5, such as
add
mov
bx,5; add 5 bytes to address in BX
dl,[bx] ; move content of BX into DL
EEL 3801C
Indirect Addressing Mode Example
• If the BX, SI or DI registers are used, the
effective (absolute) address is
understood to be an offset from the DS
register (by default).
• If BP register is used, it is understood to
be an offset from the SS register.
• One can change the default by placing
before the register the base register from
which the offset is calculated.
EEL 3801C
Indirect Addressing Mode Example
• For example:
• To use the BP register, but calculate its
address from the DS register rather than
from the SS:
mov
dl,ds:[bp]; looks in data segment DS
EEL 3801C
Based and Indexed Addressing
Mode
• A register value and a displacement can be
used as addresses to access the contents of
memory.
• The displacement is either a number, or the
offset of a label whose address (offset) is
known by the assembler at compile time.
EEL 3801C
Based and Indexed Addressing
Mode - Example
• For example, to access the number 8:
5 7 1
6
2
8
7
9
0
anarray db 5,7,1,6,2,8,7,9,0
mov bx,5
mov al,anarray[bx]
or
mov al,[anarray+bx]
or
mov al,[anarray+5]
EEL 3801C
Base-Indexed Addressing Mode
• Combining a base register and an index
register forms the effective address.
• Useful for defining 2 dimensional arrays.
• Similar to the Base and indexed, except now
instead of having a displacement that is
either a constant or the address of a label, it
is now the sum of the base and index
registers.
EEL 3801C
Base-Indexed - Example
a2darray
db
10,20,30,40,50
db
60, 70, 80, 90, A0
db
B0, C0, D0, E0, F0
• If we want to access the third element of the
second row, we set a base segment to the
first element of the second row, and then the
offset is the third column.
EEL 3801C
Base-Indexed - Example (cont.)
• For example, let’s say we want to get the
‘80’:
10 20
F0
mov
add
mov
mov
30 40 50 60 70 80 90 A0 B0 C0 D0 E0
bx,offset a2darray
bx,5; base=1st element of row 2
si,2
;offset = 2
al,[bx+si] ; moves the
;value 80 into AL
EEL 3801C
Base-indexed with Displacement
Addressing Mode
• The combination of the 2 addressing modes
above.
• Combines the displacement of a label with
that of a base and index segments.
• The same ‘80’ value in the above array can
be accessed with the operand:
mov
al, array[bx+si]
EEL 3801C
Program Structure
• Program divided into 3 primary segments:
• code segment (pointed to by register CS)
• data segment (pointed to by register DS)
• stack segment (pointed to by register SS)
• Segments can range in size between 10h (16)
bytes and 64K bytes.
• Instructions or data located on these
segments is referenced through the offsets
from the base segment (displacements).
EEL 3801C
Program Structure (cont.)
• Each segment is identified by the .stack,
.code, and .data indicators.
• Note that the SP register points to the next
memory location after the end of the stack
segment.
– This is because the stack grows from high
memory to low memory.
EEL 3801C
Program Structure (cont.)
• The title directive identifies the program
title (up to 128 characters long).
• The dosseg directive tells the assembler to
place the program segments in the standard
order used by the high level languages.
• The .model directive identifies the type of
memory model to be used.
EEL 3801C
Program Structure (cont.)
• The memory models are based on the idea
that a 16-bit address register can only
represent approximately 64K bytes of
memory range.
• Thus, if we have a segment of memory that
is less than or equal to 64K bytes, then we
can easily access that location through a
simple offset from the base segment.
EEL 3801C
Program Structure (cont.)
• This means quicker addressing, since only 1
instruction is necessary to load a 16-bit
address.
• If greater than 64K, however, then we must
change the base segment register as well, as
the offset cannot reach beyond 64K bytes.
• This requires 2 machine instructions, one for
the base segment, and one for the offset.
EEL 3801C
Program Structure (cont.)
• The various models are the following:
– Tiny:
Code + data <= 64K
– Small: Code <= 64K; data <= 64K
– Medium:
Data <= 64K; Code any size
– Compact: Code <= 64K; Data any size
– Large: No restrictions on either one, but arrays
<= 64K
– Huge: No restrictions on any of the three
EEL 3801C
Program Structure (cont.)
• The Tiny model does not result in an
executable file, but rather, in a command file
(.com).
• All others result in .exe files.
• The linker produces a map file which
indicates the location of these segments.
• See page 68 and 69 of textbook.
EEL 3801C
Discuss figure 3.2 on page 68 and
3.3 on page 69.
• Program entry point
• Absolute vs. segment register value
• No overlap between segments
EEL 3801C
Other details -“Hello World!”
Program
• The proc directive is used to declare the
main procedure – the program entry point,
or first executable instruction follows this
directive.
• Unlike C, the name main is not necessary –
any name would do.
• Int 21h is the instruction to stop or halt
execution.
EEL 3801C
Other details -“Hello World!”
Program (cont.)
• main endp indicates the end of the main
procedure.
• end main indicates the last line to be
assembled.
EEL 3801C