Microprocessors Sunday, Mar. 23 Dr. Asmaa Farouk Faculty of Engineering, Electrical Department,...

96
Microprocesso rs Sunday, Mar. 23 Dr. Asmaa Farouk Faculty of Engineering, Electrical Department, Assiut University

Transcript of Microprocessors Sunday, Mar. 23 Dr. Asmaa Farouk Faculty of Engineering, Electrical Department,...

Page 1: Microprocessors Sunday, Mar. 23 Dr. Asmaa Farouk Faculty of Engineering, Electrical Department, Assiut University.

MicroprocessorsSunday, Mar. 23

Dr. Asmaa FaroukFaculty of Engineering, Electrical Department,

Assiut University

Page 2: Microprocessors Sunday, Mar. 23 Dr. Asmaa Farouk Faculty of Engineering, Electrical Department, Assiut University.

Topics of Today• Reading:Reading:– Brey:

• Section 1.2: The Memory.• Section 2.2: Real Mode Memory Addressing.

– Mazidi:• Section 1.4: Introduction to

Program Segments.• Section 1.5: More about Segments in the 80x86.

Page 3: Microprocessors Sunday, Mar. 23 Dr. Asmaa Farouk Faculty of Engineering, Electrical Department, Assiut University.

Topics of Today

• The Memory.• Real Mode Memory Addressing.• Introduction to Program Segments.• More about Segments in the 80x86.

Page 4: Microprocessors Sunday, Mar. 23 Dr. Asmaa Farouk Faculty of Engineering, Electrical Department, Assiut University.

The Memory• Memory structure of all Intel-based personal

computers is similar.• Figure (1-7) illustrates memory map of a

personal computer system.

Page 5: Microprocessors Sunday, Mar. 23 Dr. Asmaa Farouk Faculty of Engineering, Electrical Department, Assiut University.

The Memory• This map

applies to any IBM personal computer.– Also any

IBM-compatible clones in existence.

Page 6: Microprocessors Sunday, Mar. 23 Dr. Asmaa Farouk Faculty of Engineering, Electrical Department, Assiut University.

The Memory• Main memory system divided into three parts:– TPA (Transient Program Area).– System Area.– XMS (eXtended Memory System).

• The type of the microprocessor determines whether an extended memory system exists.– For 8086 / 8088 PC, the TPA and system area exist,

but there is no extended memory area.

Page 7: Microprocessors Sunday, Mar. 23 Dr. Asmaa Farouk Faculty of Engineering, Electrical Department, Assiut University.

The Memory• First 1M

byte of memory (TPA & System Area) often called the real or conventional memory system.

Page 8: Microprocessors Sunday, Mar. 23 Dr. Asmaa Farouk Faculty of Engineering, Electrical Department, Assiut University.

The Memory The The TPATPA::– Its length is 640K bytes.– Holds the DOS (Disk

Operating System) and other programs that control the computer system.• The TPA is a DOS concept

and not really applicable in Windows.

• Also stores any currently active or inactive DOS application programs.

• First 1M byte of memory (TPA & System Area) often called the real or conventional memory system.

Page 9: Microprocessors Sunday, Mar. 23 Dr. Asmaa Farouk Faculty of Engineering, Electrical Department, Assiut University.

The Memory• The The TPATPA: (Cont.): (Cont.)– DOS memory map (figure 1-8)

shows how areas of TPA are used for system programs, data and drivers.• Hexadecimal number to left of each

area represents the memory addresses that begin and end each data area.

Page 10: Microprocessors Sunday, Mar. 23 Dr. Asmaa Farouk Faculty of Engineering, Electrical Department, Assiut University.

The Memory• The The TPATPA: (Cont.): (Cont.)– Interrupt vectors access various

features of DOS, BIOS (Basic I/O System), and applications.• System BIOS is a collection of

programs always stored in the ROM or flash memory.• Has important information for the

I/O devices to the PC to start up.

Page 11: Microprocessors Sunday, Mar. 23 Dr. Asmaa Farouk Faculty of Engineering, Electrical Department, Assiut University.

The Memory• The The TPATPA: (Cont.): (Cont.)– BIOS & DOS communications

areas contain transient data used by programs to access I/O devices and internal features of the system.• These are stored in the TPA so they

can be changed as DOS operates.

Page 12: Microprocessors Sunday, Mar. 23 Dr. Asmaa Farouk Faculty of Engineering, Electrical Department, Assiut University.

The Memory• The The TPATPA: (Cont.): (Cont.)– IO.SYS Program:• Loads into the TPA from the disk

whenever an MSDOS system is started.– MSDOS (Microsoft DOS).

• Allows DOS to use keyboard, video display, printer, and other I/O devices often found in computers.• IO.SYS program links DOS system to

the programs stored on the system BIOS ROM.

Page 13: Microprocessors Sunday, Mar. 23 Dr. Asmaa Farouk Faculty of Engineering, Electrical Department, Assiut University.

