lecture 4 - politechnika Śląskadb.zmitac.aei.polsl.pl/kt/lecture4.pdf · lecture 4 •...

47
Assembler Programming Lecture 4

Upload: others

Post on 22-Aug-2020

2 views

Category:

Documents


0 download

TRANSCRIPT

Page 1: Lecture 4 - Politechnika Śląskadb.zmitac.aei.polsl.pl/KT/Lecture4.pdf · Lecture 4 • Instructions. General purpose instructions. All instructions • General purpose. • x87

Assembler Programming

Lecture 4

Page 2: Lecture 4 - Politechnika Śląskadb.zmitac.aei.polsl.pl/KT/Lecture4.pdf · Lecture 4 • Instructions. General purpose instructions. All instructions • General purpose. • x87

Lecture 4

• Instructions. General purpose instructions.

Page 3: Lecture 4 - Politechnika Śląskadb.zmitac.aei.polsl.pl/KT/Lecture4.pdf · Lecture 4 • Instructions. General purpose instructions. All instructions • General purpose. • x87

All instructions

• General purpose.• x87 FPU.• x87 FPU and SIMD state management.• Intel MMX technology.• SSE extensions.• SSE2 extensions.• System.

Page 4: Lecture 4 - Politechnika Śląskadb.zmitac.aei.polsl.pl/KT/Lecture4.pdf · Lecture 4 • Instructions. General purpose instructions. All instructions • General purpose. • x87

General purpose instructions• Data Transfer,• Binary Arithmetic,• Decimal Arithmetic,• Logical,• Shift and rotate,• Bit and Byte,• Control transfer,• String,• Flag control,• Segment register,• Miscellanous.

Page 5: Lecture 4 - Politechnika Śląskadb.zmitac.aei.polsl.pl/KT/Lecture4.pdf · Lecture 4 • Instructions. General purpose instructions. All instructions • General purpose. • x87

Condition codes

Not equalNot zero

ZF=0…NE…NZ

EqualZero

ZF=1…E…Z

Below or equalNot above

(CF or ZF)=1…BE…NA

CarryCF=1…C

BelowNot above or equal

CF=1…B…NAE

Not carryCF=0…NC

Above or equalNot below

CF=0…AE…NB

AboveNot below or equal

(CF or ZF)=0…A…NBE

Page 6: Lecture 4 - Politechnika Śląskadb.zmitac.aei.polsl.pl/KT/Lecture4.pdf · Lecture 4 • Instructions. General purpose instructions. All instructions • General purpose. • x87

Condition codes

Not sign (non-negative)SF=0…NS

Sign (negative)SF=1…S

Not overflowOF=0…NO

OverflowOF=1…O

Less or equalNot greater

((SF xor OF) or ZF)=1…LE…NG

LessNot greater or equal

(SF xor OF)=1…L…NGE

Greater or equalNot less

(SF xor OF)=0…GE…NL

Not parityParity odd

PF=0…NP…PO

ParityParity even

PF=1…P…PE

Page 7: Lecture 4 - Politechnika Śląskadb.zmitac.aei.polsl.pl/KT/Lecture4.pdf · Lecture 4 • Instructions. General purpose instructions. All instructions • General purpose. • x87

Data transfer - move• MOV - moves the value in the source operand to the

destination operand.– MOV reg,reg– MOV mem,reg– MOV reg,mem– MOV mem,immed– MOV reg,immed– MOV mem,accum– MOV accum,mem– MOV segreg,reg16– MOV segreg,mem16– MOV reg16,segreg– MOV mem16,segreg

Page 8: Lecture 4 - Politechnika Śląskadb.zmitac.aei.polsl.pl/KT/Lecture4.pdf · Lecture 4 • Instructions. General purpose instructions. All instructions • General purpose. • x87

Data transfer – conditional move• CMOVcc – conditionally moves the value

in the source operand to the destination operand if the condition cc is satisfied

• Useful for optimizing small IF constructions

• Help eliminate branch mispredictions• P6, Pentium 4, Xeon

Page 9: Lecture 4 - Politechnika Śląskadb.zmitac.aei.polsl.pl/KT/Lecture4.pdf · Lecture 4 • Instructions. General purpose instructions. All instructions • General purpose. • x87

