Top Banner
Assembler Programming Lecture 2
36

Lecture 2 - Politechnika Śląskadb.zmitac.aei.polsl.pl/KT/Lecture2.pdf · 2003-10-17 · Lecture 2. Lecture 2 • 8086 family architecture. From 8086 to Pentium4. Registers, flags,

Mar 24, 2020

Download

Documents

dariahiddleston
Welcome message from author
This document is posted to help you gain knowledge. Please leave a comment to let me know what you think about it! Share it to your friends and learn new things together.
Transcript
Page 1: Lecture 2 - Politechnika Śląskadb.zmitac.aei.polsl.pl/KT/Lecture2.pdf · 2003-10-17 · Lecture 2. Lecture 2 • 8086 family architecture. From 8086 to Pentium4. Registers, flags,

Assembler Programming

Lecture 2

Page 2: Lecture 2 - Politechnika Śląskadb.zmitac.aei.polsl.pl/KT/Lecture2.pdf · 2003-10-17 · Lecture 2. Lecture 2 • 8086 family architecture. From 8086 to Pentium4. Registers, flags,

Lecture 2

• 8086 family architecture. From 8086 to Pentium4. Registers, flags, memory organization. Logical, physical, effectiveaddress. Addressing modes.

Page 3: Lecture 2 - Politechnika Śląskadb.zmitac.aei.polsl.pl/KT/Lecture2.pdf · 2003-10-17 · Lecture 2. Lecture 2 • 8086 family architecture. From 8086 to Pentium4. Registers, flags,

Processor

• Processor is the device that manages all actions and services in the system.

• Processor handles synchronous and asynchronous events.

• All those actions processor handles executing the programs or the procedures.

Page 4: Lecture 2 - Politechnika Śląskadb.zmitac.aei.polsl.pl/KT/Lecture2.pdf · 2003-10-17 · Lecture 2. Lecture 2 • 8086 family architecture. From 8086 to Pentium4. Registers, flags,

8086 family processors evolution• 8086 – real mode only, 16-bit, 1MB addressing.• 80186 – real mode only, 16-bit, 1MB.• 80286 – real and protected mode, 16-bit, 16MB.• 80386 – real and protected mode, 32-bit, 4GB.• i486 – real and protected mode, 32-bit, 4GB.• Pentium – real and protected, 32-bit, 4GB.• Pentium Pro – real and protected, 32-bit, 64GB.• Pentium II – real and protected, 32-bit, 64GB.• Pentium III – real and protected, 32-bit, 64GB.• Pentium 4 – real and protected, 32-bit, 64GB.

Page 5: Lecture 2 - Politechnika Śląskadb.zmitac.aei.polsl.pl/KT/Lecture2.pdf · 2003-10-17 · Lecture 2. Lecture 2 • 8086 family architecture. From 8086 to Pentium4. Registers, flags,

8086 architecture – general purpose registers

BLBH

BX

CLCH

CX

DLDH

DX

ALAH

AX

DI

BP

SI

Page 6: Lecture 2 - Politechnika Śląskadb.zmitac.aei.polsl.pl/KT/Lecture2.pdf · 2003-10-17 · Lecture 2. Lecture 2 • 8086 family architecture. From 8086 to Pentium4. Registers, flags,

8086 architecture – special purpose registers

SP IP

Flags CFPFAFZFSFTFIFDFOF

CarryParityAuxiliary CarryZeroSignTraceInterruptDirectionOverflow

Page 7: Lecture 2 - Politechnika Śląskadb.zmitac.aei.polsl.pl/KT/Lecture2.pdf · 2003-10-17 · Lecture 2. Lecture 2 • 8086 family architecture. From 8086 to Pentium4. Registers, flags,

8086 architecture – segment registers

CS

DS

ES

SS

Page 8: Lecture 2 - Politechnika Śląskadb.zmitac.aei.polsl.pl/KT/Lecture2.pdf · 2003-10-17 · Lecture 2. Lecture 2 • 8086 family architecture. From 8086 to Pentium4. Registers, flags,

Real mode 80386 registers

ALAHAX

DI

EAX

BLBHBX

EBX

CLCHCX

ECX

DLDHDX

EDX

EDI

BP

EBP

SI

ESI

SP

ESP

Page 9: Lecture 2 - Politechnika Śląskadb.zmitac.aei.polsl.pl/KT/Lecture2.pdf · 2003-10-17 · Lecture 2. Lecture 2 • 8086 family architecture. From 8086 to Pentium4. Registers, flags,

