Let's proceed by discussing how the microprocessor actually transfers control to another part of the program by updating the IP-register. So lets take a look at the following branch example:

 0030  B8 0000  top:  mov ax,0
 0033  75 03            jne arnd
 0035  BB FFFE          mov bx,-2
 0038  90       arnd:   nop
 0039  EB F5          jmp top

First you will notice that the above example branches in both directions. That is, the jne arnd is a forward branch and the jmp top is a backward branch. Both use short labels which means that we can branch -128 to 127 from the end of the branch instruction.

Q#1: Consider the instruction jne arnd. How do we come up with the 03?

Q#2: Consider the instruction jmp top. How do we come up with the F5?

Problem: If the instruction jne arnd is encountered and the ZF flag is 0, this means that the jump will be taken. Calculate the next IP value to be executed.

Answer: We know that the beginning offset of the jne arnd is 0033 and the beginning offset of the next instruction is 0035. When you calculate the next instruction to be executed, you calculate from the end of the jump instruction or the beginning of the instruction that follows the jump. Both offset values are the same. The only thing that is different is your perception of where you are starting from.

Since the IP has a value of 0035 after the jne instruction, we must do the following:
(1) sign extend the value 03
(2) add this value to the IP

IP                   0035
sign extended value  0003
-------------------------
new IP value         0038

There you have it.

P#1: Show how the IP value will be modified when the jmp top instruction is executed. Remember, this is an unconditional jump and not based on any flag value.

Double Alternative IF

In general, the double alternative if has the following form:

if(condition) then
  true statement(s)
else
  false statement(s)
endif

P#2: Write an assembly language program segment that given two 16-bit signed values in the variables X and Y will print the largest of the two values. Code your solution using a single-alternatvie IF and then recode your solution using a double-alternative IF. Which one do your think is faster? Which one uses less memory?

P#3: Write an assembly language program segment that will find and print the sum of the first 50 positive odd values. Make this code as efficient as possible.


©Douglas J. Ryan ryandj@pacificu.edu