The 8086 Flow control instructions CMP compare instruction

  • Slides: 26
Download presentation
The 8086 Flow control instructions

The 8086 Flow control instructions

CMP: compare instruction • cmp operand 1, operand 2 – Operand 1 , operand

CMP: compare instruction • cmp operand 1, operand 2 – Operand 1 , operand 2 can be register, memory location, constant value • • cmp mem, mem illegal If operand 1==operand 2 zero flag = 1 If operand 1 >= operand 2 sign flag = 0 If operand 1 < operand 2 sign flag = 1

Flag register

Flag register

JUMP instruction • Unconditional jump – JMP label • label represent an instruction location

JUMP instruction • Unconditional jump – JMP label • label represent an instruction location in the code • Conditional jump – (JE, JNE, JL, JNL, JLE, JG, JNG, JGE ) – JE label

Conditional JUMP • JE(jump if equlal), JZ(jump if zero) : – if (operand 1

Conditional JUMP • JE(jump if equlal), JZ(jump if zero) : – if (operand 1 == operand 2 ) if (flag zero == 1) • JNE(jump if not equlal), JNZ(jump if not zero): – if (operand 1 != operand 2) if ( flag zero == 0) • JL(jump if less), JS(jump if sign): – if (operand 1 < operand 2) if ( flag sign == 1) • JGE(jump if greater or equal), JNS(jump if not less) – if (operand 1 >= operand 2) if ( flag sign == 0) • JLE(jump if less or equal): – if (operand 1 <= operand 2) if ( flag sign == 1 || flag zero == 1) • JG(jump if greater): – if (operand 1 > operand 2) if ( flag sign == 0 && flag zero == 0)

example (if statement) IF (a==b) c = d ; mov ax, a cmp ax,

example (if statement) IF (a==b) c = d ; mov ax, a cmp ax, b jne End. Of. If mov bx, d mov c, bx End. Of. If:

example (if else statement) IF (a==b) c = d ; else b = b

example (if else statement) IF (a==b) c = d ; else b = b + 1; mov ax, a cmp ax, b jne Else. Blk mov bx, d mov c, bx jmp End. Of. If Else. Blk: inc b End. Of. If:

example 1(loop statement) int x=10; while(x>0) x--; mov cl, 10 loop 1: cmp cl,

example 1(loop statement) int x=10; while(x>0) x--; mov cl, 10 loop 1: cmp cl, 0 jle End. Loop dec cl jmp loop 1 End. Loop: -----for(int x=10; x>0; x--);

example 2(loop statement) int i=0; while(i<100) { var 1++; i++; } mov cl, 0

example 2(loop statement) int i=0; while(i<100) { var 1++; i++; } mov cl, 0 loop 1: cmp cl, 100 jge Loop. Done inc var 1 inc cl jmp loop 1 Loop. Done: ----for(int i=0; i<100; i++) var 1++;

example 3(loop statement) For(int i = 1 ; i<= 8; i++) k = k

example 3(loop statement) For(int i = 1 ; i<= 8; i++) k = k + i - j; mov cl, 1 FLP: mov ax, k add ax, cl sub ax, j mov k, ax inc cl cmp cl, 8 jle FLP

example 4(loop statement) For(int i = 8 ; i>=1; i++) k = k +

example 4(loop statement) For(int i = 8 ; i>=1; i++) k = k + i – j; mov cl, 8 FLP: mov ax, k add ax, cl sub ax, j mov k, ax dec cl cmp cl, 0 jg FLP

Loop instruction • Loop label equivelent to dec cx cmp cx, 0 jg label

Loop instruction • Loop label equivelent to dec cx cmp cx, 0 jg label

example 1(loop instruction) for(int i=0; i<100; i++) var 1++; mov cl, 100 loop 1:

example 1(loop instruction) for(int i=0; i<100; i++) var 1++; mov cl, 100 loop 1: inc var 1 loop 1 mov cl, 0 loop 1: inc var 1 inc cl cmp cl, 100 jl loop 1 Loop. Done:

example 2(loop instruction) for(int i=start; i>stop; i--) var 1++; loop 1: mov cl, start

example 2(loop instruction) for(int i=start; i>stop; i--) var 1++; loop 1: mov cl, start sub cl, stop inc var 1 loop 1 mov cl, start lop: inc var 1 dec cl cmp cl, stop jg lop Skip. For:

example 2 (if statement) IF ((X > Y) && (Z < T)) { C

example 2 (if statement) IF ((X > Y) && (Z < T)) { C = D; mov } cmp --------jng IF (X > Y) mov { cmp IF(Z < T) jnl { mov C = D; mov } End. Of. If: } ax, x ax, y End. Of. If ax, z ax, t End. Of. If bx, d c, bx

example 3 (if statement) IF ((X > Y) && (Z < T))|| (A!=B) {

example 3 (if statement) IF ((X > Y) && (Z < T))|| (A!=B) { mov ax , a C = D; cmp ax, b jne Do. If } mov ax, x cmp ax, y jng End. Of. If mov ax, z cmp ax, t jnl End. Of. If Do. If: mov bx, d mov c, bx End. Of. If:

example 3 ( Check if a given number is odd or even ) int

example 3 ( Check if a given number is odd or even ) int num ; cin>>num if((num%2)==0) { cout<<”it is an even ”; } else { cout<<”it is an even ”; } code to enter a number and al, 00000001 b jnz Odd. Num code to Print ‘even’ jmp exit Odd. Num: code to Print ‘odd’ exit:

Print a character on screen using interrupt mov ah, 02 h mov dl, ’A’

Print a character on screen using interrupt mov ah, 02 h mov dl, ’A’ int 21 h

Read a Character using interrupt mov ah, 1 int 21 h • The character

Read a Character using interrupt mov ah, 1 int 21 h • The character that have been read from keyboard will be stored in register AL

Print a number within 0 -9 mov add mov int dl, 5 dl, 30

Print a number within 0 -9 mov add mov int dl, 5 dl, 30 h ah, 02 h 21 h OR mov dl, ’ 5’ mov ah, 02 h int 21 h

Example (print) • Print characters from a-z mov dl, 'a' lop: cmp dl, 'z'

Example (print) • Print characters from a-z mov dl, 'a' lop: cmp dl, 'z' jg exit mov ah, 2 int 21 h inc dl jmp lop exit:

Questions 1 -Convert one of the following C if statement to assembly language a)

Questions 1 -Convert one of the following C if statement to assembly language a) if (x==0) && ((y-2) > 1) { y --; } b) if ((X *5)>100) && (Y / 4))<100)) { C = D; }

