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.
Here is an implementation of the if-then structure in assembly language.
The following is an example of an if-then-else structure implementation in assembly language.
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.
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.
CASE StatementA 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.
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.
WHILE StructureThe WHILE loop tests a condition before executing the loop body. The loop body is executed repeatedly as long as the condition is true.
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.