Difference between revisions of "W1151 Conditional and Flow Chart"

From Coder Merlin
Line 5: Line 5:
Until now, the programs which we have observed and written exhibited sequential flow.  That is, they had a single entry point and a single exit point.  Instruction execution began at the entry point, instructions were executed sequentially, and then the program execution completed.  While this type of flow is useful in some simple cases in the general case the flow will be more complex.
Until now, the programs which we have observed and written exhibited sequential flow.  That is, they had a single entry point and a single exit point.  Instruction execution began at the entry point, instructions were executed sequentially, and then the program execution completed.  While this type of flow is useful in some simple cases in the general case the flow will be more complex.


== Sequential Execution ==
{{ResponsiveImage|[[File:Sequential Execution.png]]}}
{{ResponsiveImage|[[File:Sequential Execution.png]]}}
In the example above, the program execution begins at address 0x8020 and completes at address 0x8026.  All instructions are executed sequentially.
In the example above, the program execution begins at address 0x8020 and completes at address 0x8026.  All instructions are executed sequentially.


As a quick review of a real-world example, let's examine the Hello, World! program in assembly language:
Let's examine a Hello, World! program in assembly language:


<syntaxhighlight lang="gas">
<syntaxhighlight lang="gas">
Line 15: Line 16:
         .text
         .text
_start:
_start:
         # write(1, message, 13)
         # write(1, messageA, 6)
         mov    $1, %rax                # system call 1 is write
         mov    $1, %rax                # system call 1 is write
         mov    $1, %rdi                # file handle 1 is stdout
         mov    $1, %rdi                # file handle 1 is stdout
         mov    $message, %rsi         # address of string to output
         mov    $messageA, %rsi         # address of string to output
         mov    $12, %rdx               # number of bytes
        mov    $6, %rdx                # number of bytes
        syscall                        # invoke operating system to do the write
 
        # write(1, messageB, 6)
        mov    $1, %rax                # system call 1 is write
        mov    $1, %rdi                # file handle 1 is stdout
        mov    $messageB, %rsi        # address of string to output
         mov    $6, %rdx               # number of bytes
         syscall                        # invoke operating system to do the write
         syscall                        # invoke operating system to do the write


Line 26: Line 34:
         xor    %rdi, %rdi              # we want return code 0
         xor    %rdi, %rdi              # we want return code 0
         syscall                        # invoke operating system to exit
         syscall                        # invoke operating system to exit
message:
 
         .ascii  "Hello, World!"
messageA:
         .ascii  "Hello "
messageB:
        .ascii  "World!"
</syntaxhighlight>
</syntaxhighlight>
== Flowcharts ==
A '''flowchart''' is a type of diagram that enables us to visualize the ''flow'' of execution through a program.  As such, flowcharts representing an entire program generally have a single ''entry point'', a single ''exit point'', and one or more ''processes''.  (In this case the word ''process'' is not the same as an operating system process, but represents the general usage of the word, i.e. "do something".)  Throughout this experience, we'll introduce several symbols that are typically used in flowcharts.
{| class="wikitable"
  ! Symbol
  ! Meaning
  |-
  | [[File:Flowchart Line.svg|thumb|Flowchart Line]] || Describes the direction of flow through the diagram, particularly when the flow is not in the traditional top-to-bottom order
  |}


== Topic Headers ==
== Topic Headers ==

Revision as of 17:05, 4 January 2020

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

Prerequisites[edit]

Background[edit]

Introduction[edit]

Until now, the programs which we have observed and written exhibited sequential flow. That is, they had a single entry point and a single exit point. Instruction execution began at the entry point, instructions were executed sequentially, and then the program execution completed. While this type of flow is useful in some simple cases in the general case the flow will be more complex.

Sequential Execution[edit]

In the example above, the program execution begins at address 0x8020 and completes at address 0x8026. All instructions are executed sequentially.

Let's examine a Hello, World! program in assembly language:

        .global _start

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

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

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

messageA:
        .ascii  "Hello "
messageB:
        .ascii  "World!"

Flowcharts[edit]

A flowchart is a type of diagram that enables us to visualize the flow of execution through a program. As such, flowcharts representing an entire program generally have a single entry point, a single exit point, and one or more processes. (In this case the word process is not the same as an operating system process, but represents the general usage of the word, i.e. "do something".) Throughout this experience, we'll introduce several symbols that are typically used in flowcharts.

Symbol Meaning
Flowchart Line
Describes the direction of flow through the diagram, particularly when the flow is not in the traditional top-to-bottom order

Topic Headers[edit]

Key Concepts[edit]

Exercises[edit]

References[edit]