Difference between revisions of "W1152 While Loop"

From Coder Merlin
Line 83: Line 83:
# What is the purpose of the ''Jump'' instruction after the ''consequent''?
# What is the purpose of the ''Jump'' instruction after the ''consequent''?
}}
}}
== Swift ==
<syntaxhighlight lang="swift">
var x = 8                                                                                                                                                                                                                                                                                                                                     
while x > 4 {                                                                                                                                                                                                                                                                                                                                 
    print("consequent")                                                                                                                                                                                                                                                                                                                       
    x -= 1                                                                                                                                                                                                                                                                                                                                   
}                                                                                                                                                                                                                                                                                                                                             
print("alternative") 
</syntaxhighlight>


== Key Concepts ==
== Key Concepts ==
== Exercises ==
== Exercises ==
== References ==
== References ==

Revision as of 17:17, 12 January 2020

Within these castle walls be forged Mavens of Computer Science ...
— Merlin, The Coder
Infinite loop BSOD

Prerequisites[edit]

General Loop[edit]

General Loop

Loops are the general term for executing a defined segment of code zero or more times, where the number of iterations is dependent upon test conditions within the loop.

In the case of a general loop, we can see that the three statements in the figure on the right will be repeated. However, without a test condition this loop would theoretically execute forever and is formally termed an infinite loop. To be useful, a test condition is required in order to inform the CPU when the loop should be exited. Thus, loops generally have two distinct parts:

  • A test condition which informs the CPU when the loop should exit
  • A body which is the code which is repeated for each iteration of the loop


CautionWarnIcon.png

Each iteration of the loop must perform some action, albeit slight, to move the loop closer to completion. If this does not occur, the loop would execute an infinite number of times.

Flowchart[edit]

A flowchart representing the above requirements follows:

   While Loop Flowchart.png

Assembly Language[edit]

Let's consider how the above construct is implemented in assembly language:

   While Loop Assembly.png

The condition, the Boolean test, is evaluated. If the test evaluates to false, a jump is executed to the alternative, otherwise execution continues with the consequent. At the end of the consequent, a jump is executed to the Boolean test.

Carefully study the following assembly language example:

        .global _start

        .text
_start:
        # load the loop control variable
        mov     (loopControlVariable), %r8  # load %r8 from a variable

test:
        # we then test to see if the number is greater than 4
        cmp     $4, %r8                    # subtract 4 from the contents of register r8

        # evaluate test condition, jump conditionally (less-than or equal)
        jle      alternative

consequent:
        # write(1, messageConsequent, 11)
        mov     $1, %rax                   # system call 1 is write
        mov     $1, %rdi                   # file handle 1 is stdout
        mov     $messageConsequent, %rsi   # address of string to output
        mov     $11, %rdx                  # number of bytes
        syscall                            # invoke operating system to do the write

        # alter loop control variable, one step closer to termination
        dec     %r8

        # jump to test condition at beginning of loop
        jmp     test                       # jump unconditionally

alternative:
        # write(1, messageAlternative, 12)
        mov     $1, %rax                   # system call 1 is write
        mov     $1, %rdi                   # file handle 1 is stdout
        mov     $messageAlternative, %rsi  # address of string to output
        mov     $12, %rdx                  # number of bytes
        syscall                            # invoke operating system to do the write

afterAlternative:
        # exit(0)
        mov     $60, %rax                  # system call 60 is exit
        xor     %rdi, %rdi                 # we want return code 0
        syscall                            # invoke operating system to exit

loopControlVariable:
        .quad    8
messageConsequent:
        .ascii  "Consequent\n"
messageAlternative:
        .ascii  "Alternative\n"
ObserveObserveIcon.png
Observe, Ponder, and Journal: Section 1
  1. Why is the test executed prior to the loop?
  2. Compare this diagram to that of the conditional. What are the similarities? What are the differences?
  3. What is the purpose of the Jump instruction after the consequent?

Swift[edit]

var x = 8                                                                                                                                                                                                                                                                                                                                      
while x > 4 {                                                                                                                                                                                                                                                                                                                                  
    print("consequent")                                                                                                                                                                                                                                                                                                                        
    x -= 1                                                                                                                                                                                                                                                                                                                                     
}                                                                                                                                                                                                                                                                                                                                              
print("alternative")

Key Concepts[edit]

Exercises[edit]

References[edit]