Eee 3132 Part 1 Lecture 1 To 8
Eee 3132 Part 1 Lecture 1 To 8
School of Engineering
• Rationale:
• Due to the critical role computers are playing in today's society, it has
become imperative to train students in electrical engineering on
fundamentals, advanced concepts, and principles of computer
engineering. Nowadays, knowledge and understanding of computer
engineering have become skills that are sought-after by many
employers in both public and private sectors.
• Aim:
• This course aims at equipping students with knowledge and
understanding of concepts, analysis design, construction, principles of
operations, and characteristics of modern computer systems.
2
Learning Objectives and Outcomes (1 of 2)
• After having learnt this course students will have acquired testable
knowledge and skills in the following fields:
1. When given a business problem, students will be able to analyze,
design, and develop a computer solution model.
2. When given a problem involving automation, students will be able
to analyze, design, and propose hardware and software solution
models.
3. When given a problem involving information processing, students
will be able to analyze, design, and propose hardware and software
platforms for an appropriate solution.
3
Learning Objectives and Outcomes (2 of 2)
By the end of the 1. Understand techniques for measuring the performance
programme, the of computer systems and the limitations of these
graduate should techniques
be 2. Analyse the impact of a change made to a computer’s
able to: architectural design
3. Evaluate the effectiveness of the use of parallel
processing in different environments
4. Learn about methods for designing multiprocessor
systems
4
Course Outline
1. Software
• Machine, assembly language programming. High-level languages,
structured programming, efficient programming. Program design
techniques. Timing Charts.
• Computer programming with an emphasis on C++ programming an
Object-Oriented Programming language: OOP principles, Inheritance,
Classes and Objects, Polymorphism, Separation of Interface and
Implementation; plus the introduction of generic programming and
C++ Templates.
5
Course Outline
2. Classification of Processors
• Processor architecture; 8-bit processors, 16-bit processors. Memory
structures and technologies. Memory maps, standard I/O, memory
mapped I/O, block addressing SSCs.
3. Communications
• Communication protocols; handshaking, polling, interrupts, DMA. Serial
and parallel communication USART's, UART's, PPI's, DM¡ control.
Peripheral devices; keyboards, CRTs, printers, disk drives, MODEM's.
6
Course Outline
4. System Development
Editing, linking, and location. Development systems and in-circuit emulation. Programming
languages; machine code, assembly, high-level language. Assemblers, interpreters, and
compilers. Data structures real, integer, floating point, characters excess-64, ASCII, Baudot.
Multiprogramming and multiprocessing. Semaphores and re-entrant codes.
7
Course Pre-requisite
Course Pre-requisite: Introduction to Information Technology (ENG 2139)
Time Allocation
Lectures 4 hours/ week
Laboratory/Tutorials 3 hours/week
Assessment:
Assignments/ Tutorials: 8-10 [5%]
Laboratories/Mini-Projects: 6-10 [15%]
Tests: 2 [20% ]
Final Examination: 60%
8
Assessments
Continuous Assessment Weight
Assignments 5%
Tests 20%
Laboratory 15%
Final Examination 60%
9
Prescribed Test
1. Short K.L., Microprocessors and Programmed Logic, 2nd Ed., 1997,
Prentice Hall IPE, ISBN 0-13-5806062.
2. Programming: Principles and Practice Using C++, 2nd Ed., 2014,
Paperback, ISBN-13: 978-0321992789 or ISBN-10: 0321992784
3. G. Buttazzo, Hard Real-Time Computer Systems: Predictable Scheduling
Algorithms and Applications, 3rd Ed., Springer, 2011
4. M. Wolf, Computers as Components:
5. Principles of Embedded Computing System Design, 4th Edition,
6. Morgan Kaufman/Elsevier Publishers 2016, ISBN 978-0-12-805387-4
10
Prescribed Test
7. Daniel W. Lewis, Fundamental of Embedded Software with ARM Cortex M3,
2nd Edition, Pearson 2013, ISBN 978-0-13-291654-7
8. Z. Navabi, Embedded Core Design with FPGAs, McGraw-Hill, 2007, ISBN-13:
9780071474818 (ISBN-10: 0071474811)
9. D. C. Black, J. Donovan, B. Bunton & A. Keist, SystemC: From the Ground Up,
2nd Edition, 2010, ISBN 978-0-387-69958-5
10. F. Vahid & T. Givargis, Embedded System Design, 1st Edition John Wiley 2002,
ISBN 0-471-38678-2
11. Alan Burns and Andy Wellings, Real-time Systems & Programming Languages,
Addison-Wesley 2001, ISBN 0 201 72988 1
12. Embedded Processors and Micro-controllers Data Sheets are available at the
Course Website http://www.ecb.torontomu.ca/~courses/ee8205/
11
Course Work - Scheme
Lecture Topic
1 Introduction to Computer Engineering
2 Introduction to Assembly Language
3 Implementing C Language Constructs
4 Input/Output
5 Embedded Bus Protocols
6 Time Sharing
7 Concurrency Basics
12
Course Work - Scheme
Week Topic
8 Locks: a synchronization primitive to efficiently support mutual
exclusion
9 Condition Variables and Semaphores
10 Introduction to Real-Time Scheduling
13
Conclusion
14
Course Work - Scheme
15
Introduction
EEE3132: Computer Engineering
Dr. Lukumba Phiri
[email protected]
Electrical and Electronic Engineering
University of Zambia
Overview
• Embedded Software Systems: Course Management
• Real-time and Embedded Systems
• Embedded System Applications
• Characteristics of Embedded Systems
Text by Wolf: part of Chapter 1, Text by Navabi: part of Chapters 8 and 9
In addition to the text/reference books, lectures may contain material from research articles
to be identified by the instructor.
Digital
Output Analog
Analog
Input
CPU Digital
Embedded Mem
Computer
Real- Engineering
Time
Algorithms for Interface
Clock Digital Control System
Database
Operator Operator
Console Interface
Embedded Computer
Sensor Sensor
Wheel Wheel
Brake Brake
Hydraulic
ABS Pump
Brake Brake
Wheel Wheel
Sensor Sensor
Pipe
Processing
Valve
Output valve
angle
Time
Computer
Microprocessor:
32-bit RISC
(Reduced
Instruction Set
Computer)
Microprocessor
32-bit ARM RISC
• GlowCap has a
tiny Amtel 8-bit
picoPower AVR
Processor
• Help People to take
their medication on-
time.
• Sense when the
bottle is opened.
• Connect to Vitality
server and transmit
information
wirelessly.
TIMSP430F1232: Low
Power Micro-controller
• 16-bit CPU
• 8 Kbytes of flash
memory
• 256 bytes of RAM
• 10-bit –ADC with
200 kilo-samples/second
• CPU can run at 8MHz
with 3.3V supply voltage
Functional complexity
• Often have to run sophisticated algorithms or multiple
algorithms.
Cell phone, laser printer.
• Often provide sophisticated user interfaces.
Examples:
• Single-chip multiprocessors for cell phone base band.
• Automotive network + processors.
Heterogeneous systems:
• Some custom logic for well-defined functions
• CPUs+software for everything else
Top-down design:
• Start from most abstract description;
• Work to most detailed.
Bottom-up design:
•Work from small components to big system.
Real design uses both techniques
Week3 1
Lecture overview
⚫ Introduction to microprocessors
⚫ Instruction set architecture
⚫ Typical commercial microprocessors
Week3 2
Microprocessors
⚫ A microprocessor is a CPU on a single chip.
⚫ If a microprocessor, its associated support
circuitry, memory and peripheral I/O
components are implemented on a single
chip, it is a microcontroller.
Week3 3
Microprocessors
⚫ Microprocessor Based Embedded System:
⚫ CPU for Computers
⚫ No: RAM, ROM, I/O on CPU chip itself
⚫ Ex: Intel‘s 8085 (MAT385), 8086, Motorola‘s
680xx
Week3 4
Microprocessors
⚫ Microcontroller Based Embedded System:
⚫ A Small Computer or System on Chip (SoC)
⚫ On-chip RAM, ROM, I/O ports...
⚫ Example: TI‘s MSP430, Motorola‘s 6811,
Intel‘s 8051, Zilog‘s Z8 and PIC 16X
Week3 5
Microprocessors
Week3 6
Microprocessor types
⚫ Microprocessors can be characterized based
on
⚫ the word size
⚫ 8 bit, 16 bit, 32 bit, etc. processors
⚫ Instruction set structure
⚫ RISC (Reduced Instruction Set Computer), CISC
(Complex Instruction Set Computer)
⚫ Functions
⚫ General purpose, special purpose such image
processing, floating point calculations
⚫ And more … Week3 7
Typical microprocessors
⚫ Most commonly used
⚫ 68K
⚫ Motorola
⚫ x86
⚫ Intel
⚫ IA-64
⚫ Intel
⚫ MIPS
⚫ Microprocessor without interlocked pipeline stages
⚫ ARM
⚫ Advanced RISC Machine
⚫ PowerPC
⚫ Apple-IBM-Motorola alliance
⚫ Atmel AVR
⚫ A brief summary will be given later
Week3 8
Microprocessor applications
⚫ A microprocessor application system can be
abstracted in a three-level architecture
⚫ ISA is the interface between hardware and software
FORTRAN 90 C program
program
FORTRAN 90 C program
program compiled compiled
to ISA program to ISA program
Software
ISA level
Hardware
ISA program executed
by hardware
Hardware
Week3 9
ISA
⚫ Stands for Instruction Set Architecture
⚫ Provides functional specifications for software
programmers to use/program hardware to
perform certain tasks
⚫ Provides the functional requirements for
hardware designers so that their hardware
design (called micro-architectures) can
execute software programs.
Week3 10
What makes an ISA
⚫ ISA specifies all aspects of a computer
architecture visible to a programmer
⚫ Basic
⚫ Instructions
▪ Instruction format
▪ Addressing modes
⚫ Native data types
⚫ Registers
⚫ Memory models
⚫ advanced
⚫ Interrupt handling Week3 11
Week3 12
Instructions (cont.)
⚫ Instruction set is machine oriented
⚫ Same operation, could be written differently in
different machine
⚫ AVR
▪ Addition: add r2, r1 ;r2 r2+r1
▪ Branching: breq 6 ;branch if equal condition is true
▪ Load: ldi r30, $F0 ;r30 Mem[F0]
⚫ 68K:
▪ Addition: add d1,d2 ;d2 d2+d1
▪ Branching: breq 6 ;branch if equal condition is true
▪ Load: mov #1234, D3 ;d3 1234
Week3 13
Instructions (cont.)
⚫ Instructions can be written in two languages
⚫ Machine language
⚫ made of binary digits
⚫ Used by machines
⚫ Assembly language
⚫ a textual representation of machine language
⚫ Easier to understand than machine language
⚫ Used by human beings
Week3 14
Machine code vs. assembly
code
⚫ There is a one-to-one mapping between the
machine code and assembly code
⚫ Example (Atmel AVR instruction):
For increment register 16:
⚫ 1001010100000011 (machine code)
⚫ .include “mega64def.inc”
Data types
⚫ The basic capability of using different classes of values.
⚫ Typical data types
⚫ Numbers
⚫ Integers of different lengths (8, 16, 32, 64 bits)
▪ Possibly signed or unsigned
▪ Commonly available
⚫ Floating point numbers, e.g. 32 bits (single precision) or 64 bits
(double precision)
▪ Available in some processors such as PowerPC
⚫ BCD (binary coded decimal) numbers
▪ Available in some processors, such as 68K
⚫ Non-numeric
⚫ Boolean
⚫ Characters
Week3 16
Data types (cont.)
⚫ Different machines support different data
types in hardware
⚫ e.g. Pentium II:
Data Type 8 bits 16 bits 32 bits 64 bits 128 bits
Signed integer ✓ ✓ ✓
Unsigned integer ✓ ✓ ✓
BCD integer ✓
Floating point ✓ ✓
Week3 17
Registers
⚫ Two types
⚫ General purpose
⚫ Special purpose
⚫ Used for special functions
⚫ e.g.
▪ Program Counter (PC)
▪ Status Register
▪ Stack pointer (SP)
▪ Input/Output Registers.
Week3 18
General Purpose Registers
⚫ A set of registers in the machine
⚫ Used for storing temporary data/results
⚫ For example
⚫ In (68K) instruction add d3, d5, operands are stored in
general registers d3 and d5, and the result are stored in d5.
⚫ Can be structured differently in different machines
⚫ For example
⚫ Separated general purpose registers for data and address
▪ 68K
⚫ Different numbers registers and different size of each
registers
▪ 32 32-bit in MIPS
Week3 19
▪ 16 32-bit in ARM
Program counter
⚫ Special register
⚫ For storing memory address of currently executed
instruction
⚫ Can be of different size
⚫ E.g. 16 bit, 32 bit
⚫ Can be auto-incremented
⚫ By the instruction word size
⚫ Gives rise the name “counter”
Week3 20
Status register
⚫ Contains a number of bits with each bit
associated with CPU operations
⚫ Typical status bits
⚫ V: Overflow
⚫ C: Carry
⚫ Z: Zero
⚫ N: Negative
⚫ Used for controlling program execution flow
Week3 21
Memory models
⚫ Data processed by CPU is usually large and cannot
be held in the registers at the same time.
⚫ Both data and program code need to be stored in
memory.
⚫ Memory model is related to how memory is used to
store data
⚫ Issues
⚫ Addressable unit size
⚫ Address spaces
⚫ Endianness
⚫ Alignment Week3 22
Addressable unit size
⚫ Memory has units, each of which has an
address
⚫ Most common unit size is 8 bits (1 byte)
⚫ Modern processors have multiple-byte unit
⚫ For example:
⚫ 32-bit instruction memory in MIPS
⚫ 16-bit Instruction memory in AVR
Week3 23
Address spaces
⚫ The range of addresses a processor can
access.
⚫ The address space can be one or more than one
in a processor. For example
⚫ Princeton architecture or Von Neumann architecture
▪ A single linear address space for both instructions and data
memory
⚫ Harvard architecture
▪ Separate address spaces for instructions and data
memories
Week3 24
Address spaces (cont.)
⚫ Address space is not necessarily just for
memories
⚫ E.g, all general purpose registers and I/O
registers can be accessed through memory
addresses in AVR
⚫ Address space is limited by the width of the
address bus.
⚫ The bus width: the number of bits the address is
represented
Week3 25
Endianness
⚫ Memory objects
⚫ Memory objects are basic entities that can be
accessed as a function of the address and the
length
⚫ E.g. bytes, words, longwords
⚫ For large objects (>byte), there are two
ordering conventions
⚫ Little endian – little end (least significant byte)
stored first (at lowest address)
⚫ Intel microprocessors (Pentium etc)
⚫ Big endian – big end stored first
Week3 26
⚫ SPARC, Motorola microprocessors
Endianness (cont.)
⚫ Most CPUs produced since ~1992 are
“bi-endian” (support both)
⚫ some switchable at boot time
⚫ others at run time (i.e. can change dynamically)
Week3 27
Big Endian & Little Endian
⚫ Example: 0x12345678—a long word of 4
bytes. It is stored in the memory at address
0x00000100
Address data
⚫ big endian: 0x00000100 12
0x00000101 34
0x00000102 56
0x00000103 78
Address data
⚫ little endian: 0x00000100 78
0x00000101 56
0x00000102 34
0x00000103
Week3
12 28
Alignment
⚫ Often multiple bytes can be fetched from
memory
⚫ Alignment specifies how the (beginning)
address of a multiple-byte data is determined.
⚫ data must be aligned in some way. For example
⚫ 4-byte words starting at addresses 0,4,8, …
⚫ 8-byte words starting at addresses 0, 8, 16, …
⚫ Alignment makes memory data accessing
more efficient
Week3 29
Example
⚫ A hardware design that has data fetched from
memory every 4 bytes
Week3 30
Instruction format
⚫ Is a definition
⚫ how instructions are represented in binary code
⚫ Instructions typically consist of
⚫ Opcode (Operation Code)
⚫ defines the operation (e.g. addition)
⚫ Operands
⚫ what’s being operated on
⚫ Instructions typically have 0, 1, 2 or 3
operands
Week3 31
Instruction format examples
Week3 32
Example (AVR instruction)
⚫ Subtraction with carry
⚫ Syntax: sbc Rd, Rr
⚫ Operation: Rd ← Rd – Rr – C
⚫ Rd: Destination register. 0 d 31
⚫ Rr: Source register. 0 r 31, C: Carry
⚫ Instruction format
0 0 0 0 1 0 r d d d d d r r r r
15 0
Week3 34
Instruction encoding
⚫ Operation Encoding
⚫ 2n operations needs at least n bits
⚫ Operand Encoding
⚫ Depends on the addressing modes and access
space.
⚫ For example: An operand in direct register addressing
mode requires at most 3 bits if the the number of
registers it can be stored is 8.
⚫ With a fixed instruction length, more encoding
of operations means less available bits for
encoding operands
Week3 35
⚫ Tradeoffs should be concerned
Example 1
⚫ A machine has:
⚫ 16 bit instructions
⚫ 16 registers (i.e. 4-bit register addresses)
⚫ Instructions could be formatted like this:
Week3 37
Addressing modes
⚫ Instructions need to specify where to get operands from
⚫ Some possibilities
⚫ Values are in the instruction
⚫ Values are in the register
⚫ Register number is in the instruction
⚫ Values are in memory
⚫ address is in instruction
⚫ address is in a register
▪ register number is in the instruction
⚫ address is register value plus some offset
▪ register number is in the instruction
▪ offset is in the instruction (or in a register)
⚫ These ways of specifying the operand locations are called
addressing modes
Week3 38
Immediate Addressing
⚫ The operand is from the instruction itself
⚫ I.e the operand is immediately available from the
instruction
⚫ For example, in 68K
addw #99, d7
Week3 40
Memory direct addressing
⚫ The data is from memory, the memory
address is directly given by the instruction
⚫ We use notion: (addr) to represent memory
value with a given address, addr
⚫ For example, in 68K
addw 0x123A, d7
⚫ d7 d7 + (a0); a0 a0 + 2
Week3 43
Memory Register Indirect
Auto-decrement
⚫ The data is from memory, the memory
address is given by a register and the register
number is directly given by the instruction;
but the value of the register is automatically
decreased before such an operation.
⚫ Think --i in C
⚫ For example, in 68K
addw -(a0),d7
⚫ a0 a0 –2; d7 d7 + (a0);
Week3 44
Memory Register Indirect with
Displacement
⚫ Data is from the memory with the address
given by the register plus a constant
⚫ Used in the access of a member in a data
structure
⚫ For example, in 68K
addw a0@(8), d7
⚫ d7 (a0+8) +d7
Week3 45
Address Register Indirect with
Index and displacement
⚫ The address of the data is sum of the initial address
and the index address as compared to the initial
address plus a constant
⚫ Used in accessing element of an array
⚫ For example, in 68K
addw a0@(d3)8, d7
⚫ d7 (a0 + d3+8)
⚫ With a0 as an initial address and d3 as an index
dynamically pointing to different elements, plus a constant
for a certain member in an array element.
Week3 46
RISC
⚫ RICS stands for reduced instruction set
computer
⚫ Smaller and simpler set of instructions
⚫ Smaller: small number of instructions in the instruction
set
⚫ Simpler: instruction encoding is simple
▪ Such as fixed instruction length
⚫ All instructions take about the same amount of
time to execute
Week3 47
CISC
⚫ CISC stands for complex instruction set
computer
⚫ Each instructions can execute several low-level
operations
⚫ Such operations of load memory, arithmetic and store
memory in one instructions
⚫ Required complicated hardware support
⚫ All instructions take different amount of time to
execute
Week3 48
Recall: Typical processors
⚫ Most commonly implemented in hardware
⚫ 68K
⚫ Motorola
⚫ x86
⚫ Intel
⚫ IA-64
⚫ Intel
⚫ MIPS
⚫ Microprocessor without interlocked pipeline stages
⚫ ARM
⚫ Advanced RISC Machine
⚫ PowerPC
⚫ Apple-IBM-Motorola alliance
Week3 49
⚫ Atmel AVR
X86
⚫ CISC architecture
⚫ 16 bit → 32-bit → 64-bit
⚫ Words are stored in the little endian order
⚫ Allow unaligned memory access.
⚫ Current x86-processors employs a few “extra”
decoding steps to (during execution) split (most) x86
instructions into smaller pieces (micro-instructions)
which are then readily executed by a RISC-like
micro-architecture.
⚫ Application areas (dominant)
⚫ Desktop, portable computer, small servers
Week3 50
68K
⚫ CISC processor
⚫ Early generation, hybrid 8/16/32 bit chip (8-bit
bus)
⚫ Late generation, fully 32-bit
⚫ Separate data registers and address registers
⚫ Big endian
⚫ Area applications
⚫ Early used in for calculators, control systems,
desktop computers
⚫ Later used in microcontroller/embedded
Week3 51
microprocessors.
MIPS
⚫ RISC processor
⚫ A large family designs with different configurations
⚫ Deep pipeline (>=5 stages)
⚫ With additional features
⚫ Clean instruction set
⚫ Could be booted either big-endian or little-endian
⚫ Many application areas, including embedded
systems
⚫ The design of the MIPS CPU family, together with
SPARC, another early RISC architecture, greatly
influenced later RISC designs
Week3 52
ARM
⚫ 32-bit RISC processor
⚫ Three-address architecture
⚫ No support for misaligned memory accesses
⚫ 16 x 32 bit register file
⚫ Fixed opcode width of 32 bit to ease decoding and
pipelining, at the cost of decreased code density
⚫ Mostly single-cycle execution
⚫ With additional features
⚫ Conditional execution of most instructions
▪ reducing branch overhead and compensating for the lack of
a branch predictor
▪ Powerful indexed addressing modes
Week3 53
⚫ Power saving
PowerPC
⚫ Superscalar RISC
⚫ 32-bit, 64-bit implementation
⚫ With both big-endian and little endian modes,
can switch from one mode to the other at run-
time.
⚫ Intended for high performance PC, for high-
end machines
Week3 54
Reading Material
⚫ Chap.2 in Microcontrollers and
Microcomputers.
Week3 55
Questions
1. Given an address bus width in a
processor as 16-bit, determine the
maximal address space.
2. Assume a memory address is 0xFFFF,
how many locations this address can
represent if the related computer is?
I) a Harvard machine
II) a Von Neumann machine
Week3 56
EEE 3132 - Week 4 & 5
The 80x86 Microprocessor
Architecture
Brief History of the 80x86 Family
• Evolution from 8080/8085 to 8086
– In 1987, Intel introduced a 16-bit microprocessor called the 8086
– It was a major improvement over the previous generation 8080/8085
microprocessors
• 1 Mbyte memory (20 address lines) vs 8080/8085’s capability of 64 Kbytes
• 8080/8085 was an 8 bit system, meaning that the data larger than 8 bits should
be broken into 8-bit pieces to be processed by the CPU; in contrast 8086 is a 16
bit microprocessor
• 8086 is pipelined vs nonpipelined 8080/8085; in a system with pipelining the
data and address busses are busy transferring data while the CPU is processing
information
• Evolution from 8086 to 8088
– 8086 is a microprocessor with a 16-bit data bus internally and externally
– Internal because all registers are 16 bits wide
– External because the data bus was 16 bits to transfer data in and out of the
CPU
– There was a resistance in using the 16 bit external data bus since at that
time peripherals were designed around 8-bit microprocessors
– Intel then came out with the 8088 version with 8-bit data bus
2
Brief History - Continued
• Success of 8088
– IBM picked up the 8088 as their microprocessor of choice in designing
the IBM PC
– All specification of the hardware and software of the PC are made
public by IBM and Microsoft (in contrast with Apple computers)
• Other microprocessors: 80386, 80386, 80486
– Intel introduced 80286 in 1982
– 16 bit internal and external data buses
– 24 address lines (16 Mbyte main memory)
– Virtual memory: a way of fooling the microprocessor into thinking that it
has access to almost unlimited amount of memory by swapping data
between disk storage and RAM
– Real mode vs protected mode
– Intel unveiled the 80386 (sometimes called the 80386DX) in 1985;
internally and externally a 32 bit microprocessor with a 32 bit address
bus (4 Gbyte physical memory)
– Numeric data processing chips were made available: 8087, 80287,
80387 etc.
3
Evolution of Intel’s microprocessors
4
Virtual 8086 Mode
• Real Mode
– Only one program can be run one time
– All of the protection and memory management functions are turned off
– Memory space is limited to 1MB
• Virtual 8086 Mode
– The 386 hands each real mode program its own 1MB chunk of memory
– Multiple 8086 programs to be run simultaneously but protected from
each other (multiple MSDOS prompts)
– Due to time sharing, the response becomes much slower as each new
program is launched
– The 386 can be operated in Protected Mode and Virtual 8086 mode at
the same time.
– Because each 8086 task is assigned the lowest privilege level, access
to programs or data in other segments is not allowed thus protecting
each task.
– We’ll be using the virtual 8086 mode in the lab experiments on PCs that
do have either Pentiums or 486s.
5
The 80286 and above - Modes of Operation
•Real Mode
•The address space is limited to 1MB using address lines A0-19;
the high address lines are inactive
•The segmented memory addressing mechanism of the 8086 is retained
with each segment limited to 64KB
•Two new features are available to the programmer
–Access to the 32 bit registers
–Addition of two new segments F and G
•Protected Mode
–Difference is in the new addressing mechanism and protection levels
–Each memory segment may range from a single byte to 4GB
–The addresses stored in the segment registers are now interpreted as
pointers into a descriptor table
–Each segment’s entry in this table is eight bytes long and identifies the
base address of the segment, the segment size, and access rights
–In 8088/8086 any program can access the core of the OS hence crash the
system. Access Rights are added in descriptor tables. 6
Brey 59
Virtual Memory
• 286 onward supported Virtual Memory Management and Protection
• Unlimited amount of main memory assumed
• Two methods are used:
– Segmentation
– Paging
• Both techniques involve swapping blocks of user memory with hard disk
space as necessary
– If the program needs to access a block of memory that is indicated to be stored in
the disk, the OS searches for an available memory block (typically using a least
recently used algorithm) and swaps that block with the desired data on the hard
drive
– Memory swapping is invisible to the user
– Segmentation: the block size is variable ranging up to 4GB
– Paging: Block sizes are always 4 KB at a time.
• A final protected mode feature is the ability to assign a privilege level to
individual tasks (programs). Tasks of lower privilege level cannot access
programs or data with a higher privilege level. The OS can run multiple
programs each protected from each other.
Mazidi 648
7
The 8086 and 8088
• The 8086 microprocessor represents the foundation upon which all
the 80x86 family of processors have been built
• Intel has made the commitment that as new generations of
microprocessors are developed, each will maintain software
compatibility with this first generation part.
– For example, a program designed to run on an Intel 386
microprocessor, which also runs on a Pentium, is upward compatible.
• Processor model
– BIU (Bus Interface Unit) provides hardware functions including
generation of the memory and I/O addresses for the transfer of data
between itself and the outside world
– EU (Execution Unit) receives program instruction codes and data from
the BIU executes these instructions and stores the results in the general
registers.
– EU has no connection to the system busses; it receives and outputs all
its data through the BIU.
8
Execution and Bus Interface Units
9
Fetch and Execute Cycle
• Fetch and execute cycles overlap
– BIU outputs the contents of the IP onto the address bus
– Register IP is incremented by one or more than one for the next
instruction fetch
– Once inside the BIU, the instruction is passed to the queue; this queue
is a first-in-first-out register sometimes likened to a pipeline
– Assuming that the queue is initially empty the EU immediately draws
this instruction from the queue and begins execution
– While the EU is executing this instruction, the BIU proceeds to fetch a
new instruction.
• BIU will fill the queue with several new instructions before the EU is ready to
draw its next instruction
– The cycle continues with the BIU filling the queue with instructions and
the EU fetching and executing these instructions
10
Pipelined Architecture
• Three conditions that will cause the EU to enter a wait mode
– when the instruction requires access to a memory location not in the
queue
– when the instruction to be executed is a jump instruction; the instruction
queue should be flushed out (known as branch penalty too much
jumping around reduces the efficiency of the program)
– during the execution of slow instructions
• for example the instruction AAM (ASCII Adjust for Multiplication) requires 83
clock cycles to complete for an 8086
• 8086 vs 8088
– BIU data bus width 8 bits for 8088, BIU data bus width 16 bits for 8086
– 8088 instruction queue is four bytes instead of six
– 8088 is found to be 30% slower than 8086
• WHY
– Long instructions provide more time for the BIU to fill the queue
11
Nonpipelined vs pipelined architecture
Time
Non-pipelined architecture
BIU
F F F F F F Read
Data Fd Fd Fd F F
EU
Wait E E E Er Wait E E Ej Wait E
Pipelined architecture
Er: a request for data not in the queue
Ej: jump instruction occurs Fd: Discarded
12
Registers of the 8086/80286 by Category
Category Bits Register Names
General 16 AX,BX,CX,DX
8 AH,AL,BH,BL,CH,CL,DH,DL
13
General Purpose Registers
H L
15 8 7 0
AX (Accumulator)
AH AL
BX (Base Register)
BH BL
CX (Used as a counter)
CH CL
DX (Used to point to data in I/O operations)
DH DL
Register Operations
AX Word multiply, word divide, word I/O
15
Pointer and Index Registers
SP Stack Pointer
BP Base Pointer
SI Source Index
DI Destination Index
IP Instruction Pointer
Source code 17
Edit, Assemble, Test, and Debug Cycle
• Using an editor, the source code of the program is
created. This means selecting the appropriate instruction
mnemonics to accomplish the task
• A compiler program which examines the source code file
generated by the editor and determines the object code for
each instruction in the program, is then run. In assembly
language programming, this is called an assembler
(MASM (Chapter 2 of the textbook, DEBUG: Appendix A
of the textbook, etc., )
• The object code produced by the computer is loaded into
the target computer’s memory and is then run.
• Debugging: locating and fixing the source of error
• High-level programming Languages
– Basic, Pascal, C, C++
18
MOV Instruction
• MOV destination,source
– 8 bit moves
• MOV CL,55h
• MOV DL,CL
• MOV BH,CL
• Etc.
– 16 bit moves
• MOV CX,468Fh
• MOV AX,CX
• MOV BP,DI
• Etc.
19
MOV Instruction
• Data can be moved among all registers but data cannot be
moved directly into the segment registers (CS,DS,ES,SS).
– To load as such, first load a value into a non-segment register and then
move it to the segment register
MOV AX,2345h
MOV DS,AX
• If a value less than than FFh is moved into a 16 bit register. The
rest of the bits are assumed to be all zeros.
20
MOV Instruction
• MOV AX,58FCH
• MOV DX,6678H
• MOV SI,924BH
• MOV BP,2459H
• MOV DS,2341H x
• MOV CX,8876H
• MOV CS,3F47H x
• MOV BH,99H
21
ADD Instruction
• ADD destination,source
• The ADD instruction tells the CPU to add the source and destination
operands and put out the results in the destination
MOV AL,25H
MOV BL,34h
ADD AL,BL ; (AL should read 59h once the instruction is executed)
MOV DH,25H
ADD DH,34h ; (AL should read 59h once the instruction is executed)
Immediate operand
22
Origin and Definition of a Segment
23
Advantages of Segmented Memory
• One program can work on several different sets of data. This is done
by reloading register DS to a new value.
• Programs that reference logical addresses can be loaded and run
anywhere in the memory: relocatable
• Segmented memory introduces extra complexity in both hardware in
that memory addresses require two registers.
• They also require complexity in software in that programs are limited
to the segment size
• Programs greater than 64 KB can be run on 8086 but the software
needed is more complex as it must switch to a new segment.
• Protection among segments is provided.
24
Segment Registers
25
Logical and Physical Addresses
• Addresses within a segment can range from address 0 to address
FFFFh. This corresponds to the 64Kbyte length of the segment
called an offset
• An address within a segment logical address
• Ex. Logical address 0005h in the code segment actually
corresponds to B3FF0h + 5 = B3FF5h. Example 1:
15 0 Segment base value: 1234h
Offset: 0022h
OFFSET VALUE
12340h
+ 0022h
19 5 0
12362h is the physical 20 bit address
SEGMENT REGISTER 0h
Two different logical addresses may
correspond to the same physical
address.
ADDER D470h in ES 2D90h in SS
ES:D470h SS:2D90h
20 BIT PHYSICAL ADDRESS
26
Example
•If DS=7FA2H and the offset is 438EH
a) Calculate the physical address 8FA1F
mazidi
27
Example
Question:
Assume DS=578C. To access a Data in 67F66 what should we do?
67F66
678BF
DS=578C capability
change
DS 578C0
To any value
between
57F7h - 67F6h
28
Code Segment
• To execute a program, the 8086 fetches the instructions
(opcodes and operands) from the code segment
• The logical address is in the form CS:IP
29
Logical Address vs Physical Address in the CS
31
Data Segment
• The data is first placed in the memory locations
DS:0200 = 25h
DS:0201 = 12h
DS:0202 = 15h
DS:0203 = 1Fh
DS:0204 = 2Bh
• Then the program is written as
MOV AL,0
ADD AL,[0200] ; bracket means add the contents of DS:0200 to AL
ADD AL,[0201]
ADD AL,[0202]
ADD AL,[0203]
ADD AL,[0204]
33
16 bit Segment Register Assignments
String ES None DI
Destination
Brey
34
Little Endian Convention
“Little Endian” means that the low-order byte of the number
is stored in memory at the lowest address, and the high-
order byte at the highest address. (The little end comes
first.)
Intel uses Little Endian Convention.
For example, a 4 byte LongInt
Byte3 | Byte2 | Byte1 |Byte0 will be arranged in memory
as follows: • Adobe Photoshop -- Big Endian
Base Address+0 Byte0 • BMP (Windows and OS/2 Bitmaps) – little Endian
Base Address+1 Byte1 • GIF -- Little Endian
Base Address+2 Byte2 • IMG (GEM Raster) -- Big Endian
• JPEG -- Big Endian
Base Address+3 Byte3
35
Computer Operating Systems
• What happens when the computer is first turned on?
• MS-DOS
– A startup program in the BIOS (Basic Input Output System) is
executed
– This program in turn accesses the master boot record on the
floppy or hard disk drive
– A loader then transfers the system files IO.SYS
– IO.SYS calls MSDOS.SYS. MS-DOS.SYS is basically the kernel of the
operating system.
– After initializing, MS-DOS.SYS then calls the command interpreter
COMMAND.COM which is loaded into memory. This puts the
DOS prompt on the screen that gives the user access to DOS’s
built-in commands like DIR, COPY, VER.
36
Memory Map of a PC
38
More About RAM
• Memory management is one of the most important
functions of the DOS operating systems and should be
left to DOS
• Therefore, we do not assign any values for the
DS,CS,SS registers; this is the job of DOS
• It is very important to remember that
– The DS,CS, and DS values we will experiment will be different
than those used by the textbook; do not worry
39
Flag (Status) Register
15 FlagsH FlagsL 0
X X X X OF DF IF TF SF ZF X AF X PF X CF
• Six of the flags are status indicators reflecting properties
of the last arithmetic or logical instruction.
• For example, if register AL = 7Fh and the instruction
ADD AL,1 is executed then the following happen
– AL = 80h
– CF = 0; there is no carry out of bit 7
– PF = 0; 80h has an odd number of ones
– AF = 1; there is a carry out of bit 3 into bit 4
– ZF = 0; the result is not zero
– SF = 1; bit seven is one
– OF = 1; the sign bit has changed
• Can be used to transfer program control to a new
memory location
ADD AL,1
JNZ 0100h 40
Example
• Show how the flag register is
affected by
– MOV AX, 34F5h
– ADD AX,95EBh Aux carry
41
TF, IF, and DF
• Three of the flags can be set or reset directly by the programmer
ands are used to control the operation of the microprocessor, these
are TF, IF, and DF.
• When TF (Trap Flag) is set, control is passed to special address
after each instruction is executed. Normally a program to display all
the registers and flags is stored there. Single-stepping mode.
• When IF (Interrupt Flag) is set, external interrupt requests on the
8086’s interrupt line INTR is enabled.
– For example a printer may spend several seconds printing a page of
text from its internal buffer
– When it is ready for new data, the printer control circuit drives the
8086’s INTR input line
– The processor then suspends whatever it is doing and begins running
the printer interrupt service routine (ISR)
– When the routine has finished via a IRET (interrupt return) instruction
control is transferred back to the original instruction in the main program
that was executing when the interrupt occurred
– Hardware and software interrupts
• DF (Direction Flag ) is used with block move instructions (more later!!).
– DF = 1 then the block memory pointer will automatically decrement
– DF = 0, then the block memory pointer will automatically increment
42
Memory Address Space and Organization
• Word
• Double Word
• Aligned Word
• Misaligned Word
43
Even addressed and odd-addressed banks
44
Dedicated, Reserved and General Purpose Memory
•Some address locations have dedicated functions and should not be used as
general memory for storage of data or instructions of a program
FFFFF
BIOS System ROM (Dedicated)
F0000
System Area Video & H/D Controller BIOS ROM 384 K
(Dedicated)
BFFFF
Video RAM (128 K)
A0000
9FFFF
TPA
Transient 1 MB
FREE TPA
Program 640 K
Area I/O.SYS--COMMAND.COM-- MSDOS
PUSH POP
End of
SS:0000h stack
SP
Top of
SS:SP stack
SS Bottom of
SS:FFFEh Stack
46
Example for PUSH
• Given
– SS = 0105h
– SP = 0008h
– AX = 1234h
– What is the outcome of the PUSH AX instruction?
• ABOS = 01050 + FFFEh = 1104h
• ATOS = 01050 + 0008h = 1058h
47
Example for POP
48
Addressing Modes
49
Direct Addressing Mode
MOV CX, [address]
Example:
MOV AL,[03]
AL=?
BEED
02003 FF
50
Register Indirect Addressing Mode
BX
MOV AX, DI
SI
BEED
51
Example for Register Indirect Addressing
• Assume that DS=1120, SI=2498 and AX=17FE show the memory
locations after the execution of:
MOV [SI],AX
52
Based-Relative Addressing Mode
DS:BX
MOV AH, [ ] + 1234h
SS:BP
3AH
BX +
AX
DS
1234
53
Indexed Relative Addressing Mode
SI ] + 1234h
MOV AH, [ DI
Example: What is the physical address MOV [DI-8],BL if DS=200 & DI=30h ?
DS:200 shift left once 2000 + DI + -8 = 2028 54
Based-Indexed Relative Addressing Mode
• Based Relative + Indexed Relative
• We must calculate the PA (physical address)
CS
SS BX SI 8 bit displacement
PA= DS : BP + DI + 16 bit displacement
ES
55
Based-Indexed Addressing Mode
OR
56
Summary of the addressing modes
Addressing Mode Operand Default Segment
Direct [offset] DS
57
16 bit Segment Register Assignments
String ES None DI
Destination
Brey
58
Segment override
Segment CS DS ES SS
Registers
Offset Register IP SI,DI,BX SI,DI,BX SP,BP
59
Example for default segments
• The following registers are used as offsets. Assuming that the
default segment used to get the logical address, give the segment
register associated?
SS(0)=30000
30000+4000+7000+10=3B010
60
Assembly Language
• There is a one-to-one relationship between assembly and machine
language instructions
• What is found is that a compiled machine code implementation of a
program written in a high-level language results in inefficient code
– More machine language instructions than an assembled version of an
equivalent handwritten assembly language program
• Two key benefits of assembly language programming
– It takes up less memory
– It executes much faster
61
Languages in terms of applications
62
Converting Assembly Language
Instructions to Machine Code
OPCODE D W MOD REG R/M
64
Continued
• 2-bit MOD field and 3-bit R/M field together specify the second
operand
65
Examples
• MOV BL,AL
• Opcode for MOV = 100010
• We’ll encode AL so
– D = 0 (AL source operand)
• W bit = 0 (8-bits)
• MOD = 11 (register mode)
• REG = 000 (code for AL)
• R/M = 011
OPCODE D W MOD REG R/M
100010 0 0 11 000 011
MOV BL,AL => 10001000 11000011 = 88 C3h
ADD AX,[SI] => 00000011 00000100 = 03 04 h
ADD [BX][DI] + 1234h, AX => 00000001 10000001 h
=> 01 81 34 12 h 66
Software
• The sequence of commands used to tell a microcomputer what to do is
called a program
• Each command in a program is called an instruction
• 8088 understands and performs operations for 117 basic instructions
• The native language of the IBM PC is the machine language of the
8088
• A program written in machine code is referred to as machine code
• In 8088 assembly language, each of the operations is described by
alphanumeric symbols instead of just 0s or 1s.
ADD AX, BX
Source operand
Opcode
Destination operand
67
Instructions
Address identifier
Max 31 characters Does not generate any machine code
Instruction
: indicates it opcode
generating instruction
68
DEBUG program instruction set (page 825 mzd)
• Debug instructions
• List of commands
– a Assemble [address] you can type in code this way
– c range address ; compare c 100 105 200
– d [range] ; Dump d 150 15A
– e address [list] ; Enter e 100
– f Fill range list F 100 500 ‘ ‘
– g Go [=address] addresses runs the program
– h Value1 Value2 ; addition and subtraction H 1A 10
– i Input port I 3F8
– r Show & change registers Appears to show the same thing as t,
but doesn't cause any code to be executed.
– t Trace either from the starting address or current location.
– u UnAssemble
69
Some examples with debug
0100 mov ax,24b6
0103 mov di, 85c2
0106 mov dx,5f93
0109 mov sp,1236
010c push ax
010d push di
010e int 3
70
Some examples with DEBUG
• 0100 mov al,9c
• 0102 mov dh,64
• 0104 add al,dh
• 0109 int 3
71
Example
MOV AX,2000
MOV DS,AX 100-10f
MOV SI, 100
MOV DI, 120
MOV CX, 10
120-12f
NXTPT: MOV AH, [SI]
MOV [DI], AH
INC SI
INC DI
DEC CX
JNZ NXTPT
72
Dec Hex Bin
2 2 00000010
ORG ; THREE
Assembly
Language
Programming
The x86 PC
Assembly Language, Design, and Interfacing © 2010, 2003, 2000, 1998 Pearson Higher Education, Inc.
By Muhammad Ali Mazidi, Janice Gillespie Mazidi and Danny Causey Pearson Prentice Hall - Upper Saddle River, NJ 07458
OBJECTIVES
this chapter enables the student to:
• Flag concepts
• Instruction Types in 8086
• Assembly language program basics.
• Flow charts summary
• Code simple Assembly language instructions.
• Assemble, link, and run a simple Assembly language
program.
• Procedures
• Code control transfer instructions such as conditional
and unconditional jumps and call instructions.
The x86 PC
Assembly Language, Design, and Interfacing © 2010, 2003, 2000, 1998 Pearson Higher Education, Inc.
By Muhammad Ali Mazidi, Janice Gillespie Mazidi and Danny Causey Pearson Prentice Hall - Upper Saddle River, NJ 07458
FLAG REGISTER
The x86 PC
Assembly Language, Design, and Interfacing © 2010, 2003, 2000, 1998 Pearson Higher Education, Inc.
By Muhammad Ali Mazidi, Janice Gillespie Mazidi and Danny Causey Pearson Prentice Hall - Upper Saddle River, NJ 07458
FLAG REGISTER
– These six are CF, PF, AF, ZF, SF, and OF.
– The remaining three, often called control flags, control
the operation of instructions before they are executed.
The x86 PC
Assembly Language, Design, and Interfacing © 2010, 2003, 2000, 1998 Pearson Higher Education, Inc.
By Muhammad Ali Mazidi, Janice Gillespie Mazidi and Danny Causey Pearson Prentice Hall - Upper Saddle River, NJ 07458
bits of the flag register
– SF (Sign Flag) - Binary representation of signed numbers uses the most significant bit as the
sign bit.
• After arithmetic or logic operations, the status of this sign
bit is copied into the SF, indicating the sign of the result.
– TF (Trap Flag) - When this flag is set it allows the program to single-step, meaning to execute
one instruction at a time.
• Single-stepping is used for debugging purposes.
The x86 PC
Assembly Language, Design, and Interfacing © 2010, 2003, 2000, 1998 Pearson Higher Education, Inc.
By Muhammad Ali Mazidi, Janice Gillespie Mazidi and Danny Causey Pearson Prentice Hall - Upper Saddle River, NJ 07458
bits of the flag register
The x86 PC
Assembly Language, Design, and Interfacing © 2010, 2003, 2000, 1998 Pearson Higher Education, Inc.
By Muhammad Ali Mazidi, Janice Gillespie Mazidi and Danny Causey Pearson Prentice Hall - Upper Saddle River, NJ 07458
flag register and ADD instruction
The x86 PC
Assembly Language, Design, and Interfacing © 2010, 2003, 2000, 1998 Pearson Higher Education, Inc.
By Muhammad Ali Mazidi, Janice Gillespie Mazidi and Danny Causey Pearson Prentice Hall - Upper Saddle River, NJ 07458
flag register and ADD instruction
The x86 PC
Assembly Language, Design, and Interfacing © 2010, 2003, 2000, 1998 Pearson Higher Education, Inc.
By Muhammad Ali Mazidi, Janice Gillespie Mazidi and Danny Causey Pearson Prentice Hall - Upper Saddle River, NJ 07458
flag register and ADD instruction
The x86 PC
Assembly Language, Design, and Interfacing © 2010, 2003, 2000, 1998 Pearson Higher Education, Inc.
By Muhammad Ali Mazidi, Janice Gillespie Mazidi and Danny Causey Pearson Prentice Hall - Upper Saddle River, NJ 07458
flag register and ADD instruction
The x86 PC
Assembly Language, Design, and Interfacing © 2010, 2003, 2000, 1998 Pearson Higher Education, Inc.
By Muhammad Ali Mazidi, Janice Gillespie Mazidi and Danny Causey Pearson Prentice Hall - Upper Saddle River, NJ 07458
flag register and ADD instruction
The x86 PC
Assembly Language, Design, and Interfacing © 2010, 2003, 2000, 1998 Pearson Higher Education, Inc.
By Muhammad Ali Mazidi, Janice Gillespie Mazidi and Danny Causey Pearson Prentice Hall - Upper Saddle River, NJ 07458
use of the zero flag for looping
The x86 PC
Assembly Language, Design, and Interfacing © 2010, 2003, 2000, 1998 Pearson Higher Education, Inc.
By Muhammad Ali Mazidi, Janice Gillespie Mazidi and Danny Causey Pearson Prentice Hall - Upper Saddle River, NJ 07458
use of the zero flag for looping
The x86 PC
Assembly Language, Design, and Interfacing © 2010, 2003, 2000, 1998 Pearson Higher Education, Inc.
By Muhammad Ali Mazidi, Janice Gillespie Mazidi and Danny Causey Pearson Prentice Hall - Upper Saddle River, NJ 07458
use of the zero flag for looping
The x86 PC
Assembly Language, Design, and Interfacing © 2010, 2003, 2000, 1998 Pearson Higher Education, Inc.
By Muhammad Ali Mazidi, Janice Gillespie Mazidi and Danny Causey Pearson Prentice Hall - Upper Saddle River, NJ 07458
use of the zero flag for looping
The x86 PC
Assembly Language, Design, and Interfacing © 2010, 2003, 2000, 1998 Pearson Higher Education, Inc.
By Muhammad Ali Mazidi, Janice Gillespie Mazidi and Danny Causey Pearson Prentice Hall - Upper Saddle River, NJ 07458
use of the zero flag for looping
The x86 PC
Assembly Language, Design, and Interfacing © 2010, 2003, 2000, 1998 Pearson Higher Education, Inc.
By Muhammad Ali Mazidi, Janice Gillespie Mazidi and Danny Causey Pearson Prentice Hall - Upper Saddle River, NJ 07458
Addressing Modes
The x86 PC
Assembly Language, Design, and Interfacing © 2010, 2003, 2000, 1998 Pearson Higher Education, Inc.
By Muhammad Ali Mazidi, Janice Gillespie Mazidi and Danny Causey Pearson Prent ic eHall - Upper Saddle River, NJ 07458
18
Direct Addressing Mode
BEED Example:
MOV AL,[03]
AL=?
02003 FF
The x86 PC
Assembly Language, Design, and Interfacing © 2010, 2003, 2000, 1998 Pearson Higher Education, Inc.
By Muhammad Ali Mazidi, Janice Gillespie Mazidi and Danny Causey Pearson Prent ic eHall - Upper Saddle River, NJ 07458
19
Register Indirect Addressing Mode
B
X
MOV AX, DI
SI
BEED
The x86 PC
Assembly Language, Design, and Interfacing © 2010, 2003, 2000, 1998 Pearson Higher Education, Inc.
By Muhammad Ali Mazidi, Janice Gillespie Mazidi and Danny Causey Pearson Prent ic eHall - Upper Saddle River, NJ 07458
20
Example for Register Indirect Addressing
• Assume that DS=1120, SI=2498 and AX=17FE show the memory locations
after the execution of:
MOV [SI],AX
The x86 PC
Assembly Language, Design, and Interfacing © 2010, 2003, 2000, 1998 Pearson Higher Education, Inc.
By Muhammad Ali Mazidi, Janice Gillespie Mazidi and Danny Causey Pearson Prent ic eHall - Upper Saddle River, NJ 07458
21
Based-Relative Addressing Mode
DS:BX
MOV AH, [ SS:BP ] + 1234h
3AH
BX
+
AX
DS
1234
The x86 PC
Assembly Language, Design, and Interfacing © 2010, 2003, 2000, 1998 Pearson Higher Education, Inc.
By Muhammad Ali Mazidi, Janice Gillespie Mazidi and Danny Causey Pearson Prent ic eHall - Upper Saddle River, NJ 07458
22
Indexed Relative Addressing Mode
MOV AH, [ SI
DI
] + 1234h
Example: What is the physical address MOV [DI-8],BL if DS=200 & DI=30h ?
DS:200 shift left once 2000 + DI + -8 = 2028
The x86 PC
Assembly Language, Design, and Interfacing © 2010, 2003, 2000, 1998 Pearson Higher Education, Inc.
By Muhammad Ali Mazidi, Janice Gillespie Mazidi and Danny Causey Pearson Prent ic eHall - Upper Saddle River, NJ 07458
23
Based-Indexed Relative Addressing Mode
CS
SS BX SI 8 bit displacement
PA= DS : BP + DI + 16 bit displacement
ES
The x86 PC
Assembly Language, Design, and Interfacing © 2010, 2003, 2000, 1998 Pearson Higher Education, Inc.
By Muhammad Ali Mazidi, Janice Gillespie Mazidi and Danny Causey Pearson Prent ic eHall - Upper Saddle River, NJ 07458
24
Based-Indexed Addressing Mode
OR
The x86 PC
Assembly Language, Design, and Interfacing © 2010, 2003, 2000, 1998 Pearson Higher Education, Inc.
By Muhammad Ali Mazidi, Janice Gillespie Mazidi and Danny Causey Pearson Prent ic eHall - Upper Saddle River, NJ 07458
25
Summary of the addressing modes
Direct [offset] DS
The x86 PC
Assembly Language, Design, and Interfacing © 2010, 2003, 2000, 1998 Pearson Higher Education, Inc.
By Muhammad Ali Mazidi, Janice Gillespie Mazidi and Danny Causey Pearson Prent ic eHall - Upper Saddle River, NJ 07458
26
16 bit Segment Register Assignments
Segment CS DS ES SS
Registers
Offset IP SI,DI,BX SI,DI,BX SP,BP
Register
Type of Memory Default Segment Alternate Segment Offset
Reference
The x86 PC
Assembly Language, Design, and Interfacing © 2010, 2003, 2000, 1998 Pearson Higher Education, Inc.
Brey By Muhammad Ali Mazidi, Janice Gillespie Mazidi and Danny Causey Pearson Prent ic eHall - Upper Saddle River, NJ 07458
27
Segment override
The x86 PC
Assembly Language, Design, and Interfacing © 2010, 2003, 2000, 1998 Pearson Higher Education, Inc.
By Muhammad Ali Mazidi, Janice Gillespie Mazidi and Danny Causey Pearson Prent ic eHall - Upper Saddle River, NJ 07458
28
Example for default segments
• The following registers are used as offsets. Assuming that the default
segment used to get the logical address, give the segment register
associated?
• Show the contents of the related memory locations after the execution of
this instruction
MOV [BP][SI]+10,DX
if DS=2000, SS=3000,CS=1000,SI=4000,BP=7000,DX=1299 (all hex)
SS(0)=30000
30000+4000+7000+10=3B010
The x86 PC
Assembly Language, Design, and Interfacing © 2010, 2003, 2000, 1998 Pearson Higher Education, Inc.
By Muhammad Ali Mazidi, Janice Gillespie Mazidi and Danny Causey Pearson Prent ic eHall - Upper Saddle River, NJ 07458
29
Assembly Language
The x86 PC
Assembly Language, Design, and Interfacing © 2010, 2003, 2000, 1998 Pearson Higher Education, Inc.
By Muhammad Ali Mazidi, Janice Gillespie Mazidi and Danny Causey Pearson Prent ic eHall - Upper Saddle River, NJ 07458
30
Languages in terms of applications
The x86 PC
Assembly Language, Design, and Interfacing © 2010, 2003, 2000, 1998 Pearson Higher Education, Inc.
By Muhammad Ali Mazidi, Janice Gillespie Mazidi and Danny Causey Pearson Prent ic eHall - Upper Saddle River, NJ 07458
31
Converting Assembly Language
Instructions to Machine Code
OPCODE D W MOD REG R/M
The x86 PC
Assembly Language, Design, and Interfacing © 2010, 2003, 2000, 1998 Pearson Higher Education, Inc.
By Muhammad Ali Mazidi, Janice Gillespie Mazidi and Danny Causey Pearson Prent ic eHall - Upper Saddle River, NJ 07458
32
Continued
• REG field is used to identify the register for the first operand
011 BL BX
100 AH SP
101 CH BP
110 DH SI
111 BH DI
The x86 PC
Assembly Language, Design, and Interfacing © 2010, 2003, 2000, 1998 Pearson Higher Education, Inc.
By Muhammad Ali Mazidi, Janice Gillespie Mazidi and Danny Causey Pearson Prent ic eHall - Upper Saddle River, NJ 07458
33
Continued
• 2-bit MOD field and 3-bit R/M field together specify the second operand
The x86 PC
Assembly Language, Design, and Interfacing © 2010, 2003, 2000, 1998 Pearson Higher Education, Inc.
By Muhammad Ali Mazidi, Janice Gillespie Mazidi and Danny Causey Pearson Prent ic eHall - Upper Saddle River, NJ 07458
34
2.6: FULL SEGMENT DEFINITION
the emu8086 assembler
• A simple, popular assembler for 8086 Assembly
language programs is called emu8086.
The x86 PC
Assembly Language, Design, and Interfacing © 2010, 2003, 2000, 1998 Pearson Higher Education, Inc.
By Muhammad Ali Mazidi, Janice Gillespie Mazidi and Danny Causey Pearson Prentice Hall - Upper Saddle River, NJ 07458
2.6: FULL SEGMENT DEFINITION
EXE vs. COM files
• The EXE file is used widely as it can be of any size.
– There are occasions when, due to a limited amount of memory,
one needs to have very compact code.
• COM files must fit in a single segment.
– The x86 segment size is 64K bytes, thus the COM file cannot be
larger than 64K.
• To limit the size to 64K requires defining the data inside
the code segment and using the end area
of the code segment for the stack.
– In contrast to the EXE file, the COM file has no separate data
segment definition.
The x86 PC
Assembly Language, Design, and Interfacing © 2010, 2003, 2000, 1998 Pearson Higher Education, Inc.
By Muhammad Ali Mazidi, Janice Gillespie Mazidi and Danny Causey Pearson Prentice Hall - Upper Saddle River, NJ 07458
2.6: FULL SEGMENT DEFINITION
EXE vs. COM files
• The header block, which occupies 512 bytes of memory, precedes every EXE
file.
– It contains information such as size, address location
in memory, and stack address of the EXE module.
– The COM file does not have a header block.
The x86 PC
Assembly Language, Design, and Interfacing © 2010, 2003, 2000, 1998 Pearson Higher Education, Inc.
By Muhammad Ali Mazidi, Janice Gillespie Mazidi and Danny Causey Pearson Prentice Hall - Upper Saddle River, NJ 07458
2.7: FLOWCHARTS AND PSEUDOCODE
structured programming
The x86 PC
Assembly Language, Design, and Interfacing © 2010, 2003, 2000, 1998 Pearson Higher Education, Inc.
By Muhammad Ali Mazidi, Janice Gillespie Mazidi and Danny Causey Pearson Prentice Hall - Upper Saddle River, NJ 07458
2.7: FLOWCHARTS AND PSEUDOCODE
structured programming
• Principles a structured program should follow:
– The program should be designed before it is coded.
• By using flowcharting or pseudocode, the design is clear
those coding, as well as those maintaining the program later.
– Use comments within the program and documentation.
• This will help other figure out what the program does
and how it does it.
– The main routine should consist primarily of calls to subroutines that
perform the work of the program.
• Sometimes called top-down programming.
• Using subroutines to accomplish repetitive tasks saves
time in coding, and makes the program easier to read.
The x86 PC
Assembly Language, Design, and Interfacing © 2010, 2003, 2000, 1998 Pearson Higher Education, Inc.
By Muhammad Ali Mazidi, Janice Gillespie Mazidi and Danny Causey Pearson Prentice Hall - Upper Saddle River, NJ 07458
2.7: FLOWCHARTS AND PSEUDOCODE
The x86 PC
Assembly Language, Design, and Interfacing © 2010, 2003, 2000, 1998 Pearson Higher Education, Inc.
By Muhammad Ali Mazidi, Janice Gillespie Mazidi and Danny Causey Pearson Prentice Hall - Upper Saddle River, NJ 07458
2.7: FLOWCHARTS AND PSEUDOCODE
flowcharts
The x86 PC
Assembly Language, Design, and Interfacing © 2010, 2003, 2000, 1998 Pearson Higher Education, Inc.
By Muhammad Ali Mazidi, Janice Gillespie Mazidi and Danny Causey Pearson Prentice Hall - Upper Saddle River, NJ 07458
2.7: FLOWCHARTS AND PSEUDOCODE
pseudocode
• An alternative to flowcharts, pseudocode, involves
writing brief descriptions of the flow of the code.
– SEQUENCE is executing instructions one after the other.
Figure 2-15
SEQUENCE
Pseudocode vs. Flowchart
The x86 PC
Assembly Language, Design, and Interfacing © 2010, 2003, 2000, 1998 Pearson Higher Education, Inc.
By Muhammad Ali Mazidi, Janice Gillespie Mazidi and Danny Causey Pearson Prentice Hall - Upper Saddle River, NJ 07458
2.7: FLOWCHARTS AND PSEUDOCODE
pseudocode
• An alternative to flowcharts, pseudocode, involves
writing brief descriptions of the flow of the code.
– IF-THEN-ELSE and IF-THEN are control programming structures,
which can indicate one statement or a group
of statements.
Figure 2-16
IF-THEN-ELSE
Pseudocode vs. Flowchart
The x86 PC
Assembly Language, Design, and Interfacing © 2010, 2003, 2000, 1998 Pearson Higher Education, Inc.
By Muhammad Ali Mazidi, Janice Gillespie Mazidi and Danny Causey Pearson Prentice Hall - Upper Saddle River, NJ 07458
2.7: FLOWCHARTS AND PSEUDOCODE
pseudocode
• An alternative to flowcharts, pseudocode, involves writing brief
descriptions of the flow of the code.
– IF-THEN-ELSE and IF-THEN are control programming structures,
which can indicate one statement or a group
of statements.
Figure 2-17
IF-THEN
Pseudocode vs. Flowchart
The x86 PC
Assembly Language, Design, and Interfacing © 2010, 2003, 2000, 1998 Pearson Higher Education, Inc.
By Muhammad Ali Mazidi, Janice Gillespie Mazidi and Danny Causey Pearson Prentice Hall - Upper Saddle River, NJ 07458
2.7: FLOWCHARTS AND PSEUDOCODE
pseudocode
• An alternative to flowcharts, pseudocode, involves writing brief
descriptions of the flow of the code.
– REPEAT-UNTIL and WHILE-DO are iteration control structures,
which execute a statement or group of statements repeatedly.
Figure 2-18
REPEAT-UNTIL
Pseudocode vs. Flowchart
The x86 PC
Assembly Language, Design, and Interfacing © 2010, 2003, 2000, 1998 Pearson Higher Education, Inc.
By Muhammad Ali Mazidi, Janice Gillespie Mazidi and Danny Causey Pearson Prentice Hall - Upper Saddle River, NJ 07458
2.7: FLOWCHARTS AND PSEUDOCODE
pseudocode
• An alternative to flowcharts, pseudocode, involves
writing brief descriptions of the flow of the code.
– REPEAT-UNTIL and WHILE-DO are iteration control structures,
which execute a statement or group of statements repeatedly.
Figure 2-19
WHILE-DO
Pseudocode vs. Flowchart
The x86 PC
Assembly Language, Design, and Interfacing © 2010, 2003, 2000, 1998 Pearson Higher Education, Inc.
By Muhammad Ali Mazidi, Janice Gillespie Mazidi and Danny Causey Pearson Prentice Hall - Upper Saddle River, NJ 07458
2.7: FLOWCHARTS AND PSEUDOCODE
control structures
The x86 PC
Assembly Language, Design, and Interfacing © 2010, 2003, 2000, 1998 Pearson Higher Education, Inc.
By Muhammad Ali Mazidi, Janice Gillespie Mazidi and Danny Causey Pearson Prentice Hall - Upper Saddle River, NJ 07458
2.7: FLOWCHARTS AND PSEUDOCODE
control structures
The x86 PC
Assembly Language, Design, and Interfacing © 2010, 2003, 2000, 1998 Pearson Higher Education, Inc.
By Muhammad Ali Mazidi, Janice Gillespie Mazidi and Danny Causey Pearson Prentice Hall - Upper Saddle River, NJ 07458
Assembly Language
The x86 PC
Assembly Language, Design, and Interfacing © 2010, 2003, 2000, 1998 Pearson Higher Education, Inc.
By Muhammad Ali Mazidi, Janice Gillespie Mazidi and Danny Causey Pearson Prent ic eHall - Upper Saddle River, NJ 07458
50
2.2: ASSEMBLE, LINK, AND RUN A PROGRAM
The x86 PC
Assembly Language, Design, and Interfacing © 2010, 2003, 2000, 1998 Pearson Higher Education, Inc.
By Muhammad Ali Mazidi, Janice Gillespie Mazidi and Danny Causey Pearson Prentice Hall - Upper Saddle River, NJ 07458
2.2: ASSEMBLE, LINK, AND RUN A PROGRAM
The x86 PC
Assembly Language, Design, and Interfacing © 2010, 2003, 2000, 1998 Pearson Higher Education, Inc.
By Muhammad Ali Mazidi, Janice Gillespie Mazidi and Danny Causey Pearson Prentice Hall - Upper Saddle River, NJ 07458
ORG 100h is a compiler directive (it tells compiler how to handle
the source code). This directive is very important when you work
with variables. It tells compiler that the executable file will be loaded
at the offset of 100h (256 bytes), so compiler should calculate the
correct address for all variables when it replaces the variable names
with their offsets. Directives are never converted to any real
machine code.
Why executable file is loaded at offset of 100h? Operating system
keeps some data about the program in the first 256 bytes of the
The x86 PC
Assembly Language, Design, and Interfacing © 2010, 2003, 2000, 1998 Pearson Higher Education, Inc.
By Muhammad Ali Mazidi, Janice Gillespie Mazidi and Danny Causey Pearson Prentice Hall - Upper Saddle River, NJ 07458
2.2: ASSEMBLE, LINK, AND RUN A PROGRAM
The x86 PC
Assembly Language, Design, and Interfacing © 2010, 2003, 2000, 1998 Pearson Higher Education, Inc.
By Muhammad Ali Mazidi, Janice Gillespie Mazidi and Danny Causey Pearson Prentice Hall - Upper Saddle River, NJ 07458
2.2: ASSEMBLE, LINK, AND RUN A PROGRAM
LINKing the program
• The assembler creates the opcodes, operands & offset addresses under the
".obj" file.
• The LINK program produces the ready-to-run program with the ".exe"
(EXEcutable) extension.
– The LINK program sets up the file so it can be loaded
by the OS and executed.
• The program can be run at the OS level, using the following command: C>myfile
– When the program name is typed in at the OS level, the OS loads the
program in memory.
• Referred to as mapping, which means that the program is mapped into
the physical memory of the PC.
The x86 PC
Assembly Language, Design, and Interfacing © 2010, 2003, 2000, 1998 Pearson Higher Education, Inc.
By Muhammad Ali Mazidi, Janice Gillespie Mazidi and Danny Causey Pearson Prentice Hall - Upper Saddle River, NJ 07458
2.2: ASSEMBLE, LINK, AND RUN A PROGRAM
TITLE directives
The x86 PC
Assembly Language, Design, and Interfacing © 2010, 2003, 2000, 1998 Pearson Higher Education, Inc.
By Muhammad Ali Mazidi, Janice Gillespie Mazidi and Danny Causey Pearson Prentice Hall - Upper Saddle River, NJ 07458
Software
ADD AX, BX
Destination operand
The x86 PC
Assembly Language, Design, and Interfacing © 2010, 2003, 2000, 1998 Pearson Higher Education, Inc.
By Muhammad Ali Mazidi, Janice Gillespie Mazidi and Danny Causey Pearson Prent ic eHall - Upper Saddle River, NJ 07458
57
2.0: ASSEMBLY LANGUAGE
The x86 PC
Assembly Language, Design, and Interfacing © 2010, 2003, 2000, 1998 Pearson Higher Education, Inc.
By Muhammad Ali Mazidi, Janice Gillespie Mazidi and Danny Causey Pearson Prentice Hall - Upper Saddle River, NJ 07458
2.1: DIRECTIVES AND A SAMPLE PROGRAM
The x86 PC
Assembly Language, Design, and Interfacing © 2010, 2003, 2000, 1998 Pearson Higher Education, Inc.
By Muhammad Ali Mazidi, Janice Gillespie Mazidi and Danny Causey Pearson Prentice Hall - Upper Saddle River, NJ 07458
2.1: DIRECTIVES AND A SAMPLE PROGRAM
assembly language instructions
The x86 PC
Assembly Language, Design, and Interfacing © 2010, 2003, 2000, 1998 Pearson Higher Education, Inc.
By Muhammad Ali Mazidi, Janice Gillespie Mazidi and Danny Causey Pearson Prentice Hall - Upper Saddle River, NJ 07458
2.1: DIRECTIVES AND A SAMPLE PROGRAM
assembly language instructions
[label:] mnemonic [operands][;comment]
• The mnemonic (instruction) and operand(s) fields
together accomplish the tasks for which the program
was written.
The x86 PC
Assembly Language, Design, and Interfacing © 2010, 2003, 2000, 1998 Pearson Higher Education, Inc.
By Muhammad Ali Mazidi, Janice Gillespie Mazidi and Danny Causey Pearson Prentice Hall - Upper Saddle River, NJ 07458
2.1: DIRECTIVES AND A SAMPLE PROGRAM
assembly language instructions
[label:] mnemonic [operands][;comment]
The x86 PC
Assembly Language, Design, and Interfacing © 2010, 2003, 2000, 1998 Pearson Higher Education, Inc.
By Muhammad Ali Mazidi, Janice Gillespie Mazidi and Danny Causey Pearson Prentice Hall - Upper Saddle River, NJ 07458
2.1: DIRECTIVES AND A SAMPLE PROGRAM
assembly language instructions
The x86 PC
Assembly Language, Design, and Interfacing © 2010, 2003, 2000, 1998 Pearson Higher Education, Inc.
By Muhammad Ali Mazidi, Janice Gillespie Mazidi and Danny Causey Pearson Prentice Hall - Upper Saddle River, NJ 07458
2.4: CONTROL TRANSFER INSTRUCTIONS
rules for names in Assembly language
• The names used for labels in Assembly language programming consist of…
– Alphabetic letters in both upper- and lowercase.
– The digits 0 through 9.
– Question mark (?); Period (.); At (@)
– Underline (_); Dollar sign ($)
• Each label name must be unique.
– They may be up to 31 characters long.
• The first character must be an alphabetic or special character.
– It cannot be a digit.
– The period can only be used as the first character.
The x86 PC
Assembly Language, Design, and Interfacing © 2010, 2003, 2000, 1998 Pearson Higher Education, Inc.
By Muhammad Ali Mazidi, Janice Gillespie Mazidi and Danny Causey Pearson Prentice Hall - Upper Saddle River, NJ 07458
2.5: DATA TYPES AND DATA DEFINITION
x86 data types
The x86 PC
Assembly Language, Design, and Interfacing © 2010, 2003, 2000, 1998 Pearson Higher Education, Inc.
By Muhammad Ali Mazidi, Janice Gillespie Mazidi and Danny Causey Pearson Prentice Hall - Upper Saddle River, NJ 07458
Compiler directives
name DB value
name DW value
name - can be any letter or digit combination, though it should start with a letter.
It's possible to declare unnamed variables by not specifying the name (this variable
will have an address but no name).
The x86 PC
Assembly Language, Design, and Interfacing © 2010, 2003, 2000, 1998 Pearson Higher Education, Inc.
By Muhammad Ali Mazidi, Janice Gillespie Mazidi and Danny Causey Pearson Prentice Hall - Upper Saddle River, NJ 07458
2.5: DATA TYPES AND DATA DEFINITION
DB define byte
The x86 PC
Assembly Language, Design, and Interfacing © 2010, 2003, 2000, 1998 Pearson Higher Education, Inc.
By Muhammad Ali Mazidi, Janice Gillespie Mazidi and Danny Causey Pearson Prentice Hall - Upper Saddle River, NJ 07458
2.5: DATA TYPES AND DATA DEFINITION
DB define byte
• Some DB examples:
The x86 PC
Assembly Language, Design, and Interfacing © 2010, 2003, 2000, 1998 Pearson Higher Education, Inc.
By Muhammad Ali Mazidi, Janice Gillespie Mazidi and Danny Causey Pearson Prentice Hall - Upper Saddle River, NJ 07458
2.1: DIRECTIVES AND A SAMPLE PROGRAM
data segment
The x86 PC
Assembly Language, Design, and Interfacing © 2010, 2003, 2000, 1998 Pearson Higher Education, Inc.
By Muhammad Ali Mazidi, Janice Gillespie Mazidi and Danny Causey Pearson Prentice Hall - Upper Saddle River, NJ 07458
DataTypes and Data Definition
DATA1 DB 25
DATA2 DB 10001001b
DATA3 DB 12h
ORG 0010h ;indicates distance from initial DS location
DATA4 DB “2591”
ORG 0018h ;indicates distance from initial DS location
DATA5 DB ?
The x86 PC
Assembly Language, Design, and Interfacing © 2010, 2003, 2000, 1998 Pearson Higher Education, Inc.
By Muhammad Ali Mazidi, Janice Gillespie Mazidi and Danny Causey Pearson Prent ic eHall - Upper Saddle River, NJ 07458
70
DB DW DD
MESSAGE3 DW 6667H 67 66
db 45h 45
db 'a' 61
db 11110000b F0
data2 dw 12,13 0C 00 0D 00
dw 2345h 45 23
dd 300h 00 03 00 00
The x86 PC
Assembly Language, Design, and Interfacing © 2010, 2003, 2000, 1998 Pearson Higher Education, Inc.
By Muhammad Ali Mazidi, Janice Gillespie Mazidi and Danny Causey Pearson Prent ic eHall - Upper Saddle River, NJ 07458
71
More Examples
DW 954
DW 253Fh ; allocates two bytes
DW 253Fh
COUNTER1 DB COUNT
COUNTER2 DB COUNT
The x86 PC
Assembly Language, Design, and Interfacing © 2010, 2003, 2000, 1998 Pearson Higher Education, Inc.
By Muhammad Ali Mazidi, Janice Gillespie Mazidi and Danny Causey Pearson Prent ic eHall - Upper Saddle River, NJ 07458
72
2.5: DATA TYPES AND DATA DEFINITION
DB define byte
• List file for DB examples.
The x86 PC
Assembly Language, Design, and Interfacing © 2010, 2003, 2000, 1998 Pearson Higher Education, Inc.
By Muhammad Ali Mazidi, Janice Gillespie Mazidi and Danny Causey Pearson Prentice Hall - Upper Saddle River, NJ 07458
2.5: DATA TYPES AND DATA DEFINITION
DW define word
• DW is used to allocate memory 2 bytes (one word) at a
time:
The x86 PC
Assembly Language, Design, and Interfacing © 2010, 2003, 2000, 1998 Pearson Higher Education, Inc.
By Muhammad Ali Mazidi, Janice Gillespie Mazidi and Danny Causey Pearson Prentice Hall - Upper Saddle River, NJ 07458
2.5: DATA TYPES AND DATA DEFINITION
EQU equate
The x86 PC
Assembly Language, Design, and Interfacing © 2010, 2003, 2000, 1998 Pearson Higher Education, Inc.
By Muhammad Ali Mazidi, Janice Gillespie Mazidi and Danny Causey Pearson Prentice Hall - Upper Saddle River, NJ 07458
2.5: DATA TYPES AND DATA DEFINITION
DD define doubleword
• The DD directive is used to allocate memory locations that are 4 bytes (two
words) in size.
– Data is converted to hex & placed in memory locations
• Low byte to low address and high byte to high address.
The x86 PC
Assembly Language, Design, and Interfacing © 2010, 2003, 2000, 1998 Pearson Higher Education, Inc.
By Muhammad Ali Mazidi, Janice Gillespie Mazidi and Danny Causey Pearson Prentice Hall - Upper Saddle River, NJ 07458
2.5: DATA TYPES AND DATA DEFINITION
DQ define quadword
• DQ is used to allocate memory 8 bytes (four words) in size, to
represent any variable up to 64 bits wide:
The x86 PC
Assembly Language, Design, and Interfacing © 2010, 2003, 2000, 1998 Pearson Higher Education, Inc.
By Muhammad Ali Mazidi, Janice Gillespie Mazidi and Danny Causey Pearson Prentice Hall - Upper Saddle River, NJ 07458
2.5: DATA TYPES AND DATA DEFINITION
directives
• Figure 2-7 shows the memory dump of the data section,
including all the examples in this section.
– It is essential to understand the way operands are stored in
memory.
The x86 PC
Assembly Language, Design, and Interfacing © 2010, 2003, 2000, 1998 Pearson Higher Education, Inc.
By Muhammad Ali Mazidi, Janice Gillespie Mazidi and Danny Causey Pearson Prentice Hall - Upper Saddle River, NJ 07458
2.5: DATA TYPES AND DATA DEFINITION
directives
• All of the data directives use the little endian format.
– For ASCII data, only DB can define data of any length.
• Use of DD, DQ, directives for ASCII strings of more
than 2 bytes gives an assembly error.
The x86 PC
Assembly Language, Design, and Interfacing © 2010, 2003, 2000, 1998 Pearson Higher Education, Inc.
By Muhammad Ali Mazidi, Janice Gillespie Mazidi and Danny Causey Pearson Prentice Hall - Upper Saddle River, NJ 07458
2.5: DATA TYPES AND DATA DEFINITION
directives
• Review "DATA20 DQ 4523C2", residing in memory
starting at offset 00C0H.
– C2, the least significant byte, is in location 00C0, with
23 in 00C1, and 45, the most significant byte, in 00C2.
The x86 PC
Assembly Language, Design, and Interfacing © 2010, 2003, 2000, 1998 Pearson Higher Education, Inc.
By Muhammad Ali Mazidi, Janice Gillespie Mazidi and Danny Causey Pearson Prentice Hall - Upper Saddle River, NJ 07458
2.5: DATA TYPES AND DATA DEFINITION
directives
• When DB is used for ASCII numbers, it places them
backwards in memory.
– Review "DATA4 DB '2591'" at origin 10H:32,
• ASCII for 2, is in memory location 10H;35; for 5, in 11H; etc.
The x86 PC
Assembly Language, Design, and Interfacing © 2010, 2003, 2000, 1998 Pearson Higher Education, Inc.
By Muhammad Ali Mazidi, Janice Gillespie Mazidi and Danny Causey Pearson Prentice Hall - Upper Saddle River, NJ 07458
More assembly – OFFESET, SEG, EQU
• OFFSET
– The offset operator returns the distance of a label or variable from the
beginning of its segment. The destination must be 16 bits
– mov BX, offset count
• SEG
– The segment operator returns the segment part of a label or variable’s
address.
Push DS
Mov AX, seg array
Mov DS, AX
Mov BX, offset array
.
Pop DS
The x86 PC
Assembly Language, Design, and Interfacing © 2010, 2003, 2000, 1998 Pearson Higher Education, Inc.
By Muhammad Ali Mazidi, Janice Gillespie Mazidi and Danny Causey Pearson Prent ic eHall - Upper Saddle River, NJ 07458
82
DUP (Duplicate)
for example:
c DB 5 DUP(9)
is an alternative way of declaring:
c DB 9, 9, 9, 9, 9
The x86 PC
Assembly Language, Design, and Interfacing © 2010, 2003, 2000, 1998 Pearson Higher Education, Inc.
By Muhammad Ali Mazidi, Janice Gillespie Mazidi and Danny Causey Pearson Prentice Hall - Upper Saddle River, NJ 07458
2.5: DATA TYPES AND DATA DEFINITION
DUP duplicate
The x86 PC
Assembly Language, Design, and Interfacing © 2010, 2003, 2000, 1998 Pearson Higher Education, Inc.
By Muhammad Ali Mazidi, Janice Gillespie Mazidi and Danny Causey Pearson Prentice Hall - Upper Saddle River, NJ 07458
5. : DATA TYPES AND DATA DEFINITION
DUP duplicate
• List file for DUP examples.
The x86 PC
Assembly Language, Design, and Interfacing © 2010, 2003, 2000, 1998 Pearson Higher Education, Inc.
By Muhammad Ali Mazidi, Janice Gillespie Mazidi and Danny Causey Pearson Prentice Hall - Upper Saddle River, NJ 07458
The PTR Operator - Byte or word or doubleword?
The x86 PC
Assembly Language, Design, and Interfacing © 2010, 2003, 2000, 1998 Pearson Higher Education, Inc.
By Muhammad Ali Mazidi, Janice Gillespie Mazidi and Danny Causey Pearson Prent ic eHall - Upper Saddle River, NJ 07458
86
The PTR Operator
.data
BVAL DB 10H,20H
WVAL DW 1000H
The x86 PC
Assembly Language, Design, and Interfacing © 2010, 2003, 2000, 1998 Pearson Higher Education, Inc.
By Muhammad Ali Mazidi, Janice Gillespie Mazidi and Danny Causey Pearson Prent ic eHall - Upper Saddle River, NJ 07458
87
ORG 100h MOV AL, var1 MOV BX, var2 RET ; stops the program. VAR1 DB 7 var2 DW 1234h
ORG 100h
VAR1 DB 7
var2 DW 1234h
The x86 PC
Assembly Language, Design, and Interfacing © 2010, 2003, 2000, 1998 Pearson Higher Education, Inc.
By Muhammad Ali Mazidi, Janice Gillespie Mazidi and Danny Causey Pearson Prentice Hall - Upper Saddle River, NJ 07458
Equivalent code using only DB
ORG 100h
DB 0A0h
DB 08h
DB 01h
DB 8Bh
DB 1Eh
DB 09h
DB 01h
DB 0C3h
DB 7
DB 34h
DB 12h
The x86 PC
Assembly Language, Design, and Interfacing © 2010, 2003, 2000, 1998 Pearson Higher Education, Inc.
By Muhammad Ali Mazidi, Janice Gillespie Mazidi and Danny Causey Pearson Prentice Hall - Upper Saddle River, NJ 07458
Procedures
• A procedure is a group of instructions designed to
accomplish a specific function.
– A code segment is organized into several small procedures to
make the program more structured.
• Every procedure must have a name defined by the PROC
directive.
– Followed by the assembly language instructions, and closed by
the ENDP directive.
• The PROC and ENDP statements must have the same label.
– The PROC directive may have the option FAR or NEAR.
• The OS requires the entry point to the user program
to be a FAR procedure.
The x86 PC
Assembly Language, Design, and Interfacing © 2010, 2003, 2000, 1998 Pearson Higher Education, Inc.
By Muhammad Ali Mazidi, Janice Gillespie Mazidi and Danny Causey Pearson Prentice Hall - Upper Saddle River, NJ 07458
Procedures
name PROC
; here goes the code
; of the procedure ...
RET
name ENDP
The x86 PC
Assembly Language, Design, and Interfacing © 2010, 2003, 2000, 1998 Pearson Higher Education, Inc.
By Muhammad Ali Mazidi, Janice Gillespie Mazidi and Danny Causey Pearson Prentice Hall - Upper Saddle River, NJ 07458
Example Proc
ORG 100h
main proc ; this is optional but very strongly recommended
MOV AL, 1
MOV BL, 2
CALL m2
CALL m2
CALL m2
CALL m2
m2 PROC
MUL BL ; AX = AL * BL.
RET ; return to caller.
m2 ENDP
The x86 PC
Assembly Language, Design, and Interfacing © 2010, 2003, 2000, 1998 Pearson Higher Education, Inc.
By Muhammad Ali Mazidi, Janice Gillespie Mazidi and Danny Causey Pearson Prentice Hall - Upper Saddle River, NJ 07458
assembly language subroutines
The x86 PC
Assembly Language, Design, and Interfacing © 2010, 2003, 2000, 1998 Pearson Higher Education, Inc.
By Muhammad Ali Mazidi, Janice Gillespie Mazidi and Danny Causey Pearson Prentice Hall - Upper Saddle River, NJ 07458
2.3: MORE SAMPLE PROGRAMS
various approaches to Program 2-1
• Variations of Program 2-1 clarify use of addressing
modes, and show that the x86 can use any general-
purpose register for arithmetic and logic operations.
The x86 PC
Assembly Language, Design, and Interfacing © 2010, 2003, 2000, 1998 Pearson Higher Education, Inc.
By Muhammad Ali Mazidi, Janice Gillespie Mazidi and Danny Causey Pearson Prentice Hall - Upper Saddle River, NJ 07458
2.3: MORE SAMPLE PROGRAMS
• Program 2-1, and the list file generated when the program was assembled.
TITLE ADD_5_BYTES
org 100h
DATA_IN DB 25H,12H,15H,1FH,2BH
SUM DB ?
MAIN PROC FAR
MOV AX,@DATA
MOV DS, AX
MOV CX,5
MOV BX, OFFSET DATA_IN
MOV AL,0
CALL ADDC
MOV SUM, AL
MOV AH, 4CH
INT 21H
RET
MAIN ENDP
ADDC PROC ; A PROCEDURE USED!!!!!!
AGAIN: ADD AL, [BX]
INC BX
DEC CX
JNZ AGAIN
RET
ADDC ENDP
END
The x86 PC
Assembly Language, Design, and Interfacing © 2010, 2003, 2000, 1998 Pearson Higher Education, Inc.
By Muhammad Ali Mazidi, Janice Gillespie Mazidi and Danny Causey Pearson Prentice Hall - Upper Saddle River, NJ 07458
2.3: MORE SAMPLE PROGRAMS
analysis of Program 2-1
• Program 2-1, explained instruction by instruction:
– "MOV CX,05" will load the value 05 into the CX register.
• Used by the program as a counter for iteration (looping).
– "MOV BX,OFFSET DATA_IN" will load into BX the
offset address assigned to DATA_IN.
• The assembler starts at offset 0000? and uses memory for
the data, then assigns the next available offset memory for SUM (in this
case, 0005).
– "ADD AL,[BX]" adds the contents of the memory location pointed at by the
register BX to AL.
• Note that [BX] is a pointer to a memory location.
– "INC BX" increments the pointer by adding 1 to BX.
• This will cause BX to point to the next data item. (next byte)
The x86 PC
Assembly Language, Design, and Interfacing © 2010, 2003, 2000, 1998 Pearson Higher Education, Inc.
By Muhammad Ali Mazidi, Janice Gillespie Mazidi and Danny Causey Pearson Prentice Hall - Upper Saddle River, NJ 07458
2.3: MORE SAMPLE PROGRAMS
analysis of Program 2-1
• Program 2-1, explained instruction by instruction:
The x86 PC
Assembly Language, Design, and Interfacing © 2010, 2003, 2000, 1998 Pearson Higher Education, Inc.
By Muhammad Ali Mazidi, Janice Gillespie Mazidi and Danny Causey Pearson Prentice Hall - Upper Saddle River, NJ 07458
2.3: MORE SAMPLE PROGRAMS
analysis of Program 2-2
Write a program that adds four words of data and saves the result. The values will be 234DH,1DE6H,
3BC7H and 566AH. Verify the result is: D364H
TITLE ADDS_4_words_data
ORG 100H
DATA_IN DW 234DH, 1DE6H, 3BC7H,566AH
ORG 10H
SUM DW ? ; The 16-bit data (a word) is stored with the low-order byte first, referred to as "little
endian.“
MAIN PROC FAR
MOV AX,@DATA
MOV DS, AX
MOV CX,4
MOV DI, OFFSET DATA_IN
MOV BX,00 ADD_16 PROC
CALL ADD_16 ADD_LP: ADD BX,[DI]
MOV SI, OFFSET SUM INC DI
MOV [SI], BX INC DI
DEC CX
MOV AH, 4CH
JNZ ADD_LP
INT 21H RET
MAIN ENDP ENDP ADD_16
END
The x86 PC
Assembly Language, Design, and Interfacing © 2010, 2003, 2000, 1998 Pearson Higher Education, Inc.
By Muhammad Ali Mazidi, Janice Gillespie Mazidi and Danny Causey Pearson Prentice Hall - Upper Saddle River, NJ 07458
2.3: MORE SAMPLE PROGRAMS
analysis of Program 2-2
• The address pointer is incremented twice, since the operand being
accessed is a word (two bytes).
– The program could have used "ADD DI,2" instead of using "INC
DI" twice.
• "MOV SI,OFFSET SUM" was used to load the pointer for the
memory allocated for the label SUM.
• "MOV [SI],BX" moves the contents of register BX to memory
locations with offsets 0010 and 0011.
• Program 2-2 uses the ORG directive to set the offset addresses for
data items.
– This caused SUM to be stored at DS:0010.
The x86 PC
Assembly Language, Design, and Interfacing © 2010, 2003, 2000, 1998 Pearson Higher Education, Inc.
By Muhammad Ali Mazidi, Janice Gillespie Mazidi and Danny Causey Pearson Prentice Hall - Upper Saddle River, NJ 07458
Example program
MOV AX,2000
MOV DS,AX 100-10f
MOV SI, 100
MOV DI, 120
MOV CX, 10
NXTPT: MOV AH, [SI]
MOV [DI], AH 120-12f
INC SI
INC DI
DEC CX
JNZ NXTPT
The x86 PC
Assembly Language, Design, and Interfacing © 2010, 2003, 2000, 1998 Pearson Higher Education, Inc.
By Muhammad Ali Mazidi, Janice Gillespie Mazidi and Danny Causey Pearson Prent ic eH all- Upper Saddle River, NJ 07458
10 0
2.3: MORE SAMPLE PROGRAMS
analysis of Program 2-3
• ACTUAL EXAMPLE TO RUN
TITLE TRANSFER_6_BYTES
ORG 100H
DATA_IN DB 25H,4FH,85H,1FH,2BH,0C4H
ORG 10H
COPY DB 6 DUP (?)
MAIN PROC FAR
MOV AX,@DATA
MOV DS, AX
MOV SI,OFFSET DATA_IN
MOV DI,OFFSET COPY
MOV CX, 06H
MOV_LOOP: MOV AL,[SI]
MOV [DI],AL
INC SI
INC DI
DEC CX
JNZ MOV_LOOP
MOV AH,4CH
INT 21H
MAIN ENDP
END
The x86 PC
Assembly Language, Design, and Interfacing © 2010, 2003, 2000, 1998 Pearson Higher Education, Inc.
By Muhammad Ali Mazidi, Janice Gillespie Mazidi and Danny Causey Pearson Prentice Hall - Upper Saddle River, NJ 07458
2.3: MORE SAMPLE PROGRAMS
analysis of Program 2-3
The x86 PC
Assembly Language, Design, and Interfacing © 2010, 2003, 2000, 1998 Pearson Higher Education, Inc.
By Muhammad Ali Mazidi, Janice Gillespie Mazidi and Danny Causey Pearson Prentice Hall - Upper Saddle River, NJ 07458
2.6: FULL SEGMENT DEFINITION
segment definition
• The SEGMENT and ENDS directives indicate the beginning &ending of a
segment, in this format:
The x86 PC
Assembly Language, Design, and Interfacing © 2010, 2003, 2000, 1998 Pearson Higher Education, Inc.
By Muhammad Ali Mazidi, Janice Gillespie Mazidi and Danny Causey Pearson Prentice Hall - Upper Saddle River, NJ 07458
2.6: FULL SEGMENT DEFINITION
segment definition
Figure 2-8
The x86 PC
Assembly Language, Design, and Interfacing © 2010, 2003, 2000, 1998 Pearson Higher Education, Inc.
By Muhammad Ali Mazidi, Janice Gillespie Mazidi and Danny Causey Pearson Prentice Hall - Upper Saddle River, NJ 07458
2.6: FULL SEGMENT DEFINITION
segment definition
• using full segment definition.
The x86 PC
Assembly Language, Design, and Interfacing © 2010, 2003, 2000, 1998 Pearson Higher Education, Inc.
By Muhammad Ali Mazidi, Janice Gillespie Mazidi and Danny Causey Pearson Prentice Hall - Upper Saddle River, NJ 07458
2.6: FULL SEGMENT DEFINITION
segment definition
• rewritten using full segment definition.
TITLE TRANSFER
STSEG SEGMENT
DB 32 DUP (?)
STSEG ENDS
DTSEG SEGMENT
ORG 10H
DATA_IN DB 25H,4FH,85H,1FH,2BH,0C4H
ORG 28H
COPY DB 6 DUP (?)
DTSEG ENDS
CDSEG SEGMENT
MAIN PROC FAR
ASSUME CS:CDSEG, DS:DTSEG, SS:STSEG
MOV AX,DTSEG
MOV DS,AX
MOV SI, OFFSET DATA_IN
MOV DI, OFFSET COPY
MOV CX,06H
MOV_LOOP: MOV AL,[SI]
MOV [DI],AL
INC SI
INC DI
DEC CX
JNZ MOV_LOOP
MOV AH,4CH
INT 21H
MAIN ENDP
CDSEG ENDS
END MAIN
The x86 PC
Assembly Language, Design, and Interfacing © 2010, 2003, 2000, 1998 Pearson Higher Education, Inc.
By Muhammad Ali Mazidi, Janice Gillespie Mazidi and Danny Causey Pearson Prentice Hall - Upper Saddle River, NJ 07458
2.6: FULL SEGMENT DEFINITION
stack segment definition
• The stack segment shown contains the line
"DB 64 DUP (?)" to reserve 64 bytes of memory
for the stack.
The x86 PC
Assembly Language, Design, and Interfacing © 2010, 2003, 2000, 1998 Pearson Higher Education, Inc.
By Muhammad Ali Mazidi, Janice Gillespie Mazidi and Danny Causey Pearson Prentice Hall - Upper Saddle River, NJ 07458
2.6: FULL SEGMENT DEFINITION
data segment definition
• In full segment definition, the SEGMENT directive names the data
segment and must appear before the data.
– The ENDS segment marks the end of the data segment:
• The code segment also begins and ends with SEGMENT and ENDS
directives:
The x86 PC
Assembly Language, Design, and Interfacing © 2010, 2003, 2000, 1998 Pearson Higher Education, Inc.
By Muhammad Ali Mazidi, Janice Gillespie Mazidi and Danny Causey Pearson Prentice Hall - Upper Saddle River, NJ 07458
2.6: FULL SEGMENT DEFINITION
code segment definition
• Immediately after PROC, the ASSUME directive, associates
segments with specific registers.
– By assuming the segment register is equal to the segment
labels used in the program.
• If an extra segment had been used, ES would
also be included in the ASSUME statement.
– ASSUME tells the assembler which of the segments, defined by
SEGMENT, should be used.
• Also helps the assembler to calculate the offset
addresses from the beginning of that segment.
• In "MOV AL, [BX] " the BX register is the offset of the data
segment.
The x86 PC
Assembly Language, Design, and Interfacing © 2010, 2003, 2000, 1998 Pearson Higher Education, Inc.
By Muhammad Ali Mazidi, Janice Gillespie Mazidi and Danny Causey Pearson Prentice Hall - Upper Saddle River, NJ 07458
2.6: FULL SEGMENT DEFINITION
code segment definition
The x86 PC
Assembly Language, Design, and Interfacing © 2010, 2003, 2000, 1998 Pearson Higher Education, Inc.
By Muhammad Ali Mazidi, Janice Gillespie Mazidi and Danny Causey Pearson Prentice Hall - Upper Saddle River, NJ 07458
code segment
main proc
start:
data segment mov ax,data
DATA_IN DW 234DH, 1DE6H, 3BC7H,566AH mov ds,ax
SUM DW ? ;referred to as "little endi MOV CX,4
ends MOV DI, OFFSET DATA_IN
MOV BX,00
ADD_LP: ADD BX,[DI]
stack segment INC DI
dw 128 dup(0) INC DI DEC
ends CX JNZ
ADD_LP
MOV SI, OFFSET SUM
MOV [SI], BX
MOV AH, 4CH
INT 21H
ret
end main
ends
The x86 PC
Assembly Language, Design, and Interfacing © 2010, 2003, 2000, 1998 Pearson Higher Education, Inc.
By Muhammad Ali Mazidi, Janice Gillespie Mazidi and Danny Causey Pearson Prentice Hall - Upper Saddle River, NJ 07458
2.4: CONTROL TRANSFER INSTRUCTIONS
conditional jumps
• Conditional jumps have mnemonics such as JNZ (jump not zero)
and JC (jump if carry).
– In the conditional jump, control is transferred to a new location
if a certain condition is met.
– The flag register indicates the current condition.
• For example, with "JNZ label", the processor looks at the zero flag
to see if it is raised.
– If not, the CPU starts to fetch and execute instructions from the
address of the label.
– If ZF = 1, it will not jump but will execute the next instruction
below the JNZ.
The x86 PC
Assembly Language, Design, and Interfacing © 2010, 2003, 2000, 1998 Pearson Higher Education, Inc.
By Muhammad Ali Mazidi, Janice Gillespie Mazidi and Danny Causey Pearson Prentice Hall - Upper Saddle River, NJ 07458
2.4: CONTROL TRANSFER INSTRUCTIONS
conditional jumps
The x86 PC
Assembly Language, Design, and Interfacing © 2010, 2003, 2000, 1998 Pearson Higher Education, Inc.
By Muhammad Ali Mazidi, Janice Gillespie Mazidi and Danny Causey Pearson Prentice Hall - Upper Saddle River, NJ 07458
2.4: CONTROL TRANSFER INSTRUCTIONS
short jumps
The x86 PC
Assembly Language, Design, and Interfacing © 2010, 2003, 2000, 1998 Pearson Higher Education, Inc.
By Muhammad Ali Mazidi, Janice Gillespie Mazidi and Danny Causey Pearson Prentice Hall - Upper Saddle River, NJ 07458
2.4: CONTROL TRANSFER INSTRUCTIONS
CALL statements
• For control to be transferred back to the caller, the last
subroutine instruction must be RET (return).
– For NEAR calls, the IP is restored..
• Assume SP = FFFEH:
The x86 PC
Assembly Language, Design, and Interfacing © 2010, 2003, 2000, 1998 Pearson Higher Education, Inc.
By Muhammad Ali Mazidi, Janice Gillespie Mazidi and Danny Causey Pearson Prentice Hall - Upper Saddle River, NJ 07458
2.4: CONTROL TRANSFER INSTRUCTIONS
short jumps
• The last instruction of the called subroutine must be a
RET instruction that directs the CPU to POP the top 2
bytes of the stack into the IP and resume executing at
offset address 0206.
– The number of PUSH and POP instructions (which alter the SP)
must match.
• For every PUSH there must be a POP.
The x86 PC
Assembly Language, Design, and Interfacing © 2010, 2003, 2000, 1998 Pearson Higher Education, Inc.
By Muhammad Ali Mazidi, Janice Gillespie Mazidi and Danny Causey Pearson Prentice Hall - Upper Saddle River, NJ 07458
Dec Hex Bin
2 2 00000010
ENDS ; THREE
The x86 PC
Assembly Language, Design, and Interfacing © 2010, 2003, 2000, 1998 Pearson Higher Education, Inc.
By Muhammad Ali Mazidi, Janice Gillespie Mazidi and Danny Causey Pearson Prentice Hall - Upper Saddle River, NJ 07458
EEE3132: MICROPROCESSORS I
8086
Lecture Notes # 7
Dr. Lukumba Phiri
• Introduction to Assembly Programming
• Program Segments
EEE3132: MICROPROCESSORS I
8086
Introduction to Assembly Language Programming
• ADD instruction
mnemonic operands
Example:
MOV AL,24H ;move 24H into AL
MOV DL,11H ;move 11H into DL
ADD AL,DL ;AL=AL+DL (AL=35H)(DL =11H)
mnemonic operands
❖ 8-bit registers can hold FFH (255) as the maximum value. Addition of larger
numbers can be performed by the 16-bit nonsegment registers.
MOV AX,34EH
MOV DX,6A5H
ADD DX,AX ;DX=DX+AX (DX=9F3H)
MOV CX,34EH
ADD CX,6A5H ;CX=34EH+6A5=9F3H
EEE3132: MICROPROCESSORS I
8086
Introduction to Program Segments
• Segment
▪ A segment is an area of memory that includes up to 64K bytes and begins an address
evenly divisible by 16 (such an address ends in 0H).
▪ Assembly Language Program consists of three segments:
❖ code segment : contains the program code (instructions)
❖ data segment : used to store data to be processed by the program
❖ stack segment: used to store information temporarily.
▪ The logical address of an instruction consists CS (Code Segment) and IP(instruction pointer).
▪ Logical Address in Code segment is represented by using segment address in CS register and
Offset Address in IP register as follows:
Example: If CS register contains 2500H and IP register contains 95F3H. What is the
Locical Adress in the code segment?
1. Start with CS
2. Shift left CS (insert 0 as the Least significant digit)
3. Add IP
Example: If CS register contains 1980H and IP register contains 78FEH. What is the
Physical Adress in the code segment?
Physical address: The microprocessor will retrieve the instruction from the memory locations
starting from 210FE (20 bit address).
EEE3132: MICROPROCESSORS I
8086
Introduction to Program Segments
• Addressing in Code Segment
Example: If CS=24F6H and IP=634AH, determine:
a) The logical address
b) The offset address
c) The physical address
d) The lower range of the code segment
e) The upper range of the code segment
Solution:
a) The logical address is; 24F6:634A
b) The offset address is; 634A
c) The Physical address is; 24F60+634A= 2B2AA
d) The lower range of the code segment: 24F6:0000 → 24F60+0000 = 24F60
e) The upper range of the code segment: 24F6:FFFF → 24F60+FFFF = 34F5F
EEE3132: MICROPROCESSORS I
8086
Introduction to Program Segments
• Addressing in Data Segment
• The area of memory allocated strictly for data is called data segment.
• Data segment contains variables containing single values and arrays of values, where code
segment only contain program instructions.
▪ Logical Address in Data Segment is represented by using segment address in DS register and
Offset Address in BX, SI or DI registers.
DS:BX
DS:SI
DS:DI
▪ At any time three locations in the data segment are pointed with DS:BX, DS:SI and DS:DI
respectively.
EEE3132: MICROPROCESSORS I
8086
Introduction to Program Segments
• Addressing in Data Segment
Solution:
a) The Physical address is; 7FA20+438E = 83DAE
b) The lower range: 7FA20+0000= 7FA20
c) The upper range: 7FA20+FFFF = 8FA1F
d) The logical address is; 7FA2:438E
EEE3132: MICROPROCESSORS I
8086
Introduction to Program Segments
• Addressing in Data Segment
Why do we use data segment?
Assume that a program is needed to add 5 bytes of data (25H, 12H, 15H,1FH and 2BH)
Better way: Assume that the Data segment contains the array of bytes starting from offset
address 0200H.
DS:01FF ?
MOV AL,0 ;clear AL DS:0200 25
ADD AL,[0200];add the contents of DS:200 to AL DS:0201 12
ADD AL,[0201];add the contents of DS:201 to AL code and data are separated
ADD AL,[0202];add the contents of DS:202 to AL DS:0202 15
(good programming practice)
ADD AL,[0203];add the contents of DS:203 to AL DS:0203 1F
ADD AL,[0204];add the contents of DS:204 to AL DS:0204 2B
DS:0205 ?
Data Segment
EEE3132: MICROPROCESSORS I
8086
Introduction to Program Segments
• Little endian convention
Given 8-bit (1-byte) data, bytes are stored one after the other in the memory. However given16-
bit (2-bytes) of data how are date stored?
In such a case the low byte goes to the low memory location and high byte goes to the high
memory location.
DS:1500 = F3 DS:1501 = 35
This convention is called little endian convention: This convention is used by Intel.
Lecture Continued
• Pushing and Popping Operations (Stack)
• Flag Registers and bit fields
EEE3132: MICROPROCESSORS I
8086
Introduction to Program Segments
• Addressing in Stack Segment
▪ Logical Address in Stack Segment is represented by using segment address in SS register and
Offset Address in SP register.
SS:SP
EEE3132: MICROPROCESSORS I
8086
Introduction to Program Segments
• Stack: Pushing and Popping Operations
• Push Instruction
Storing the CPU register in the stack is called a push.
PUSH source; Copy the content of source (16-bit register) into stack
Example: Given that SP=1456H, what are the contents of AX, top of the stack and SP after the
execution of the following instruction.
Example: SP=1236H, AX=24B6H, DI=85C2H, and DX=5F93H, show the contents of the stack
as each of the following instructions is executed.
PUSH AX
PUSH DI
PUSH DX
SS:1230 93
Solution:
SS:1231 5F
Note that in 80x86 the lower byte of
SS:1232 C2 C2 the register is stored to the lower
SS:1233 85 85 address. (Little Endian Convention)
SS;1234 B6 B6 B6
SS;1235 24 24
24
SS:1236
POP destination; Copy the top of the stack into destination (16-bit register)
mnemonic operand * SP register is incremented by 2
Example: Assume that SP=134AH and the illustration on the right SS:1349 ?
shows the content of the top of the stack. What will be the content SS:134A 76
of AX and SP after of after the execution of the following instruction.
SS:134B FC
POP AX SS:134C ?
SS:134E ?
Solution: AX=FC76H and SP=134CH (incremented by 2), SS:134F ?
Stack Segment
EEE3132: MICROPROCESSORS I
8086
Introduction to Program Segments
• Stack: Pushing and Popping Operations
• Popping the stack
Loading the contents of the stack into the CPU register is called a pop.
Example: Assume that the stack is shown below, and SP=18FAH, show the contents of the
stack and registers as each of the following instructions is executed.
POP CX
POP DX
POP BX
SS:18FA 23
SS:18FB 14
Solution:
SS:18FC 6B 6B
Note that in 80x86 the byte in the
SS:18FD 2C 2C Low address goes into the low byte.
SS;18FE 91 91 91 the byte in the high address goes into
F6
the high byte. (Little Endian Convention)
SS;18FF F6 F6
SS:1900
START After After
After
SP=18FA POP CX POP DX
POP BX
SP=18FC SP=18FE
SP=1900
CX=1423 DX=2C6B BX=F691
EEE3132: MICROPROCESSORS I
8086
Introduction to Program Segments
• Logical vs. physical address of the stack
• Calculating the physical address for the stack, the same principle is applied as was used for
the code and data segments. Physical address depends on the value of stack segment (SS)
register and the stack pointer (SP).
• Conditional flags: 6 of the flags are called the conditional flags, meaning that they
indicate some condition that resulted after an instruction was executed. These 6 are: CF,
PF, AF, ZF, SF, and OF.
Example: Copy the content of the flag register into register AX.
Example: Clear the flag bits. (Make the flag bits to be all 0)
MOV AX,0000H
PUSH AX ; now top of the stack contains 16-bit 0.
POPF ; copy the content of stack into flag register.
ALGORITHMS AND
FLOWCHARTS
ALGORITHMS AND FLOWCHARTS
◼ A typical programming task can be divided into
two phases:
◼ Problem solving phase
produce an ordered sequence of steps that describe
solution of problem
this sequence of steps is called an algorithm
◼ Implementation phase
implement the program in some programming
language
Steps in Problem Solving
◼ First produce a general algorithm (one can use
pseudocode)
◼ Refine the algorithm successively to get step by
step detailed algorithm that is very close to a
computer language.
◼ Pseudocode is an artificial and informal
language that helps programmers develop
algorithms. Pseudocode is very similar to
everyday English.
Pseudocode & Algorithm
◼ Example 1: Write an algorithm to
determine a student’s final grade and
indicate whether it is passing or failing.
The final grade is calculated as the
average of four marks.
Pseudocode & Algorithm
Pseudocode:
◼ Input a set of 4 marks
◼ Calculate their average by summing and dividing
by 4
◼ if average is below 50
Print “FAIL”
else
Print “PASS”
Pseudocode & Algorithm
◼ Detailed Algorithm
◼ Step 1: Input M1,M2,M3,M4
Step 2: GRADE (M1+M2+M3+M4)/4
Step 3: if (GRADE < 50) then
Print “FAIL”
else
Print “PASS”
endif
The Flowchart
◼ (Dictionary) A schematic representation of a sequence of
operations, as in a manufacturing process or computer
program.
◼ (Technical) A graphical representation of the sequence of
operations in an information system or program.
Information system flowcharts show how data flows from
source documents through the computer to final
distribution to users. Program flowcharts show the
sequence of instructions in a single program or
subroutine. Different symbols are used to draw each
type of flowchart.
The Flowchart
A Flowchart
shows logic of an algorithm
emphasizes individual steps and their
interconnections
e.g. control flow from one action to the next
Flowchart Symbols
Basic
Name Symbol Use in Flowchart
PRINT PRINT
“PASS” “FAIL”
STOP
Example 2
◼ Write an algorithm and draw a flowchart to
convert the length in feet to centimeter.
Pseudocode:
◼ Input the length in feet (Lft)
◼ Calculate the length in cm (Lcm) by
multiplying LFT with 30
◼ Print length in cm (LCM)
Example 2
Flowchart
Algorithm START
Print
Lcm
STOP
Example 3
Write an algorithm and draw a flowchart that
will read the two sides of a rectangle and
calculate its area.
Pseudocode
◼ Input the width (W) and Length (L) of a rectangle
◼ Calculate the area (A) by multiplying L with W
◼ Print A
Example 3
Algorithm START
Print
A
STOP
Example 4
◼ Write an algorithm and draw a flowchart that
will calculate the roots of a quadratic equation
ax 2 + bx + c = 0
◼ Hint: d = sqrt ( b 2 − 4ac ), and the roots are:
x1 = (–b + d)/2a and x2 = (–b – d)/2a
Example 4
Pseudocode:
◼ Input the coefficients (a, b, c) of the
quadratic equation
◼ Calculate d
◼ Calculate x1
◼ Calculate x2
◼ Print x1 and x2
Example 4
START
◼ Algorithm:
Input
◼ Step 1: Input a, b, c a, b, c
◼ Step 2: d sqrt ( b b − 4 a c )
◼ Step 3: x1 (–b + d) / (2 x a) d sqrt(b x b – 4 x a x c)
◼ Step 4: x2 (–b – d) / (2 x a)
x1 (–b + d) / (2 x a)
◼ Step 5: Print x1, x2
X2 (–b – d) / (2 x a)
Print
x1 ,x2
STOP
DECISION STRUCTURES
◼ The expression A>B is a logical expression
◼ it describes a condition we want to test
◼ if A>B is true (if A is greater than B) we take
the action on left
◼ print the value of A
◼ if A>B is false (if A is not greater than B) we
take the action on right
◼ print the value of B
DECISION STRUCTURES
Y N
is
A>B
Print Print
A B
IF–THEN–ELSE STRUCTURE
◼ The structure is as follows
If condition then
true alternative
else
false alternative
endif
IF–THEN–ELSE STRUCTURE
◼ The algorithm for the flowchart is as
follows:
If A>B then
Y N
print A is
A>B
else
print B Print
A
Print
B
endif
Relational Operators
Relational Operators
Operator Description
> Greater than
< Less than
= Equal to
Greater than or equal to
Less than or equal to
Not equal to
Example 5
◼ Write an algorithm that reads two values, determines the
largest value and prints the largest value with an
identifying message.
ALGORITHM
Step 1: Input VALUE1, VALUE2
Step 2: if (VALUE1 > VALUE2) then
MAX VALUE1
else
MAX VALUE2
endif
Step 3: Print “The largest value is”, MAX
Example 5
START
Input
VALUE1,VALUE2
Y is
N
VALUE1>VALUE2
Print
“The largest value is”,
MAX
STOP
NESTED IFS
◼ One of the alternatives within an IF–
THEN–ELSE statement
may involve further IF–THEN–ELSE
statement
Example 6
◼ Write an algorithm that reads three
numbers and prints the value of the largest
number.
Example 6
Step 1: Input N1, N2, N3
Step 2: if (N1>N2) then
if (N1>N3) then
MAX N1 [N1>N2, N1>N3]
else
MAX N3 [N3>N1>N2]
endif
else
if (N2>N3) then
MAX N2 [N2>N1, N2>N3]
else
MAX N3 [N3>N2>N1]
endif
endif
Step 3: Print “The largest number is”, MAX
Example 6
◼ Flowchart: Draw the flowchart of the
above Algorithm.
Example 7
◼ Write and algorithm and draw a flowchart
to
a) read an employee name (NAME),
overtime hours worked (OVERTIME),
hours absent (ABSENT) and
b) determine the bonus payment
(PAYMENT).
Example 7
Bonus Schedule
OVERTIME – (2/3)*ABSENT Bonus Paid