intel x86 memory architecture - h ac · 2020. 7. 24. · hardware/software systems and assembly...

49
1 Dr. Martin Land — Hadassah College — Fall 2014 x86 Memory Architecture Hardware/Software Systems and Assembly Programming Intel x86 Memory Architecture

Upload: others

Post on 20-Jan-2021

6 views

Category:

Documents


0 download

TRANSCRIPT

Page 1: Intel x86 Memory Architecture - h Ac · 2020. 7. 24. · Hardware/Software Systems and Assembly Programming x86 Memory Architecture Dr. Martin Land — Hadassah College — Fall 2014

1Dr. Martin Land — Hadassah College — Fall 2014x86 Memory ArchitectureHardware/Software Systems and Assembly Programming

Intel x86Memory

Architecture

Page 2: Intel x86 Memory Architecture - h Ac · 2020. 7. 24. · Hardware/Software Systems and Assembly Programming x86 Memory Architecture Dr. Martin Land — Hadassah College — Fall 2014

2Dr. Martin Land — Hadassah College — Fall 2014x86 Memory ArchitectureHardware/Software Systems and Assembly Programming

“The x86 isn't all that complex — it just doesn't make a lot of sense.”

Mike Johnson, Leader of 80x86 Design at AMD, Microprocessor Report (1994)

Page 3: Intel x86 Memory Architecture - h Ac · 2020. 7. 24. · Hardware/Software Systems and Assembly Programming x86 Memory Architecture Dr. Martin Land — Hadassah College — Fall 2014

3Dr. Martin Land — Hadassah College — Fall 2014x86 Memory ArchitectureHardware/Software Systems and Assembly Programming

Program SegmentsData and BSS sections

ConstantsInitialized and uninitialized variablesTables, arrays, stringsStructures

Text sectionProcedures / functions / control blocksData access

Load operations — RAM to registerStore operations — register to RAMPeripheral I/O operations

CalculationArithmetic operations Logic operations

Stack sectionLIFO buffer accessible by PUSH / POP operations

Page 4: Intel x86 Memory Architecture - h Ac · 2020. 7. 24. · Hardware/Software Systems and Assembly Programming x86 Memory Architecture Dr. Martin Land — Hadassah College — Fall 2014

4Dr. Martin Land — Hadassah College — Fall 2014x86 Memory ArchitectureHardware/Software Systems and Assembly Programming

x86 Data and Address Ranges

data

physical addressx86 RAM

64-bit ModeProtected ModeReal Mode

000000000000 to FFFFFFFFFFFF

256 TB

48

-8E to 8E – 1

0 to 16E

64

00000000 to FFFFFFFF

4 GB

00000 to FFFFF1 MB

Physical Memory

3220Address bits

–2G to +2G - 1–32K to 32K – 1Signed Integer

0 to 4G0 to 64KUnsigned Integer

3216Data bits

T = 240 = 10244 (Tera)P = 250 = 10245 (Peta)E = 260 = 10246 (Exa)

K = 210 = 1024 (Kilo)M = 220 = 10242 (Mega)G = 230 = 10243 (Giga)

Page 5: Intel x86 Memory Architecture - h Ac · 2020. 7. 24. · Hardware/Software Systems and Assembly Programming x86 Memory Architecture Dr. Martin Land — Hadassah College — Fall 2014

5Dr. Martin Land — Hadassah College — Fall 2014x86 Memory ArchitectureHardware/Software Systems and Assembly Programming

Logical Address CPUx86 access to physical RAM

CPU accesses external deviceAccess by physical address

32 bits or 48 bits

Programs do not see physical addressSoftware address = logical address = SEG:OFFSEG

Segment selector in CPU segment registerMaps to Physical Base AddressPointer to start of segment in memory

OFF

Offset formed by pointer arithmeticPointer to byte in segment

Physical Address = Physical Base Address + Offset

PhysicalBase

Address

Offset

PhysicalAddress

RAM

data

addressx86 RAM

Page 6: Intel x86 Memory Architecture - h Ac · 2020. 7. 24. · Hardware/Software Systems and Assembly Programming x86 Memory Architecture Dr. Martin Land — Hadassah College — Fall 2014

6Dr. Martin Land — Hadassah College — Fall 2014x86 Memory ArchitectureHardware/Software Systems and Assembly Programming

x86 Segment Registersx86 segments are sections of physical memory

No one-to-one connection with program segments

Six defined memory pointers

DS (name ~ Data Segment)CS (name ~ Code Segment)SS (name ~ Stack Segment)ES (name ~ Extra Segment)FS, GS

Six segment registers

GS selector GS FS selector FS ES selector ES SS selector SS CS selector CS DS selector DS

15 0

ES

RAM

SS

CS

DS

FS

GS

Page 7: Intel x86 Memory Architecture - h Ac · 2020. 7. 24. · Hardware/Software Systems and Assembly Programming x86 Memory Architecture Dr. Martin Land — Hadassah College — Fall 2014

7Dr. Martin Land — Hadassah College — Fall 2014x86 Memory ArchitectureHardware/Software Systems and Assembly Programming

Typical Segment Register Usage

GS

FS

ES

DS

CS

SSSeparate 

selector values in

segment registersDS, ES, CS, SS,FS, GS

