Docsity

Logical Instructions and Conditional Processing-Assembly Language, Microprocessors and Computer Architecture-Lab Mannual, Exercises of Computer Architecture and Organization

Toggle sidebar
bg1
2
EXPERIMENT NO 07
Logical Instructions and Conditional Processing
1. Objectives
To use different Boolean and Comparison instructions
To use Conditional Jump instructions for different applications
2. Basic Information
There are no high-level logic structures in the 80x86 instruction set, but you can im plement any
logic structure, no matter how complex, using a combination of comparison and jumps.
2.1. Jumps
Jumps are the m ost direct way to change program control from one location to another. At the
processor level, jum ps work by changing the value of the IP (Instruction Pointer) register to a
target offset and, for far jum ps, by changing the CS register to a new segm ent address. Jum p
instructions fall into only two categories: conditional and unconditional.
2.2. Unconditional Jumps
The JMP instruction transfers control unc onditionally to another instruction. JMP’s single
operand contains the address of the target instruction.
Unconditional jumps skip over code that should not be executed, as shown here:
; Handle one case
label1: .
.
.
jmp continue
; Handle second case
label2: .
.
.
jmp continue
.
.
.
continue:
The distance of the target from the jum p instruction and the size of the operand determ ine the
assembler’s encoding of the instruction. The longe r the distance, the m ore bytes the assem bler
uses to code the instruction.
docsity.com
pf3
pf4
pf5

Partial preview of the text

Download Logical Instructions and Conditional Processing-Assembly Language, Microprocessors and Computer Architecture-Lab Mannual and more Exercises Computer Architecture and Organization in PDF only on Docsity!

EXPERIMENT NO 07

Logical Instructions and Conditional Processing

1. Objectives

 To use different Boolean and Comparison instructions

 To use Conditional Jump instructions for different applications

2. Basic Information

There are no high-level logic structures in the 80x86 instruction set, but you can implement any

logic structure, no matter how complex, using a combination of comparison and jumps.

2.1. Jumps

Jumps are the m ost direct way to change program control from one location to another. At the

processor level, jum ps work by changing the value of the IP (InstructionPointer) register to a

target offset and, for far jum ps, by changing theCS register to a new segm ent address. Jum p

instructions fall into only two categories: conditional and unconditional.

2.2. Unconditional Jumps

The JMP instruction transfers control unconditionally to another instruction. JMP ’s single

operand contains the address of the target instruction.

Unconditional jumps skip over code that should not be executed, as shown here:

; Handle one case label1:. . . jmp continue

; Handle second case label2:. . . jmp continue . . . continue:

The distance of the target from the jum p instruction and the size of the operand determ ine the

assembler’s encoding of the instruction. The longer the distance, the m ore bytes the assem bler

uses to code the instruction.

2.3. conditional Jumps

The m ost com mon way to transfer control in assembly language is to use a conditional jum p.

This is a two-step process:

1. First test the condition.

2. Then jump if the condition is true or continue if it is false.

All conditional jumps except two ( JCXZ and JECXZ ) use the processor flags for their criteria.

Thus, any statement that sets or clears a flag can serve as a test basis for a conditional jum p. The

jump statem ent can be any one of 30 conditional-jump instructions. A conditional-jum p

instruction takes a single operand containing the target address. You cannot use a pointer value

as a target as you can with unconditional jumps.

2.4. Jumping Based on the CX Register

JCXZ and JECXZ are special conditional jumps that do not consult the processor flags. Instead,

as their names imply, these instructions cause a jump only if the CX or ECX register is zero. The

use of JCXZ and JECXZ with program loops is covered in the next section, “Loops.”

2.5. Jumping Based on the Processor Flags

The rem aining conditional jum ps in the processor’srepertoire all depend on the status of the

flags register. As the following list shows, several conditional jumps have two or three names —

JE (Jump if Equal) and JZ (Jump if Zero), for exam ple. Shared names assemble to exactly the

same m achine instruction, so you m ay choose whichever m nemonic seem s m ore appropriate.

Jumps that depend on the status of the flags register include:

Instruction Jumps if

JC/JB/JNAE Carry flag is set

JNC/JNB/JAE Carry flag is clear

