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.
MOV Operands Algorithm Examples Copy 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, memory memory, REG REG, REG memory, immediate REG, immediate SREG, memory memory, SREG REG, SREG SREG, REG operand1 = operand2 FLAGS MOV has no impact on FLAGS register MOV AL,[0100] MOV [0100],AH MOV AX,BX MOV [0200],5 MOV BL,0EH MOV DS, MyVar * MOV MyVar, CS MOV AX,ES MOV SS,DX * MyVar is a variable of size 2 bytes (Not available in Debug)
ADD Operands Algorithm Examples Add REG, memory memory, REG REG, REG memory, immediate REG, immediate operand1 = operand1 + operand2 FLAGS ADD AL,[0105] ADD [0100],AH ADD BX,CX ADD [0200],5 ADD 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 Operands Algorithm Examples Increment. REG memory operand = operand + 1 FLAGS CF-Not changed INC AL INC [0100] INC SI INC CX INC MyVar * * MyVar is a variable of any size
SUB Operands Algorithm Examples Subtract REG, memory memory, REG REG, REG memory, immediate REG, immediate operand1 = operand1 - operand2 FLAGS SUB AL,[0105] SUB [0100],AH SUB BX,CX SUB [0200],5 SUB 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 Operands Algorithm Examples Decrement. REG memory operand = operand - 1 FLAGS CF-Not changed DEC AL DEC [0100] DEC SI DEC CX DEC MyVar * * MyVar is a variable of any size
MUL Operands Algorithm Examples Unsigned multiply. REG memory 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 BL MUL [0302] MUL BX MUL MyVar * * MyVar is a variable of any size
IMUL Operands Algorithm Examples Signed multiply. REG memory 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 BL IMUL [0302] IMUL BX IMUL MyVar * * MyVar is a variable of any size
DIV Operands Algorithm Examples Unsigned divide. REG memory when operand is a byte: AL = AX / operand AH = remainder (modulus) when operand is a word: AX = (DX AX) / operand DX = remainder (modulus) FLAGS DIV BL DIV [0302] DIV BX DIV MyVar * * MyVar is a variable of any size
IDIV Operands Algorithm Examples Signed divide. REG memory when operand is a byte: AL = AX / operand AH = remainder (modulus) when operand is a word: AX = (DX AX) / operand DX = remainder (modulus) FLAGS IDIV BL IDIV [0302] IDIV BX IDIV MyVar * * MyVar is a variable of any size
NEG Operands Algorithm Examples Negate. Makes operand negative (two's complement). Operands Algorithm Examples REG memory Invert all bits of the operand Add 1 to inverted operand FLAGS NEG BL NEG [0302] NEG BX NEG DI NEG MyVar * * MyVar is a variable of any size
NOP Operands Algorithm Examples No Operation. Do nothing No Operands FLAGS
NOT Operands Algorithm Examples Invert each bit of the operand. REG memory if bit is 1 turn it to 0. if bit is 0 turn it to 1. FLAGS NOT BL NOT [0302] NOT BX NOT DI NOT MyVar * * MyVar is a variable of any size
AND Operands Algorithm Examples Logical AND. REG, memory memory, REG REG, REG memory, immediate REG, immediate Logical AND between all bits of two operands. Result is stored in operand1. These rules apply: 1 AND 1 = 1 1 AND 0 = 0 0 AND 1 = 0 0 AND 0 = 0 FLAGS AND AL,[0105] AND [0100],AH AND BX,CX AND [0200],5 AND 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 Operands Algorithm Examples Logical OR. REG, memory memory, REG REG, REG memory, immediate REG, immediate Logical OR between all bits of two operands. Result is stored in first operand. These rules apply: 1 OR 1 = 1 1 OR 0 = 1 0 OR 1 = 1 0 OR 0 = 0 FLAGS OR AL,[0105] OR [0100],AH OR BX,CX OR [0200],5 OR 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 Operands Algorithm Examples Logical OR. REG, memory memory, REG REG, REG memory, immediate REG, immediate Logical XOR (Exclusive OR) between all bits of two operands. Result is stored in first operand. These rules apply: 1 XOR 1 = 0 1 XOR 0 = 1 0 XOR 1 = 1 0 XOR 0 = 0 FLAGS XOR AL,[0105] XOR [0100],AH XOR BX,CX XOR [0200],5 XOR 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 Operands Algorithm Examples Shift operand1 Left. The number of shifts is set by operand2. Operands Algorithm Examples memory, immediate REG, immediate memory, CL REG, 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],2 SHL AH, 3 SHL [0200],CL SHL BX, CL SHL MyVar, 4 * SHL MyVar, CL * * MyVar is a variable of any size
SHR Operands Algorithm Examples Shift operand1 Right. The number of shifts is set by operand2. Operands Algorithm Examples memory, immediate REG, immediate memory, CL REG, 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],2 SHR AH, 3 SHR [0200],CL SHR BX, CL SHR MyVar, 4 * SHR MyVar, CL * * MyVar is a variable of any size
ROL Operands Algorithm Examples Rotate operand1 left. The number of rotates is set by operand2. Operands Algorithm Examples memory, immediate REG, immediate memory, CL REG, 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],2 ROL AH, 3 ROL [0200],CL ROL BX, CL ROL MyVar, 4 * ROL MyVar, CL * * MyVar is a variable of any size
ROR Operands Algorithm Examples Rotate operand1 right. The number of rotates is set by operand2. Operands Algorithm Examples memory, immediate REG, immediate memory, CL REG, 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],2 ROR AH, 3 ROR [0200],CL ROR BX, CL ROR MyVar, 4 * ROR MyVar, CL * * MyVar is a variable of any size
XCHG Operands Algorithm Examples Exchange values of two operands. REG, memory memory, REG REG, REG operand1 < - > operand2 FLAGS XCHG AL,[020E] XCHG [020E], AL XCHG AX,BX XCHG DL,MyVar * XCHG MyVar, BP ** * MyVar is a variable of size Byte ** MyVar is a variable of size Word
PUSH Operands Algorithm Examples Store 16 bit value in the stack. Note: PUSH immediate works only on 80186 CPU and later! Operands Algorithm Examples REG SREG memory immediate SP = SP - 2 SS:[SP] (top of the stack) = operand FLAGS PUSH AX PUSH CS PUSH [020E] PUSH 1AH PUSH MyVar * * MyVar is a variable of any size
POP Operands Algorithm Examples Get 16 bit value from the stack. REG SREG memory operand = SS:[SP] (top of the stack) SP = SP + 2 FLAGS POP AX POP CS POP [020E] POP MyVar * * MyVar is a variable of any size
LOOP Operands Algorithm Examples Decrease CX, jump to label if CX not zero. Operands Algorithm Examples Label/address CX = CX - 1 if CX <> 0 then jump else no jump, continue FLAGS MOV CX,5 label1: ; Some Code LOOP label1 CS:0100 MOV CX,5 CS:0103 INC SI CS:107 LOOP 103
CMP Operands Algorithm Examples Compare. REG, memory memory, REG REG, REG memory, immediate REG, 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], CL CMP AX, DX CMP [012A], 12H CMP BL,0FH CMP DL,MyVar * CMP MyVar, BX ** * MyVar is a variable of size Byte ** MyVar is a variable of size Word
JMP Operands Algorithm Examples 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 Label address/4-byte address always jump FLAGS label1: ; Some Code JMP label1 CS:0100 JMP 104 CS:0104 INC SI
JE Operands Algorithm Examples Short Jump if first operand is Equal to second operand (as set by CMP instruction). Signed/Unsigned. Operands Algorithm Examples Label address if ZF = 1 then jump else continue FLAGS CMP AL,BL JE label1 ; Code to skip if ZF = 0 label1: ; Code to execute if ZF = 1 CS:0100 CMP AL,BL CS:0102 JE 107 CS:0107 INC SI
JNE Operands Algorithm Examples Short Jump if first operand is Not Equal to second operand (as set by CMP instruction). Signed/Unsigned. Operands Algorithm Examples Label address if ZF = 0 then jump else continue FLAGS CMP AL,BL JNE label1 ; Code to skip if ZF = 1 label1: ; Code to execute if ZF = 0 CS:0100 CMP AL,BL CS:0102 JNE 107 CS:0107 INC SI
JZ Operands Algorithm Examples Short Jump if Zero (equal). Set by CMP, SUB, ADD, TEST, AND, OR, XOR instructions. Operands Algorithm Examples Label address if ZF = 1 then jump else continue FLAGS CMP AL,BL JZ label1 ; Code to skip if ZF = 0 label1: ; Code to execute if ZF = 1 CS:0100 CMP AL,BL CS:0102 JZ 107 CS:0107 INC SI
JNZ Operands Algorithm Examples Short Jump if Not Zero (not equal). Set by CMP, SUB, ADD, TEST, AND, OR, XOR instructions. Operands Algorithm Examples Label address if ZF = 0 then jump else continue FLAGS CMP AL,BL JNZ label1 ; Code to skip if ZF = 1 label1: ; Code to execute if ZF = 0 CS:0100 CMP AL,BL CS:0102 JNZ 107 CS:0107 INC SI
JA Operands Algorithm Examples Short Jump if first operand is Above second operand (as set by CMP instruction). Unsigned. Operands Algorithm Examples Label address if (CF = 0) and (ZF = 0) then jump else continue FLAGS CMP AL,BL JA label1 ; Code to skip if CF <> 0 and ;ZF <> 0 label1: ; Code to execute if CF = 0 and ;ZF = 0 CS:0100 CMP AL,BL CS:0102 JA 107 CS:0107 INC SI
JNA Operands Algorithm Examples Short Jump if first operand is Not Above second operand (as set by CMP instruction). Unsigned. Operands Algorithm Examples Label address if CF = 1 or ZF = 1 then jump else continue FLAGS CMP AL,BL JNA label1 ; Code to skip if CF <> 1 and ;ZF <> 1 label1: ; Code to execute if CF = 1 and ;ZF = 1 CS:0100 CMP AL,BL CS:0102 JNA 107 CS:0107 INC SI
JAE Operands Algorithm Examples Short Jump if first operand is Above or Equal to second operand (as set by CMP instruction). Unsigned. Operands Algorithm Examples Label address if CF = 0 then jump else continue FLAGS CMP AL,BL JAE label1 ; Code to skip if CF <> 0 label1: ; Code to execute if CF = 0 CS:0100 CMP AL,BL CS:0102 JAE 107 CS:0107 INC SI
JNAE Operands Algorithm Examples Short Jump if first operand is Not Above and Not Equal to second operand (as set by CMP instruction). Unsigned. Operands Algorithm Examples Label address if CF = 1 then jump else continue FLAGS CMP AL,BL JNAE label1 ; Code to skip if CF <> 1 label1: ; Code to execute if CF = 1 CS:0100 CMP AL,BL CS:0102 JNAE 107 CS:0107 INC SI
JB Operands Algorithm Example if CF = 1 then jump Short Jump if first operand is Below second operand (as set by CMP instruction). Unsigned. Operands Algorithm Example Label address 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 Operands Algorithm Example Short Jump if first operand is Below or Equal to second operand (as set by CMP instruction). Unsigned. Operands Algorithm Example Label address if CF = 1 or ZF = 1 then jump include 'emu8086.inc' ORG 100h MOV AL, 5 CMP AL, 5 JBE label1 PRINT 'AL is not below or equal to 5' JMP exit label1: PRINT 'AL is below or equal to 5' exit: RET
JNB Operands Algorithm Example Short Jump if first operand is Not Below second operand (as set by CMP instruction). Unsigned. Operands Algorithm Example Label address 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 Operands Algorithm Example Short Jump if first operand is Not Below and Not Equal to second operand (as set by CMP instruction). Unsigned. Operands Algorithm Example Label address 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 Operands Algorithm Example Short Jump if first operand is Greater then second operand (as set by CMP instruction). Signed. Operands Algorithm Example Label address 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 Operands Algorithm Example include 'emu8086.inc' Short Jump if first operand is Greater or Equal to second operand (as set by CMP instruction). Signed. Operands Algorithm Example Label address 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 Operands Algorithm Example include 'emu8086.inc' Short Jump if first operand is Not Greater then second operand (as set by CMP instruction). Signed. Operands Algorithm Example Label address 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 Operands Algorithm Example include 'emu8086.inc' Short Jump if first operand is Not Greater and Not Equal to second operand (as set by CMP instruction) Signed. Operands Algorithm Example Label address 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 Operands Algorithm Example Short Jump if first operand is Less then second operand (as set by CMP instruction). Signed. Operands Algorithm Example Label address 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 Operands Algorithm Example include 'emu8086.inc' Short Jump if first operand is Less or Equal to second operand (as set by CMP instruction). Signed. Operands Algorithm Example Label address 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 Operands Algorithm Example Short Jump if first operand is Not Less then second operand (as set by CMP instruction). Signed. Operands Algorithm Example Label address 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 Operands Algorithm Example Short Jump if first operand is Not Less and Not Equal to second operand (as set by CMP instruction)Signed. Operands Algorithm Example Label address 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 Label address
Operands Algorithm Example Label address