All selectors point to same 

segment 

32‐bit Windows program

6 defined segments

Segment ≤ 4 GB

64‐bit program

1 segment

Segment ≤ 256 TB

ES

DS

CS

SS

Real mode program

4 defined segments

Segment ≤ 64 KB

Separate selector values 

in

segment registersDS, ES, CS, SS,FS, GS

All selectors point to same 

segment 

32‐bit Linux program

1 segment

Segment ≤ 4 GB

Page 8: Intel x86 Memory Architecture - h Ac · 2020. 7. 24. · Hardware/Software Systems and Assembly Programming x86 Memory Architecture Dr. Martin Land — Hadassah College — Fall 2014

8Dr. Martin Land — Hadassah College — Fall 2014x86 Memory ArchitectureHardware/Software Systems and Assembly Programming

Mapping SegmentReal mode segment mapping

SEG = 16-bit segment SELECTOR in segment registerSEG × 10h = 20-bit physical base address

segment register const

0SEG = segment selector

4 bits16 bits

Protected mode segment mappingSEG = 16-bit segment SELECTOR in segment register

Selector = index to descriptor tableDescriptor = table entry holding

32-bit or 64-bit physical base addressSegment sizeSegment typeSegment access rights

descriptor

descriptor

descriptor

descriptor

descriptor

descriptor

selector

physical base address

Page 9: Intel x86 Memory Architecture - h Ac · 2020. 7. 24. · Hardware/Software Systems and Assembly Programming x86 Memory Architecture Dr. Martin Land — Hadassah College — Fall 2014

9Dr. Martin Land — Hadassah College — Fall 2014x86 Memory ArchitectureHardware/Software Systems and Assembly Programming

Real Mode Segments RegistersFour segment registers

Extra ES Stack SS Code CS Data DS

15 0

DS × 10h

RAM

CS × 10h

SS × 10h

ES × 10h

20-bit segmentbase addresses

segment register const

0SEG = segment selector

4 bits16 bits

Page 10: Intel x86 Memory Architecture - h Ac · 2020. 7. 24. · Hardware/Software Systems and Assembly Programming x86 Memory Architecture Dr. Martin Land — Hadassah College — Fall 2014

10Dr. Martin Land — Hadassah College — Fall 2014x86 Memory ArchitectureHardware/Software Systems and Assembly Programming

Real Mode Segment Example16-bit segment registers

4000 ES 3000 SS 2000 CS 1000 DS

15 0

extra

10000

RAM

stack

code

data

20000

30000

40000

20-bit segmentbase addresses

40000 Extra 30000 Stack 20000 Code 10000 Data

20-bit physical base addresses

Page 11: Intel x86 Memory Architecture - h Ac · 2020. 7. 24. · Hardware/Software Systems and Assembly Programming x86 Memory Architecture Dr. Martin Land — Hadassah College — Fall 2014

11Dr. Martin Land — Hadassah College — Fall 2014x86 Memory ArchitectureHardware/Software Systems and Assembly Programming

Real Mode OffsetOffset

16-bit numberCombination of registers and immediate values Offset ∈ {0000, 0001, 0002, … , FFFF}

216 possible offset values Maximum segment = 216 bytes = 64 KB

Physical Address = physical base address + offset

ExampleLogical Address = 1234:0005Segment selector = 1234Physical base address = 12340Physical Address = 12340 + 0005 = 12345 Physical

BaseAddress

Offset

PhysicalAddress

RAM

byte

byte

byte

byte

byte FFFF

0000

0001

0002

Page 12: Intel x86 Memory Architecture - h Ac · 2020. 7. 24. · Hardware/Software Systems and Assembly Programming x86 Memory Architecture Dr. Martin Land — Hadassah College — Fall 2014

12Dr. Martin Land — Hadassah College — Fall 2014x86 Memory ArchitectureHardware/Software Systems and Assembly Programming

Protected Mode OffsetOffset

32-bit numberCombination of registers and immediate values Offset ∈ {00000000, … , FFFFFFFF}

232 possible offset values Maximum segment = 232 bytes = 4 GB

Physical Address = physical base address + offset

ExampleLogical Address = 1234:11223344Segment selector = 1234Physical base address = 00000000Physical Address = 0 + 11223344 = 11223344

PhysicalBase

Address

Offset

PhysicalAddress

RAM

byte

byte

byte

byte

byte FFFFFFFF

00000000

00000001

00000002

descriptor table

Page 13: Intel x86 Memory Architecture - h Ac · 2020. 7. 24. · Hardware/Software Systems and Assembly Programming x86 Memory Architecture Dr. Martin Land — Hadassah College — Fall 2014

13Dr. Martin Land — Hadassah College — Fall 2014x86 Memory ArchitectureHardware/Software Systems and Assembly Programming

Program Segments versus Memory SegmentsSegment

Segment = any section, part, portion, subdivisionSegment ≠ specific, 1-to-1 technical meaning

Program segments (sections)Sections of assembly source code or executable programData segment

Space allocated for storing program data

Text (code) segmentSpace allocated for program instructions

Stack segmentSpace allocated for user stack operations

x86 memory segmentsDS, CS, SS, ES, FS, GS

Pointers to sections of physical memory (RAM)No one-to-one connection to program segments

