Trusted answers to developer questions
Trusted Answers to Developer Questions

Related Tags

assembly

What are conditions in assembly language?

Ayesha Naeem

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.

Compare and jump 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.

Conditional jump instrcutions

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

Examples

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.

  • The rax register stores the iteration number, and the rcx register stores the total number of iterations and is initialized to 10.
  • The 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.
  • At the beginning of the next iteration, the program compares the value of 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.

RELATED TAGS

assembly

CONTRIBUTOR

Ayesha Naeem
Copyright ©2022 Educative, Inc. All rights reserved
RELATED COURSES

View all Courses

Keep Exploring