Conditional Branching in MASM
We have shown that the jump instructions can be used to implement branches and looops. However these instructions are difficult for beginners to apply as a substitute of high level branching statements.
In high level languages we use these basic structures -
IF-THEN
The if-then structure is the most common type of branching statement used. The pseudocode for IF-THEN is :
IF CONDITION IS TRUE
THEN EXECUTE TRUE BRANCH STATEMENTS
END_IF
MASM example:
Suppose we want to code the following pseudocode in MASM
IF AX< 0
THEN REPLACE AX WITH ITS COMPLIMENTED VALUE
END_IF
The MASM code will be:
CMP AX,0
JNL END_IF
NEG AX
END_IF:
IF-THEN-ELSE
The if-then-else structure is the expansion of most common type of branching statement IF-THEN. The pseudocode for IF-THEN-ELSE is :
IF Condition is TRUE
THEN
Execute true-branch statements
ELSE
Execute false-branch statements
END_IF
Lets explain by applying the following pseudocode in MASM
IF AL<= BL
THEN
DISPLAY THE CHARACTER IN AL
ELSE
DISPLAY THE CHARACTER IN BL
END_IF
The MASM code will be -
MOV AH,2
CMP AL,BL
JNBE ELSE_
MOV DL,AL
JMP DISPLAY_
ELSE_:
MOV DL,BL
DISPLAY_:
INT 21H
END_IF:
CASE
The case in high-level language is a multiway branch structure that tests a register, variable or expression for particular values or range of values. The pseudocode is :
CASE expression
value_1: statement_1
value_2: statement_2
value_3: statement_3
value_4: statement_4
.................................
value_n: statement_n
END_CASE
Consider the following pseudocode in MASM
IF AX a negative number
THEN put -1 in BX
IF AX contains 0
THEN put 0 in BX
IF AX contains a positive number
THEN put 1 in BX
The MASM code will be -
CMP AX,0
JL NEGATIVE_
JG POSITIVE
MOV BX,0
JMP END_CASE
NEGATIVE_:
MOV BX,-1
JMP END_CASE
POSITIVE_:
MOV BX,1
JMP END_CASE
END_CASE:
further reading:
http://www.cs.princeton.edu/
Assembly Language Programming and Organization of IBM PC
Ytha Yu
Charles Marut
Showing posts with label Control. Show all posts
Showing posts with label Control. Show all posts
Sunday, January 24, 2010
Assembly Language Part 5
Looping instructions in Assembly
In assembly a loop is a sequence of instructions that is repeated like the loop to the left.
The final instruction (Tail) jumps (touches) to the first instruction (Head).
The basic form of looping in Assembly is
REPEAT UNTIL
REPEAT
STATEMENTS
UNTIL CONDITION
For example if we want to read all input characters given by the user until he/she inputs
MOV AH,1
REPEAT_:
INT 21H
CMP AL,' '
JNE REPEAT_
Some other frequently used looping structures are-
FOR LOOP
In these kind of lop structures the instructions are repeated for known number of times. pseudocode -
FOR loop_count times DO
statements
END_FOR
In assembly we use the instruction LOOP to implement for loop. The general form is-
LOOP header_label
Here header label is the name or label of the instruction block to be executed in the loop. The LOOP instruction uses CX as the counter so it has to be defined first. When the program executes LOOP it first checks if CX = 0 if true it continues to execute the next instructions. If the CX !=0 then control is transferred to the "header_label" , at the same time it also decreases the value of CX by 1.
Let us implement it in the program to print 100 '*' in assembly.
MOV CX,80
MOV AH,2
MOV DL,'*'
TOP: ; header_label
INT 21H
LOOP TOP
WHILE LOOP
These kind of loops depends on a condition. pseudocode -
WHILE condition DO
statements
END_WHILE
The condition is checked at the top of the loop. If the condition is satisfied the the statements are executed. If false the program skips to whatever follows. It is possible that the condition is never satisfied (even at the initial stage) in that case the body is never executed.
Lets count the number of character in the input line.
MOV DX,0
MOV AH,1
INT 21H
WHILE_:
CMP AL,13
JE END_WHILE
INC DX
INT 21H
JMP WHILE_
END_WHILE:
The basic difference between WHILE and REPEAT loop is that using WHILE loop gives opportunity to skip the whole process even at the initial stage if the condition is false.
On the other hand the statements of REPEAT has to be done atleast once. REPEAT is useful on other cases as it leads to shorter coding.
Further reading:
Assembly Language Programming and Organization of IBM PC
Ytha Yu
Charles Marut
http://www.cs.princeton.edu/
What comes to your mind after watching the loop at the left? That's it the Head is Touching the Tail at some point.
The final instruction (Tail) jumps (touches) to the first instruction (Head).
The basic form of looping in Assembly is
REPEAT UNTIL
REPEAT
STATEMENTS
UNTIL CONDITION
For example if we want to read all input characters given by the user until he/she inputs
MOV AH,1
REPEAT_:
INT 21H
CMP AL,' '
JNE REPEAT_
Some other frequently used looping structures are-
FOR LOOP
In these kind of lop structures the instructions are repeated for known number of times. pseudocode -
FOR loop_count times DO
statements
END_FOR
In assembly we use the instruction LOOP to implement for loop. The general form is-
LOOP header_label
Here header label is the name or label of the instruction block to be executed in the loop. The LOOP instruction uses CX as the counter so it has to be defined first. When the program executes LOOP it first checks if CX = 0 if true it continues to execute the next instructions. If the CX !=0 then control is transferred to the "header_label" , at the same time it also decreases the value of CX by 1.
Let us implement it in the program to print 100 '*' in assembly.
MOV CX,80
MOV AH,2
MOV DL,'*'
TOP: ; header_label
INT 21H
LOOP TOP
WHILE LOOP
These kind of loops depends on a condition. pseudocode -
WHILE condition DO
statements
END_WHILE
The condition is checked at the top of the loop. If the condition is satisfied the the statements are executed. If false the program skips to whatever follows. It is possible that the condition is never satisfied (even at the initial stage) in that case the body is never executed.
Lets count the number of character in the input line.
MOV DX,0
MOV AH,1
INT 21H
WHILE_:
CMP AL,13
JE END_WHILE
INC DX
INT 21H
JMP WHILE_
END_WHILE:
The basic difference between WHILE and REPEAT loop is that using WHILE loop gives opportunity to skip the whole process even at the initial stage if the condition is false.
On the other hand the statements of REPEAT has to be done atleast once. REPEAT is useful on other cases as it leads to shorter coding.
Further reading:
Assembly Language Programming and Organization of IBM PC
Ytha Yu
Charles Marut
http://www.cs.princeton.edu/
Subscribe to:
Posts (Atom)