Page 14: Intel x86 Memory Architecture - h Ac · 2020. 7. 24. · Hardware/Software Systems and Assembly Programming x86 Memory Architecture Dr. Martin Land — Hadassah College — Fall 2014

14Dr. Martin Land — Hadassah College — Fall 2014x86 Memory ArchitectureHardware/Software Systems and Assembly Programming

x86 Addressing ModesAddressing mode

Location of data in register, memory, or instruction

Effective Address (EA) Combination of register values and literal values

EAX ← mem[SS:ESP]ESP ← ESP + 4

POP EAXStack

AL ← mem[DS:ESI]ESI ← ESI + 1

LODSBString

regs[EAX] ← mem[DS:11223344]MOV EAX, [11223344]Absolute

regs[EAX] ← mem[SEG:EA]MOV EAX, [EA]Effective Address

regs[EAX] ← #12MOV EAX, 12hImmediate

regs[EAX] ← regs[EBX]MOV EAX, EBXRegister

Operation / Addressing ModeExampleMode

Page 15: Intel x86 Memory Architecture - h Ac · 2020. 7. 24. · Hardware/Software Systems and Assembly Programming x86 Memory Architecture Dr. Martin Land — Hadassah College — Fall 2014

15Dr. Martin Land — Hadassah College — Fall 2014x86 Memory ArchitectureHardware/Software Systems and Assembly Programming

32‐bit Effective AddressEffective Address (EA)

EA = base + index * scale + displacementBase register — 0 or 1 register name

base ∈ {EAX, EBX, ECX, EDX, ESP, EBP, ESI, EDI}

Index register — 0 or 1 register nameindex ∈ {EAX, EBX, ECX, EDX, EBP, ESI, EDI}

Scalescale ∈ {1, 2, 4, 8}

Displacement — 0 or 1 literal value

ExamplesMOV EAX,[EAX] Base 

MOV EAX,[EBX+EDI] Base + Index

MOV EAX,[ECX+4*EDI] Base + Index * Scale

MOV EAX,[EDX+8*ESI+1122h] Base + Index * Scale + Disp

MOV EAX,[11223344h] Displacement

Page 16: Intel x86 Memory Architecture - h Ac · 2020. 7. 24. · Hardware/Software Systems and Assembly Programming x86 Memory Architecture Dr. Martin Land — Hadassah College — Fall 2014

16Dr. Martin Land — Hadassah College — Fall 2014x86 Memory ArchitectureHardware/Software Systems and Assembly Programming

32‐bit Effective Address ⇒ SegmentEffective Address

MOV REG, [EA] ⇒ regs[REG] ← mem[SEG:EA]

EA = base + index * scale + displacement

Default segmentbase = ESP or EBP

SEG = SS

OtherwiseSEG = DS

ExamplesMOV EAX, [EAX] regs[EAX] ← mem[DS:EAX]MOV EAX, [ESP] regs[EAX] ← mem[SS:ESP]MOV EAX, [EBP + ESI] regs[EAX] ← mem[SS:EBP + ESI]MOV EAX, [EAX + 4*EPB] regs[EAX] ← mem[DS:EAX + 4*EPB]MOV EAX, [1123344] regs[EAX] ← mem[DS:1123344]

Page 17: Intel x86 Memory Architecture - h Ac · 2020. 7. 24. · Hardware/Software Systems and Assembly Programming x86 Memory Architecture Dr. Martin Land — Hadassah College — Fall 2014

17Dr. Martin Land — Hadassah College — Fall 2014x86 Memory ArchitectureHardware/Software Systems and Assembly Programming

x86 Register Addressing Mode

Addressing Mode

Example Instruction Meaning

ADD BX,AX REGS[BX] ← REGS[BX] + REGS[AX] Register

Used for operands that the ALU is using now

ESI EDI EBP EAX EBX ECX EDX ESP00001025 00001004 00000003 00000031 00000096 0000D400 00009008 00000000

0031

0096

00C7

Registers

Page 18: Intel x86 Memory Architecture - h Ac · 2020. 7. 24. · Hardware/Software Systems and Assembly Programming x86 Memory Architecture Dr. Martin Land — Hadassah College — Fall 2014

18Dr. Martin Land — Hadassah College — Fall 2014x86 Memory ArchitectureHardware/Software Systems and Assembly Programming

x86 Immediate Addressing Mode

Addressing Mode

Example Instruction Meaning

ADD BX,3 REGS[BX] ← REGS[BX] + 3 Immediate

Used for constant operands

ESI EDI EBP EAX EBX ECX EDX ESP00001025 00001004 00000003 00000031 00000096 0000D400 00009008 00000000

3

0096

0099

Registers

Page 19: Intel x86 Memory Architecture - h Ac · 2020. 7. 24. · Hardware/Software Systems and Assembly Programming x86 Memory Architecture Dr. Martin Land — Hadassah College — Fall 2014

19Dr. Martin Land — Hadassah College — Fall 2014x86 Memory ArchitectureHardware/Software Systems and Assembly Programming

x86 Absolute Addressing Mode

Addressing Mode

Example Instruction Meaning

ADD AL,[1001] REGS[AL] ← REGS[AL] + MEM[DS_start + 1001] Direct Or Absolute Used for storing general data in main memory

