40
Introduction to x86 Introduction to x86 Computer Architecture Computer Architecture

Introduction to x86 Computer Architecture. What is an x86 Processor An x86 Processor is now a general term used to refer to a class of processors that

Embed Size (px)

Citation preview

Page 1: Introduction to x86 Computer Architecture. What is an x86 Processor An x86 Processor is now a general term used to refer to a class of processors that

Introduction to x86Introduction to x86Introduction to x86Introduction to x86

Computer ArchitectureComputer Architecture

Page 2: Introduction to x86 Computer Architecture. What is an x86 Processor An x86 Processor is now a general term used to refer to a class of processors that

What is an x86 Processor

• An x86 Processor is now a general term used to refer to a class of processors that share a common subset of instructions

• Some x86 microprocessors are: Athlon (AMD), Opteron (AMD), Pentium IV (Intel), Xeon (Intel), Pentium Core Duo (Intel), Athlon X2 (AMD)

– Most popular desktop and sever processors– Knowing instructions for one, you can program

for any other processor• Some processors have additional instructions that

cannot be used on others!

Page 3: Introduction to x86 Computer Architecture. What is an x86 Processor An x86 Processor is now a general term used to refer to a class of processors that

x86 Philosophy on Instructions

• x86 Processors follow the Complex Instruction Set Computer (CISC) Philosophy

• It is converse to the Reduced Instruction Set Computer (RISC) Philosophy

– The data path we discussed in class falls under the category of RISC

– x86 processors provide a lot of (relatively) complex instructions

• When you look at the manuals the number of instructions will strike you.

• Each instruction has its own peculiarity to watch out for.

Page 4: Introduction to x86 Computer Architecture. What is an x86 Processor An x86 Processor is now a general term used to refer to a class of processors that

CISC vs. RISC

CISC RISCLarger number of instructions. Fewer number of instructions.

Instructions have different formats.

All instructions have the same format.

Instructions vary in size. (some instructions are just 1 byte while others are 9 bytes long!)

Instructions have the same size.

Data path is very complex and involves multiple clocks.

Data path is simpler and typically uses a single clock.

Code for software is smaller (example you can multiply or divide two registers)

Code for software is larger (Have to write code for multiplication or division!)

Puts burden on hardware Puts burden on software.

Page 5: Introduction to x86 Computer Architecture. What is an x86 Processor An x86 Processor is now a general term used to refer to a class of processors that

CISC vs. RISC (Contd.)

CISC RISCCan extract more Instruction Level Parallelism (ILP) – complex instructions are broken internally into smaller pieces and run in parallel.

Harder to extract ILP

Complex instructions enable better use of other parts of CPU such as caches.

Software developer may need to handle some gritty details.

Compiler development is harder.

Streamlines compiler development.

Athlon, Pentium, Opteron, Xeon.

ARM, MIPS, Ultra Sparc, Ultra Sparc T1

Page 6: Introduction to x86 Computer Architecture. What is an x86 Processor An x86 Processor is now a general term used to refer to a class of processors that

Microprogramming

• Microprogramming is a computer architecture design strategy in which complex instructions are actually implemented as a sequence of micro (or smaller) instructions.– Sequence of microinstructions is called a micro

program.• The are carefully designed and optimized for fastest

execution.• CPU is actually executing micro programs rather than

the main code and consequently has a RISC type computing core!

– CISC architectures often use microprogramming.

Page 7: Introduction to x86 Computer Architecture. What is an x86 Processor An x86 Processor is now a general term used to refer to a class of processors that

Micro program Example

• Example of a micro program to perform operation Add R1, R2 (R2 = R2 + R1)– Select register R1 as 1st Input to ALU– Select register R2 as 2nd Input to ALU– Set carry input of ALU to zero– Select ALU to perform 2’s complement addition– Direct result from ALU to register R2– Update flags as necessary

Page 8: Introduction to x86 Computer Architecture. What is an x86 Processor An x86 Processor is now a general term used to refer to a class of processors that

Micro program storage

• Micro programs need to be retrieved and processed at high speed!– They are stored directly on the CPU and are

hardwired to the data path.• The storage location is called a control store.

– Micro programs are typically just bits that are applied to inputs of various devices in the data path.

• Like selection bits for multiplexers and de-multiplexers

Page 9: Introduction to x86 Computer Architecture. What is an x86 Processor An x86 Processor is now a general term used to refer to a class of processors that

Micro instruction implementations