EIP and extended flag register

EFlags CFPFAFZFSFTFIFDFOFRFVM

I/O Privilege LevelNested TaskResume FlagVirtual ModeAlignment CheckVirt. Interrupt FlagVirt. Interrupt PendingIdentification

IP

EIP

NT PL PLACVIFVIPID

Page 10: Lecture 2 - Politechnika Śląskadb.zmitac.aei.polsl.pl/KT/Lecture2.pdf · 2003-10-17 · Lecture 2. Lecture 2 • 8086 family architecture. From 8086 to Pentium4. Registers, flags,

80386 segment registers

CS

DS

ES

SS

FS

GS

Page 11: Lecture 2 - Politechnika Śląskadb.zmitac.aei.polsl.pl/KT/Lecture2.pdf · 2003-10-17 · Lecture 2. Lecture 2 • 8086 family architecture. From 8086 to Pentium4. Registers, flags,

Other registers

• 80386 has registers for protected mode operation.

• i486 and above has math coprocessor’s registers.

• Pentium and above has multimedia extension registers for MMX.

• Pentium4 has another multimedia registers for SSE.

Page 12: Lecture 2 - Politechnika Śląskadb.zmitac.aei.polsl.pl/KT/Lecture2.pdf · 2003-10-17 · Lecture 2. Lecture 2 • 8086 family architecture. From 8086 to Pentium4. Registers, flags,

Memory addressing in real mode

• Memory is organized in segments– for 16-bit real mode segments’ size is 64kB,– for 32-bit mode segments’ size is 4GB.

• Logical address consists of two values– segment address,– offset within segment.

• Logical address is recalculated into linear address which in real mode corresponds to thephysical address of the memory.

Page 13: Lecture 2 - Politechnika Śląskadb.zmitac.aei.polsl.pl/KT/Lecture2.pdf · 2003-10-17 · Lecture 2. Lecture 2 • 8086 family architecture. From 8086 to Pentium4. Registers, flags,

Real address calculating

Segment (16 bit)

Segment

Offset (16 bit)

Linear address (20 bit)

Shift left 4 bits

0 0 0 0

Add Offset value

Fill with four zeros

+

Page 14: Lecture 2 - Politechnika Śląskadb.zmitac.aei.polsl.pl/KT/Lecture2.pdf · 2003-10-17 · Lecture 2. Lecture 2 • 8086 family architecture. From 8086 to Pentium4. Registers, flags,

Segmented addressing

CS

DS

ES

SS

FS

GS

Code Segment

Data Segment

Stack Segment

Data Segment

Data Segment

Data Segment

Page 15: Lecture 2 - Politechnika Śląskadb.zmitac.aei.polsl.pl/KT/Lecture2.pdf · 2003-10-17 · Lecture 2. Lecture 2 • 8086 family architecture. From 8086 to Pentium4. Registers, flags,

Addressing modes

• Instructions usually have the operands.– the right operand is the source,– the left operand is the destination.

• Operands can be of one of the types:– register,– immediate,– direct memory,– indirect memory.

• In the examples we will use mov instruction.

Page 16: Lecture 2 - Politechnika Śląskadb.zmitac.aei.polsl.pl/KT/Lecture2.pdf · 2003-10-17 · Lecture 2. Lecture 2 • 8086 family architecture. From 8086 to Pentium4. Registers, flags,

8086 register addressingmov ax, bxmov dl, almov si, dxmov sp, bpmov dh, clmov ax, ax

mov ax, csmov ds, ax

Page 17: Lecture 2 - Politechnika Śląskadb.zmitac.aei.polsl.pl/KT/Lecture2.pdf · 2003-10-17 · Lecture 2. Lecture 2 • 8086 family architecture. From 8086 to Pentium4. Registers, flags,

8086 immediate values

• Immediate value is a constant or result of constant expression.

• It is calculated during assembling.• It is placed in the code as part of the instruction.

mov ax, ‘A’mov ax, 0mov bx, 12*7

Page 18: Lecture 2 - Politechnika Śląskadb.zmitac.aei.polsl.pl/KT/Lecture2.pdf · 2003-10-17 · Lecture 2. Lecture 2 • 8086 family architecture. From 8086 to Pentium4. Registers, flags,

8086 memory addressing

