Assembly - Conditions

Prahlad Godara ------ From DOOSEEP

Conditional instructions are used in assembly language to control the flow of a program. Conditional statements in assembly language are used through the program to direct the computer to make decisions when certain conditions are given. These decisions are made only when pre-stated conditions are true or false, depending on what the programmer has in mind.


Conditional instructions in assembly language - cms jmp lable

Conditional statements are used in assembly language to control the flow of the program. i.e. which code to run next if the pre-stated conditions are true or false. Like - if, elas, jmp, loop etc.

jump Conditions

There are 2 types of jump statements in assembly language. - Unconditional jump, Conditional jump.

  1. Unconditional jump - There is no condition before this instruction. When this instruction is run, the program runs the code block specified by this instruction first, skipping the further code. This is done by JMP instruction.
  2. Conditional jump - This instruction is preceded by a condition. When this instruction is run, it checks the condition, then depending on whether the condition is true or false, the program runs the next code or directed code block. This is done by JMP instruction. This is done by a set of jump instructions j < condition>.

CMP Instruction

The CMP instruction compares two operands or numeric data fields. It is commonly used in conditional execution. This instruction basically compares one operand by subtracting another to see if the operands are equal or not. It does not disturb the destination or source operands. It is used with conditional jump instructions to make decisions.

Syntax
 CMP   destination, source

For example

 INC	EDX
  CMP	EDX, 10	; Compares whether the counter has reached 10
  JLE	LP1     ; If it is less than or equal to 10, then jump to LP1

CMP is often used to compare whether the counter value has reached the number of times the loop needs to be run.


What is a label in assembly language code?

What is a label in assembly language code? A label is a symbol that represents the memory address of an instruction or data. Addresses can be PC-relative, register-relative, or absolute. Labels are local to the source file unless you make them global using the export directive. You can think of it like an "id" in markup language.

Unconditional jump

Unconditional jump is done by JMP keyword. This JMP instruction jumps from one code block to another without any condition.

Syntax
 JMP	label

For example

 MOV  AX, 00    ; Initializing AX to 0
  MOV  BX, 00    ; Initializing BX to 0
  MOV  CX, 01    ; Initializing CX to 1
  L20:
  ADD  AX, 01    ; Increment AX
  ADD  BX, AX    ; Add AX to BX
  SHL  CX, 1     ; shift left CX, this in turn doubles the CX value
  JMP  L20       ; repeats the statements


Conditional jump

Conditional execution often involves transfer of control to an instruction address. If certain specified conditions are met in a conditional jump, the control flow is transferred to the target instruction. There are many conditional jump instructions based on condition and data.

In nasm assembly language, we can divide conditional jump instructions into three categories on the basis of usage. Arithmetic, logical, special uses.

Arithmetic Jump Instruction

Instruction Description Flags tested
JE/JZ Jump Equal or Jump Zero ZF
JNE/JNZ Jump not Equal or Jump Not Zero ZF
JG/JNLE Jump Greater or Jump Not Less/Equal OF, SF, ZF
JGE/JNL Jump Greater/Equal or Jump Not Less OF, SF
JL/JNGE Jump Less or Jump Not Greater/Equal OF, SF
JLE/JNG Jump Less/Equal or Jump Not Greater OF, SF, ZF

Logical Jump Instructions

Instruction Description Flags tested
JE/JZ Jump Equal or Jump Zero ZF
JNE/JNZ Jump not Equal or Jump Not Zero ZF
JA/JNBE Jump Above or Jump Not Below/Equal CF, ZF
JAE/JNB Jump Above/Equal or Jump Not Below CF
JB/JNAE Jump Below or Jump Not Above/Equal CF
JBE/JNA Jump Below/Equal or Jump Not Above AF, CF

Special uses and check the value of the flags

Instruction Description Flags tested
JXCZ Jump if CX is Zero none
JC Jump If Carry CF
JNC Jump If No Carry CF
JO Jump If Overflow OF
JNO Jump If No Overflow OF
JP/JPE Jump Parity or Jump Parity Even PF
JNP/JPO Jump No Parity or Jump Parity Odd PF
JS Jump Sign (negative value) SF
JNS Jump No Sign (positive value) SF
Syntax
j<condition>

For example

 CMP	AL, BL
  JE	EQUAL
  CMP	AL, BH
  JE	EQUAL
  CMP	AL, CL
  JE	EQUAL
  NON_EQUAL: ...
  EQUAL: ...

Exampal

The following program displays the largest of three variables. The variables are double-digit variables. The three variables num1, num2 and num3 have values 37, 22 and 41, respectively −


  section	.text
  global _start         ;must be declared for using gcc

_start:	                 ;tell linker entry point
  mov   ecx, [num1]
  cmp   ecx, [num2]
  jg    check_third_num
  mov   ecx, [num2]
  
 check_third_num:

  cmp   ecx, [num3]
  jg    _exit
  mov   ecx, [num3]
  
 _exit:
  
  mov   [largest], ecx
  mov   ecx,msg
  mov   edx, len
  mov   ebx,1	;file descriptor (stdout)
  mov   eax,4	;system call number (sys_write)
  int   0x80	;call kernel
 
  mov   ecx,largest
  mov   edx, 2
  mov   ebx,1	;file descriptor (stdout)
  mov   eax,4	;system call number (sys_write)
  int   0x80	;call kernel
   
  mov   eax, 1
  int   80h

section	.data
  
  msg db "The largest digit is: ", 0xA,0xD 
  len equ $- msg 
  num1 dd '37'
  num2 dd '22'
  num3 dd '41'

segment .bss
  largest resb 2  

When the above code is compiled and executed, it produces the following result −

 41


Tags- education, Assembly - Conditions, assembly language, type of Assembly Conditions , example program in assembly language, Conditions Operators, This blogcreates content similar to stackoverflow geeks for geeks tutorialspoint w3schools and dooseep