The Memory• The The TPATPA: (Cont.): (Cont.)– Drivers: are programs that control

installable I/O devices.• Mouse, disk cache, hand scanner, CD-

ROM memory (Compact Disk Read-Only Memory), DVD (Digital Versatile Disk), as well as programs.

Page 14: Microprocessors Sunday, Mar. 23 Dr. Asmaa Farouk Faculty of Engineering, Electrical Department, Assiut University.

The Memory• The The TPATPA: (Cont.): (Cont.)– Drivers: are programs that control

installable I/O devices.• DOS drivers normally have an

extension of .SYS, e.g. MOUSE.SYS. – DOS version 3.2 and later files have an

extension of .EXE; EMM386.EXE.

• Even though these files are not used by Windows, they are still used to execute DOS applications.

Page 15: Microprocessors Sunday, Mar. 23 Dr. Asmaa Farouk Faculty of Engineering, Electrical Department, Assiut University.

The Memory• The The TPATPA: (Cont.): (Cont.)– COMMAND.COM program

(command processor): controls the operation of the computer from the keyboard when operated in the DOS mode. • Processes the DOS commands

as they are typed from the keyboard. – For example, if DIR is typed, the

program displays a directory of the disk files in the current disk directory.

Page 16: Microprocessors Sunday, Mar. 23 Dr. Asmaa Farouk Faculty of Engineering, Electrical Department, Assiut University.

The Memory The The System AreaSystem Area::– Smaller than the TPA.

• Its length is 384K bytes.

– Contains programs on ROM or flash memory, and areas of RAM memory for data storage.

• First 1M byte of memory (TPA & System Area) often called the real or conventional memory system.

Page 17: Microprocessors Sunday, Mar. 23 Dr. Asmaa Farouk Faculty of Engineering, Electrical Department, Assiut University.

The Memory• The The System AreaSystem Area: (Cont.): (Cont.)– Figure (1-9) shows the system

area of a typical personal computer system.• The map also includes the

hexadecimal memory addresses of the various areas.

Page 18: Microprocessors Sunday, Mar. 23 Dr. Asmaa Farouk Faculty of Engineering, Electrical Department, Assiut University.

The Memory• The The System AreaSystem Area: (Cont.): (Cont.)– First area: contains video display

RAM and video control programs on ROM or flash memory.• Area starts at location A0000H and

extends to C7FFFH.• Size/amount of memory depends

on type of video display adapter attached.

Page 19: Microprocessors Sunday, Mar. 23 Dr. Asmaa Farouk Faculty of Engineering, Electrical Department, Assiut University.

The Memory• The The System AreaSystem Area: (Cont.): (Cont.)– Area at locations C8000H–

DFFFFH is often open or free.• Its use depends on the system and

its configuration.

Page 20: Microprocessors Sunday, Mar. 23 Dr. Asmaa Farouk Faculty of Engineering, Electrical Department, Assiut University.

The Memory• The The System AreaSystem Area: (Cont.): (Cont.)– Memory locations E0000H–

EFFFFH contain the cassette BASIC language on ROM found in early IBM personal computer systems. • This area is often open or free in

newer computer systems.

Page 21: Microprocessors Sunday, Mar. 23 Dr. Asmaa Farouk Faculty of Engineering, Electrical Department, Assiut University.

The Memory• The The System AreaSystem Area: (Cont.): (Cont.)– The BIOS system ROM is located

in the top 64K bytes of the system area (F0000H – FFFFFH).• Controls the operation of the basic

I/O devices connected to the computer system.

Page 22: Microprocessors Sunday, Mar. 23 Dr. Asmaa Farouk Faculty of Engineering, Electrical Department, Assiut University.

The Memory• Windows Systems:Windows Systems:– Modern computers use a different memory map

with Windows than DOS memory maps.• It has two main areas; a TPA and system area.• The difference between this map and the DOS memory

map are sizes and locations of these areas.

– Figure (1-10) shows Windows XP memory map.

Page 23: Microprocessors Sunday, Mar. 23 Dr. Asmaa Farouk Faculty of Engineering, Electrical Department, Assiut University.

The Memory• Windows Systems: (Cont.)Windows Systems: (Cont.)– TPA is first 2G bytes from

locations 00000000H to 7FFFFFFFH.• Every program written for

Windows can use only up to 2G bytes of memory (even in a 64-bit system).

– System area is the last 2G bytes from 80000000H to FFFFFFFFH.

Page 24: Microprocessors Sunday, Mar. 23 Dr. Asmaa Farouk Faculty of Engineering, Electrical Department, Assiut University.

Topics of Today

• The Memory.• Real Mode Memory Addressing.• Introduction to Program Segments.• More about Segments in the 80x86.

Page 25: Microprocessors Sunday, Mar. 23 Dr. Asmaa Farouk Faculty of Engineering, Electrical Department, Assiut University.

Real Mode Memory Addressing• 80286 and above operate in either the real or

protected mode.• Only the 8086 and 8088 operate exclusively in

