Assembly Language Programming Part 2

50
Assembly Language Programming Part 2 • Microprocessor’s Instruction Set

description

Microprocessor’s Instruction Set. Assembly Language Programming Part 2. 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. - PowerPoint PPT Presentation

Transcript of Assembly Language Programming Part 2

Page 1: Assembly Language Programming Part 2

Assembly Language Programming

Part 2

• Microprocessor’s Instruction Set

Page 2: Assembly Language Programming Part 2

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.

Page 3: Assembly Language Programming Part 2

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.

Page 4: Assembly Language Programming Part 2

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)

Page 5: Assembly Language Programming Part 2

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.

Page 6: Assembly Language Programming Part 2

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

Page 7: Assembly Language Programming Part 2

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.

Page 8: Assembly Language Programming Part 2

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

Page 9: Assembly Language Programming Part 2

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

Page 10: Assembly Language Programming Part 2

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

Page 11: Assembly Language Programming Part 2

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

Page 12: Assembly Language Programming Part 2

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

Page 13: Assembly Language Programming Part 2

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

Page 14: Assembly Language Programming Part 2

NOP

No Operation.

Operands Algorithm Examples

No OperandsDo nothing

FLAGS NOP

Page 15: Assembly Language Programming Part 2

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

Page 16: Assembly Language Programming Part 2

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.

Page 17: Assembly Language Programming Part 2

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.

Page 18: Assembly Language Programming Part 2

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.

Page 19: Assembly Language Programming Part 2

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

Page 20: Assembly Language Programming Part 2

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

Page 21: Assembly Language Programming Part 2

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

Page 22: Assembly Language Programming Part 2

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

Page 23: Assembly Language Programming Part 2

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

Page 24: Assembly Language Programming Part 2

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

Page 25: Assembly Language Programming Part 2

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

Page 26: Assembly Language Programming Part 2

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

Page 27: Assembly Language Programming Part 2

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

Page 28: Assembly Language Programming Part 2

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

Page 29: Assembly Language Programming Part 2

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

Page 30: Assembly Language Programming Part 2

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

Page 31: Assembly Language Programming Part 2

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

Page 32: Assembly Language Programming Part 2

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

Page 33: Assembly Language Programming Part 2

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

Page 34: Assembly Language Programming Part 2

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

Page 35: Assembly Language Programming Part 2

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

Page 36: Assembly Language Programming Part 2

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

Page 37: Assembly Language Programming Part 2

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

Page 38: Assembly Language Programming Part 2

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

Page 39: Assembly Language Programming Part 2

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

Page 40: Assembly Language Programming Part 2

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

Page 41: Assembly Language Programming Part 2

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

Page 42: Assembly Language Programming Part 2

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

Page 43: Assembly Language Programming Part 2

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

Page 44: Assembly Language Programming Part 2

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

Page 45: Assembly Language Programming Part 2

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

Page 46: Assembly Language Programming Part 2

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

Page 47: Assembly Language Programming Part 2

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

Page 48: Assembly Language Programming Part 2

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

Page 49: Assembly Language Programming Part 2

Operands Algorithm Example

Labeladdress

Page 50: Assembly Language Programming Part 2

Operands Algorithm Example

Labeladdress