Wednesday 22 November 2017

Compare and Jump instructions-8086 assembly

Compare instruction-CMP

CMP works like sub instruction with the only difference that it doesn’t change operands. Subtracts source operand from destination operand and updates flags only.
Example:
CMP AX, BX ; Performs (AX-BX) and updates flags. Source and destination remains unchanged.

CMP instruction: Effect on flags

MOV AX, 5
CMP AX, 5; Which flag will be set?
Zero flag will be set, ZF=1, also PF=1;


MOV AX, 5
MOV CX, 10
CMP AX, CX; Which flag will be set?
Subtracting 10 from 5 requires borrow, so carry flag will be set, CF=1; SF=1;

MOV DL, 15
MOV BL, 4
CMP DL, BL; Which flag will be set?
None

MOV AL, 127
MOV BL, -2
cmp AL, BL; Which flag will be set?
Subtracting -2 from 127 makes it 129, max positive number in one byte is 127? So overflow occurs. OF=1; SF=1 (negative result)

Compare and Jump instructions

A compare instruction (CMP) without jump instruction is meaningless. Why? Because there is no use of comparison if you’re not taking a decision after comparison. In a jump instruction, decision is taken based on flag bits. Those flag bits might have been updated previously by an ALU instruction or a CMP instruction. Decisions are taken using conditional jump instructions.

JUMP instructions

Jump instructions can be used to start executing instructions from any address location within memory. Two main types of jump instructions
  1. Unonditional jump instructions
  2. Conditional jump instructions

Unconditional jump

An unconditional jump instruction forces processor to start executing instructions from a specific address unconditionally. For example if you write JMP 03h, it assigns address 0003 to instruction pointer and processor starts executing instructions from physical address formed via logical pair CS:IP. If CS=0100, for example, processor will start executing instructions from physical memory address 010003.

The address in JMP instruction can be specified via a label. Each label has a unique address. If you write instruction JMP L1, it will force processor to execute instructions from label L1. Following code elaborates this concept:

JMP start                               ; unconditional JMP instruction
VAR1 DB 5
VAR2 DW 77

start:                                      ; start: is a label/address in a program
MOV AX, 2;
ADD AX, 1;
.EXIT

In this example we have used unconditional jump instruction "JMP start" at the beginning to skip executing variable declaration statements as executable instructions.

Conditional jump

A conditional jump instruction forces processor to start executing instructions from specified address when a condition met. In a conditional jump instruction, two parts are specified:

  1. Combination of characters to specify which flags bits will be checked?
  2. An address from which instructions will be executed if condition comes out to be true.
Example
JC Lab1                               ; Read as Jump if carry; 

Processor starts executing instructions from label “Lab1” if carry flag happens to be "1" in previous instruction, i.e., CF=1. 

Program to check if a number is even or odd? 

Task: Check if number N1 is even put 0 in AL else put FF in AL?

MOV AL, N1
SHR AL,1                         ; Shifts right AL by one bit, CF becomes 1;
JC L1                                ; Jump to label L1if CF=1 (odd number)
MOV AL, 0 ;
JMP L2                             ; skip executing instructions specified at L1
L1:
MOV AL, 0xFF
L2:
.exit
N1 db 7;

Other conditional jump instructions

JC ;Jump if carry flag set 
JNC ;Jump if not carry

JZ ;Jump if zero flag set
JNZ ;Jump if zero flag not set

JE ;Jump if zero flag set
JNE ;Jump if zero flag not set

JS ;Jump if sign flag is set
JNS ;Jump if sign flag not set

JP ;Jump if parity flag is set
JNP ;Jump if not parity

JO ;Jump if overflow;
JNO ;Jump if not overflow

Conditional jumps for signed operand comparison

JG Jump if greater
JNG Jump if not greater
JGE Jump if greater or equal
JNGE Jump if not greater or equal

JL Jump if less
JNL jump if not less
JLE Jump if less or equal
JNLE jump if not less or equal

Task: If (N1>N2) copy N1 into DX else copy N2 in DX?

Start:
MOV AX, N1;
CMP AX, N2
JG L1                                                         ; jump if AX is greater than N2
MOV DX, N2;                                           ; else statement
JMP skip;
L1: MOV DX, N1
skip:
.EXIT
N1 DW 5;
N2 DW 7;

Conditional jumps for unsigned operand comparison

JA Jump if above
JNA Jump if not above
JAE Jump if above or equal
JNAE Jump if not above or equal

JB Jump if below
JNB Jump if not below
JBE Jump if below or equal
JNBE jump if not below or equal

Example explaining the difference between signed and unsigned number comparison

Code1:
After executing the following code what value would be placed in AX register?

MOV AL, 5
MOV BL, -1
CMP AL, BL
JG L1                             ; signed statement; jump if greater …
MOV AX, 0
JMP Exit
L1:
MOV AX, 1
Exit:

Code2:
After executing the following code what value would be placed in AX register?

MOV AL, 5
MOV BL, -1
CMP AL, BL
JA L1                              ; unsigned statement; jump if above …
MOV AX, 0
JMP Exit
L1:
MOV AX, 1
Exit:

Above two codes are similar with just difference of one conditional jump statement: JG or JA. JG takes operand as signed numbers and performs comparison. In signed notation, 5 is greater than -1, so jump is taken and value saved in AX register is 1. Whereas, JA condition, treats operands and unsigned numbers. In unsigned notation, 5 is less than -1 (FF=255 in decimal), so jump is not taken and value 0 is stored in AX register. 

No comments:

Post a Comment

Effect of ADD and SUB instructions on Flag register-Assembly Language for 8086 processor

Flag Register NOTE: ONE OR MORE FLAGS MAY GET SET OR RESET AFTER AN ARITHMETIC LOGIC INSTRUCTION MOV INSTRUCTION DOESN’T AF...