29

31

5A

1007 001006 581005 001004 FF1003 9E1002 101001 291000 45

ESI EDI EBP EAX EBX ECX EDX ESP00001025 00001004 00000003 00000031 00000096 0000D400 00009008 00000000

Registers

Main Memory(RAM)

Page 20: Intel x86 Memory Architecture - h Ac · 2020. 7. 24. · Hardware/Software Systems and Assembly Programming x86 Memory Architecture Dr. Martin Land — Hadassah College — Fall 2014

20Dr. Martin Land — Hadassah College — Fall 2014x86 Memory ArchitectureHardware/Software Systems and Assembly Programming

x86 Absolute Addressing Mode

Addressing Mode

Example Instruction Meaning

ADD DI,[1001] REGS[DI] ← REGS[DI] + MEM[DS_start + 1001] Direct Or Absolute Used for storing general data in main memory

1029

1004

202D

1007 001006 581005 001004 FF1003 9E1002 101001 291000 45

ESI EDI EBP EAX EBX ECX EDX ESP00001025 00001004 00000003 00000031 00000096 0000D400 00009008 00000000

Registers

Main Memory(RAM)

Page 21: Intel x86 Memory Architecture - h Ac · 2020. 7. 24. · Hardware/Software Systems and Assembly Programming x86 Memory Architecture Dr. Martin Land — Hadassah College — Fall 2014

21Dr. Martin Land — Hadassah College — Fall 2014x86 Memory ArchitectureHardware/Software Systems and Assembly Programming

x86 Based Addressing Mode

Addressing Mode

Example Instruction Meaning

ADD BX,[EDI] REGS[BX] ← REGS[BX] + MEM[DS_start + REGS[EDI]] Register Deferred A pointer to data is kept in the register

00FF

0096

0195

1007 001006 581005 001004 FF1003 9E1002 A81001 291000 45

ESI EDI EBP EAX EBX ECX EDX ESP00001025 00001004 00000003 00000031 00000096 0000D400 00009008 00000000

Registers

Main Memory(RAM)

Page 22: Intel x86 Memory Architecture - h Ac · 2020. 7. 24. · Hardware/Software Systems and Assembly Programming x86 Memory Architecture Dr. Martin Land — Hadassah College — Fall 2014

22Dr. Martin Land — Hadassah College — Fall 2014x86 Memory ArchitectureHardware/Software Systems and Assembly Programming

x86 Based Addressing Mode

Addressing Mode

Example Instruction Meaning

ADD EBX,[EDI] REGS[EBX] ← REGS[EBX] + MEM[DS_start + REGS[EDI]] Register Deferred A pointer to data is kept in the register

005800FF

0096

00580195

1007 001006 581005 001004 FF1003 9E1002 A81001 291000 45

ESI EDI EBP EAX EBX ECX EDX ESP00001025 00001004 00000003 00000031 00000096 0000D400 00009008 00000000

Registers

Main Memory(RAM)

Page 23: Intel x86 Memory Architecture - h Ac · 2020. 7. 24. · Hardware/Software Systems and Assembly Programming x86 Memory Architecture Dr. Martin Land — Hadassah College — Fall 2014

23Dr. Martin Land — Hadassah College — Fall 2014x86 Memory ArchitectureHardware/Software Systems and Assembly Programming

x86 Based Addressing Mode

Addressing Mode

Example Instruction Meaning

ADD BX,[EBP] REGS[BX] ← REGS[BX] + MEM[SS_start + REGS[EBP]] Register Deferred A pointer to data is kept in the register

00FF

0096

0195

0007 120006 630005 240004 000003 FF0002 230001 350000 34

ESI EDI EBP EAX EBX ECX EDX ESP00001025 00001004 00000003 00000031 00000096 0000D400 00009008 00000000

Registers

Main Memory(RAM)

EBP forces access to SS

Page 24: Intel x86 Memory Architecture - h Ac · 2020. 7. 24. · Hardware/Software Systems and Assembly Programming x86 Memory Architecture Dr. Martin Land — Hadassah College — Fall 2014

24Dr. Martin Land — Hadassah College — Fall 2014x86 Memory ArchitectureHardware/Software Systems and Assembly Programming

x86 Based with Displacement Addressing Mode

Addressing Mode

Example Instruction Meaning

ADD BX,[EDI+2] REGS[BX] ← REGS[BX] + MEM[DS_start + REGS[DI] + 2] Displacement

A constant offset is added to the pointer

0058

0096

00EE

1007 001006 581005 001004 FF1003 9E1002 A81001 291000 45

+2

ESI EDI EBP EAX EBX ECX EDX ESP00001025 00001004 00000003 00000031 00000096 0000D400 00009008 00000000

Registers

Main Memory(RAM)

Page 25: Intel x86 Memory Architecture - h Ac · 2020. 7. 24. · Hardware/Software Systems and Assembly Programming x86 Memory Architecture Dr. Martin Land — Hadassah College — Fall 2014

25Dr. Martin Land — Hadassah College — Fall 2014x86 Memory ArchitectureHardware/Software Systems and Assembly Programming

x86 Based and Indexed Addressing Mode

Addressing Mode Example Instruction Meaning