the real mode.• Real mode operation: allows addressing of only

the first 1M byte of memory space - even in Pentium 4 or Core2 microprocessors.– Remember: first 1M byte of memory is called the

real memory, conventional memory, or DOS memory system.

Page 26: Microprocessors Sunday, Mar. 23 Dr. Asmaa Farouk Faculty of Engineering, Electrical Department, Assiut University.

Real Mode Memory Addressing• Segments and Offsets:Segments and Offsets:– All real mode memory addresses must consist of a

segment address plus an offset address.– Segment address: • Defines the beginning address of any 64K-byte memory

segment.• Located within one of the segment registers.• Real mode segments can begin only at a 16-byte

boundary in the memory system (called a paragraph).

– Offset address:• Selects any location within the 64K-byte memory

segment.

Page 27: Microprocessors Sunday, Mar. 23 Dr. Asmaa Farouk Faculty of Engineering, Electrical Department, Assiut University.

Real Mode Memory Addressing• Segments and Offsets:Segments and Offsets:– Figure (2-3) shows:• A memory segment

beginning at 10000H, ending at location 1FFFFH.– 64K-bytes in length.

(Cont.)(Cont.)

Page 28: Microprocessors Sunday, Mar. 23 Dr. Asmaa Farouk Faculty of Engineering, Electrical Department, Assiut University.

Real Mode Memory Addressing• Segments and Offsets:Segments and Offsets:– Figure (2-3) shows:• A memory segment

beginning at 10000H, ending at location 1FFFFH.– 64K-bytes in length.

• An offset address, sometimes called a displacement, of F000H selects location 1F000H in the memory segment (10000H + F000H).

(Cont.)(Cont.)

Page 29: Microprocessors Sunday, Mar. 23 Dr. Asmaa Farouk Faculty of Engineering, Electrical Department, Assiut University.

Real Mode Memory Addressing• Segments and Offsets: (Cont.)Segments and Offsets: (Cont.)– Once the beginning address is known, the ending

address is found by adding FFFFH.• Because a real mode segment of memory size 64K length.

– The offset address is always added to the segment’s starting address to locate the data.

– Segment and offset address are sometimes written as 1000:2000.• A segment address of 1000H; an offset of 2000H.

Page 30: Microprocessors Sunday, Mar. 23 Dr. Asmaa Farouk Faculty of Engineering, Electrical Department, Assiut University.

Real Mode Memory Addressing• Default Segment and Offset Registers:Default Segment and Offset Registers:– The microprocessor has rules that apply to

segments whenever memory is addressed.• Define the segment and offset register combination.

– Example: the Code Segment (CS) register with The Instruction Pointer (IP) :• CS defines the start of the code segment.• IP locates the next instruction within the code segment.• This combination is CS:IP or CS:EIP, depending on the

microprocessor’s mode of operation.

Page 31: Microprocessors Sunday, Mar. 23 Dr. Asmaa Farouk Faculty of Engineering, Electrical Department, Assiut University.

Real Mode Memory Addressing• Default Segment and Offset Registers: (Cont.)Default Segment and Offset Registers: (Cont.)– Another default combination is the stack.• Stack data are referenced through the Stack Segment

(SS) register with offset addressed by either the Stack Pointer (SP/ESP) or the base pointer (BP/EBP).

– Other defaults are shown in table (2-3):

Page 32: Microprocessors Sunday, Mar. 23 Dr. Asmaa Farouk Faculty of Engineering, Electrical Department, Assiut University.

Real Mode Memory Addressing• Default Segment and Offset Registers: (Cont.)Default Segment and Offset Registers: (Cont.)– 8086 allows four memory

segments.– Figure (2-4) shows a system

that contains four memory segments.

Page 33: Microprocessors Sunday, Mar. 23 Dr. Asmaa Farouk Faculty of Engineering, Electrical Department, Assiut University.

Real Mode Memory Addressing• Default Segment and Offset Registers: (Cont.)Default Segment and Offset Registers: (Cont.)– A memory segment can touch or

overlap if 64K-bytes of memory are not required for a segment.

– A program can have more than four or six segments, but only access four or six segments at a time.

Page 34: Microprocessors Sunday, Mar. 23 Dr. Asmaa Farouk Faculty of Engineering, Electrical Department, Assiut University.

Real Mode Memory Addressing• Default Segment and Default Segment and

Offset Registers: (Cont.)Offset Registers: (Cont.)– Example: A program

placed in memory by DOS is loaded in the TPA at the first available area of memory above drivers and other TPA programs.• This area is indicated by a

free-pointer maintained by DOS.

Page 35: Microprocessors Sunday, Mar. 23 Dr. Asmaa Farouk Faculty of Engineering, Electrical Department, Assiut University.

Real Mode Memory Addressing• Default Segment and Default Segment and

Offset Registers: (Cont.)Offset Registers: (Cont.)– Example:• Program loading is

handled automatically by the program loader within DOS.