JBE/JNA Either carry or zero flag is set

JA/JNBE Carry and zero flag are clear

JE/JZ Zero flag is set

JNE/JNZ Zero flag is clear

JL/JNGE Sign  overflow flag flag

JGE/JNL Sign flag = overflow flag

JLE/JNG Zero flag is set or sign  overflow

JG/JNLE Zero flag is clear and sign = overflow

JS Sign flag is set

JNS Sign flag is clear

JO Overflow flag is set

JNO Overflow flag is clear

The conditional jumps in the preceding list can follow any instruction that changes the processor

flags, as these examples show:

The m nemonic nam es of jum ps always refer to the com parison of CMP ’s first operand

(destination) with the second operand (source). For instance, in this exam ple, JG tests whether

the first operand is greater than the second.

cmp ax, bx ; Compare AX and BX jg next1 ; Equivalent to: If ( AX > BX ) goto next jl next2 ; Equivalent to: If ( AX < BX ) goto next

2.7. Jumps Based on Bit Settings

The individual bit settings in a single value can also serve as the criteria for a conditional jump.

The TEST instruction tests whether specif ic bits inan operand are on or off (set or clear), and

sets the zero flag accordingly.

The TEST instruction is the sam e as the AND instruction, except that TEST changes neither

operand. The following example shows an application of TEST.

.DATA

bits BYTE? .CODE . . ; If bit 2 or bit 4 is set, then call task_a ; Assume "bits" is 0D3h 11010011 test bits, 10100y ; If 2 or 4 is set AND 00010100 jz skip1 ; -------- call task_a ; Then call task_a 00010000 skip1: ; Jump taken . . .

; If bits 2 and 4 are clear, then call task_b ; Assume "bits" is 0E9h 11101001 test bits, 10100y ; If 2 and 4 are clear AND 00010100 jnz skip2 ; -------- call task_b ; Then call task_b 00000000 skip2: ; Jump taken

The source operand for TEST is of ten a m ask in which the testbits are the only bits set. The

destination operand contains the value to be tested. If all the bits set in the mask are clear in the

destination operand, TEST sets the zero flag. If any of the flags set in the mask are also set in the

destination operand, TEST clears the zero flag.

2.8. Jumps Based on a Value of Zero

A program often needs to jum p based on whether aparticular register contains a value of zero.

We’ve seen how the JCXZ instruction jum ps depending on the value in the CX register. You

can test for zero in other data registers nearly as efficiently with the OR instruction. A program

can OR a register with itself without changing the register’s contents, then act on the resulting

flags status. For example, the following example tests whether BX is zero:

or bx, bx ; Is BX = 0? jz is_zero ; Jump if so

This code is functionally equivalent to:

cmp bx, 0 ; Is BX = 0? je is_zero ; Jump if so

but produces smaller and faster code, since it does not use an immediate number as an operand.

The same technique also lets you test a register’s sign bit:

or dx, dx ; Is DX sign bit set? js sign_set ; Jump if so

3. Experimental Work

Write a program using a bubble sort algorithm to sort a string in ascending order stored in the

data segment. Use a string of at least 10 num bers (8-bit each) in length and the starting string

should not be in ascending order.

The implementation of the bubble sort consists of a simple double loop. The first iteration of the

inner for loop moves through the string from bottom to top, comparing adjacent characters. If the

lower indexed character' s value is higher than its higher-indexed neighbor, then the two values

are swapped. Once the sm allest value is encountered, this process will cause it to "bubble" up to

the top of the array. The second iteration of the inner for loop repeats this process. Since the

smallest value reached the top of the array on the first pass, there is no need to com pare the top

two elem ents on the second pass. Likewise, each succeeding pass through the array com pares

adjacent elements, looking at one less value than the preceding pass.

The bubble sort algorithm is similar to the following:

For i = 0 to string_length - 2

for j = string_length - 2 to i

if string[j] > string[j + 1] then

swap string[j] with string[j + 1];

Show the contents of original and sorted string in the following memory map

Question# 2: Write a program that checks whether a value is present in array or not and

prints “Found”, “Not Found” respectively.

Address Content Address Content

Original String Sorted String