Assembly: If (MASM/TASM)

Contributor Icon Contributed by William_Wilson Date Icon March 5, 2006  
Tag Icon Tagged: Computer programming

How to implement the in assembly, the effect of the if statement, which is available to other languages.


Being that Assembly is a low level language, as opposed to the high level most programmers are used to (eg: Java, C, C++, Perl, etc), it doesn’t have all the same features that these languages have.. like an if statement, but it can be implemented in a more baic way.

assembly language runs top down, from label to label through the main of the program, only varying upon jumps etc (described in full later), we can take advantage of this to implement our if.

Example:

.MODEL SMALL
.STACK 100H
.DATA
result_msg DB 'true inside if statement',0

.486
.CODE
INCLUDE io.mac

main PROC
.STARTUP

start_go:
mov Ax,0
cmp Ax,0
jne done

putStr result_msg

done:
.EXIT

main ENDP
END main

The ouptput will be: true inside if statement, try changing the value placed into Ax, anything else and it will print out nothing. The if condition will not be satisfied.

Combining the compare and jump on not equal or any other dynamic jump is the basis of an if statment.

Questions/Comments: william_a_wilson@hotmail.com
-William. ยง (marvin_gohan)

Previous recipe | Next recipe |
 
blog comments powered by Disqus