Implementing High-Level Language Decision Structures 

Modern High-level languages provide a variety of decision structures. These structures include selection structures such as if constructs and iterative structures such as while and for loop constructs. Assembly language does not provide these structures directly. However, it provides several basic instructions that can be used to construct these high-level selection and iterative structures. In this section, we demonstrate how these structures can be implemented in assembly language.

IF-Then and IF-THEN-ELSE Structures

The following is a pseudo code that shows the if-then structure used in a high level language.

Example: IF-THEN Structure in HLL.
 int n;

 if (n>7) 
    do_it();
 ...	

Here is an implementation of the if-then structure in assembly language.

Example: Implementing IF-THEN Structure in Assembly.
	...
	;if (n>7)
  	CMP n, 7
  	JNA skip
	;then-part
  	CALL do_it
	;end if
skip:
	...

The following is an example of an if-then-else structure implementation in assembly language.

Example: IF-THEN-ELSE Structure in HLL.
 char n;
 ...
 if (n=='7')
    do_it();
 else
    do_that();
 ...

Example: Implementing IF-THEN-ELSE Structure in Assembly.
  	;if (n == '7')
   	CMP n,'7'
   	JNE else_
 	;then-part
   	CALL do_it
   	JMP SHORT endif
else_:	CALL do_that
endif:	...

IF Structure with Logical Operators

The next example demonstrates the implementation of the IF structure with an AND logical operator. It tests whether ch is a lower case character or not. If it is a lower case it converts it to upper case.

Example: IF Structure with an AND logical operator in HLL.
  	if ((ch >= 'a') && (ch <= 'z'))
             ch = ch - 32 ;

Example: Implementing IF Structure with an AND logical operator in Assembly.
  	        CMP ch, 'a'
	        JB NotLowerCase
	        CMP ch, 'z'
	        JA NotLowerCase
LowerCase:
               SUB ch, 32
NotLoweCase:
		  ...

In the next example, we demonstrate the IF structure with an OR logical operator. The function display() is called if the entered character is either an upper case 'Y' or lower case 'y' character.

Example: IF Structure with an OR logical operator in HLL.
  	if ((ch = 'y') || (ch = 'Y'))
              display();

Example: Implementing IF Structure with an OR logical operator in Assembly.
               CMP ch, 'y'
	        JE Skip
	        CMP ch, 'Y'
	        JNE Done
Skip:          call display
Done:
		...

CASE Statement

A CASE statement is a multi-way branch structure that tests a register, variable, or expression for particular values or a range of values. The general form is a s follows:

CASE expression
	values-1: statements-1
	values-2: statements-2
	.
	.
	.
	values-n: statements-n
EndCase

In this structure, the expression is tested and if its value is a member of the test values-i, then statements-i are executed. It is assumed that values-1,..., values-n are disjoint.

The following example illustrates the case statement in HLL and its implementation in assembly.

Example: Case Statement in HLL.
CASE AX
	<0: BX=-1
	=0: BX=0
	>0: BX=1
EndCase

Example: Implementing Case Statement in assembly.
; case AX
	   CMP AX, 0	; test AX
	   JL Negative	; AX < 0
	   JE Zero	; AX=0
	   MOV BX, 1	; AX>0
	   JMP EndCase
Negative: MOV BX,-1
	   JMP EndCase
Zero:	   MOV BX, 0
EndCase:

Note that in the above example one CMP instruction is needed, because jump instructions do not affect the flags. We next give another example illustrating the CASE statement.

Example: Another Case Statement in HLL.
CASE A
	1, 3: display 'o'
	2, 4: display 'e'
EndCase

Example: Implementing another Case Statement in assembly.
; case AL
; 1, 3
	   CMP AL, 1	 
	   JE Odd	 
	   CMP AL, 3	 
	   JE Odd
; 2, 4
	   CMP AL, 2
	   JE Even
	   CMP AL, 4
	   JE Even
; not 1..4
	   JMP EndCase	
Odd:	   MOV DL, 'o'
	   JMP Display
Even:	   MOV DL, 'e'
Display:  MOV AH, 2
	   INT 21H
EndCase:

WHILE Structure

The WHILE loop tests a condition before executing the loop body. The loop body is executed repeatedly as long as the condition is true.

Example: WHILE Structure in HLL.
  int n;
  ...
  while (n>0) 
	n = n-2;
  ...

Example: Implementing WHILE Structure in Assembly.
	...
  	;while (n>0)
while:  CMP n,0
  	JLE endwhile
	;loop-body
	SUB n,2
	JMP while
endwhile:
	...

Repeat-Until Structure

The Repeat-Until or Do-While structures test the repeat condition after executing the loop body. Thus, the loop body is executed at least once. The next example is a program that reads a string of characters and stores it in a table until a carriage return character is encountered, implementing a Repeat-Until Structure.

Example: Implementing Repeat-Until Structure in Assembly.
              XOR SI, SI ; initialize table index 
              MOV AH, 1 ; reading a character 
Repeat:	       INT 21H
              MOV TABLE[SI], AL
	       INC SI
	       CMP AL, 13 ; until condition 
              JNE Repeat
	       ...