Data transfer - exchange• Swapping contents of operands.• XCHG – swaps the contents of two operands.• BSWAP – reverses the order in a 32-bit register

– bits 0-7 are exchanged with bits 24-31,– bits 8-15 are exchanged with bits 16-23.

• XADD - swaps and adds two operands.• CMPXCHG - compares and exchanges

operands.• CMPXCHG8B - compares and exchanges

8bytes.

Page 10: Lecture 4 - Politechnika Śląskadb.zmitac.aei.polsl.pl/KT/Lecture4.pdf · Lecture 4 • Instructions. General purpose instructions. All instructions • General purpose. • x87

Data transfer - stack

• PUSH – pushes word or doubleword on thestack.

• POP – pops word or doubleword from thestack.

• PUSHA – pushes all eight general-purposeregisters on the stack.

• POPA – pops all eight general-purposeregisters from the stack.

Page 11: Lecture 4 - Politechnika Śląskadb.zmitac.aei.polsl.pl/KT/Lecture4.pdf · Lecture 4 • Instructions. General purpose instructions. All instructions • General purpose. • x87

Data transfer – stack PUSH

ESP

Stack

Adresses growth

Stack growth

Operand

Stack

Page 12: Lecture 4 - Politechnika Śląskadb.zmitac.aei.polsl.pl/KT/Lecture4.pdf · Lecture 4 • Instructions. General purpose instructions. All instructions • General purpose. • x87

Data transfer – stack POP

ESP

Stack

Adresses growth

Stack growth

Operand

Stack

Page 13: Lecture 4 - Politechnika Śląskadb.zmitac.aei.polsl.pl/KT/Lecture4.pdf · Lecture 4 • Instructions. General purpose instructions. All instructions • General purpose. • x87

Data transfer – PUSHA, POPA

EBX

EBPESI

old ESP

EAXECXEDX

EDIESP after PUSHA

ESP before PUSHA ESP after POPA

ESP before POPA

Page 14: Lecture 4 - Politechnika Śląskadb.zmitac.aei.polsl.pl/KT/Lecture4.pdf · Lecture 4 • Instructions. General purpose instructions. All instructions • General purpose. • x87

Data transfer – type conversion

• CBW – convert byte to word.• CWDE – convert word to doubleword extended.• CWD – convert word to doubleword.• CDQ – convert doubleword to quadword.• MOVSX – moves data with sign extension.• MOVZX – moves data with zero extension.

Page 15: Lecture 4 - Politechnika Śląskadb.zmitac.aei.polsl.pl/KT/Lecture4.pdf · Lecture 4 • Instructions. General purpose instructions. All instructions • General purpose. • x87

Binary arithmetic• Addition and substraction instructions:

– ADD – sum of two integer operands,– ADC – sum of two integer operands, plus CF flag,– SUB – difference of two integer operands,– SBB – difference of two integer operands, minus CF

flag.• Increment and decrement instructions:

– INC – add 1 to an unsigned integer operand,– DEC – subtract 1 from an unsigned integer

operand.

Page 16: Lecture 4 - Politechnika Śląskadb.zmitac.aei.polsl.pl/KT/Lecture4.pdf · Lecture 4 • Instructions. General purpose instructions. All instructions • General purpose. • x87

Binary arithmetic• Comparison and sign change instructions.

– CMP – difference between two integer operands,– NEG – change the sign of a two’s complement

operand.• Multiplication and divide instructions:

– MUL – multiplies two unsigned integer operands,– IMUL – multiplies two signed integer operands,– DIV – divides unsigned operands,– IDIV – divides signed operands.

Page 17: Lecture 4 - Politechnika Śląskadb.zmitac.aei.polsl.pl/KT/Lecture4.pdf · Lecture 4 • Instructions. General purpose instructions. All instructions • General purpose. • x87

Binary arithmetic example

; Adding 32-bit digits with 16-bit registers

mov AX,var1_LW ; Take low word of var1add AX,var2_LW ; Add low word of var2mov sum_LW,AX ; Store low word of sum

mov AX,var1_HW ; Take high word of var1adc AX,var2_HW ; Add var2 with carrymov sum_HW,AX ; Store high word of sum

; Adding 32-bit digits with 16-bit registers