Questions 2 -Trace DL and Y a. model small . data stop db 10

Questions 2 -Trace DL and Y a. model small . data stop db 10 start db 5 y db 0 . code mov ax, @data mov ds, ax mov cl, stop sub cl, start jl Skip. For loop 1: inc y loop 1 Skip. For: mov ah, 02 mov dl, y or dl, 30 h int 21 h y=0 y=1, y=2, y=3, y=4, y=5 dl = 35 h

Questions 2 -Trace DL and Y b- mov dl, 5 mov al, 10 mov

Questions 2 -Trace DL and Y b- mov dl, 5 mov al, 10 mov bl, 10 cmp al, bl jne Else. Blk add dl, 3 jmp End. Of. If Else. Blk: dec dl End. Of. If: dl = 5 dl = 8

Questions 3. Correct the following codes: a mov ax, a ; if else statement

Questions 3. Correct the following codes: a mov ax, a ; if else statement cmp ax, b jne Else. Blk mov bx, d mov c, bx ----- jmp End. Of. If Else. Blk: inc b End. Of. If: b mov al, 10 loop 1: cmp al, 0 jle End. Loop dec al ----- jmp loop 1 End. Loop: ; loop statement

Questions 3. Correct the following codes: cmov dl, stop sub dl, start jl Skip.

Questions 3. Correct the following codes: cmov dl, stop sub dl, start jl Skip. For loop 1: inc var 1 loop 1 ----- Skip. For: d- mov dl, 5 ; print 5 ----- add dl, 30 h mov ah, 02 h int 21 h