ADD BL,[EBP+EDI] REGS[BL]← REGS[BL]+ MEM[SS_start + REGS[EBP]] + REGS[EDI] Indexed

A stored offset or table index is added to the pointer

00

96

96

1007 001006 231005 451004 341003 911002 381001 241000 35

+

ESI EDI EBP EAX EBX ECX EDX ESP00001025 00001004 00000003 00000031 00000096 0000D400 00009008 00000000

Registers

Main Memory(RAM)

BP forces access to SS

Page 26: Intel x86 Memory Architecture - h Ac · 2020. 7. 24. · Hardware/Software Systems and Assembly Programming x86 Memory Architecture Dr. Martin Land — Hadassah College — Fall 2014

26Dr. Martin Land — Hadassah College — Fall 2014x86 Memory ArchitectureHardware/Software Systems and Assembly Programming

Segmentation Example:  Instruction Fetch

Code SegmentAddress = 10000 h

Offset = 0057 Code Segment

Code Byte

Physical ByteAddress = 10057 h

RAM

Instruction Fetch -- Logical Address = CS:IPCS = 0012IP = 0057Logical Address = CS:IP = 0012:0057Code Segment Physical Base Address = 10000 h

Page 27: Intel x86 Memory Architecture - h Ac · 2020. 7. 24. · Hardware/Software Systems and Assembly Programming x86 Memory Architecture Dr. Martin Land — Hadassah College — Fall 2014

27Dr. Martin Land — Hadassah College — Fall 2014x86 Memory ArchitectureHardware/Software Systems and Assembly Programming

8086 Segmentation Example:  MOV AX,[BX]

Data SegmentAddress = 21000 h

Offset = 0123 Data Segment

Data Byte

Physical ByteAddress = 21123 h

RAM

MOV Instruction -- DSEffective Address = EBXLogical Address = DS:EBX = 0047:0123Data Segment physical base address = 21000h

Page 28: Intel x86 Memory Architecture - h Ac · 2020. 7. 24. · Hardware/Software Systems and Assembly Programming x86 Memory Architecture Dr. Martin Land — Hadassah College — Fall 2014

28Dr. Martin Land — Hadassah College — Fall 2014x86 Memory ArchitectureHardware/Software Systems and Assembly Programming

8086 Segmentation Example:  MOV AX,[BX+SI+7]

Data SegmentAddress = 21000 h

Offset = 112A Data Segment

Data Byte

Physical ByteAddress = 2212A h

RAM

MOV Instruction -- DSEffective Address = EBX+ESI+7Logical Address = DS:EBX+ESI+7 = 1234:0123 + 1000 + 7

= 1234:112AData Segment physical base address = 21000

Page 29: Intel x86 Memory Architecture - h Ac · 2020. 7. 24. · Hardware/Software Systems and Assembly Programming x86 Memory Architecture Dr. Martin Land — Hadassah College — Fall 2014

29Dr. Martin Land — Hadassah College — Fall 2014x86 Memory ArchitectureHardware/Software Systems and Assembly Programming

MOV in x86  dest, src dest src

AX, [1234] REGS[AX] MEM[DS_start + 00001234]16-bits

REGS[AL] MEM[DS_start + 00001234] 8-bits

REGS[AH] MEM[DS_start + 00001235] 8-bits

AL, [1234] REGS[

MOV

M V

AL]

O

MOV

←⎯⎯⎯⎯⎯

←⎯⎯⎯⎯⎯

←⎯⎯⎯⎯⎯

MEM[DS_start + 00001234] 8-bits

AX, [EBX] REGS[AX] MEM[DS_start + REGS[EBX] ]16-bits

EAX, [EBX+ESI]

REGS[AX] MEM[DS_start + REGS[EBX] + REGS[ESI] ]32-bits

EAX, [EBX+ESI

MOV

MOV

MOV +12

←⎯⎯⎯⎯⎯

←⎯⎯⎯⎯⎯

←⎯⎯⎯⎯⎯

]

REGS[AX] MEM[DS_start + REGS[EBX] + REGS[ESI] + 12]32-bits

←⎯⎯⎯⎯⎯

Page 30: Intel x86 Memory Architecture - h Ac · 2020. 7. 24. · Hardware/Software Systems and Assembly Programming x86 Memory Architecture Dr. Martin Land — Hadassah College — Fall 2014

30Dr. Martin Land — Hadassah College — Fall 2014x86 Memory ArchitectureHardware/Software Systems and Assembly Programming

x86 Stack OperationsPUSH src

POP dest

[ ] 32-bits

ESP ESP - 4

SS:ESP src

←⎯⎯⎯⎯

[ ]32-bitsdest SS:ESP

ESP ESP + 4

←⎯⎯⎯⎯

Page 31: Intel x86 Memory Architecture - h Ac · 2020. 7. 24. · Hardware/Software Systems and Assembly Programming x86 Memory Architecture Dr. Martin Land — Hadassah College — Fall 2014

31Dr. Martin Land — Hadassah College — Fall 2014x86 Memory ArchitectureHardware/Software Systems and Assembly Programming

x86 Segment Override

