Post on 17-Dec-2015
Dept. of ECE, GMU ECE 511 - 20012
Overview
• Most of assembler instructions presented are supported by all of the following Intel processors:
– 80186, 80286, 80386, 80486, Pentium (80586)
• Five major instruction categories:– Transfer and Set– Arithmetic– Logic– Jump– Miscellaneous
Dept. of ECE, GMU ECE 511 - 20013
80x86 Transfer & Set Instructions 1
Name Comment Syntax MOV Move (copy) MOV Dest,Source XCHG Exchange XCHG Op1,Op2 STC Set Carry STC CLC Clear Carry CLC CMC Complement Carry CMC STD Set Direction STD CLD Clear Direction CLD STI Set Interrupt STI CLI Clear Interrupt CLI
Dept. of ECE, GMU ECE 511 - 20014
Transfer Instructions Examples 1
Example 1:MOV AX,0006h ; Move immediate data to AX registerMOV BX,AX ; Move AX register data to BX registerThis small program moves the value of 0006H to the AX register, then itmoves the content of AX (0006h) to the BX register
Example 2:MOV AX,2000h ; Move immediate data to AX registerMOV DS,AX ; Load DS segment registerMOV SI, 100h ; Load immediate data to SI index registerMOV DI, 120h ; Load immediate data to DI index registerMOV CX, 10h ; Load immediate data to CX registerMOV AH, [SI], ; Move memory data pointed by [SI] to AH
registerMOV [DI], AH ; Move AH register to memory located at [DI]
Dept. of ECE, GMU ECE 511 - 20015
Set Instructions That Test Flags
Instruction Description Condition Comments
SETC Set if carry Carry = 1 Same as SETB, SETNAE
SETNC Set if no carry Carry = 0 Same as SETNB, SETAE
SETZ Set if zero Zero = 1 Same as SETE SETNZ Set if not zero Zero = 0 Same as SETNE SETS Set if sign Sign = 1 - SETNS Set if no sign Sign = 0 - SETO Set if overflow Ovrflw=1 - SETNO Set if no overflow Ovrflw=0 - SETP Set if parity Parity = 1 Same as SETPE SETPE Set if parity even Parity = 1 Same as SETP SETNP Set if no parity Parity = 0 Same as SETPO SETPO Set if parity odd Parity = 0 Same as SETNP
Dept. of ECE, GMU ECE 511 - 20016
Set Instructions for Unsigned Comparisons
Instruction Description Condition Comments SETA Set if above (>) Carry=0, Zero=0 Same as SETNBE
SETNBE Set if not below or equal (not <=)
Carry=0, Zero=0 Same as SETA
SETAE Set if above or equal (>=)
Carry = 0 Same as SETNC, SETNB
SETNB Set if not below (not <)
Carry = 0 Same as SETNC, SETAE
SETB Set if below (<) Carry = 1 Same as SETC, SETNAE
SETNAE Set if not above or equal (not >=)
Carry = 1 Same as SETC, SETB
SETBE Set if below or equal (<=)
Carry = 1 or Zero = 1
Same as SETNA
SETNA Set if not above (not >)
Carry = 1 or Zero = 1
Same as SETBE
SETE Set if equal (=) Zero = 1 Same as SETZ SETNE Set if not equal () Zero = 0 Same as SETNZ
Dept. of ECE, GMU ECE 511 - 20017
Set Instructions for Signed Comparisons
Instruction Description Condition Comments
SETG Set if greater (>) Sign = Ovrflw or Zero=0
Same as SETNLE
SETNLE Set if not less than or equal (not <=)
Sign = Ovrflw or Zero=0
Same as SETG
SETGE Set if greater than or equal (>=)
Sign = Ovrflw Same as SETNL
SETNL Set if not less than (not <)
Sign = Ovrflw Same as SETGE
SETL Set if less than (<) Sign Ovrflw Same as SETNGE
SETNGE Set if not greater or equal (not >=)
Sign Ovrflw Same as SETL
SETLE Set if less than or equal (<=)
Sign Ovrflw or Zero = 1
Same as SETNG
SETNG Set if not greater than (not >)
Sign Ovrflw or Zero = 1
Same as SETLE
SETE Set if equal (=) Zero = 1 Same as SETZ SETNE Set if not equal () Zero = 0 Same as SETNZ
Dept. of ECE, GMU ECE 511 - 20018
Set Instructions Example
• Bool := ((A <= B) and (D = E)) or (F <> G)MOV AX, A CMP AX, B SETLE BL MOV AX, D CMP AX, E SETE BH AND BL, BH MOV AX, F CMP AX, G SETNE BH OR BL, BH MOV Bool, BH
Dept. of ECE, GMU ECE 511 - 20019
80x86 Transfer Instructions 2
Name Comment Syntax PUSH Push onto stack PUSH Source PUSHF Push flags PUSHF PUSHA Push all general registers PUSHA POP Pop from stack POP Dest POPF Pop flags POPF POPA Pop all general registers POPA CBW Convert byte to word CBW CWD Convert word to double CWD CWDE Convert word extended
double CWDE
IN Input IN Dest, Port OUT Output OUT Port, Source
Dept. of ECE, GMU ECE 511 - 200110
Transfer Instructions Examples 2
Example 1:PUSH DS ; Push current DS content to stackMOV AX,0 ; Move immediate data to AX registerPUSH AX ; Push 0 to stackMOV AL, 0A1HCBWCWD
Example 2:IN AL, 60h ; Read keyboard port MOV DX,378h ; Point at LPT1: data port IN AL, DX ; Read data from printer port
INC AX ; Bump ASCII code by oneOUT DX, AL ; Write data in AL to printer port
Dept. of ECE, GMU ECE 511 - 200111
80x86 String (Transfer) Instructions 3
Name Comment Syntax MOVS Move string MOVS LODS Load string element into
accumulator LODS
STOS Store accumulator into string element
STOS
SCAS Scan string and check for match against the value in the accumulator
SCAS
CMPS Compare two strings CMPS INS Input a string from an I/O
port INS
OUTS output a string to an I/O port
OUTS
REP Repeat a string operation REP REPZ Repeat while zero REPZ REPE Repeat while equal REPE REPNZ Repeat while not zero REPNZ REPNE Repeat while not equal REPNE Note: Assume that ds:si contains segmented address of a source string and es:di contains segmented address of a destination string
Dept. of ECE, GMU ECE 511 - 200112
80x86 Arithmetic Instructions 1
Name Comment Syntax ADD Add ADD Dest,Source ADC Add with Carry ADC Dest,Source SUB Subtract SUB Dest,Source SBB Subtract with borrow SBB Dest,Source DIV Divide (unsigned) DIV Op DIV Divide (unsigned) DIV Op DIV Divide (unsigned) DIV Op IDIV Signed Integer Divide IDIV Op IDIV Signed Integer Divide IDIV Op IDIV Signed Integer Divide IDIV Op
Dept. of ECE, GMU ECE 511 - 200113
Arithmetic Instructions Examples 1
Example 1: J = K + MMOV AX, KADD AX,MMOV J, AX
Example 2: J = K + M + N + PMOV AX, KADD AX,MADD AX,NADD AX,PMOV J, AX
Dept. of ECE, GMU ECE 511 - 200114
Arithmetic Instructions Examples 2
Example 1: J = K - JMOV AX, KSUB J, AXMOV J, AX
Example 1: J = J - (K + M)MOV AX, JSUB AX, KSUB AX, MMOV J, AX
or
MOV AX, KADD AX, MSUB J, AX
Dept. of ECE, GMU ECE 511 - 200115
80x86 Arithmetic Instructions 2
Name Comment Syntax MUL Multiply (unsigned) MUL Op MUL Multiply (unsigned) MUL Op MUL Multiply (unsigned) MUL Op IMUL Signed Integer Multiply IMUL Op IMUL Signed Integer Multiply IMUL Op IMUL Signed Integer Multiply IMUL Op INC Increment INC Op DEC Decrement DEC Op
Dept. of ECE, GMU ECE 511 - 200116
Arithmetic Instructions Examples 3
Example 1: ((J*7 + K) * 6 + M) * 2
MOV BX, JIMUL BX, 7 ADD BX, K IMUL BX, 6 ADD BX, M
ADD BX, BX Example 2: J = K/ M (unsigned)
MOV AX, KMOV DX, 0 ; Zero extend unsigned value in AX to DXDIV M MOV J, AX
Dept. of ECE, GMU ECE 511 - 200117
80x86 Arithmetic Instructions 3
Name Comment Syntax CMP Compare CMP Op1,Op2 SAL Shift arithmetic left SAL Op,Quantity SAR Shift arithmetic right SAR Op,Quantity RCL Rotate left through Carry RCL Op,Quantity RCR Rotate right through
Carry RCR Op,Quantity
ROL Rotate left ROL Op,Quantity ROR Rotate right ROR Op,Quantity
Dept. of ECE, GMU ECE 511 - 200118
SHL\SAL\SAR Instructions
• SHL/SAL – Move each bit in destination operand one bit to left by No. of times specified
by count operand– Zeros fill vacated positions at L.O. bit; H.O. bit shifts into carry flag
SHL AH, 4 ; Move L.O. bits to H.O. position
• SAR – Move each bit in destination operand one bit to right by No. of times
specified by count operand– H.O. bit fills vacated position at H.O. bit; L.O. bit shifts into carry flag
SAR AH, 4
Dept. of ECE, GMU ECE 511 - 200120
80x86 Logic Instructions 1
Name Comment Syntax NEG Negate (2-complement) NEG Op NOT Invert each bit NOT Op AND Logical and AND Dest,Source OR Logical or OR Dest,Source XOR Logical exclusive or XOR Dest,Source SHL Shift logical left SHL Op,Quantity SHR Shift logical right SHR Op,Quantity
Dept. of ECE, GMU ECE 511 - 200121
80x86 Logic Instructions 2
• Except NOT, AND, OR and XOR instructions affect flags as follows:
– Clear carry flag
– Clear overflow flag
– Set zero flag if result is zero and clear it otherwise
– Copy H.O. bit of result into sign flag
– Set parity flag according to parity (number of one bits) in result
– Scramble auxiliary carry flag
• NOT instruction does not affect any flags
Dept. of ECE, GMU ECE 511 - 200122
SHR\SHLD\SHRD Instructions
• SHR
• SHLD/SHRD– Provide double precision shift left and right operations, respectively– Available only on 80386 and later processors with forms of SHLD operand1, operand2, immediate SHLD operand1, operand2, CL SHRD operand1, operand2, immediate SHRD operand1, operand2, CL
Dept. of ECE, GMU ECE 511 - 200123
SHRD Instruction Example
• Let
– ax contains a value in range 0..99 representing a year (1900..1999)
– bx contains a value in the range 1..31 representing a day, and
– cx contains a value in the range 1..12 representing a month
• Pack these data into dx as follows: SHRD DX, AX, 7
SHRD DX, BX, 5
SHRD DX, CX, 4
Dept. of ECE, GMU ECE 511 - 200124
80x86 Miscellaneous Instructions
Name Comment Syntax NOP No operation NOP LEA Load effective adress LEA Dest,Source INT Interrupt INT Nr LEA - Loads specified 16 or 32 bit general purpose register with effectiveaddress of the specified memory location using formats of:
LEA reg16, mem or LEA reg32, mem (only for 80386 or later processors)Examples:
LEA AX, [BX] LEA BX, 3[BX] LEA AX, 3[BX] LEA BX, 4[BP+SI] LEA AX, -123[DI]
Dept. of ECE, GMU ECE 511 - 200125
80x86 General Jump Instructions 1
Name Comment Syntax CALL Call subroutine CALL Proc JMP Jump JMP Dest JE Jump if Equal JE Dest JZ Jump if Zero JZ Dest JCXZ Jump if CX Zero JCXZ Dest JP Jump if Parity (Parity
Even) JP Dest
JPE Jump if Parity Even JPE Dest
Dept. of ECE, GMU ECE 511 - 200126
80x86 General Jump Instructions 2
Name Comment Syntax RET Return from subroutine RET JNE Jump if not Equal JNE Dest JNZ Jump if not Zero JNZ Dest JECXZ Jump if ECX Zero JECXZ Dest JNP Jump if no Parity (Parity
Odd) JNP Dest
JPO Jump if Parity Odd JPO Dest
Dept. of ECE, GMU ECE 511 - 200127
80x86 Unsigned Jump (Cardinal) Instructions
Name Comment Syntax JA Jump if Above JA Dest JAE Jump if Above or Equal JAE Dest JB Jump if Below JB Dest JBE Jump if Below or Equal JBE Dest JNA Jump if not Above JNA Dest JNAE Jump if not Above or
Equal JNAE Dest
JNB Jump if not Below JNB Dest JNBE Jump if not Below or
Equal JNBE Dest
JC Jump if Carry JC Dest JNC Jump if no Carry JNC Dest
Dept. of ECE, GMU ECE 511 - 200128
80x86 Signed Jump (Integer) Instructions
Name Comment Syntax JG Jump if Greater JG Dest JGE Jump if Greater or Equal JGE Dest JL Jump if Less JL Dest JLE Jump if Less or Equal JLE Dest JNG Jump if not Greater JNG Dest JNGE Jump if not Greater or
Equal JNGE Dest
JNL Jump if not Less JNL Dest JNLE Jump if not Less or Equal JNLE Dest JO Jump if Overflow JO Dest JNO Jump if no Overflow JNO Dest JS Jump if Sign (= negative) JS Dest JNS Jump if no Sign (=
positive) JNS Dest