Control Instructions

Execution in assembly can be controlled by several branching instructions. These instructions can change the execution flow of the program or simply execution could jump from one place(procedure/instructions) to other.

Before moving to the types of jump, lets look into CMP instruction.

CMP instruction : It compares two operands and generally used in conditional execution. It basically subtract one operand from other to check whether operands were equal or not. On subtracting, if result comes to 0, then they are equal. Otherwise unequal.

cmp ebx, 10 ; jle abcd→ This means, if ebx is less than or equal to 10, jump to abcd function/address.

TEST instruction : It performs bitwise AND operation between 2 operands. As an outcome, ZF, SF, PF flags will be modified while result of bitwise AND is ignored. A test of something with itself is often used to check for a NULL value. test eax, eax. This require fewer bytes and less cpu cycle rather than comparing eax with 0. If eax is 10, test eax, eax (AND eax, eax) will result in 10. And when its 0, we will have 0 as output. So to check if eax is 0, we directly use test eax, eax and if it is zero, ZF is set.

These jumps are divided into:

  • Unconditional Jump

    This is performed by JMP instruction. It transfer control for (may be) to re-execute set of instructions. Similar to goto function of C lanuguage. No condition is required, it will directly execute.

  • Conditional Jump

    This is performed by J<x><x> (J<condition>) set of instructions depending upon the condition. They transfer control by breaking the sequential flow. A few such instructions are JE (jump if equal), JNE (jump if not equal), JZ (jump if zero), JNZ (jump if not zero), etc etc.

Lets understand the Unconditional and Conditional jump. (control.nasm)

  • In the start, an Unconditional jump is taken. jmp printHW

  • Inside printHW, a condition is checked, cmp edx,0xc. Inside edx we have mlen i.e, 0xd(13), which can be seen in the instruction above cmp. Hence, jmp edx, 0xc will result in NOT EQUAL

  • jne checks if comparision resulted in NOT EQUAL, if yes(they are unequal), then it will get executed otherwise ignored.

  • jne otherFunc, will get execute, transferring the execution to otherFunc set of instructions, skipping the Hello World! part.

  • Eventually, printing ‘Unconditional Jump Taken!’

Last updated