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
JMP instructions implement conditional instructions.
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 DST SRC
DST (destination operand) can be in register or memory. SRC (source operand) can be a constant in register or memory.
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
The conditional jump instructions evaluate if the condition is satisfied through flags, then jump to the label indicated in the instruction.
|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 .text global _start ;must be declared for using gcc _start: ;tell linker entry point mov rcx,10 ;loop runs until the value of rcx is 0 mov rax, '1' ;rax holds the character that needs to be printed l1: cmp rcx,0 ;compare rcx with 0 jle l2 ; jump to l2 if rcx less thn or equal to 0 mov [num], rax ;value in rax moved to variable num mov rax, 4 ;4 is the system call number for the write system call mov rbx, 1 ;1 is the file descriptor for the output stream push rcx ;value of rcx pushed to stack and stored here temporarily ;rbx, rcx and rdx are arguments to the write system call mov rcx, num ;num moved to rcx, as rcx contains the character that will be printed mov rdx, 1 ;1 is the size (1 byte) of the character that is to be printed int 0x80 ;interrupt that executes the write system call in kernel mode mov rax, [num] ;the first character has been output, value of num moved to eax sub rax, '0' ;converts character in eax to decimal inc rax ;increments decimal value in eax by 1 add rax, '0' ;converts decimal back to character pop rcx ;pops back value of ecx temporarily stored on the stack dec rcx ;loops, value of ecx auto decremented jmp l1 l2: mov eax,1 ;system call number (sys_exit) int 0x80 ;call kernel section .bss num resb 1
The above program prints the first ten numbers.
raxregister stores the iteration number, and the
rcxregister stores the total number of iterations and is initialized to 10.
l1block represents the loop code. At each iteration, the iteration count in the
rcxregister is pushed onto the stack. The current value of
raxis moved to
rcx, and a write system call is made, which prints the number on the screen.
raxis incremented, and the iteration count is popped from the stack into the
rcxregister. The program then decrements
rcxand jumps to
rcxwith zero, and jumps to the
rcxis less than or equal to zero. The program terminates after entering the
View all Courses