Friday, 25 January 2013

8086 microprocessor Programming (Part 7)


JUMP INSTRUCTIONS

OBJECTIVES

  • To become more familiar with the 8086 compare instruction.
  • To study the 8086 instructions for implementing conditional logic structures.


INTRODUCTION:

Here are no conditional logic structures in the 8086 assembly language. But we can implement almost any structure using a combination of statements. A logic structure is simply a group of conditional statements working together. The WHILE structure, for instance, has an implied IF statement at its beginning that evaluates a condition:

Structured Unstructured

DO WHILE; L1 IF (a>=b)THEN
<statement-1> jump to L2
<statement-2>   ENDIF
<statement-1>
ENDDO <statement-2>
:
jump to L1
L2: (continue here)
The angle brackets (<>) around the word statement identify a program statement. Two steps are involved in executing IF statement. First, an arithmetic or comparison instruction sets one or more flages based on its result. Second, a conditional jump instruction may cause the CPU to jump to a new address. The instructions used belong to one of two groups:

Group 1: Comparison and arithmetic instructions, in which the CPU sets individual flags according to the result.
Group 2: Conditional jump instructions, in which the CPU takes action based on the flags.

The instructions from the two groups work in tandem: An instruction from Group 1 is executed affecting the flags, then a conditional jump instruction from Group 2 executes, based on the value of one of the flags. In the following example the JZ instruction jumps to ‘next’ if AL = 0.
Cmp a1 , 0
Jz next
  next :               ;  label
Conditional Jump Instructions: A Conditional jump instruction, transfers control to a destination address when a flag condition is true. The syntax is:
J cond destination
Here cond refers to a flag condition, identifying the state of one or more flags. For example:

C Carry flag set
NC Carry flag not set (clear)
Z Zero flag set
NZ Zero flag not set (clear)

We have already seen that flags are set by arithmetic, comparison, and Boolean instructions. Each conditional jump instruction checks one or more flags returning a result of true or false. If the result is true the jump is taken; otherwise, the program does nothing and continues to the next instruction. Note that these instructions themselves do not affect any of the six status flags.

EXPERIMENT: Here are four parts of this experiment, each containing a relatively smaller program. It will be convenient for user to put the main body of the program in the program template defined earlier in experiment 4. This will help you save time writing four times the common MASM directives and initialization instructions.

PART-1: The following program is taken from chapter 7 of [1]. It converts each character type at the keyboard is uppercase. The program ends when ENTER is pressed. Write it in a text editor , assemble and link it and then run it from DOS prompt to verify.
Page, 132
Title  uppercase display program
dosseg
.model small
.stack 100h
carr_ret equ 0Dh ; ASCII code of carriage return code
main proc
label _ 1
mov ah. 08h ; input a character, no echo
int 21h
cmp al, carr_ret ; ENTER pressed?
Je label _ 3 ; yes: quit
cmp al, ‘a’ ; character < ‘a’?
Jb label _ 2 ; if below, display it
cmp al,   ‘z’ ; character > ‘z’ ?
ja label _ 2 ;  if above, display it
sub al , 20h ; no , subtract 20h from ASCII code
label _ 2
mov ah, 02h ; function display character
mov dl , al ; character is in Dl
int 21h ; call DOS interrupt
;  get another character
jmp label _ 1 ; unconditional jump
label _ 3
mov ; ax , 4c00h
int 21h
main endp
data ; variables here
end main
  1. What happens if you enter a character other than from a-z?
  2. If you want to make a program which converts an uppercase characters in to lower case, what changes will be required in the above program?
  3. Which flags are tested the execution of ‘je’ , ‘jb’ , and ‘ja’ instructions. Use DEBUG to find the answer.


Part – II: The following program is adopted from chapter 7 of [1]. It finds the largest and smallest signed numbers in an array of integers. The numbers input don’t have any suffix (e,g, h, b, or d). This means that they will be treated as decimal numbers. These numbers will be converted by the MASM in to proper hex numbers. A negative number will be converted in to its 2’s complement from by the MASM. Run the program in DEBUG.
page, 132
title Largest and Smallest Signed Numbers
dosseg
.model small
.stack 100h
.code
num_digits equ 6
main proc
mov ax . @data ; initialize the DS register
mov ds , ax
mov di , offset  array
mov ax , [di]
mov largest . ax ; initialize largest
mov smallest . ax ; initialize smallest
mov cx , num digits-1
label_1
add di , 2h ; point to next number
mov ax , [di] ; get next number
cmp ax , smallest ; [DI] >= smallest?
jge label_2 ; yes : skip
mov smallest , ax ;no:  move  [DI] to smallest
label_2:
cmp ax , largest ; [DI]  <=  largest?
jle label_3 ; yes : skip
mov largest . ax ; no:  move [DI] to largest
label_3:
loop  label_1 ; repeat until  CX =0
mov ax , 4C00h
int 21h
main endp
 data ; variable here
array dw -1 , 2000, 32767, 500,0
largest dw ?
smallest dw ?
end main
  1. Note down from DEBUG the hex equivalent of the above six numbers.
  2. Modify the program, so that it finds the largest and smallest of the following array of numbers: 200, -300, -7699, -3289, -2278, -9910, -5436, 98, 10.
  3. Why are the contents of CX registers initialized at num_digits – 1 and not num_digits?
  4. What is the range of integers over which this program works ? What happens if the array has a number out of this range.

Part –III:
  • write a program that sorts, in descending order, a given list of integers in the range of

-32, 768  to +32,767. First make a flow chart with the help of following algorithm:
Call the elements of the second number, A(0), A(1), A(2), …., A(n). Take the first number in array, which is A(0), and compare it with the second number, A(1),. If A(0) is greater than A(1), the two numbers are swapped otherwise they are left alone. Next A(0) is compared with A(2) and on the basis of the result of this comparison, they are either swapped or left alone. This sequence is repeated until A(0) has been compared with all numbers up through A(n). When this is complete, the smallest number will be in A(0) position. New A(1) must be compared with A(2) through A(n) in the same way . After this is done, the second smallest number will be in A(1) position. The same procedure is repeated for A(2) through A(n-1) to complete the soft.

REFERENCES:

  1. Kip R. Irvine. Assembly Language for the IBM-PC Macmillan Publishing Company, 1990.
  2. Avtar Singh and Walter A. Triebel, The 8086 and 80286 Microprocessors, Hardware, Software, and Interfacing. Prentice-Hall Inc, 1990.


1 comment:

  1. Thank you for sharing Amazing Blog. It's providing very useful guideline for Engineering students.
    get more: Microprocessor Programming


    ReplyDelete