Page 36: Microprocessors Sunday, Mar. 23 Dr. Asmaa Farouk Faculty of Engineering, Electrical Department, Assiut University.

Real Mode Memory Addressing• Default Segment and Default Segment and

Offset Registers: (Cont.)Offset Registers: (Cont.)– Example:• The side view of the

segments shows an overlap because the amount of data in them does not require 64K bytes of memory.

Page 37: Microprocessors Sunday, Mar. 23 Dr. Asmaa Farouk Faculty of Engineering, Electrical Department, Assiut University.

Real Mode Memory Addressing• Default Segment and Default Segment and

Offset Registers: (Cont.)Offset Registers: (Cont.)– Example:• Memory segments can be

moved to any place in the memory system without changing any of the offset addresses.– Only by changing the

segment starting address.– The DOS program loader

calculates and assigns segment starting addresses.

Page 38: Microprocessors Sunday, Mar. 23 Dr. Asmaa Farouk Faculty of Engineering, Electrical Department, Assiut University.

Real Mode Memory Addressing• Segment and Offset Addressing Scheme Allows Segment and Offset Addressing Scheme Allows

Relocation:Relocation:– Segment plus offset addressing allows DOS

programs to be relocated in memory.– A relocatable program: is one that can be placed

into any area of memory and executed without change.

– Relocatable data: are data that can be placed in any area of memory and used without any change to the program.

Page 39: Microprocessors Sunday, Mar. 23 Dr. Asmaa Farouk Faculty of Engineering, Electrical Department, Assiut University.

Real Mode Memory Addressing• Segment and Offset Addressing Scheme Allows Segment and Offset Addressing Scheme Allows

Relocation: (Cont.)Relocation: (Cont.)– Because memory is addressed within a segment by

an offset address, the memory segment can be moved to any place in the memory system without changing any of the offset addresses.• Only the contents of the segment register must be

changed to address the program in the new area of memory.

– Windows programs are written assuming that the first 2G of memory are available for code and data.

Page 40: Microprocessors Sunday, Mar. 23 Dr. Asmaa Farouk Faculty of Engineering, Electrical Department, Assiut University.

Topics of Today

• The Memory.• Real Mode Memory Addressing.• Introduction to Program Segments.• More about Segments in the 80x86.

Page 41: Microprocessors Sunday, Mar. 23 Dr. Asmaa Farouk Faculty of Engineering, Electrical Department, Assiut University.

Introduction to Program Segments• A typical Assembly language program consists of

at least three segments:– A code segment: which contains the assembly

language instructions that perform the tasks that the program was designed to accomplish.

– A data segment: used to store information (data) to be processed by the instructions in the code segment.

– A stack segment: used by the CPU to store information temporarily.

Page 42: Microprocessors Sunday, Mar. 23 Dr. Asmaa Farouk Faculty of Engineering, Electrical Department, Assiut University.

Introduction to Program Segments• Origin and Definition of the Segment:Origin and Definition of the Segment:– A segment: is an area of memory that includes up to

64K-bytes, and begins on an address evenly divisible by 16 (such an address ends in 0H).

– In 8085 there was 64K-bytes of memory for all code, data, and stack information.• It had only 16 pins for address lines (216 = 64K).

– In 8088/86 there can be up to 64K-bytes in each category.

– The code segment, data segment, and stack segment.

Page 43: Microprocessors Sunday, Mar. 23 Dr. Asmaa Farouk Faculty of Engineering, Electrical Department, Assiut University.

Introduction to Program Segments• Logical Address and Physical Address:Logical Address and Physical Address:– In 8086, there are three types of addresses:• Physical address: 20-bit address actually on the address

pins of the processor, decoded by the memory interfacing circuitry.– An actual physical location in RAM or ROM within the 1 MB

memory range.– This address can have a range of 00000H to FFFFFH.

• Offset address: a location in a 64K-byte segment which can range from 0000H to FFFFH.• Logical address: which consists of a segment address and

an offset address.

Page 44: Microprocessors Sunday, Mar. 23 Dr. Asmaa Farouk Faculty of Engineering, Electrical Department, Assiut University.

Introduction to Program Segments• Code Segment:Code Segment:– To execute a program, 8086 fetches the instructions

(opcodes and operands) from the code segment.• The logical address of an instruction always consists of a

CS (code segment) and an IP (instruction pointer), shown in CS:IP format.• The physical address for the location of the instruction is

generated by shifting the CS left one hex digit, then adding it to the IP.– IP contains the offset address.– The resulting 20-bit address is the physical address since it is put

on the external physical address bus pins.

Page 45: Microprocessors Sunday, Mar. 23 Dr. Asmaa Farouk Faculty of Engineering, Electrical Department, Assiut University.

Introduction to Program Segments• Code Segment: (Cont.)Code Segment: (Cont.)– Assume values in CS & IP as shown below:

• The offset address: contained in IP, is 95F3H.• The logical address: is CS:IP, or 2500:95F3H.• The physical address: will be 25000 + 95F3 = 2E5F3H.