mov AX,var1_LW ; Take low word of var1add AX,var2_LW ; Add low word of var2mov sum_LW,AX ; Store low word of sum

mov AX,var1_HW ; Take high word of var1adc AX,var2_HW ; Add var2 with carrymov sum_HW,AX ; Store high word of sum

Page 18: Lecture 4 - Politechnika Śląskadb.zmitac.aei.polsl.pl/KT/Lecture4.pdf · Lecture 4 • Instructions. General purpose instructions. All instructions • General purpose. • x87

Binary arithmetic example

; Substracting 32-bit digits with 16-bit registers

mov AX,var1_LW ; Take low word of var1sub AX,var2_LW ; Substract low word of var2mov sub_LW,AX ; Store low word of result

mov AX,var1_HW ; Take high word of var1sbb AX,var2_HW ; Substr. var2 with carrymov sub_HW,AX ; Store high word of result

; Substracting 32-bit digits with 16-bit registers

mov AX,var1_LW ; Take low word of var1sub AX,var2_LW ; Substract low word of var2mov sub_LW,AX ; Store low word of result

mov AX,var1_HW ; Take high word of var1sbb AX,var2_HW ; Substr. var2 with carrymov sub_HW,AX ; Store high word of result

Page 19: Lecture 4 - Politechnika Śląskadb.zmitac.aei.polsl.pl/KT/Lecture4.pdf · Lecture 4 • Instructions. General purpose instructions. All instructions • General purpose. • x87

Binary arithmetic example

; Multiplying 16-bit digit with 16-bit digit

mov AX,var1 ; Take first operandmov BX,var2 ; Take second operandmul BX ; Multilply AX with BX

mov result_LW,AX ; Store low word of productmov result_HW,DX ; Store high word of prod.

; Multiplying 16-bit digit with 16-bit digit

mov AX,var1 ; Take first operandmov BX,var2 ; Take second operandmul BX ; Multilply AX with BX

mov result_LW,AX ; Store low word of productmov result_HW,DX ; Store high word of prod.

Page 20: Lecture 4 - Politechnika Śląskadb.zmitac.aei.polsl.pl/KT/Lecture4.pdf · Lecture 4 • Instructions. General purpose instructions. All instructions • General purpose. • x87

Binary arithmetic example; Dividing 16-bit digit with 8-bit digitmov AX,var1 ; Take dividientdiv BYTE PTR var2 ; Divide AX with var2 (byte)mov result,AL ; Store the 8-bit quotientmov rest,AH ; Store the 8-bit remainder

; Dividing 32-bit digit with 16-bit digitmov AX,var1_LW ; Take low word of dividentmov DX,var1_HW ; Take high worddiv WORD PTR var2 ; Divide DX.AX with divisormov result,AX ; Store the 16-bit quotientmov rest,DX ; Store the 16-bit remainder

; Dividing 16-bit digit with 8-bit digitmov AX,var1 ; Take dividientdiv BYTE PTR var2 ; Divide AX with var2 (byte)mov result,AL ; Store the 8-bit quotientmov rest,AH ; Store the 8-bit remainder

; Dividing 32-bit digit with 16-bit digitmov AX,var1_LW ; Take low word of dividentmov DX,var1_HW ; Take high worddiv WORD PTR var2 ; Divide DX.AX with divisormov result,AX ; Store the 16-bit quotientmov rest,DX ; Store the 16-bit remainder

Page 21: Lecture 4 - Politechnika Śląskadb.zmitac.aei.polsl.pl/KT/Lecture4.pdf · Lecture 4 • Instructions. General purpose instructions. All instructions • General purpose. • x87

Decimal arithmetic

• To adjust the results of a previous binaryarithmetic operation to produce a valid BCD result.

• To adjust the operands of a subsequent binaryarithmetic operation so that the operation will produce a valid BCD result.

• Operate only on both packed and unpackedBCD values.

Page 22: Lecture 4 - Politechnika Śląskadb.zmitac.aei.polsl.pl/KT/Lecture4.pdf · Lecture 4 • Instructions. General purpose instructions. All instructions • General purpose. • x87

Decimal arithmetic

• Packed BCD:– DAA – Decimal Adjust after Addition,– DAS – Decimal Adjust after Substraction.

