Lecture 4 - Politechnika Śląskadb.zmitac.aei.polsl.pl/KT/Lecture4.pdf · Lecture 4 •...
Transcript of Lecture 4 - Politechnika Śląskadb.zmitac.aei.polsl.pl/KT/Lecture4.pdf · Lecture 4 •...
Assembler Programming
Lecture 4
Lecture 4
• Instructions. General purpose instructions.
All instructions
• General purpose.• x87 FPU.• x87 FPU and SIMD state management.• Intel MMX technology.• SSE extensions.• SSE2 extensions.• System.
General purpose instructions• Data Transfer,• Binary Arithmetic,• Decimal Arithmetic,• Logical,• Shift and rotate,• Bit and Byte,• Control transfer,• String,• Flag control,• Segment register,• Miscellanous.
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
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
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
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
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.
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.
Data transfer – stack PUSH
ESP
Stack
Adresses growth
Stack growth
Operand
Stack
Data transfer – stack POP
ESP
Stack
Adresses growth
Stack growth
Operand
Stack
Data transfer – PUSHA, POPA
EBX
EBPESI
old ESP
EAXECXEDX
EDIESP after PUSHA
ESP before PUSHA ESP after POPA
ESP before POPA
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.
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.
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.
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
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
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.
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
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.
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.
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)
Logical
• AND – Perform bitwise logical AND.• OR – Perform bitwise logical OR.• XOR – Perform bitwise logical exclusive OR.• NOT – Perform bitwise logical NOT.
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.
Shift
CF
SAL / SHL
SHR
SAR
0
CF0
S CFSign
Double shift
CF
SHLD
SHRD
S CF
Source operand
Source operand
Destination operand
Destination operand
Rotate
CF
ROL
RORCF
CF
RCL
RCRCF
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.
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.
Control transfer
• Conditional transfer.• Unconditional transfer.• Near transfer – within segment.• Far transfer – different segment.• Relative address.• Absolute address.
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.
Procedure calling
Procedure
Program
Call
Ret
Stack
Returnaddress
Interrupt handling
Handler
Program
Iret
Stack
Returnaddress
Interrupt
EFLAGS
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.
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.
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.
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
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.
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.
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.
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.
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.
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
Miscellanous instructions
• LEA – load effective address.• XLAT, XLATB – table lookup.• CPUID – processor identification.• NOP – no operation.• UD2 – undefined instruction.
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
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