Study with the several resources on Docsity
Earn points by helping other students or get them with a premium plan
Prepare for your exams
Study with the several resources on Docsity
Earn points to download
Earn points by helping other students or get them with a premium plan
Community
Ask the community for help and clear up your study doubts
Discover the best universities in your country according to Docsity users
Free resources
Download our free guides on studying techniques, anxiety management strategies, and thesis advice from Docsity tutors
Prof. Abhay Aggrawal provided this lab handout for assistance at Birla Institute of Technology and Science for lab of Assembly Language Programming. It includes: Conditional, Complex, Jumps, JCXZ, JECXZ, Bit, Settings, Test, Code, Jumps, Value, Zero
Typology: Exercises
1 / 6
To use different Boolean and Comparison instructions To use Conditional Jump instructions for different applications
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 most direct way to change program control from one location to another. At the processor level, jumps work by changing the value of the IP (Instruction Pointer) register to a target offset and, for far jumps, by changing the CS register to a new segment address. Jump 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 jump instruction and the size of the operand determine the assembler’s encoding of the instruction. The longer the distance, the more bytes the assembler uses to code the instruction.
2.3. conditional Jumps
The most common way to transfer control in assembly language is to use a conditional jump. This is a two-step process:
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 jump. The jump statement can be any one of 30 conditional-jump instructions. A conditional-jump 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 remaining conditional jumps in the processor’s repertoire 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 example. Shared names assemble to exactly the same machine instruction, so you may choose whichever mnemonic seems more 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 flag overflow 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:
; Uses JO to handle overflow condition add ax, bx ; Add two values jo overflow ; If value too large, adjust
; Uses JNZ to check for zero as the result of subtraction sub ax, bx ; Subtract mov cx, Count ; First, initialize CX jnz skip ; If the result is not zero, continue call zhandler ; Else do special case
As the second example shows, the jump does not have to immediately follow the instruction that alters the flags. Since MOV does not change the flags, it can appear between the SUB instruction and the dependent jump.
There are three categories of conditional jumps: u Comparison of two values u Individual bit settings in a value u Whether a value is zero or nonzero
2.6. Jumps Based on Comparison of Two Values
The CMP instruction is the most common way to test for conditional jumps. It compares two values without changing either, then sets or clears the processor flags according to the results of the comparison. Internally, the CMP instruction is the same as the SUB instruction, except that CMP does not change the destination operand. Both set flags according to the result of the subtraction.
You can compare signed or unsigned values, but you must choose the subsequent conditional jump to reflect the correct value type. For example, JL (Jump if Less Than) and JB (Jump if below) may seem conceptually similar, but a failure to understand the difference between them can result in program bugs. Table given below shows the correct conditional jumps for comparisons of signed and unsigned values. The table shows the zero, carry, sign, and overflow flags as ZF, CF, SF, and OF, respectively.
Conditional Jumps Based on Comparisons of Two Values Signed Comparisons Instruction Jump if True
Unsigned Comparisons Instruction Jump if True JE ZF = 1 JE ZF = 1 JNE ZF = 0 JNE ZF = 0 JG/JNLE ZF = 0 and SF = OF JA/JNBE CF = 0 and ZF = 0 JLE/JNG ZF = 1 or SF OF JBE/JNA CF = 1 or ZF = 1 JL/JNGE SF OF JB/JNAE CF = 1 JGE/JNL SF = OF JAE/JNB CF = 0
The mnemonic names of jumps always refer to the comparison of CMP ’s first operand (destination) with the second operand (source). For instance, in this example, 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 specific bits in an operand are on or off (set or clear), and sets the zero flag accordingly. The TEST instruction is the same 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 often a mask in which the test bits 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 jump based on whether a particular register contains a value of zero. We’ve seen how the JCXZ instruction jumps 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
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 numbers (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 smallest 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 compare the top two elements on the second pass. Likewise, each succeeding pass through the array compares 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
Address Content Address Content
Original String Sorted String
Question# 2: Write a program that checks whether a value is present in array or not and prints “Found”, “Not Found” respectively.
Question#3: Write a program that inserts a value in an array at the position mentioned.