• Unpacked BCD:– AAA – ASCII Adjust after Addition,– AAS – ASCII Adjust after Substraction,– AAM – ASCII Adjust after Multiplication,– AAD – ASCII Adjust before Division.

Page 23: Lecture 4 - Politechnika Śląskadb.zmitac.aei.polsl.pl/KT/Lecture4.pdf · Lecture 4 • Instructions. General purpose instructions. All instructions • General purpose. • x87

Decimal arithmetic example

; Substracting two packed BCD digits

mov AL,var1 ; Take first digit (24h)sub AL,var2 ; Substract second digit (07h)das ; Adjustment (1Dh)mov substr,AL ; Store the result (17h)

; Adding two packed BCD digits

mov AL,var1 ; Take first digit (47h)add AL,var2 ; Add second digit (27h)daa ; Adjustment (6Eh)mov sum,AL ; Store the result (74h)

; Substracting two packed BCD digits

mov AL,var1 ; Take first digit (24h)sub AL,var2 ; Substract second digit (07h)das ; Adjustment (1Dh)mov substr,AL ; Store the result (17h)

; Adding two packed BCD digits

mov AL,var1 ; Take first digit (47h)add AL,var2 ; Add second digit (27h)daa ; Adjustment (6Eh)mov sum,AL ; Store the result (74h)

Page 24: Lecture 4 - Politechnika Śląskadb.zmitac.aei.polsl.pl/KT/Lecture4.pdf · Lecture 4 • Instructions. General purpose instructions. All instructions • General purpose. • x87

Logical

• AND – Perform bitwise logical AND.• OR – Perform bitwise logical OR.• XOR – Perform bitwise logical exclusive OR.• NOT – Perform bitwise logical NOT.

Page 25: Lecture 4 - Politechnika Śląskadb.zmitac.aei.polsl.pl/KT/Lecture4.pdf · Lecture 4 • Instructions. General purpose instructions. All instructions • General purpose. • x87

Shift and rotate• SAL/SHL Shift arithmetic left/Shift logical left.• SAR Shift arithmetic right.• SHR Shift logical right.• SHRD Shift right double.• SHLD Shift left double.• ROR Rotate right.• ROL Rotate left.• RCR Rotate through carry right.• RCL Rotate through carry left.

Page 26: Lecture 4 - Politechnika Śląskadb.zmitac.aei.polsl.pl/KT/Lecture4.pdf · Lecture 4 • Instructions. General purpose instructions. All instructions • General purpose. • x87

Shift

CF

SAL / SHL

SHR

SAR

0

CF0

S CFSign

Page 27: Lecture 4 - Politechnika Śląskadb.zmitac.aei.polsl.pl/KT/Lecture4.pdf · Lecture 4 • Instructions. General purpose instructions. All instructions • General purpose. • x87

Double shift

CF

SHLD

SHRD

S CF

Source operand

Source operand

Destination operand

Destination operand

Page 28: Lecture 4 - Politechnika Śląskadb.zmitac.aei.polsl.pl/KT/Lecture4.pdf · Lecture 4 • Instructions. General purpose instructions. All instructions • General purpose. • x87

Rotate

CF

ROL

RORCF

CF

RCL

RCRCF

Page 29: Lecture 4 - Politechnika Śląskadb.zmitac.aei.polsl.pl/KT/Lecture4.pdf · Lecture 4 • Instructions. General purpose instructions. All instructions • General purpose. • x87

Bit instructions

• Bit test and modify:– BT – Bit Test,– BTS – Bit Test and Set,– BTR – Bit Test and Reset,– BTC – Bit Test and Complement.

• Bit scan:– BSF – Bit Scan Forward,– BSR – Bit Scan Reverse.

Page 30: Lecture 4 - Politechnika Śląskadb.zmitac.aei.polsl.pl/KT/Lecture4.pdf · Lecture 4 • Instructions. General purpose instructions. All instructions • General purpose. • x87

Byte instructions

• Byte set on condition:– SETcc – Set byte on condition cc,– conditions are the same as in CMOVcc instruction.

• Test:– TEST – Logical and of two operands,– does not alter operands.

Page 31: Lecture 4 - Politechnika Śląskadb.zmitac.aei.polsl.pl/KT/Lecture4.pdf · Lecture 4 • Instructions. General purpose instructions. All instructions • General purpose. • x87

