44
x86 Architecture & Its Assembly language programming Dr A Sahu Dept of Computer Science & Engineering IIT Guwahati

Lec05

Embed Size (px)

Citation preview

Page 1: Lec05

x86 Architecture & Its Assembly language programming

Dr A SahuDept of Computer Science & Engineering

IIT Guwahati

Page 2: Lec05

Outline• Review of 8086 Architecture

– Block diagram (Data Path)• Similarity with x86 (i386, Pentium, etc)

– Very IMP for interview/knowledge– Not part of Examination

• x86 Assembly language program – Memory model – Example programs– Data Segment– Loop and Nested Loop

• Next Class: Detail of assembly language – Summary of 8085/8086/i386 Arch & programming

Page 3: Lec05

Introduction to 8086 & i386 processor

• 16 bit Microprocessor• All internal registers as well as internal and

external data buses were 16 bits wide • 4 Main Register, 4 Index Register, 4 Segment

Register, Status Reg, Instr Ptr.• Not compatible with 8085, but with successors• Two Unit works in parallel:

– Bus Interface Unit (BIU)– Execution Unit (EI)

Page 4: Lec05

8086 Architecture

AH ALBH BLCH CLDH DL

SI (Source Idx )DI (Dest. Idx)BP (Base Ptr )SP (Stack Ptr)

Z (Flag Reg)

CS (Code Seg Reg)DS (Data Seg Reg )ES (Extra Seg Reg )SS (Stack Seg Reg)

IP (Intr Ptr)OperandInDirect

Temp ATemp B Temp C

Q6Q5Q4Q3Q2Q1

Sequencer

Bus Interface Unit

ExecutionUnit

SUM

C BUS

A BUS

ALU

Page 5: Lec05

8086 Architecture• Execution Unit :

– ALU may be loaded from three temp registers (TMPA, TMPB, TMPC) – Execute operations on bytes or 16-bit words. – The result stored into temp reg or registers connected to the

internal data bus. • Bus Interface Unit

– BIU is intended to compute the addresses.– Two temporary registers – indirect addressing– four segment registers (DS, CS, SS and ES), – Program counter (IP - Instruction Pointer), – A 6-byte Queue Buffer to store the pre-fetched opcodes and data.– This Prefetch Queue optimize the bus usage. – To execute a jump instruction the queue has to be flushed since the

pre-fetched instructions do not have to be executed.

Page 6: Lec05

History of Intel Architectures

• 1978: 8086 (16 bit architecture)• 1980: 8087

• Floating point coprocessor is added

• 1982: 80286 • Increases address space to 24 bits

• 1985: 80386: • 32 bits Add, • Virtual Mem & new add modes • Protected mode (OS support)

• 1989-95: 80486/Pentium/Pro• Added a few instructions of base MMX

Page 7: Lec05

History of Intel Architectures

• 1997: Pentium II • 57 new “MMX” instructions are added,

• 1999: Pentium III: • Out of Order, added another 70 Streaming SIMD Ext (SSE)

• 2001: Pentium 4• Net burst, another 144 instructions (SSE2)

• 2003: PI4 HT, Trace Cache• 2005: Centrino, low power• 2007: Core architecture, Duo • 2008: Atom, Quad core with HT….• 2009---:Multi core (Large chip multiprocessor)

Page 8: Lec05

Superscalar Pipeline

IF

Time in base Cycle 0 1 2 3 4 5 6 7 8 9

D IS WBEX

How Complex the HW will be

Page 9: Lec05

Fetch

UnitCache/

memoryMultiple instruction

Sequential stream of instructions

FU FU FU

Register file

Decode

& issue

Instruction/control

Data

FU Funtional Unit

ILP in Superscalar processors

Page 10: Lec05

Instruction 1

Instruction 2

Instruction 3

Instruction 4

Decoder EX Unit WB Unit

Pipeline

Pipe 1

Pipe 2

• Used in initial Pentium processor• Could execute up to 2 instructions simultaneously• Instructions sent through the pipeline in order - if the next

two instructions had a dependency issue, only one instruction (pipe) would be executed and the second execution unit (pipe) went unused for that clock cycle.

Intel P5 Architecture (Generation 5)

Page 11: Lec05

Intel P6 Architecture (Generation 6)

Instruction 1

Instruction 2

Instruction 3

Instruction 4

Pipeline

DecoderDecoderDecoder

WB Unit

Scheduler

EX Unit

Re OrderBuffer

Out of Order EU

• Used in the Pentium II, III and Pro processors

• 3 instruction decoders, which break each CISC instruction (macro-op) into equivalent micro-operations (µops) for the Out-of-Order Execution unit

• 10 stage instruction pipeline utilized in this architecture