• Microprogramming maybe implemented using two different strategies– Vertical micro codes

• One micro instruction at a time just like a conventional program.

– Horizontal micro codes• Several or all micro instructions are processed

simultaneously.

Page 10: Introduction to x86 Computer Architecture. What is an x86 Processor An x86 Processor is now a general term used to refer to a class of processors that

Vertical Micro Codes

• This is older/simple approach.– In this strategy micro codes are executed one

after another in a serial fashion. This is type is similar to conventional program execution (one instruction at a time)

00110001Sequence of short micro codes to implement a macro instruction (vertical organization)

00000001

00001111

11110000

Page 11: Introduction to x86 Computer Architecture. What is an x86 Processor An x86 Processor is now a general term used to refer to a class of processors that

Horizontal Micro Codes

• This is a newer and faster approach• Adopted as transistors started becoming cheaper

– Micro codes are typically executed in parallel making micro instructions much faster!

– The drawback is that microinstructions become much wider as they have to accommodate all bits (even if they are not used) in each instructions.

• All instructions have microinstructions of the same size

00110001 00000001 00001111 11110000

Horizontal organization of micro codes to make a single microinstruction!

Page 12: Introduction to x86 Computer Architecture. What is an x86 Processor An x86 Processor is now a general term used to refer to a class of processors that

x86 Architecture

• Most x86 processors use little microprogramming.

• Most instructions are directly executed by a complex data path

• Some advanced instructions and looping instructions use microprogramming

– The x86 manual typically provides the microinstructions to explain the working of each instruction

– You really don’t have to worry about all the details when writing assembly language code for a given processor.

Page 13: Introduction to x86 Computer Architecture. What is an x86 Processor An x86 Processor is now a general term used to refer to a class of processors that

Assembly programming with x86

• In order to do assembly programming with x86 you need to know the following:– Registers provided by x86 processors– Instruction set

• Refer to x86 instruction set manuals available from Intel.

– http://www.intel.com/design/pentium4/manuals/253666.htm– http://www.intel.com/design/pentium4/manuals/253667.htm

– A text editor to type out your assembly program– An assembler & linker to convert assembly to

binary

Page 14: Introduction to x86 Computer Architecture. What is an x86 Processor An x86 Processor is now a general term used to refer to a class of processors that

x86 Registers

• x86 registers are grouped into two categories– General purpose registers

• Used for doing arithmetic and logic operations• Used like temporary variables in programs

– Reserved (or specialized) registers• These are used by the microprocessor internally for

performing certain operations• The values in these registers are typically not modified

by programs– They are set and handled by the operating system that loads

and runs your programs.

Page 15: Introduction to x86 Computer Architecture. What is an x86 Processor An x86 Processor is now a general term used to refer to a class of processors that

General purpose x86 Registers

AH AL

078151632

BH BL

CH CL

DH DL

• 4-basic 32-bit registers that can be reused as 16-bit or 8-bit registers

• 32-bit registers are called: eax, ebx, ecx, and edx• Low 16-bit parts of 32-bit registers are correspondingly called: ax,

bx, cx, and dx• 8-bits parts are called: ah, al, bh, bl, ch, cl, dh, & dl

Bit positions

Cannot use high 16-bits independently!

EAX

EBX

ECX

EDX

AX

BX

CX

DX

Names for low 16-bits

Page 16: Introduction to x86 Computer Architecture. What is an x86 Processor An x86 Processor is now a general term used to refer to a class of processors that

Using parts of registers

• Note that registers eax, ax, ah, and al all share the same register space!– They are names for parts of the same register!– Altering any one correspondingly alters other

registers as well!

• Example

EAX

Set EAX to 1

00 00 00 01

Change AH to 1

00 00 01 01

Now AH = 1, AL = 1 but EAX and AX = 257!

Page 17: Introduction to x86 Computer Architecture. What is an x86 Processor An x86 Processor is now a general term used to refer to a class of processors that

Other general purpose registers

• The x86 processor also provides other general purpose registers.– They cannot be accessed as bytes!

• Smallest unit is lower 16-bits

BP

078151632Bit positions

Cannot use high 16-bits independently!

EBP (Base Pointer)

ESI (Source Index)

EDI (Destination Index)

SI

DI

SP

Register Names for low 16-bits

ESP (Stack Pointer)

Page 18: Introduction to x86 Computer Architecture. What is an x86 Processor An x86 Processor is now a general term used to refer to a class of processors that