Control transfer

• Conditional transfer.• Unconditional transfer.• Near transfer – within segment.• Far transfer – different segment.• Relative address.• Absolute address.

Page 32: Lecture 4 - Politechnika Śląskadb.zmitac.aei.polsl.pl/KT/Lecture4.pdf · Lecture 4 • Instructions. General purpose instructions. All instructions • General purpose. • x87

Unconditional transfer

• JMP – jump to a destination address.• CALL – jump to a procedure with pushing the

return address.• RET – return from a procedure.• IRET – return from an interrupt-handling

procedure.

Page 33: Lecture 4 - Politechnika Śląskadb.zmitac.aei.polsl.pl/KT/Lecture4.pdf · Lecture 4 • Instructions. General purpose instructions. All instructions • General purpose. • x87

Procedure calling

Procedure

Program

Call

Ret

Stack

Returnaddress

Page 34: Lecture 4 - Politechnika Śląskadb.zmitac.aei.polsl.pl/KT/Lecture4.pdf · Lecture 4 • Instructions. General purpose instructions. All instructions • General purpose. • x87

Interrupt handling

Handler

Program

Iret

Stack

Returnaddress

Interrupt

EFLAGS

Page 35: Lecture 4 - Politechnika Śląskadb.zmitac.aei.polsl.pl/KT/Lecture4.pdf · Lecture 4 • Instructions. General purpose instructions. All instructions • General purpose. • x87

Conditional transfer• Jcc – conditional jump to a destination address

– conditions are the same as in CMOV.• Loop instructions:

– LOOP– LOOPE– LOOPZ– LOOPNE– LOOPNZ

• JECXZ – jump if ECX is zero.• JCXZ – jump if CX is zero.

Page 36: Lecture 4 - Politechnika Śląskadb.zmitac.aei.polsl.pl/KT/Lecture4.pdf · Lecture 4 • Instructions. General purpose instructions. All instructions • General purpose. • x87

Software interrupts• INT n – software interrupt of given number.• INTO – software overflow exception.• BOUND – raises the bound range exceeded

exception when tested value is over or underthe bounds.

Page 37: Lecture 4 - Politechnika Śląskadb.zmitac.aei.polsl.pl/KT/Lecture4.pdf · Lecture 4 • Instructions. General purpose instructions. All instructions • General purpose. • x87

String instructions• Operate on elements of the string.• Operate on bytes, words, doublewords.• Source string is identified with ESI.• Destination string is identified with EDI.• Default segment for ESI is identified with DS.• Segment for EDI is identified with ES.

Page 38: Lecture 4 - Politechnika Śląskadb.zmitac.aei.polsl.pl/KT/Lecture4.pdf · Lecture 4 • Instructions. General purpose instructions. All instructions • General purpose. • x87

String instructions• MOVS – move string:

– MOVSB – move byte,– MOVSW – move word,– MOVSD – move doubleword.

• CMPS – compare string:– CMPSB,CMPSW,CMPSD.

• SCAS – substract string from accumulator:– SCASB, SCASW, SCASD.

• LODS – load string into the accumulator:– LODSB, LODSW, LODSD

• STOS – store string from the accumulator:– STOSB, STOSW, STOSD

Page 39: Lecture 4 - Politechnika Śląskadb.zmitac.aei.polsl.pl/KT/Lecture4.pdf · Lecture 4 • Instructions. General purpose instructions. All instructions • General purpose. • x87

Repeated string instructions• DF – direction flag,

– STD – set direction flag – increment ESI and EDI,– CLD – clear direction flag – decrement.

• REP – repeat while the ECX register not zero.• REPE / REPZ – repeat while the ECX register

not zero and the ZF flag is set.• REPNE / REPNZ – repeat while the ECX

register not zero and the ZF flag is clear.

Page 40: Lecture 4 - Politechnika Śląskadb.zmitac.aei.polsl.pl/KT/Lecture4.pdf · Lecture 4 • Instructions. General purpose instructions. All instructions • General purpose. • x87

I/O instructions

• IN – input from port to accumulator.• OUT – output from accumulator to port.• INS – input from port to string:

– INSB, INSW, INSD.• OUTS – output string to port:

– OUTSB, OUTSW, OUTSD.

Page 41: Lecture 4 - Politechnika Śląskadb.zmitac.aei.polsl.pl/KT/Lecture4.pdf · Lecture 4 • Instructions. General purpose instructions. All instructions • General purpose. • x87

EFLAGS bit instructions

• STC – set carry flag – CF=1.• CLC – clear carry flag – CF=0.• CMC – complement carry flag – CF=not CF.• STD – set direction flag – DF=1.• CLD – clear direction flag – DF=0.• STI – set interrupt flag – IF=1.• CLI – clear interrupt flag – IF=0.

Page 42: Lecture 4 - Politechnika Śląskadb.zmitac.aei.polsl.pl/KT/Lecture4.pdf · Lecture 4 • Instructions. General purpose instructions. All instructions • General purpose. • x87

EFLAGS byte instructions

• LAHF – load 8-bit flag register to accumulator.• SAHF – save 8-bit flag register.• PUSHF – push FLAGS register.• POPF – pop FLAGS register.• PUSHFD – push EFLAGS register.• POPFD – pop EFLAGS register.

Page 43: Lecture 4 - Politechnika Śląskadb.zmitac.aei.polsl.pl/KT/Lecture4.pdf · Lecture 4 • Instructions. General purpose instructions. All instructions • General purpose. • x87

Segment register instructions

• LDS – load far pointer using DS.• LES – load far pointer using ES.• LFS – load far pointer using FS.• LGS – load far pointer using GS.• LSS – load far pointer using SS.

Page 44: Lecture 4 - Politechnika Śląskadb.zmitac.aei.polsl.pl/KT/Lecture4.pdf · Lecture 4 • Instructions. General purpose instructions. All instructions • General purpose. • x87

Loading far pointers

; Load far pointer to DS:BX; Variable Far_point holds the 32-bit address

lds BX,Far_point

; Instruction above is equal to:

mov AX,WORD PTR Far_point+2 ; Take HWmov DS,AX ; Store HW in DSmov BX,WORD PTR Far_point ; Store LW in BX

; Load far pointer to DS:BX; Variable Far_point holds the 32-bit address

lds BX,Far_point

; Instruction above is equal to:

mov AX,WORD PTR Far_point+2 ; Take HWmov DS,AX ; Store HW in DSmov BX,WORD PTR Far_point ; Store LW in BX

Page 45: Lecture 4 - Politechnika Śląskadb.zmitac.aei.polsl.pl/KT/Lecture4.pdf · Lecture 4 • Instructions. General purpose instructions. All instructions • General purpose. • x87

Miscellanous instructions

• LEA – load effective address.• XLAT, XLATB – table lookup.• CPUID – processor identification.• NOP – no operation.• UD2 – undefined instruction.

Page 46: Lecture 4 - Politechnika Śląskadb.zmitac.aei.polsl.pl/KT/Lecture4.pdf · Lecture 4 • Instructions. General purpose instructions. All instructions • General purpose. • x87

Loading effective address

; Load effective address to BX; Table is the beginning of the table in thememory

lea BX,Table[SI+BX]

; Now we can use BX only to make program run faster:

hoop:mov AX,[BX] ; Take value from tableinc BX ; Next element in the tablecmp AX,0 ; Check if element is 0jne hoop ; Jump to „hoop” if AX isn’t 0

; Load effective address to BX; Table is the beginning of the table in thememory

lea BX,Table[SI+BX]

; Now we can use BX only to make program run faster:

hoop:mov AX,[BX] ; Take value from tableinc BX ; Next element in the tablecmp AX,0 ; Check if element is 0jne hoop ; Jump to „hoop” if AX isn’t 0

Page 47: Lecture 4 - Politechnika Śląskadb.zmitac.aei.polsl.pl/KT/Lecture4.pdf · Lecture 4 • Instructions. General purpose instructions. All instructions • General purpose. • x87

Identifying the processor

mov EAX,0 ; Load input value to EAXcpuid ; Take information about CPUmov EAX,0 ; Load input value to EAXcpuid ; Take information about CPU

ECXEDXEBXEAX6c65746e49656e69756e65470 … 2

uneG Ieni letn

Genu ineI ntel

GenuineIntel