• Direct memory addressing (displacement only).• Indirect memory addressing.

– Base addressing.– Index addressing.– Base addressing with indexing.– Base addressing with indexing and displacement.

Page 19: Lecture 2 - Politechnika Śląskadb.zmitac.aei.polsl.pl/KT/Lecture2.pdf · 2003-10-17 · Lecture 2. Lecture 2 • 8086 family architecture. From 8086 to Pentium4. Registers, flags,

8086 direct memory addressing

• Specifies the data at given address

mov ax, variablemov ax, [variable]mov si, ES:[variable]mov di, ES:[100]

Page 20: Lecture 2 - Politechnika Śląskadb.zmitac.aei.polsl.pl/KT/Lecture2.pdf · 2003-10-17 · Lecture 2. Lecture 2 • 8086 family architecture. From 8086 to Pentium4. Registers, flags,

8086 direct memory addressing

AX

BL one bytemov ds:[0800h], bl

low byte

high byte

0800h

mov ax, ds:[0600h]

0600h0601h

Page 21: Lecture 2 - Politechnika Śląskadb.zmitac.aei.polsl.pl/KT/Lecture2.pdf · 2003-10-17 · Lecture 2. Lecture 2 • 8086 family architecture. From 8086 to Pentium4. Registers, flags,

Indirect memory addressing

• Address of the data is held in the base or indexregister.

• While BX, SI or DI is used the processor as a default takes DS segment register.

• While BP is used the processor takes SS segment register.

mov ax, [bx]mov ax, [bp]mov al, [di]mov ah, [si]

Page 22: Lecture 2 - Politechnika Śląskadb.zmitac.aei.polsl.pl/KT/Lecture2.pdf · 2003-10-17 · Lecture 2. Lecture 2 • 8086 family architecture. From 8086 to Pentium4. Registers, flags,

Indirect memory addressing

BX

ALdatamov al, [bx]

DS

+

Page 23: Lecture 2 - Politechnika Śląskadb.zmitac.aei.polsl.pl/KT/Lecture2.pdf · 2003-10-17 · Lecture 2. Lecture 2 • 8086 family architecture. From 8086 to Pentium4. Registers, flags,

Base memory addressing

• Address of the beggining of the data table is held in the base register.

• Displacement inside the table is a constant.• All of the following variations are legal:

mov ax, element[bx]mov ax, [bx+element]mov ax, [bx]+element

Page 24: Lecture 2 - Politechnika Śląskadb.zmitac.aei.polsl.pl/KT/Lecture2.pdf · 2003-10-17 · Lecture 2. Lecture 2 • 8086 family architecture. From 8086 to Pentium4. Registers, flags,

Base memory addressing

BX

ALdatamov al, [bx]+element

DS

+ table beginning

element +

Page 25: Lecture 2 - Politechnika Śląskadb.zmitac.aei.polsl.pl/KT/Lecture2.pdf · 2003-10-17 · Lecture 2. Lecture 2 • 8086 family architecture. From 8086 to Pentium4. Registers, flags,

Index memory addressing

• Address of the data table is a constant.• Number of the element (byte) is held in the

index register.• All of the following variations are legal:

mov ax, table[si]mov ax, [si+table]mov ax, [si]+table

Page 26: Lecture 2 - Politechnika Śląskadb.zmitac.aei.polsl.pl/KT/Lecture2.pdf · 2003-10-17 · Lecture 2. Lecture 2 • 8086 family architecture. From 8086 to Pentium4. Registers, flags,

Index memory addressing

DI

ALdatamov al, table[di]

DS

+

+

table beginningtable

Page 27: Lecture 2 - Politechnika Śląskadb.zmitac.aei.polsl.pl/KT/Lecture2.pdf · 2003-10-17 · Lecture 2. Lecture 2 • 8086 family architecture. From 8086 to Pentium4. Registers, flags,

Base memory addressing with indexing

• Address of the data table is held in the baseregister.

• Number of the element (byte) is held in the index register.

• All of the following variations are legal:

mov ax, [bx][si]mov ax, [bx+si]mov ax, [si][bx]

Page 28: Lecture 2 - Politechnika Śląskadb.zmitac.aei.polsl.pl/KT/Lecture2.pdf · 2003-10-17 · Lecture 2. Lecture 2 • 8086 family architecture. From 8086 to Pentium4. Registers, flags,

Base memory addressing with indexing

DI

ALdatamov al, [bp][di]