Segment Registers

• Segment registers (16-bits) are special registers– 4 Special segment registers

• CS – Code Segment register– Indicates an area of memory in which instructions are stored.

• DS – Data Segment register– Indicates an area of memory in which R/W data is stored.

• SS – Stack Segment register– Area of memory in which stack for a program is stored.

– Stack is used for calling methods/functions/subprograms

• ES – Extra Segment register– Used for copying data from one segment to another.

– They are typically set by the operating system– You will never change them (in this course)– We will discuss segment registers further in the course

Page 19: Introduction to x86 Computer Architecture. What is an x86 Processor An x86 Processor is now a general term used to refer to a class of processors that

EFLAGS Register

• EFLAGS register is a special (32-bit) register that contains flags (bits) generated from results of ALU operations.– Typically, the only way to change the values is to

perform an ALU operation.• Usually a set of individual flags (or bits) are selectively

changed by each instruction.• You have to know which instruction changes which bit!

– Initially it is hard but with some practice you will get the hang of it.

• Each bit can be indirectly inspected using suitable instruction

– Typically a conditional jump instruction!

Page 20: Introduction to x86 Computer Architecture. What is an x86 Processor An x86 Processor is now a general term used to refer to a class of processors that

Flags in EFLAGS

• Certain flags in EFLAGS are frequently used– ZF: Zero Flag

• Set if output from ALU is zero. Cleared otherwise.

– CF: Carry Flag• Set if arithmetic operation generates a carry or borrow. • This flag indicates overflow for unsigned arithmetic.

– PF: Parity Flag• Set if least significant byte (8-bits) of result from ALU

has an even number of 1s.

– SF: Sign Flag• Set to most significant bit of the result (1 indicates

negative result while 0 indicates positive result)

Page 21: Introduction to x86 Computer Architecture. What is an x86 Processor An x86 Processor is now a general term used to refer to a class of processors that

Instruction Pointer (IP)

• It is a special 32-bit register that indicates the address of the next instruction to be executed by the microprocessor.– The register is called EIP in x86 processors– Value is typically initialized by the OS when

programs are loaded into memory.– Changed by conditional or unconditional jumps– By function/method calls

Page 22: Introduction to x86 Computer Architecture. What is an x86 Processor An x86 Processor is now a general term used to refer to a class of processors that

Reading the Manual

• Now that you know about registers, it is now time to start exploring instructions supported by x86 processors.– For this task you have to refer to the Intel

manuals for details of instructions.– First you need to understand the notations used

• I will explain some of the notation using an example.• You have to learn other notations by reading the

manual.

Page 23: Introduction to x86 Computer Architecture. What is an x86 Processor An x86 Processor is now a general term used to refer to a class of processors that

The MOV instruction

• One of the most overloaded instruction in the x86 manual for assignment operation– Copy value from one location to another

• Location can be memory, register, or constant!

– Handles all possible combinations using different op-codes

• Mnemonic is the same but op-codes are different!

– Refer to page 635 of Instruction Set reference• http://download.intel.com/design/Pentium4/manuals/25

366620.pdf

Page 24: Introduction to x86 Computer Architecture. What is an x86 Processor An x86 Processor is now a general term used to refer to a class of processors that

Snippet from Manual

Opcode Instruction Description

88 /r Mov r/m8, r8 Mov r8 to r/m8.

89 /r Mov r/m16, r16 Mov r16 to r/m16.

89 /r Mov r/m32, r32 Mov r32 to r/m32.

C6 /0 Mov r/m8, imm8 Mov imm8 to r/m8.

C7 /0 Mov r/m16, imm16 Mov imm16 to r/m16.All you need to know is what do the notations (r/m32 or imm8) actually

imply.

Page 25: Introduction to x86 Computer Architecture. What is an x86 Processor An x86 Processor is now a general term used to refer to a class of processors that

Understanding notation

• Review section 3.1.1.2 (Page 51) of the instruction manual

• http://download.intel.com/design/Pentium4/manuals/25366620.pdf

– rN:A N-bit register (EAX etc. for 32-bit, AX etc. for 16-bit, AL/AH etc. for 8-bit)

– immN: An N-bit immediate or constant value (represented as unsigned or 2’s complement)

– mN: A N-bit memory address from where K-bytes are to be stored. Value of K depends on size of value being R/W

– r/mN (r/m8, r/m16, r/m32) : A N-bit register or N-bit memory address to read/write data to.