CS: MOV [EBP],ECX [CS:EBP] ← ECX ES: MOV [EBP],ECX [ES:EBP] ← ECX DS: MOV [EBP],ECX [DS:EBP] ← ECX SS: MOV [ESI],ECX [SS:ESI] ← ECX

Page 32: Intel x86 Memory Architecture - h Ac · 2020. 7. 24. · Hardware/Software Systems and Assembly Programming x86 Memory Architecture Dr. Martin Land — Hadassah College — Fall 2014

32Dr. Martin Land — Hadassah College — Fall 2014x86 Memory ArchitectureHardware/Software Systems and Assembly Programming

x86 String Instructions ⎯ 1[ES:EDI] ← AL EDI ← EDI+1 (default) STOSB

Store String Byte (EDI ← EDI-1 if flag DF = 1)

[ES:EDI] ← AL [ES:EDI+1] ← AH EDI ← EDI+2 (default)

STOSW Store String Word

(EDI ← EDI-2 if flag DF = 1) [ES:EDI] ← EAX EDI ← EDI+4 (default) STOSD

Store String Double (EDI ← EDI-4 if flag DF = 1)

LODSB Load String Byte

AL ← [DS:ESI] ESI ← ESI+1 (default) (ESI ← ESI-1 if flag DF = 1) AL ← [DS:ESI] AH ← [DS:ESI+1] LODSW

Load String Word ESI ← ESI+2 (default)

(ESI ← ESI-2 if flag DF = 1)

LODSD Load String Double

EAX ← [DS:ESI] ESI ← ESI+4 (default) (ESI ← ESI-4 if flag DF = 1)

Page 33: Intel x86 Memory Architecture - h Ac · 2020. 7. 24. · Hardware/Software Systems and Assembly Programming x86 Memory Architecture Dr. Martin Land — Hadassah College — Fall 2014

33Dr. Martin Land — Hadassah College — Fall 2014x86 Memory ArchitectureHardware/Software Systems and Assembly Programming

x86 String Instructions ⎯ 2

MOVSB Move String Byte

[ES:EDI] ← [DS:ESI] (byte) EDI ← EDI+1 (default) ESI ← ESI+1

MOVSW Move String Word

[ES:EDI] ← [DS:ESI] (word) EDI ← EDI+2 (default) ESI ← ESI+2

MOVSD Move String Double

[ES:EDI] ← [DS:ESI] (dword) EDI ← EDI+4 (default) ESI ← ESI+4

SCASB Scan String Byte

AL-[ES:DI]; update flags (byte) EDI ← EDI+1 (default)

SCASW Scan String Word

AL-[ES:DI]; update flags (word) DI ← DI+2 (default)

SCASD Scan String Double

AL-[ES:DI]; update flags (dword) DI ← DI+4 (default)

Page 34: Intel x86 Memory Architecture - h Ac · 2020. 7. 24. · Hardware/Software Systems and Assembly Programming x86 Memory Architecture Dr. Martin Land — Hadassah College — Fall 2014

34Dr. Martin Land — Hadassah College — Fall 2014x86 Memory ArchitectureHardware/Software Systems and Assembly Programming

x86 String Instructions ⎯ 3

CMPSB Compare String Byte

[DS:ESI]-[ES:EDI] (byte) Update flags EDI ← EDI+1 (default) ESI ← ESI+1

CMPSW Compare String Word

[DS:ESI]-[ES:EDI] (word) Update flags EDI ← EDI+2 (default) ESI ← ESI+2

CMPSD Compare String Double

[DS:ESI]-[ES:EDI] (dword) Update flags EDI ← EDI+4 (default) ESI ← ESI+4

Page 35: Intel x86 Memory Architecture - h Ac · 2020. 7. 24. · Hardware/Software Systems and Assembly Programming x86 Memory Architecture Dr. Martin Land — Hadassah College — Fall 2014

35Dr. Martin Land — Hadassah College — Fall 2014x86 Memory ArchitectureHardware/Software Systems and Assembly Programming

8086 String Instructions ⎯ 4

REP STOSB STOSB ECX ← ECX - 1 Repeat until ECX = 0

REP MOVSB MOVSB ECX ← ECX - 1 Repeat until ECX = 0

Page 36: Intel x86 Memory Architecture - h Ac · 2020. 7. 24. · Hardware/Software Systems and Assembly Programming x86 Memory Architecture Dr. Martin Land — Hadassah College — Fall 2014

36Dr. Martin Land — Hadassah College — Fall 2014x86 Memory ArchitectureHardware/Software Systems and Assembly Programming

Working with StringsPUSH ES ; ESP ← ESP – 4

; [SS:ESP] ← ESPUSH DS ; ESP ← ESP – 4

; [SS:ESP] ← DSPOP ES ; ES ← DS

; ESP ← ESP + 4MOV SI,0 ; SI ← 0MOV DI,1000 ; DI ← 1000MOV ECX,200 ; ECX ← 200REP MOVSB ; COPY 200 H BYTES FROM

; DS:0000 – DS:01FF to ; DS:1000 – DS:11FF

POP ES ; restore old ES

; ESP ← ESP + 4

Page 37: Intel x86 Memory Architecture - h Ac · 2020. 7. 24. · Hardware/Software Systems and Assembly Programming x86 Memory Architecture Dr. Martin Land — Hadassah College — Fall 2014