Page 12: Lec05

Intel NetBurst MicroArchitecture

Instruction 1

Instruction 2

Instruction 3

Instruction 4

DecoderDecoderDecoder

Scheduler

EX Unit

Re OrderBuffer WB Unit

Decoded Instructions(Execution

Trace Cache)

• New architecture used for the Intel Pentium IV and Pentium Xeon processors

PipelineOut of Order EU

Page 13: Lec05

Tasks of superscalar processing

Parallel Parallel Preserving thedecoding instruction sequential and issue execution consistency of instruction execution and exception processing

Page 14: Lec05

Superscalar decode and issueI - cache

Instructionbuffer

Decode & Issue

IF D/I

I - cache

Instructionbuffer

Decode & Issue

IF D I

ScalarIssue

SuperscalarIssue

IF D IIF D I

Page 15: Lec05

Parallel Decoding• Fetch multiple instructions in instruction

buffer• Decode multiple instructions in parallel –

instruction window• Possibly check dependencies among these

as well as with the instructions already under execution

Page 16: Lec05

Dependent/Independent Instructions

• ADD T A B T= A+B• ADD W C D W= C+D• LD A, 0(W) A=M[W]• ST C, 0(B) M[B]=C Read After Write (RAW), W after W, W after R RAW (Ins2-Ins3): True dependency WAW, WAR (Ins1 ot Ins3) : false dependency

Page 17: Lec05

Issue vs Dispatch

Blocking Issue• Decode and issue to EU

Instructions may be blocked due to data dependency

Non-blocking Issue• Decode and issue to buffer• From buffer dispatch to EU

Instructions are not blocked due to data dependency

Page 18: Lec05

Blocking Issue

EU EU EU

Decode Check & Issue

InstructionBuffer

issue window

Page 19: Lec05

Non-blocking (shelved) Issue

Reservationstation

Dep. Checking/dispatch

EU

Reservationstation

Dep. Checking/dispatch

EU

Reservationstation

Dep. Checking/dispatch

EU

Decode & Issue

Instructionbuffer

Page 20: Lec05

Handling of Issue Blockages

Preserving issue order Alignment of instruction issue

aligned unalignedin-order out of order

Page 21: Lec05

Dependent/Independent Instructions

• ADD T A B T= A+B• ADD W C D W= C+D• LD A, 0(W) A=M[W]• ST C, 0(B) M[B]=C

Page 22: Lec05

Issue Order

cd abe

a

Issue windowInstructionsto be issued

Instructionsissued

cd abe

a

Issue windowInstructionsto be issued

Instructionsissued

Issue in strict program order Out of order Issue

c

Example: MC 88110, PowerPC 601

Independent instruction

Dependent instruction

Issued instruction

Page 23: Lec05

Alignment

cd abe

a

fixed windowcheckedin cycle 1

Aligned Issue Unaligned Issue

issuedin cycle 1

fgh

next window

cd be

b

checkedin cycle 2

issuedin cycle 2

fgh

de

d

checkedin cycle 3

issuedin cycle 3

fgh

c

cd abe

a

gliding window

fgh

cd be

b

fgh

defgh

c

def

Page 24: Lec05

Design choices in instruction issue

Coping with Coping with Use of Handling of Issuefalse data unresolved shelving issue blockages ratedependencies control (2-6) dependencies

no Register renaming wait speculative

blocking shelved

Page 25: Lec05

Layout of Shelving Buffers

Type of the Number of Number of readshelving buffers shelving buffer entries and write ports

Stand combined withalone renaming and(RS) reordering

individual 2-4group 6-16central 20total 15-40

depends onno. of EUsconnected

Page 26: Lec05

Reservation Stations (RS)

EU EU EU EU EU EU EU EU

RS RS RS RS RS

Individual RSs Group RSs Central RS

Page 27: Lec05

Issue bound operand fetch(with single register file)

EU EU

RS RS

EU EU

RS RS

Decode/issue

RF

instructiondata

Page 28: Lec05

Dispatch bound operand fetch (with single register file)

EU EU

RS RS

EU EU

RS RS

Decode/issue instructiondata

RF

Page 29: Lec05

Why Renaming and Reordering?• Register Renaming

– Removes false dependencies (WAR and WAW) • Reordering Buffer (ROB) : Pentitum Out of

order instruction processing– Ensures sequential consistency of interrupts

(precise vs imprecise interrupts)– Facilitates speculative execution

• Branch execution • Execute both path and discard after getting CC

Value

Page 30: Lec05

RAW, WAR and WAW(in Superscalar)

IF IS DP EX WB

IF IS DP EX WB

IF IS DP EX WB

write

read

write

RAW

WARWAW

Page 31: Lec05

