Assembly Language Conditionals and Loops

To look at the various loop structures and conditional control structures; and have an understanding how they are implemented in assembly language.

Introduction

When working with control structures and loops in assembly language, the concepts are not much different than when working with them in high level languages. As with all aspects of assembly language, we are dealing with these same high level ideas at a much lower level. We have loops, and conditional control_structures to use just as in high level languages.

mov rcx, low ; rcx register commonly used for index counter
lp: . ; statements inside for loop
inc rcx ; increment index by one
cmp rcx, high ; compare rcx to high
jle lp ; jump if less than or equal to lp (lp can be whatever loop label you want)
. ; code after the for loop
mov rcx, low ; rcx register commonly used for index counter
lp: . ; statements inside for loop
add rcx, increment ; increment index by one
cmp rcx, high ; compare rcx to high
jle lp ; jump if less than or equal to lp (lp can be whatever loop label you want)
. ; code after the for loop
mov rcx, high ; rcx register commonly used for index counter
lp: . ; statements inside for loop
add rcx, decrement ; increment index by one - use add if decrement is a negative value, sub if decrement is positive
cmp rcx, low ; compare rcx to high
jge lp ; jump if greater than or equal to lp (lp can be whatever loop label you want)
. ; code after the for loop
mov rcx, some_value
lp: cmp rcx, test_value ; do our pre_test for some value to be less than test_value
jg ext ; jump to label ext (exit) if greater than test_value
. ; code to execute when some_value is less than test_value
inc rcx ; increment rcx, we could use an add rcx, increment value if increment is more than one
jmp lp ; jump back to lp - this is okay until we get into superscalar programming, then it can be iffy.
ext: . ; code to execute after pre-test loop done
mov rcx, some_value
lp: . ; code to execute when some_value is less than test_value
. ; code to execute when some_value is less than test_value
inc rcx ; increment rcx, we could use an add rcx, increment value if increment is more than one
cmp rcx, test_value ; do our pre_test for some value to be less than test_value
jle lp ; jump to label lp if less than or equal to test_value
. ; code to execute after pre-test loop done
cmp , some_value ; compare a register or memory location with some_value
j_cc ext ; jump to ext when condition code is evaluated to false
. ; code to execute when comparision and condition code are true
ext: . ; code to continue after if block
cmp , some_value ; compare a register or memory location with some_value
j_cc fls ; jump to fls when condition code is evaluated to false
. ; code to execute when comparision and condition code are true
jmp ext ; jump over the false block (label with fls) to ext
fls: . ; code to execute if condition is false
ext: . ; code to continue after if/else blocks
cmp , some_value ; compare a register or memory location with some_value
j_cc c1 ; jump to fls when condition code is evaluated to false
. ; code to execute when comparision and condition code are true
jmp ext ; jump over the false block (label with fls) to ext
c1: cmp , some_value2 ; compare a register or memory location with some_value
j_cc c2 ; jump to fls when condition code is evaluated to false
jmp ext
c2: cmp , some_value3 ; compare a register or memory location with some_value
j_cc fls ; jump to fls when condition code is evaluated to false
jmp ext
fls: . ; code when all conditions are false
ext: . ; code to continue after if/else blocks
cmp , value1 ; switch variable value in reg or memory, same reg/memory location through out entire block
jg c2 ; test case #2 if greater than, a jne would not allow us to fall through to next condition
; if needed
. ; code to execute for case #1
[jmp ext] ; this is an optional instruction, if we have a c/c++ style break; keyword, jump to ext, otherwise
; do not jump and have it fall through to the next case
c2: cmp , value2 ; same as above, but testing for value2.
jg c3 ; jump to c3 if value2 is smaller than
[jmp ext]
c3: cmp , value3 ; same as above, but testing for value3.
jg dft ; jump to default if greater
[jmp ext]
dft: . ; default code block
ext: . ; code after switch block