37Dr. Martin Land — Hadassah College — Fall 2014x86 Memory ArchitectureHardware/Software Systems and Assembly Programming

Branch Instructions

Fall-throughInstruction following branch in program listingNext instruction if branch not takenCS:EIP points to fall-through

TargetNext instruction if branch takenCS:EIP points to target

DisplacementDisplacement = target EIP – fall-through EIP

Displacement > 0 is forward jumpDisplacement < 0 is backward jump

branch

target

fall through

branchtaken

Page 38: Intel x86 Memory Architecture - h Ac · 2020. 7. 24. · Hardware/Software Systems and Assembly Programming x86 Memory Architecture Dr. Martin Land — Hadassah College — Fall 2014

38Dr. Martin Land — Hadassah College — Fall 2014x86 Memory ArchitectureHardware/Software Systems and Assembly Programming

Jump DistanceDisplacement

target EIP – fall-through EIP

Short Jump Target in same code segment Displacement is byte

Near Jump Target in same code segment Pointer is EIP

Far JumpTarget in different code segmentPointer is CS:EIPDisplacement not relevant

branch

short target

near target

far target

shortjump

nearjump

farjump

fall through

≤ ≤10 10-128 = 80 displacement 7F = 127

Page 39: Intel x86 Memory Architecture - h Ac · 2020. 7. 24. · Hardware/Software Systems and Assembly Programming x86 Memory Architecture Dr. Martin Land — Hadassah College — Fall 2014

39Dr. Martin Land — Hadassah College — Fall 2014x86 Memory ArchitectureHardware/Software Systems and Assembly Programming

Jump Instruction

JMP 1024 EIP ←32 1024

JMP [1024] EIP ←32 [1024] JMP target

JMP [ESI] EIP ←32 [DS:ESI]

JMP FAR [1024] CS ←16 [1028] EIP ←32 [1024]

JMP far target

JMP FAR [SI] CS ←16 [DS:ESI+4] EIP ←32 [DS:ESI]

Page 40: Intel x86 Memory Architecture - h Ac · 2020. 7. 24. · Hardware/Software Systems and Assembly Programming x86 Memory Architecture Dr. Martin Land — Hadassah College — Fall 2014

40Dr. Martin Land — Hadassah College — Fall 2014x86 Memory ArchitectureHardware/Software Systems and Assembly Programming

Call and Return

CALL near target

PUSH EIP

EIP ← target

CALL 1024 PUSH EIP

EIP ← 1024

CALL ESI PUSH EIP

EIP ← ESI

CALL [ESI] PUSH EIP

EIP ← [DS:ESI]

RET POP EIP

CALL far target

PUSH CS PUSH EIP

CS ← SEG

EIP ← OFF

CALL [ESI] PUSH CS PUSH EIP

CS ← [DS:ESI+4]

EIP ← [DS:ESI]

RETF POP EIP POP CS

Page 41: Intel x86 Memory Architecture - h Ac · 2020. 7. 24. · Hardware/Software Systems and Assembly Programming x86 Memory Architecture Dr. Martin Land — Hadassah College — Fall 2014

41Dr. Martin Land — Hadassah College — Fall 2014x86 Memory ArchitectureHardware/Software Systems and Assembly Programming

Indirect Far Call

stackESP

CSEIP

fall-throughCALL [ESI]

DS

ESI

665544332211

stack

ESP

CS = 6655

EIP = 44332211

fall-throughCALL [ESI]

DS

ESI

665544332211

fall-through CSfall-through EIP

Page 42: Intel x86 Memory Architecture - h Ac · 2020. 7. 24. · Hardware/Software Systems and Assembly Programming x86 Memory Architecture Dr. Martin Land — Hadassah College — Fall 2014

42Dr. Martin Land — Hadassah College — Fall 2014x86 Memory ArchitectureHardware/Software Systems and Assembly Programming

InterruptInterrupt

Pushes values of flags and CS:EIP onto stackTransfers control to Interrupt Service Routine (ISR)

ISR can be stored anywhere in memoryCS:EIP for ISR found in table

Interrupt Vector Descriptor Table (IDT)Managed by operating systemInterrupt descriptor

Target address of ISRPermissions for running ISR

Hardware interruptInitiated by system hardware and peripheral devices

TrapINT instructionOperates exactly as hardware interrupt

Page 43: Intel x86 Memory Architecture - h Ac · 2020. 7. 24. · Hardware/Software Systems and Assembly Programming x86 Memory Architecture Dr. Martin Land — Hadassah College — Fall 2014

43Dr. Martin Land — Hadassah College — Fall 2014x86 Memory ArchitectureHardware/Software Systems and Assembly Programming

Software Interrupt Instructions

INT type INT 80H

PUSH flags IF ← 0 ; block further interrupts TF ← 0 ; no debug mode PUSH CS PUSH EIP CS:EIP ← IDT values for interrupt 80

IRET none IRET POP EIP POP CS POP flags

Page 44: Intel x86 Memory Architecture - h Ac · 2020. 7. 24. · Hardware/Software Systems and Assembly Programming x86 Memory Architecture Dr. Martin Land — Hadassah College — Fall 2014

44Dr. Martin Land — Hadassah College — Fall 2014x86 Memory ArchitectureHardware/Software Systems and Assembly Programming