Register renaming

write R5 RAWread R5 WARwrite R5 RAWread R5

write R5 RAWread R5

write R8 RAWread R8

Page 32: Lec05

Who does renaming?

• Compiler– Done statically– Limited by registers visible to compiler

• Hardware– Done dynamically– Limited by registers available to hardware

Page 33: Lec05

X86 Assembly language program• Kernel code writing

– Process switching code– Thread synchronization code– Lock, barrier (test & set, fetch & increment, xcng)– pthread spin_lock (in ASM) is 28% faster than intel

tbb::spin_mutex (in C)• Time critical coding (Coding for DSP)

– /usr/include/asm-i386• Use of MASM/TASM/GCC/NASM compiler

– gcc –S test.c –o test.s• C/C++ code with asm block• 8086 compatible with ii386/pentium

Page 34: Lec05

8086 Registers• AX - the accumulator register (divided into AH / AL) • BX - the base address register (divided into BH / BL) • CX - the count register (divided into CH / CL) • DX - the data register (divided into DH / DL)

• SI - source index register. • DI - destination index register. • BP - base pointer. • SP - stack pointer.

AH ALBH BLCH CLDH DLSI (Source Idx )DI (Dest. Idx)BP (Base Ptr )SP (Stack Ptr)

CS (Code Seg Reg)DS (Data Seg Reg )ES (Extra Seg Reg )SS (Stack Seg Reg)

IP (Intr Ptr)

Z (Flag Reg)

Page 35: Lec05

i386/i486/i686 Registers

AH ALBH BLCH CLDH DLSI (Source Idx )DI (Dest. Idx)BP (Base Ptr )SP (Stack Ptr)

CS (Code Seg Reg)DS (Data Seg Reg )ES (Extra Seg Reg )SS (Stack Seg Reg)

IP (Intr Ptr)

Z (Flag Reg)

EAXEBXECXEDXESIEDIEBPESP

ECSEDSEESESS

EIP

EZ

31 15 7 0

Extended

Page 36: Lec05

Memory layout of C program

• Stack– automatic (default), local– Initialized/uninitialized

• Data– Global, static, extern– BSS: Block Started by Symbol– BBS: Uninitialized Data Seg.

• Code– program instructions

• Heap– malloc, calloc

Data

Code

Heap

Stack

BSS

Page 37: Lec05

Memory layout of C program

int A;int B=10;main(){ int Alocal; int *p; p=(int*)malloc(10);}

Data

Code

Heap

Stack

BSS

Page 38: Lec05

MASM : Hello world .model small .stack 100h ; reserve 256 bytes of stack space.data message db "Hello world, I'm learning Assembly$”.codemain proc mov ax, seg message

mov ds, axmov ah, 09 // 9 in the AH reg indicates Procedure

//should write a bit-string to the screen. lea dx, message // Load Eff Addressint 21hmov ax,4c00h // Halt for DOS routine (Exit Program)int 21hmain endp

end main

Page 39: Lec05

Memory Model: Segment Definition

• .model small– Most widely used memory model.– The code must fit in 64k.– The data must fit in 64k.

• .model medium– The code can exceed 64k.– The data must fit in 64k.

• .model compact– The code must fit in 64k.– The data can exceed 64k.

• .medium and .compact are opposites.

Page 40: Lec05

Data Allocation Directives• db : define byte dw: def. word (2 bytes) • dd: def double word (4) dq : def quad word (8) • equ : equate assign numeric expr to a name

.data db A 100 dup (?) ; define 100 bytes, with no initial

values for bytes

db “Hello” ; define 5 bytes, ASCII equivalent of “Hello”.

dd PtrArray 4 dup (?) ;array[0..3] of dword

maxint equ 32767 ; define maxint=32767

count equ 10 * 20 ; calculate a value (200)

Page 41: Lec05

MASM: Loop• Assemby code: Loop

– Loop simply decreases CX and checks if CX != 0, if so, a Jump to the specified memory location

– LOOPNZ : LOOPs when the zero flag is not set

MOV CX,100_LABEL: INC AX

LOOP _LABEL

MOV CX,10_CMPLOOP: DEC AX

CMP AX,3LOOPNE CMPLOOP

Page 42: Lec05

MASM: Nested Loop• Assemby code: Nested Loop: One CX register

mov cx, 8Loop1: push cx mov cx, 4Loop2: stmts loop Loop2 pop cx stmts loop Loop1

Page 43: Lec05

Next Class Agenda• Detail of Assembly language program • Addressing mode• Example program • Assignment problem • Summary of 8085/8085/i386 Arch &

programming (1st Unit of Syllabus)• Introduction to device interfacing

– Device type – Interfacing

Page 44: Lec05

Thanks