Post on 02-Feb-2016
description
Assembly Language Programming
Part 2
• Microprocessor’s Instruction Set
Microprocessor’s Instruction Set
• An instruction set is a list of all the instructions, and all their variations, that a processor (or in the case of a virtual machine, an interpreter) can execute.
• an instruction is a single operation of a processor defined by an instruction set architecture.
• On traditional architectures, an instruction includes
• An opcode specifying the operation to be performed, such as "add contents of memory to register",
• zero or more operand specifiers, which may specify registers, memory locations, or literal data. The operand specifiers may have addressing modes determining their meaning or may be in fixed fields.
Microprocessor’s Instruction Set
• Instructions include:
• Arithmetic such as add and subtract
• Logic instructions such as and, or, and not
• Data instructions such as move, input, output, load, and store
• Control flow instructions such as goto, if ... goto, call, and return.
MOVCopy operand2 to operand1.The MOV instruction cannot:•set the value of the CS and IP registers. •copy value of one segment register to another segment register (should copy to general register first). •copy immediate value to segment register (should copy to general register first).
Operands Algorithm Examples
REG, memorymemory, REGREG, REGmemory, immediateREG, immediate
SREG, memorymemory, SREGREG, SREGSREG, REG
operand1 = operand2
FLAGS MOV has no impact on FLAGS register
MOV AL,[0100]MOV [0100],AHMOV AX,BXMOV [0200],5MOV BL,0EH
MOV DS, MyVar *MOV MyVar, CSMOV AX,ESMOV SS,DX
* MyVar is a variable of size 2 bytes (Not available in Debug)
ADD
Add
Operands Algorithm Examples
REG, memorymemory, REGREG, REGmemory, immediateREG, immediate operand1 = operand1 + operand2
FLAGS
ADD AL,[0105]ADD [0100],AHADD BX,CXADD [0200],5ADD BL,1AH
ADD AL, MyVar *ADD MyVar, DX **
* MyVar is a variable of size 1 bytes.
** MyVar is a variable of size 2 bytes.
INC
Increment.
Operands Algorithm Examples
REGmemory
operand = operand + 1
FLAGS
CF-Not changed
INC ALINC [0100]
INC SIINC CXINC MyVar *
* MyVar is a variable of any size
SUB
Subtract
Operands Algorithm Examples
REG, memorymemory, REGREG, REGmemory, immediateREG, immediate operand1 = operand1 - operand2
FLAGS
SUB AL,[0105]SUB [0100],AHSUB BX,CXSUB [0200],5SUB BL,1AH
SUB AL, MyVar *SUB MyVar, DX **
* MyVar is a variable of size 1 bytes.
** MyVar is a variable of size 2 bytes.
DEC
Decrement.
Operands Algorithm Examples
REGmemory
operand = operand - 1
FLAGS
CF-Not changed
DEC ALDEC [0100]
DEC SIDEC CXDEC MyVar *
* MyVar is a variable of any size
MUL
Unsigned multiply.
Operands Algorithm Examples
REGmemory
when operand is a byte:AX = AL * operand.
when operand is a word (2 Bytes):(DX AX) = AX * operand.
FLAGS
CF=OF=0 when high section of the result is zero.
MUL BLMUL [0302]
MUL BXMUL MyVar *
* MyVar is a variable of any size
IMUL
Signed multiply.
Operands Algorithm Examples
REGmemory
when operand is a byte:AX = AL * operand.
when operand is a word (2 Bytes):(DX AX) = AX * operand.
FLAGS
CF=OF=0 when result fits into operand of IMUL.
IMUL BLIMUL [0302]
IMUL BXIMUL MyVar *
* MyVar is a variable of any size
DIV
Unsigned divide.
Operands Algorithm Examples
REGmemory
when operand is a byte:AL = AX / operandAH = remainder (modulus)
when operand is a word:AX = (DX AX) / operandDX = remainder (modulus)
FLAGS
DIV BLDIV [0302]
DIV BXDIV MyVar *
* MyVar is a variable of any size
IDIV
Signed divide.
Operands Algorithm Examples
REGmemory
when operand is a byte:AL = AX / operandAH = remainder (modulus)
when operand is a word:AX = (DX AX) / operandDX = remainder (modulus)
FLAGS
IDIV BLIDIV [0302]
IDIV BXIDIV MyVar *
* MyVar is a variable of any size
NEG
Negate. Makes operand negative (two's complement).
Operands Algorithm Examples
REGmemory Invert all bits of the operand
Add 1 to inverted operand
FLAGS
NEG BLNEG [0302]
NEG BXNEG DINEG MyVar *
* MyVar is a variable of any size
NOP
No Operation.
Operands Algorithm Examples
No OperandsDo nothing
FLAGS NOP
NOT
Invert each bit of the operand.
Operands Algorithm Examples
REGmemory if bit is 1 turn it to 0.
if bit is 0 turn it to 1.
FLAGS
NOT BLNOT [0302]
NOT BXNOT DINOT MyVar *
* MyVar is a variable of any size
AND
Logical AND.
Operands Algorithm Examples
REG, memorymemory, REGREG, REGmemory, immediateREG, immediate
Logical AND between all bits of two operands. Result is stored in operand1.
These rules apply:1 AND 1 = 11 AND 0 = 00 AND 1 = 00 AND 0 = 0
FLAGS
AND AL,[0105]AND [0100],AHAND BX,CXAND [0200],5AND BL,1AH
AND AL, MyVar *AND MyVar, DX **
* MyVar is a variable of size 1 bytes.
** MyVar is a variable of size 2 bytes.
OR
Logical OR.
Operands Algorithm Examples
REG, memorymemory, REGREG, REGmemory, immediateREG, immediate
Logical OR between all bits of two operands. Result is stored in first operand.
These rules apply:1 OR 1 = 11 OR 0 = 10 OR 1 = 10 OR 0 = 0
FLAGS
OR AL,[0105]OR [0100],AHOR BX,CXOR [0200],5OR BL,1AH
OR AL, MyVar *OR MyVar, DX **
* MyVar is a variable of size 1 bytes.
** MyVar is a variable of size 2 bytes.
XOR
Logical OR.
Operands Algorithm Examples
REG, memorymemory, REGREG, REGmemory, immediateREG, immediate
Logical XOR (Exclusive OR) between all bits of two operands. Result is stored in first operand.
These rules apply:1 XOR 1 = 01 XOR 0 = 10 XOR 1 = 10 XOR 0 = 0
FLAGS
XOR AL,[0105]XOR [0100],AHXOR BX,CXXOR [0200],5XOR BL,1AH
XOR AL, MyVar *XOR MyVar, DX **
* MyVar is a variable of size 1 bytes.
** MyVar is a variable of size 2 bytes.
SHL
Shift operand1 Left. The number of shifts is set by operand2.
Operands Algorithm Examples
memory, immediateREG, immediate
memory, CLREG, CL
Shift all bits left, the bit that goes off is set to CF. Zero bit is inserted to the right-most position.
FLAGS
OF=0 if first operand keeps original sign.
SHL [0105],2SHL AH, 3
SHL [0200],CLSHL BX, CL
SHL MyVar, 4 *SHL MyVar, CL *
* MyVar is a variable of any size
SHR
Shift operand1 Right. The number of shifts is set by operand2.
Operands Algorithm Examples
memory, immediateREG, immediate
memory, CLREG, CL
Shift all bits right, the bit that goes off is set to CF. Zero bit is inserted to the left-most position.
FLAGS
OF=0 if first operand keeps original sign.
SHR [0105],2SHR AH, 3
SHR [0200],CLSHR BX, CL
SHR MyVar, 4 *SHR MyVar, CL *
* MyVar is a variable of any size
ROL
Rotate operand1 left. The number of rotates is set by operand2.
Operands Algorithm Examples
memory, immediateREG, immediate
memory, CLREG, CL
shift all bits left, the bit that goes off is set to CF and the same bit is inserted to the right-most position.
FLAGS
OF=0 if first operand keeps original sign.
ROL [0105],2ROL AH, 3
ROL [0200],CLROL BX, CL
ROL MyVar, 4 *ROL MyVar, CL *
* MyVar is a variable of any size
ROR
Rotate operand1 right. The number of rotates is set by operand2.
Operands Algorithm Examples
memory, immediateREG, immediate
memory, CLREG, CL
shift all bits right, the bit that goes off is set to CF and the same bit is inserted to the left-most position.
FLAGS
OF=0 if first operand keeps original sign.
ROR [0105],2ROR AH, 3
ROR [0200],CLROR BX, CL
ROR MyVar, 4 *ROR MyVar, CL *
* MyVar is a variable of any size
XCHG
Exchange values of two operands.
Operands Algorithm Examples
REG, memorymemory, REGREG, REG operand1 < - > operand2
FLAGS
XCHG AL,[020E]XCHG [020E], ALXCHG AX,BX
XCHG DL,MyVar *XCHG MyVar, BP **
* MyVar is a variable of size Byte
** MyVar is a variable of size Word
PUSH
Store 16 bit value in the stack.Note: PUSH immediate works only on 80186 CPU and later!
Operands Algorithm Examples
REGSREGmemoryimmediate
SP = SP - 2 SS:[SP] (top of the stack) = operand
FLAGS
PUSH AXPUSH CSPUSH [020E]PUSH 1AH
PUSH MyVar *
* MyVar is a variable of any size
POP
Get 16 bit value from the stack.
Operands Algorithm Examples
REGSREGmemory
operand = SS:[SP] (top of the stack) SP = SP + 2
FLAGS
POP AXPOP CSPOP [020E]
POP MyVar *
* MyVar is a variable of any size
LOOP
Decrease CX, jump to label if CX not zero.
Operands Algorithm Examples
Label/address CX = CX - 1if CX <> 0 then jump else no jump, continue
FLAGS
MOV CX,5label1:; Some CodeLOOP label1
CS:0100 MOV CX,5CS:0103 INC SI; Some CodeCS:107 LOOP 103
CMP
Compare.
Operands Algorithm Examples
REG, memorymemory, REGREG, REGmemory, immediateREG, immediate
operand1 - operand2
result is not stored anywhere, flags are set (OF, SF, ZF, AF, PF, CF) according to result.
FLAGS
CMP DL,[021A]CMP [021A], CLCMP AX, DXCMP [012A], 12HCMP BL,0FH
CMP DL,MyVar *CMP MyVar, BX **
* MyVar is a variable of size Byte
** MyVar is a variable of size Word
JMP
Unconditional Jump. Transfers control to another part of the program. 4-byte address may be entered in this form: 1234h:5678h, first value is a segment second value is an offset.
Operands Algorithm Examples
Labeladdress/4-byte
addressalways jump
FLAGS
label1:; Some CodeJMP label1
CS:0100 JMP 104; Some CodeCS:0104 INC SI
JE
Short Jump if first operand is Equal to second operand (as set by CMP instruction). Signed/Unsigned.
Operands Algorithm Examples
Labeladdress
if ZF = 1 then jump else continue
FLAGS
CMP AL,BLJE label1; Code to skip if ZF = 0label1:; Code to execute if ZF = 1
CS:0100 CMP AL,BLCS:0102 JE 107; Code to skip if ZF = 0CS:0107 INC SI; Code to execute if ZF = 1
JNE
Short Jump if first operand is Not Equal to second operand (as set by CMP instruction). Signed/Unsigned.
Operands Algorithm Examples
Labeladdress
if ZF = 0 then jump else continue
FLAGS
CMP AL,BLJNE label1; Code to skip if ZF = 1label1:; Code to execute if ZF = 0
CS:0100 CMP AL,BLCS:0102 JNE 107; Code to skip if ZF = 1CS:0107 INC SI; Code to execute if ZF = 0
JZ
Short Jump if Zero (equal). Set by CMP, SUB, ADD, TEST, AND, OR, XOR instructions.
Operands Algorithm Examples
Labeladdress
if ZF = 1 then jump else continue
FLAGS
CMP AL,BLJZ label1; Code to skip if ZF = 0label1:; Code to execute if ZF = 1
CS:0100 CMP AL,BLCS:0102 JZ 107; Code to skip if ZF = 0CS:0107 INC SI; Code to execute if ZF = 1
JNZ
Short Jump if Not Zero (not equal). Set by CMP, SUB, ADD, TEST, AND, OR, XOR instructions.
Operands Algorithm Examples
Labeladdress
if ZF = 0 then jump else continue
FLAGS
CMP AL,BLJNZ label1; Code to skip if ZF = 1label1:; Code to execute if ZF = 0
CS:0100 CMP AL,BLCS:0102 JNZ 107; Code to skip if ZF = 1CS:0107 INC SI; Code to execute if ZF = 0
JA
Short Jump if first operand is Above second operand (as set by CMP instruction). Unsigned.
Operands Algorithm Examples
Labeladdress
if (CF = 0) and (ZF = 0) then jump else continue
FLAGS
CMP AL,BLJA label1; Code to skip if CF <> 0 and ;ZF <> 0label1:; Code to execute if CF = 0 and;ZF = 0
CS:0100 CMP AL,BLCS:0102 JA 107; Code to skip if CF <> 0 and ;ZF <> 0CS:0107 INC SI; Code to execute if CF = 0 and;ZF = 0
JNA
Short Jump if first operand is Not Above second operand (as set by CMP instruction). Unsigned.
Operands Algorithm Examples
Labeladdress
if CF = 1 or ZF = 1 then jump else continue
FLAGS
CMP AL,BLJNA label1; Code to skip if CF <> 1 and ;ZF <> 1label1:; Code to execute if CF = 1 and;ZF = 1
CS:0100 CMP AL,BLCS:0102 JNA 107; Code to skip if CF <> 1 and ;ZF <> 1CS:0107 INC SI; Code to execute if CF = 1 and;ZF = 1
JAE
Short Jump if first operand is Above or Equal to second operand (as set by CMP instruction). Unsigned.
Operands Algorithm Examples
Labeladdress
if CF = 0 then jump else continue
FLAGS
CMP AL,BLJAE label1; Code to skip if CF <> 0label1:; Code to execute if CF = 0
CS:0100 CMP AL,BLCS:0102 JAE 107; Code to skip if CF <> 0CS:0107 INC SI; Code to execute if CF = 0
JNAE
Short Jump if first operand is Not Above and Not Equal to second operand (as set by CMP instruction). Unsigned.
Operands Algorithm Examples
Labeladdress
if CF = 1 then jump else continue
FLAGS
CMP AL,BLJNAE label1; Code to skip if CF <> 1label1:; Code to execute if CF = 1
CS:0100 CMP AL,BLCS:0102 JNAE 107; Code to skip if CF <> 1CS:0107 INC SI; Code to execute if CF = 1
JB
Short Jump if first operand is Below second operand (as set by CMP instruction). Unsigned.
Operands Algorithm Example
Labeladdress
if CF = 1 then jump
include 'emu8086.inc' ORG 100h MOV AL, 1 CMP AL, 5 JB label1 PRINT 'AL is not below 5' JMP exit
label1: PRINT 'AL is below 5' exit: RET
JBE
Short Jump if first operand is Below or Equal to second operand (as set by CMP instruction). Unsigned.
Operands Algorithm Example
Labeladdress
if CF = 1 or ZF = 1 then jump
include 'emu8086.inc' ORG 100h MOV AL, 5 CMP AL, 5 JBE label1PRINT 'AL is not below or equal to 5' JMP exit label1: PRINT 'AL is below or equal to 5' exit: RET
JNB
Short Jump if first operand is Not Below second operand (as set by CMP instruction). Unsigned.
Operands Algorithm Example
Labeladdress
if CF = 0 then jump
include 'emu8086.inc' ORG 100h MOV AL, 7 CMP AL, 5 JNB label1 PRINT 'AL < 5.' JMP exit label1: PRINT 'AL >= 5.' exit: RET
JNBE
Short Jump if first operand is Not Below and Not Equal to second operand (as set by CMP instruction). Unsigned.
Operands Algorithm Example
Labeladdress
if (CF = 0) and (ZF = 0) then jump
include 'emu8086.inc' ORG 100h MOV AL, 7 CMP AL, 5 JNBE label1 PRINT 'AL <= 5.' JMP exit label1: PRINT 'AL > 5.' exit: RET
JG
Short Jump if first operand is Greater then second operand (as set by CMP instruction). Signed.
Operands Algorithm Example
Labeladdress
if (ZF = 0) and (SF = OF) then jump
include 'emu8086.inc' ORG 100h MOV AL, 5 CMP AL, -5 JG label1 PRINT 'AL is not greater -5.' JMP exit label1: PRINT 'AL is greater -5.' exit: RET
JGE
Short Jump if first operand is Greater or Equal to second operand (as set by CMP instruction). Signed.
Operands Algorithm Example
Labeladdress
if SF = OF then jump
include 'emu8086.inc' ORG 100h MOV AL, 2 CMP AL, -5 JGE label1 PRINT 'AL < -5' JMP exit label1: PRINT 'AL >= -5' exit: RET
JNG
Short Jump if first operand is Not Greater then second operand (as set by CMP instruction). Signed.
Operands Algorithm Example
Labeladdress
if (ZF = 1) and (SF <> OF) then jump
include 'emu8086.inc' ORG 100h MOV AL, 2 CMP AL, 3 JNG label1 PRINT 'AL > 3.' JMP exit label1: PRINT 'Al <= 3.' exit: RET
JNGE
Short Jump if first operand is Not Greater and Not Equal to second operand (as set by CMP instruction) Signed.
Operands Algorithm Example
Labeladdress
if SF <> OF then jump
include 'emu8086.inc' ORG 100h MOV AL, 2 CMP AL, 3 JNGE label1 PRINT 'AL >= 3.' JMP exit label1: PRINT 'Al < 3.' exit: RET
JL
Short Jump if first operand is Less then second operand (as set by CMP instruction). Signed.
Operands Algorithm Example
Labeladdress
if SF <> OF then jump
include 'emu8086.inc' ORG 100h MOV AL, -2 CMP AL, 5 JL label1 PRINT 'AL >= 5.' JMP exit label1: PRINT 'AL < 5.' exit: RET
JLE
Short Jump if first operand is Less or Equal to second operand (as set by CMP instruction). Signed.
Operands Algorithm Example
Labeladdress
if SF <> OF or ZF = 1 then jump
include 'emu8086.inc' ORG 100h MOV AL, -2 CMP AL, 5 JLE label1 PRINT 'AL > 5.' JMP exit label1: PRINT 'AL <= 5.' exit: RET
JNL
Short Jump if first operand is Not Less then second operand (as set by CMP instruction). Signed.
Operands Algorithm Example
Labeladdress
if SF = OF then jump
include 'emu8086.inc' ORG 100h MOV AL, 2 CMP AL, -3 JNL label1 PRINT 'AL < -3.' JMP exit label1: PRINT 'Al >= -3.' exit: RET
JNLE
Short Jump if first operand is Not Less and Not Equal to second operand (as set by CMP instruction)Signed.
Operands Algorithm Example
Labeladdress
if (SF = OF) and (ZF = 0) then jump
include 'emu8086.inc' ORG 100h MOV AL, 2 CMP AL, -3 JNLE label1 PRINT 'AL <= -3.' JMP exit label1: PRINT 'Al > -3.' exit: RET
Operands Algorithm Example
Labeladdress
Operands Algorithm Example
Labeladdress