Page 26: Introduction to x86 Computer Architecture. What is an x86 Processor An x86 Processor is now a general term used to refer to a class of processors that

Manual to Assembly Conversion

• The assembler we are going to use uses a different notation– Instructions are written with destination registers

last– Register names are prefixed in % sign– Constant values are prefixed with $ sign

• Example: $31, $3.142• Hexadecimal constants are written as $0x7F

– Hexadecimal constants cannot use fractions like $0x7F.A

• Octal constants have leading 0 as $071• Binary constants have a ‘b’ at the end like $01110b

Page 27: Introduction to x86 Computer Architecture. What is an x86 Processor An x86 Processor is now a general term used to refer to a class of processors that

Example Translation

• Examples of various MOV instructions:

Manual Assembly Comments

Mov r/m8, r8 Movb %al, %ah

Movb k*, %bl

AH = AL

BL=k, Loads 1 byte from k

Mov r/m32, r32

Movl %ebx,%eax

Movl k, %eax

EAX = EBX

EAX=k, Loads 4 bytes!

Mov r/m32, imm32

Movl $10, %eax

Movl $-20, %ebx

EAX = 10 (4 bytes!)

EBX = -20 (4 bytes!)* where k is symbol (or variable) for a memory address.

Suffix Interpretation

b – byte w – word (2-bytes) l – int/long (4-bytes)

Page 28: Introduction to x86 Computer Architecture. What is an x86 Processor An x86 Processor is now a general term used to refer to a class of processors that

Variables

• In assembly symbols (or variables) are typically used to refer to addresses– Variables have a type associated with them which defines

the following attributes:• Size (in bytes): Most important in assembly

– Once defined you cannot change the size!

• Data type: Weak concept in assembly– You can always reinterpret values in different ways

– Valid types are: • byte (1-byte)• word (2-bytes)• int or long (4-bytes)• float (4-bytes)• string (array of bytes)

Page 29: Introduction to x86 Computer Architecture. What is an x86 Processor An x86 Processor is now a general term used to refer to a class of processors that

Defining Variables

• Variables are defined by defining a symbol– With a given type– And a default initial value.

var1: .int -32 /* Java: int var1 = -32; */var2: .byte 0 /* Java: byte var2 = 0; */var3: .float -3.142 /* Java: float var3 = -3.142; */

Comments are delimited by /* and */ character

sequences!

Page 30: Introduction to x86 Computer Architecture. What is an x86 Processor An x86 Processor is now a general term used to refer to a class of processors that

Putting it all together!

• The first almost complete assembly:

/* Program to swap values of variables var1 & var2 */

.text /* Every thing below goes in Code Segment */

.global _start /* Global entry point into program */

_start: /* Indicate where program starts */ movl val1, %eax /* eax = val1 */ movl val2, %ebx /* ebx = val2 */ movl %eax, val2 /* val2 = eax */ movl %ebx, val1 /* val1 = ebx */

.data /* Everything below goes in data segment */val1: .int 10val2: .int 20

Page 31: Introduction to x86 Computer Architecture. What is an x86 Processor An x86 Processor is now a general term used to refer to a class of processors that

Problem with earlier code

• The previous assembly is actually valid– It will compile, link, run do swapping of integers– However, it does not terminate!

• How to make the program terminate?• One way is to stop the microprocessor from

processing further instructions.

– Have to tell the OS to stop the program (or process) from running further

• Need to interact with OS for this task.

Page 32: Introduction to x86 Computer Architecture. What is an x86 Processor An x86 Processor is now a general term used to refer to a class of processors that

OS Interactions

• Example code to stop a process (or program) from running– Invoke an OS routine or “system call”

.text /* Every thing below goes in Code Segment */

.global _start /* Global entry point into program */

_start: /* Indicate where program starts */ /* Rest of the program actually goes here */ movl $1, %eax /* Set eax=1, SysCall Code for exit */ movl $0, %ebx /* Exit code value set to zero */ int $0x80 /* Transfer control to OS */

.data

Read as call Interrupt 80 hex! (Transfer control to OS). This is OS specific! Will

Not work on Windows!

Page 33: Introduction to x86 Computer Architecture. What is an x86 Processor An x86 Processor is now a general term used to refer to a class of processors that

The Final Assembly

/* Program to swap values of variables var1 & var2 */.text /* Every thing below goes in Code Segment */

.global _start /* Global entry point into program */