Page 46: Microprocessors Sunday, Mar. 23 Dr. Asmaa Farouk Faculty of Engineering, Electrical Department, Assiut University.

Introduction to Program Segments• Code Segment: (Cont.)Code Segment: (Cont.)– Calculation of the physical address of an instruction:

• The microprocessor will retrieve the instruction from memory locations starting at 2E5F3H.

Page 47: Microprocessors Sunday, Mar. 23 Dr. Asmaa Farouk Faculty of Engineering, Electrical Department, Assiut University.

Introduction to Program Segments• Code Segment: (Cont.)Code Segment: (Cont.)– Calculation of the physical address of an instruction:

• Since IP can have a minimum value of 0000H and a maximum of FFFFH, the logical address range in this example is 2500:0000 to 2500:FFFF.

Page 48: Microprocessors Sunday, Mar. 23 Dr. Asmaa Farouk Faculty of Engineering, Electrical Department, Assiut University.

Introduction to Program Segments• Code Segment: (Cont.)Code Segment: (Cont.)– Calculation of the physical address of an instruction:

• This means that the lowest memory location of the code segment above will be 25000H (25000 + 0000) and the highest memory location will be 34FFFH (25000 + FFFF).

Page 49: Microprocessors Sunday, Mar. 23 Dr. Asmaa Farouk Faculty of Engineering, Electrical Department, Assiut University.

Introduction to Program Segments• Code Segment: (Cont.)Code Segment: (Cont.)– What happens if the desired instructions are located

beyond these two limits?• The value of CS must be changed to access those

instructions.

Page 50: Microprocessors Sunday, Mar. 23 Dr. Asmaa Farouk Faculty of Engineering, Electrical Department, Assiut University.

Introduction to Program Segments• Code Segment: (Cont.)Code Segment: (Cont.)– Example:

Page 51: Microprocessors Sunday, Mar. 23 Dr. Asmaa Farouk Faculty of Engineering, Electrical Department, Assiut University.

Introduction to Program Segments• Logical vs. Physical Address in the Code Logical vs. Physical Address in the Code

Segment:Segment:

• The following Assembly language instructions have been assembled (translated into machine language) and stored in memory.

Page 52: Microprocessors Sunday, Mar. 23 Dr. Asmaa Farouk Faculty of Engineering, Electrical Department, Assiut University.

Introduction to Program Segments• Logical vs. Physical Address in the Code Logical vs. Physical Address in the Code

Segment:Segment:• The logical address of

CS:IP.• The machine code

stored at that address.

• The corresponding Assembly language code.

Page 53: Microprocessors Sunday, Mar. 23 Dr. Asmaa Farouk Faculty of Engineering, Electrical Department, Assiut University.

Introduction to Program Segments• Logical vs. Physical Address in the Code Logical vs. Physical Address in the Code

Segment: (Cont.)Segment: (Cont.)

• Instruction "MOV AL,57" has a machine code of B057.– B0 is the opcode and 57 is the operand.

Page 54: Microprocessors Sunday, Mar. 23 Dr. Asmaa Farouk Faculty of Engineering, Electrical Department, Assiut University.

Introduction to Program Segments• Logical vs. Physical Address in the Code Logical vs. Physical Address in the Code

Segment: (Cont.)Segment: (Cont.)

• The byte at address 1132:0100 contains B0, the opcode for moving a value into register AL.

Page 55: Microprocessors Sunday, Mar. 23 Dr. Asmaa Farouk Faculty of Engineering, Electrical Department, Assiut University.

Introduction to Program Segments• Logical vs. Physical Address in the Code Logical vs. Physical Address in the Code

Segment: (Cont.)Segment: (Cont.)

• Address 1132:0101 contains 57, the operand to be moved to AL.

Page 56: Microprocessors Sunday, Mar. 23 Dr. Asmaa Farouk Faculty of Engineering, Electrical Department, Assiut University.

Introduction to Program Segments• Data Segment:Data Segment:– Assume a program to add 5 bytes of data, such as

25H, 12H, 15H, 1FH, and 2BH.• One way to add them is as follows:

• In this program, the data & code are mixed together in the instructions.– If the data changes, the code must be searched for every place it

is included, and the data retyped.

Page 57: Microprocessors Sunday, Mar. 23 Dr. Asmaa Farouk Faculty of Engineering, Electrical Department, Assiut University.

Introduction to Program Segments• Data Segment: (Cont.)Data Segment: (Cont.)– In x86 microprocessors, the area of memory set for

data is called the data segment.• It uses the register DS and an offset value.

– The next program demonstrates how data can be stored in the data segment and the program rewritten so that it can be used for any set of data.

Page 58: Microprocessors Sunday, Mar. 23 Dr. Asmaa Farouk Faculty of Engineering, Electrical Department, Assiut University.

Introduction to Program Segments• Data Segment: (Cont.)Data Segment: (Cont.)– Assume data segment offset begins at 200H.• The data is placed in memory locations:

• The program can be rewritten as follows:

Page 59: Microprocessors Sunday, Mar. 23 Dr. Asmaa Farouk Faculty of Engineering, Electrical Department, Assiut University.

Introduction to Program Segments• Data Segment: (Cont.)Data Segment: (Cont.)– The offset address is enclosed in brackets, which

indicate that the operand represents the address of the data and not the data itself.

– If the brackets were not included, as in "MOV AL,0200", the CPU would attempt to move 200 into AL instead of the contents of offset address 200.• This program will run with any set of data.• Changing the data has no effect on the code.

Page 60: Microprocessors Sunday, Mar. 23 Dr. Asmaa Farouk Faculty of Engineering, Electrical Department, Assiut University.

Introduction to Program Segments• Data Segment: (Cont.)Data Segment: (Cont.)– If the data had to be stored at a different offset

address the program would have to be rewritten.• A way to solve this problem is to use a register to hold the

offset address, and before each ADD, increment the register to access the next byte.

– 8088/86 allows only the use of registers BX, SI, and DI as offset registers for the data segment.• The term pointer is often used for a register holding an

offset address.

Page 61: Microprocessors Sunday, Mar. 23 Dr. Asmaa Farouk Faculty of Engineering, Electrical Department, Assiut University.

Introduction to Program Segments• Data Segment: (Cont.)Data Segment: (Cont.)– In the following example, BX is used as a pointer:

• The INC instruction adds 1 to its operand (increments).• "INC BX" achieves the same result as "ADD BX,1“

Page 62: Microprocessors Sunday, Mar. 23 Dr. Asmaa Farouk Faculty of Engineering, Electrical Department, Assiut University.

Introduction to Program Segments• Data Segment: (Cont.)Data Segment: (Cont.)– In the following example, BX is used as a pointer:

• The INC instruction adds 1 to its operand (increments).• "INC BX" achieves the same result as "ADD BX,1“• If the offset address where data is located is changed,

only one instruction will need to be modified.

Page 63: Microprocessors Sunday, Mar. 23 Dr. Asmaa Farouk Faculty of Engineering, Electrical Department, Assiut University.

Introduction to Program Segments• Logical vs. Physical Address in the Data Logical vs. Physical Address in the Data

Segment:Segment:– The physical address for data is calculated using the

same rules as for the code segment.• The physical address of data is calculated by shifting DS

left one hex digit and adding the offset value.

Page 64: Microprocessors Sunday, Mar. 23 Dr. Asmaa Farouk Faculty of Engineering, Electrical Department, Assiut University.

Introduction to Program Segments• Logical vs. Physical Address in the Data Logical vs. Physical Address in the Data

Segment: (Cont.)Segment: (Cont.)– Example (1):

Page 65: Microprocessors Sunday, Mar. 23 Dr. Asmaa Farouk Faculty of Engineering, Electrical Department, Assiut University.

Introduction to Program Segments• Logical vs. Physical Address in the Data Logical vs. Physical Address in the Data

Segment: (Cont.)Segment: (Cont.)– Example (2):

Page 66: Microprocessors Sunday, Mar. 23 Dr. Asmaa Farouk Faculty of Engineering, Electrical Department, Assiut University.

Introduction to Program Segments• Logical vs. Physical Address in the Data Logical vs. Physical Address in the Data

Segment: (Cont.)Segment: (Cont.)– Example (2):

Page 67: Microprocessors Sunday, Mar. 23 Dr. Asmaa Farouk Faculty of Engineering, Electrical Department, Assiut University.

Introduction to Program Segments• Logical vs. Physical Address in the Data Logical vs. Physical Address in the Data

Segment: (Cont.)Segment: (Cont.)– Example (2):

Page 68: Microprocessors Sunday, Mar. 23 Dr. Asmaa Farouk Faculty of Engineering, Electrical Department, Assiut University.

Introduction to Program Segments• Logical vs. Physical Address in the Data Logical vs. Physical Address in the Data

Segment: (Cont.)Segment: (Cont.)– Example (2):

Page 69: Microprocessors Sunday, Mar. 23 Dr. Asmaa Farouk Faculty of Engineering, Electrical Department, Assiut University.

Introduction to Program Segments• Logical vs. Physical Address in the Data Logical vs. Physical Address in the Data

Segment: (Cont.)Segment: (Cont.)– Example (2):

Page 70: Microprocessors Sunday, Mar. 23 Dr. Asmaa Farouk Faculty of Engineering, Electrical Department, Assiut University.

Introduction to Program Segments• Logical vs. Physical Address in the Data Logical vs. Physical Address in the Data

Segment: (Cont.)Segment: (Cont.)– Example (3):

• Lower range = 578C0 + 0000 = 578C0H.• Higher range = 578C0 + FFFF = 678BFH.

Page 71: Microprocessors Sunday, Mar. 23 Dr. Asmaa Farouk Faculty of Engineering, Electrical Department, Assiut University.