Interrupt

stackSP

CS

EIP fall-throughINT 80

IDT

665544332211

stack

SP

fall-throughINT 80

flagsfall-through CSfall-through IP

CS = 6655

IP = 44332211

IDT

665544332211

Page 45: Intel x86 Memory Architecture - h Ac · 2020. 7. 24. · Hardware/Software Systems and Assembly Programming x86 Memory Architecture Dr. Martin Land — Hadassah College — Fall 2014

45Dr. Martin Land — Hadassah College — Fall 2014x86 Memory ArchitectureHardware/Software Systems and Assembly Programming

Processor Control Instructions

STC CF ← I Set carry flag

CLC CF ← 0 Clear carry flag

CMC CF ← not(CF) Complement carry flag

STD DF ← 1 Set direction flag

CLD DF ← 0 Clear direction flag

STI IF ← I Set interrupt flag

CLI IF ← 0 Clear interrupt flag

HLT None CPU stops — requires reset

WAIT None Enter wait state

NOP None No operation

Page 46: Intel x86 Memory Architecture - h Ac · 2020. 7. 24. · Hardware/Software Systems and Assembly Programming x86 Memory Architecture Dr. Martin Land — Hadassah College — Fall 2014

46Dr. Martin Land — Hadassah College — Fall 2014x86 Memory ArchitectureHardware/Software Systems and Assembly Programming

LEALoad Effective Address

Similar to MOVCopies address (pointer) of memory locationDoes not access memory

32-bits

32-bits

32-bits

32-bits

32-bits

dest, [EA] dest EA

BX, [x] BX &(x)

AX, [1234] AX

LEA

LEA

1234

CX, [SI+12] CX SI+12

DX, [BX +DI] DX B

LEA

LEA

L X +DIEA

←⎯⎯⎯⎯

←⎯⎯⎯⎯

←⎯⎯⎯⎯

←⎯⎯⎯⎯

←⎯⎯⎯⎯

Page 47: Intel x86 Memory Architecture - h Ac · 2020. 7. 24. · Hardware/Software Systems and Assembly Programming x86 Memory Architecture Dr. Martin Land — Hadassah College — Fall 2014

47Dr. Martin Land — Hadassah College — Fall 2014x86 Memory ArchitectureHardware/Software Systems and Assembly Programming

Moving Data Around

MOV ESI,1122 ; ESI ← 1122

MOV [0000],ESI ; [DS:0000] ← 1122

MOV EBX,3344 ; EBX ← 3344

MOV [EBX],ESI ; [DS:3344] ← 1122

MOV [EBX+ESI],EBX ; [DS:4466] ← 3344

LEA EBX,[EBX+ESI] ; EBX ← 4466

MOV EDI,[EBX] ; EDI ← [DS:4466]

MOV ES,EDI ; ES ← [DS:4466]

MOV EAX,[EBX+2] ; EAX ← [DS:4468]

Page 48: Intel x86 Memory Architecture - h Ac · 2020. 7. 24. · Hardware/Software Systems and Assembly Programming x86 Memory Architecture Dr. Martin Land — Hadassah College — Fall 2014

48Dr. Martin Land — Hadassah College — Fall 2014x86 Memory ArchitectureHardware/Software Systems and Assembly Programming

LDS and LESLoads 48-bit logical address of type DS:EA

32-bits

16-bits

32-bits

16-bits

32-bits

16-bits

LDS dest, [EA] dest [EA]

DS [EA +4]

LDS EBX, [ESI] EBX [DS:ESI]

DS [DS:ESI+4]

LES dest, [EA] dest [EA]

ES [EA +4]

←⎯⎯⎯⎯

←⎯⎯⎯⎯

←⎯⎯⎯⎯

←⎯⎯⎯⎯

←⎯⎯⎯⎯

←⎯⎯⎯⎯

11 22 DS ← 1122 33 44 55 DS:ESI → 66

EBX ← 33445566

77 88 99 AA

DS

Page 49: Intel x86 Memory Architecture - h Ac · 2020. 7. 24. · Hardware/Software Systems and Assembly Programming x86 Memory Architecture Dr. Martin Land — Hadassah College — Fall 2014

49Dr. Martin Land — Hadassah College — Fall 2014x86 Memory ArchitectureHardware/Software Systems and Assembly Programming

Switching Data Tables/* DO ARITHMETIC WITH DS:BX = 1111:2222 *//* SWITCH DATA TABLES */

MOV [ESI], 4444 ; [ESI] ← 4444MOV [ESI+4], 3333 ; [ESI+4] ← 3333PUSH DS ; ESP ← ESP – 4

; [SS:ESP] ← 1111PUSH BX ; ESP ← ESP – 4

; [SS:ESP] ← 2222LDS EBX,[SI] ; EBX ← 4444

; DS ← 3333/* DO ARITHMETIC WITH DS:BX = 3333:4444 *//* SWITCH BACK TO FIRST DATA TABLE */

POP [ESI] ; [ESI] ← 2222; ESP ← ESP + 4

POP [ESI+4] ; [ESI+4] ← 1111; ESP ← ESP + 4

LDS EBX,[ESI] ; EBX ← 2222; DS ← 1111