Conditions in assembly language control the execution of loops and branches.
The program evaluates the conditional instruction and executes certain instructions based on the evaluation. The CMP
and JMP
instructions implement conditional instructions.
The CMP
instruction takes two operands and subtracts one from the other, then sets O, S, Z, A, P, and C flags accordingly. CMP
discards the result of subtraction and leaves the operands unaffected.
The following syntax is used for CMP
instructions:
CMP DST SRC
DST (destination operand) can be in register or memory. SRC (source operand) can be a constant in register or memory.
The JMP
instruction transfers the program control to a new set of instructions indicated by the label in the instruction.
The following syntax is used for JMP
instructions:
JMP label
The conditional jump instructions evaluate if the condition is satisfied through flags, then jump to the label indicated in the instruction.
Instruction | Description | Data Type | Flags |
---|---|---|---|
JE/JZ | Jump equal/Jump zero | Signed/Unsigned | ZF |
JNE/JNZ | Jump not equal/Jump not zero | Signed/Unsigned | ZF |
JG/JNLE | Jump greater/Jump not less or equal | Signed | OF, SF, ZF |
JGE/JNL | Jump greater or equal /Jump not less | Signed | OF, SF |
JL/JNGE | Jump less /Jump not greater or equal | Signed | OF, SF |
JLE/JNG | Jump less or equal /Jump not greater | Signed | OF, SF, ZF |
JA/JNBE | Jump above/Jump not below or equal | Unigned | CF, ZF |
JAE/JNB | Jump above or equal /Jump not below | Unsigned | CF |
JB/JNAE | Jump below /Jump not above or equal | Unsigned | CF |
JBE/JNA | Jump below or equal /Jump not above | Unsigned | AF, CF |
The following code snippet illustrates the use of unconditional and conditional jump instructions:
section .textglobal _start ;must be declared for using gcc_start: ;tell linker entry pointmov rcx,10 ;loop runs until the value of rcx is 0mov rax, '1' ;rax holds the character that needs to be printedl1:cmp rcx,0 ;compare rcx with 0jle l2 ; jump to l2 if rcx less thn or equal to 0mov [num], rax ;value in rax moved to variable nummov rax, 4 ;4 is the system call number for the write system callmov rbx, 1 ;1 is the file descriptor for the output streampush rcx ;value of rcx pushed to stack and stored here temporarily;rbx, rcx and rdx are arguments to the write system callmov rcx, num ;num moved to rcx, as rcx contains the character that will be printedmov rdx, 1 ;1 is the size (1 byte) of the character that is to be printedint 0x80 ;interrupt that executes the write system call in kernel modemov rax, [num] ;the first character has been output, value of num moved to eaxsub rax, '0' ;converts character in eax to decimalinc rax ;increments decimal value in eax by 1add rax, '0' ;converts decimal back to characterpop rcx ;pops back value of ecx temporarily stored on the stackdec rcx ;loops, value of ecx auto decrementedjmp l1l2:mov eax,1 ;system call number (sys_exit)int 0x80 ;call kernelsection .bssnum resb 1
The above program prints the first ten numbers.
rax
register stores the iteration number, and the rcx
register stores the total number of iterations and is initialized to 10.l1
block represents the loop code. At each iteration, the iteration count in the rcx
register is pushed onto the stack. The current value of rax
is moved to rcx
, and a write system call is made, which prints the number on the screen.rax
is incremented, and the iteration count is popped from the stack into the rcx
register. The program then decrements rcx
and jumps to l1
using the jmp
command.rcx
with zero, and jumps to the l2
block if rcx
is less than or equal to zero. The program terminates after entering the l2
block.Free Resources