Introduction to Program Segments• Logical vs. Physical Address in the Data Logical vs. Physical Address in the Data

Segment: (Cont.)Segment: (Cont.)– Example (3):

Page 72: Microprocessors Sunday, Mar. 23 Dr. Asmaa Farouk Faculty of Engineering, Electrical Department, Assiut University.

Introduction to Program Segments• Little Endian Convention:Little Endian Convention:– Previous examples used 8-bit or 1-byte data.• What happens when 16-bit data is used?

– The low byte goes to the low memory location and the high byte goes to the high memory address.• Memory location DS:1500 contains F3H.• Memory location DS:1501 contains 35H.

– (DS:1500 = F3 DS:1501 = 35).

• This convention is called little endian vs. big endian.

Page 73: Microprocessors Sunday, Mar. 23 Dr. Asmaa Farouk Faculty of Engineering, Electrical Department, Assiut University.

Introduction to Program Segments• Little Endian Convention: (Cont.)Little Endian Convention: (Cont.)– In the big endian method, the high byte goes to the

low address.• In the little endian method, the high byte goes to the high

address and the low byte to the low address.

– Example:

Page 74: Microprocessors Sunday, Mar. 23 Dr. Asmaa Farouk Faculty of Engineering, Electrical Department, Assiut University.

Introduction to Program Segments• Extra Segment (ES):Extra Segment (ES):– ES is a segment register used as an extra data

segment.• In many normal programs this segment is not used.• Using it is essential for string operations.

Page 75: Microprocessors Sunday, Mar. 23 Dr. Asmaa Farouk Faculty of Engineering, Electrical Department, Assiut University.

Topics of Today

• The Memory.• Real Mode Memory Addressing.• Introduction to Program Segments.• More about Segments in the 80x86.

Page 76: Microprocessors Sunday, Mar. 23 Dr. Asmaa Farouk Faculty of Engineering, Electrical Department, Assiut University.

More about Segments in the 80x86• What is the Stack? Why is it Needed?What is the Stack? Why is it Needed?– The stack is a section of read/write memory (RAM)

used by the CPU to store information temporarily.– The CPU needs this storage area since there are only

a limited number of registers.– Main disadvantage of the stack is the access time.• Since the stack is in RAM, it takes much longer to access

compared to the access time of registers.

– Some very powerful (expensive) computers do not have a stack.• The CPU has a large number of registers to work with.

Page 77: Microprocessors Sunday, Mar. 23 Dr. Asmaa Farouk Faculty of Engineering, Electrical Department, Assiut University.

More about Segments in the 80x86• How Stacks are Accessed:How Stacks are Accessed:– The stack is a section of RAM, so there must be

registers inside the CPU to point to it.• The SS (stack segment) register.• The SP (stack pointer) register.

– These registers must be loaded before any instructions accessing the stack are used.

Page 78: Microprocessors Sunday, Mar. 23 Dr. Asmaa Farouk Faculty of Engineering, Electrical Department, Assiut University.

More about Segments in the 80x86• How Stacks are Accessed: (Cont.)How Stacks are Accessed: (Cont.)– Every register inside the x86 can be stored in the

stack, and brought back into the CPU from the stack, except segment registers and SP.• Storing a CPU register in the stack is called a PUSH.• Loading the contents of the stack into the CPU register is

called a POP.

Page 79: Microprocessors Sunday, Mar. 23 Dr. Asmaa Farouk Faculty of Engineering, Electrical Department, Assiut University.

More about Segments in the 80x86• How Stacks are Accessed: (Cont.)How Stacks are Accessed: (Cont.)– The stack pointer register (SP) points to the current

memory location used as the top of the stack.• As data is pushed onto the stack it is decremented.• As data is popped off the stack into the CPU, it is

incremented.

– When an instruction pushes or pops a general-purpose register, it must be the entire 16-bit register.• One must code "PUSH AX".

– There are no instructions such as "PUSH AL" or "PUSH AH".

Page 80: Microprocessors Sunday, Mar. 23 Dr. Asmaa Farouk Faculty of Engineering, Electrical Department, Assiut University.

More about Segments in the 80x86• How Stacks are Accessed: (Cont.)How Stacks are Accessed: (Cont.)– The SP is decremented after the push is to make

sure the stack is growing downward from upper addresses to lower addresses.• The opposite of the IP (instruction pointer).

– To ensure the code section & stack section of the program never write over each other, they are located at opposite ends of the RAM set aside for the program.• They grow toward each other but must not meet.• If they meet, the program will crash.

Page 81: Microprocessors Sunday, Mar. 23 Dr. Asmaa Farouk Faculty of Engineering, Electrical Department, Assiut University.

More about Segments in the 80x86• Pushing Onto the Stack:Pushing Onto the Stack:– As each PUSH is executed, the register contents are

saved on the stack and SP is decremented by 2.• For every byte of data saved on the stack, SP is

