W1154-Annex-1

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



Memory Sequences[edit]

SEQUENCE 1

   MemorySequence-3.png

SEQUENCE 2

   MemorySequence-2.png

SEQUENCE 3

   MemorySequence-4.png

Sequence 4

   MemorySequence-1.png

Flowcharts[edit]

FLOWCHART 1

   Flowchart-3.png

FLOWCHART 2

   Flowchart-1.png

FLOWCHART 3

   Flowchart-2.png

Assembly Language Listing[edit]

LISTING 1

        .global _start

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


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

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

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:
        # jump to statements at beginning of loop
        jmp     statements                 # 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
messageStatement:
        .ascii  "Statement\n"
messageAlternative:
        .ascii  "Alternative\n"

LISTING 2

        .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"

LISTING 3

        .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!"

LISTING 4

        .global _start

        .text
_start:
        # assume we begin with the number 5
        mov     $5, %rax                   # move 5 into the register rax

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

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

consequent:
        # write(1, messageConsequent, 10)
        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     $10, %rdx                  # number of bytes
        syscall                            # invoke operating system to do the write
        jmp     afterAlternative

alternative:
        # write(1, messageAlternative, 11)
        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     $11, %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

messageConsequent:
        .ascii  "Consequent"
messageAlternative:
        .ascii  "Alternative"