_start: /* Indicate where program starts */ movl val1, %eax /* eax = val1 */ movl val2, %ebx /* ebx = val2 */ movl %eax, val2 /* val2 = eax */ movl %ebx, val1 /* val1 = ebx */

movl $1, %eax /* Set eax=1, SysCall Code for exit */ movl $0, %ebx /* Exit code value set to zero */ int $0x80 /* Transfer control to OS */

.data /* Everything below goes in data segment */val1: .int 10val2: .int 20

Page 34: Introduction to x86 Computer Architecture. What is an x86 Processor An x86 Processor is now a general term used to refer to a class of processors that

More Instructions

• I will cover some basic, core instructions in class.– You are expected to review the instruction manuals to

obtain a comprehensive list of instructions!

Instruction Resultaddb $8, %AL AL += 8;

addl %ebx, %eax EAX += EBX

addl k, %eax EAX += k

addl %eax, k k += EAX

Page 35: Introduction to x86 Computer Architecture. What is an x86 Processor An x86 Processor is now a general term used to refer to a class of processors that

Add Instruction

• Add instruction performs integer addition– It is applicable for both signed & unsigned numbers– Sets OF, CF, and SF based on result of addition

Instruction Resultaddb $8, %AL AL += 8;

addl %ebx, %eax EAX += EBX

addl k, %eax EAX += k

addl %eax, k k += EAX

* Where k is an int variable

Page 36: Introduction to x86 Computer Architecture. What is an x86 Processor An x86 Processor is now a general term used to refer to a class of processors that

Sub Instruction

• Sub instruction performs integer subtraction– It is applicable for both signed & unsigned numbers– Sets OF, CF, and SF based on result of addition

Instruction Resultsubb $8, %AL AL -= 8;

subl %ebx, %eax EAX -= EBX

subl k, %eax EAX -= k

subl %eax, k k -= EAX

* Where k is an int variable

Page 37: Introduction to x86 Computer Architecture. What is an x86 Processor An x86 Processor is now a general term used to refer to a class of processors that

MUL Instruction• MUL Instruction comes in 2 flavors

• Note: Multiplying 2 n-bit numbers generates a 2n-bit result– Example: Multiplying 2 32-bit numbers generates a 64-bit result.

– MUL: Performs unsigned multiplication– IMUL: Performs signed integer multiplication– Source and destination registers are implied!

• Source: Uses AL, AX, or EAX depending on size of operand

• Destination: Uses DX:AX or EDX:EAX to store 32-bit or 64-bit result!

Instruction Resultmulb %BL AX = AL * BL

mull %ebx EDX:EAX = EAX * EBX

imull k EDX:EAX = EAX * K

* Where k is an int variable

Page 38: Introduction to x86 Computer Architecture. What is an x86 Processor An x86 Processor is now a general term used to refer to a class of processors that

MUL (Continued)

• OF and CF are set to 0 if upper half of the result is 0– Values in SF, ZF, PF are undefined at the end of

the instruction!

Operand Size Operand 1 (Implicit)

Operand 2 Destination

Byte AL r/m8 AX

Word AX r/m16 DX:AX

Int EAX r/m32 EDX:EAX

Page 39: Introduction to x86 Computer Architecture. What is an x86 Processor An x86 Processor is now a general term used to refer to a class of processors that

DIV Instruction• DIV Instruction comes in 2 flavors

– DIV: Performs unsigned division– IDIV: Performs signed integer division– Source and destination registers are implied!

• Source: Uses AL, AX, or DX:AX or EDX:EAX depending on size of divisor

• Destination: Uses AX or DX:AX or EDX:EAX to store remainder and quotient respectively

Instruction Result

divb %BL AL=(AX/BL); AH=AX%BL

divl %ebx EAX=(EDX:EAX / EBX);

EDX=(EDX:EAX % EBX)

divl k EAX=(EDX:EAX / k);

EDX=(EDX:EAX % k)

* Where k is an int variable

Page 40: Introduction to x86 Computer Architecture. What is an x86 Processor An x86 Processor is now a general term used to refer to a class of processors that

DIV (Continued)

• Values of CF, OF, SF, ZF, and PF are undefined!• Before performing idiv use cdq instruction to sign

extend eax value to edx. Otherwise your division will give incorrect results for negative numbers!

Operand Size Dividend Divisor Quotient Remainder

Word/Byte AX r/m8 AL AH

Word/word DX:AX r/m16 AX DX

Int/int EDX:EAX r/m32 EAX EDX