decremented once.• Since the PUSH is saving the contents of a 16-bit register,

it decrements twice.

Page 82: Microprocessors Sunday, Mar. 23 Dr. Asmaa Farouk Faculty of Engineering, Electrical Department, Assiut University.

More about Segments in the 80x86• Pushing Onto the Stack: (Cont.)Pushing Onto the Stack: (Cont.)– Example:

Page 83: Microprocessors Sunday, Mar. 23 Dr. Asmaa Farouk Faculty of Engineering, Electrical Department, Assiut University.

More about Segments in the 80x86• Pushing Onto the Stack: (Cont.)Pushing Onto the Stack: (Cont.)– Example:

– In the x86, the lower byte is always stored in the memory location with the lower address.• 24H, the content of AH, is

saved in the memory location with the address 1235.

• AL is stored in location 1234.

Page 84: Microprocessors Sunday, Mar. 23 Dr. Asmaa Farouk Faculty of Engineering, Electrical Department, Assiut University.

More about Segments in the 80x86• Pushing Onto the Stack: (Cont.)Pushing Onto the Stack: (Cont.)– Example:

Page 85: Microprocessors Sunday, Mar. 23 Dr. Asmaa Farouk Faculty of Engineering, Electrical Department, Assiut University.

More about Segments in the 80x86• Popping the Stack:Popping the Stack:– With every POP, the top 2 bytes of the stack are

copied to the x86 CPU register specified by the instruction and SP is incremented twice.

Page 86: Microprocessors Sunday, Mar. 23 Dr. Asmaa Farouk Faculty of Engineering, Electrical Department, Assiut University.

More about Segments in the 80x86• Popping the Stack: (Cont.)Popping the Stack: (Cont.)– Example:

Page 87: Microprocessors Sunday, Mar. 23 Dr. Asmaa Farouk Faculty of Engineering, Electrical Department, Assiut University.

More about Segments in the 80x86• Popping the Stack: (Cont.)Popping the Stack: (Cont.)– Example:

– Note that, While the data actually remains in memory, it is not accessible, since the stack pointer, SP is beyond that point.

Page 88: Microprocessors Sunday, Mar. 23 Dr. Asmaa Farouk Faculty of Engineering, Electrical Department, Assiut University.

More about Segments in the 80x86• Popping the Stack: (Cont.)Popping the Stack: (Cont.)– Example:

Page 89: Microprocessors Sunday, Mar. 23 Dr. Asmaa Farouk Faculty of Engineering, Electrical Department, Assiut University.

More about Segments in the 80x86• Logical vs. Physical Address for the Stack:Logical vs. Physical Address for the Stack:– The exact physical location of the stack depends on

the value of the stack segment (SS) register and the stack pointer (SP). • To compute physical addresses for the stack, shift left SS,

then add offset SP.• The operating system assigns values for the SP and SS.

– Example:

Page 90: Microprocessors Sunday, Mar. 23 Dr. Asmaa Farouk Faculty of Engineering, Electrical Department, Assiut University.

More about Segments in the 80x86• A few more Words about Segments in 80x86:A few more Words about Segments in 80x86:

1. Can a single physical address belong to many different logical addresses?

• Yes, many possible logical addresses can represent a single physical address.• This shows the dynamic behavior of the segment and

offset concept in the 8086 CPU.

Page 91: Microprocessors Sunday, Mar. 23 Dr. Asmaa Farouk Faculty of Engineering, Electrical Department, Assiut University.

More about Segments in the 80x86• A few more Words about Segments in 80x86:A few more Words about Segments in 80x86:

2. When adding the offset to the shifted segment register that results in an address beyond the maximum allowed range of FFFFFH, wrap-around will occur.

Page 92: Microprocessors Sunday, Mar. 23 Dr. Asmaa Farouk Faculty of Engineering, Electrical Department, Assiut University.

More about Segments in the 80x86• A few more Words about Segments in 80x86:A few more Words about Segments in 80x86:– Example:

Page 93: Microprocessors Sunday, Mar. 23 Dr. Asmaa Farouk Faculty of Engineering, Electrical Department, Assiut University.

More about Segments in the 80x86• A few more Words about Segments in 80x86:A few more Words about Segments in 80x86:– Example:

The higher range = FF590 + FFFF = 10F58FH.

Page 94: Microprocessors Sunday, Mar. 23 Dr. Asmaa Farouk Faculty of Engineering, Electrical Department, Assiut University.

More about Segments in the 80x86• A few more Words about Segments in 80x86:A few more Words about Segments in 80x86:– Example:

Page 95: Microprocessors Sunday, Mar. 23 Dr. Asmaa Farouk Faculty of Engineering, Electrical Department, Assiut University.

More about Segments in the 80x86• Overlapping:Overlapping:– In calculating the physical address, it is possible that

two segments can overlap.

Page 96: Microprocessors Sunday, Mar. 23 Dr. Asmaa Farouk Faculty of Engineering, Electrical Department, Assiut University.

Questions?Questions?