Conditional Structures


A conditional structure is one or more conditional expressions that trigger a choice between different logical branches. Each branch causes a different sequence of instructions to execute. There are many ways to implement a compound expression. The following code shows some of the ways.

  if (opnd1 is even)
     and (opnd2 is odd) 
  then print (opnd1 - opnd2)
  else print (opnd1 + opnd2)
  ebx = 0
  while (opnd < 8)
     ebx += opnd
  print ebx
 INCLUDE Irvine32.inc
 .data
  count = 10
  dummy  WORD  ?

 .code
 main PROC
  ; Generate the opnd1.
  call   Randomize
  mov    eax, count
  call   RandomRange
  call   WriteDec
  mov    ecx, eax
 
  ; Generate the opnd2 < opnd1.
  cmp    eax, 0
  jne    L0
  inc    eax
 L0:
  call   RandomRange
  mov    edx, eax
  test   cl, 00000001b
 

; opnd1 is even test dl, 00000001b jz L1 ; opnd2 is odd ; The THEN part mov al, '-' call WriteChar sub ecx, edx jmp L2 ; The ELSE part L1: mov al, '+' call WriteChar add ecx, edx L2: mov eax, edx call WriteDec mov al, '=' call WriteChar mov eax, ecx call WriteDec exit main ENDP END main
 INCLUDE Irvine32.inc
 .data
  count = 10
  TRUE  = 1
  FALSE = 0
  First  BYTE  TRUE

 .code
 main PROC
  call   Randomize
  mov    ebx, 0

  ; Generate the opnd.
 L1:
  mov    eax, count
  call   RandomRange
  mov    ecx, eax
  test   cl, 11111000b
 

; opnd < 8 cmp First, TRUE je L2 mov al, '+' call WriteChar L2: mov First, FALSE mov eax, ecx call WriteDec add ebx, ecx jmp L1 ; Check the condition 0 = 0 L3: cmp First, TRUE jne L4 mov eax, 0 call WriteDec L4: mov al, '=' call WriteChar mov eax, ebx call WriteDec exit main ENDP END main
An Execution Example
An Execution Example

    8-1=7


    5+7+5+3+3=23


The italic white text with navy background is entered by users.





      “I’m not arguing, I’m just explaining why I’m right.”    
      ― Unknown