SS

+

+

base addressBP

Page 29: Lecture 2 - Politechnika Śląskadb.zmitac.aei.polsl.pl/KT/Lecture2.pdf · 2003-10-17 · Lecture 2. Lecture 2 • 8086 family architecture. From 8086 to Pentium4. Registers, flags,

Base memory addressing with indexing

• It’s illegal to use two registers of the same type.• Only the following register variations are

possible:

mov ax, [bx][si]mov ax, [bx][di]mov ax, [bp][si]mov ax, [bp][di]

Page 30: Lecture 2 - Politechnika Śląskadb.zmitac.aei.polsl.pl/KT/Lecture2.pdf · 2003-10-17 · Lecture 2. Lecture 2 • 8086 family architecture. From 8086 to Pentium4. Registers, flags,

Base memory addressing with indexingand displacement

• Address of the data table is an constant.• Displacment of the structure is held in the base

register.• Number of element in the structure (byte) is held in the

index register.• All of the following variations are legal:

mov ax, table[bx][di]mov ax, table[di][bx]mov ax, table[bx+di]mov ax, [table+bx+di]mov ax, [bx][di]+table

Page 31: Lecture 2 - Politechnika Śląskadb.zmitac.aei.polsl.pl/KT/Lecture2.pdf · 2003-10-17 · Lecture 2. Lecture 2 • 8086 family architecture. From 8086 to Pentium4. Registers, flags,

Base memory addressing with indexingand displacement

SI

ALdatamov al, table[bx][si]

DS

+

+

structure addressBX

table addresstable +

Page 32: Lecture 2 - Politechnika Śląskadb.zmitac.aei.polsl.pl/KT/Lecture2.pdf · 2003-10-17 · Lecture 2. Lecture 2 • 8086 family architecture. From 8086 to Pentium4. Registers, flags,

Additional notes

• Calculated offset is called effective address.• Different addressing modes have different

timings.• More complicated modes take much time.• If effective address is greater than 0FFFFh the

carry bit is ignored.• Constant displacement is a 8-bit or 16-bit

signed value. Using 8-bit value is faster.

Page 33: Lecture 2 - Politechnika Śląskadb.zmitac.aei.polsl.pl/KT/Lecture2.pdf · 2003-10-17 · Lecture 2. Lecture 2 • 8086 family architecture. From 8086 to Pentium4. Registers, flags,

80386 register addressing

• The same as in 8086 with additional 32-bit registers.

• Only lower 16-bit half of 32-bit registers is accessible directly.

mov eax, ebxmov esi, edx

Page 34: Lecture 2 - Politechnika Śląskadb.zmitac.aei.polsl.pl/KT/Lecture2.pdf · 2003-10-17 · Lecture 2. Lecture 2 • 8086 family architecture. From 8086 to Pentium4. Registers, flags,

Indirect memory addressing

• Address of the data is held in the base and/orindex register. It is possible to add the constantdisplacement too.

• Almost any of the 32-bit registers can be baseor index register.

• ESP can be the base register only. • In 16-bit addressing mode the displacement

must not exceed 0FFFFh.• In this mode maximum segment size is 64kB.

Page 35: Lecture 2 - Politechnika Śląskadb.zmitac.aei.polsl.pl/KT/Lecture2.pdf · 2003-10-17 · Lecture 2. Lecture 2 • 8086 family architecture. From 8086 to Pentium4. Registers, flags,

Indirect memory addressing withscaling

• It is very useful mode for tables containingvalues greater than 1 byte.

• Index registers can be scaled by a factor of 1, 2, 4 or 8.

• If the scaling factor is 1 the base register is thefirst used in the instruction.

• If EBP is scaled it’s treated as index register and DS segment is used instead of SS.

Page 36: Lecture 2 - Politechnika Śląskadb.zmitac.aei.polsl.pl/KT/Lecture2.pdf · 2003-10-17 · Lecture 2. Lecture 2 • 8086 family architecture. From 8086 to Pentium4. Registers, flags,

Indirect memory addressing withscaling

mov ax, [ebx][ebp] ;DS bx-basemov ax, [ebp][ebx] ;SS bp-basemov ax, [ebp][ebx*2] ;SS bp-basemov ax, [ebp*2][ebx] ;DS bx-basemov ax, [ebp*2] ;DS no basemov ax, [ebp] ;SS no indexmov ax, es:[ebp][ebx*2] ;ES