W1154-Annex-1

From Coder Merlin
Jump to navigation Jump to search


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

 1        .global _start
 2
 3        .text
 4_start:
 5        # load the loop control variable
 6        mov     (loopControlVariable), %r8  # load %r8 from a variable
 7
 8
 9statements:
10        # write(1, messageStatement, 10)
11        mov     $1, %rax                   # system call 1 is write
12        mov     $1, %rdi                   # file handle 1 is stdout
13        mov     $messageStatement, %rsi    # address of string to output
14        mov     $10, %rdx                  # number of bytes
15        syscall                            # invoke operating system to do the write
16
17        # alter loop control variable, one step closer to termination
18        dec     %r8
19
20test:   
21        # we then test to see if the number is greater than 4
22        cmp     $4, %r8                    # subtract 4 from the contents of register r8
23
24        # evaluate test condition, jump conditionally (less-than or equal)
25        jle      alternative
26
27consequent:
28        # jump to statements at beginning of loop
29        jmp     statements                 # jump unconditionally
30
31alternative:
32        # write(1, messageAlternative, 12)
33        mov     $1, %rax                   # system call 1 is write
34        mov     $1, %rdi                   # file handle 1 is stdout
35        mov     $messageAlternative, %rsi  # address of string to output
36        mov     $12, %rdx                  # number of bytes
37        syscall                            # invoke operating system to do the write
38
39afterAlternative:
40        # exit(0)
41        mov     $60, %rax                  # system call 60 is exit
42        xor     %rdi, %rdi                 # we want return code 0
43        syscall                            # invoke operating system to exit
44
45loopControlVariable:
46        .quad    8
47messageStatement:
48        .ascii  "Statement\n"
49messageAlternative:
50        .ascii  "Alternative\n"

LISTING 2

 1        .global _start
 2
 3        .text
 4_start:
 5        # load the loop control variable
 6        mov     (loopControlVariable), %r8  # load %r8 from a variable
 7
 8test:
 9        # we then test to see if the number is greater than 4
10        cmp     $4, %r8                    # subtract 4 from the contents of register r8
11
12        # evaluate test condition, jump conditionally (less-than or equal)
13        jle      alternative
14
15consequent:
16        # write(1, messageConsequent, 11)
17        mov     $1, %rax                   # system call 1 is write
18        mov     $1, %rdi                   # file handle 1 is stdout
19        mov     $messageConsequent, %rsi   # address of string to output
20        mov     $11, %rdx                  # number of bytes
21        syscall                            # invoke operating system to do the write
22
23        # alter loop control variable, one step closer to termination
24        dec     %r8
25
26        # jump to test condition at beginning of loop
27        jmp     test                       # jump unconditionally
28
29alternative:
30        # write(1, messageAlternative, 12)
31        mov     $1, %rax                   # system call 1 is write
32        mov     $1, %rdi                   # file handle 1 is stdout
33        mov     $messageAlternative, %rsi  # address of string to output
34        mov     $12, %rdx                  # number of bytes
35        syscall                            # invoke operating system to do the write
36
37afterAlternative:
38        # exit(0)
39        mov     $60, %rax                  # system call 60 is exit
40        xor     %rdi, %rdi                 # we want return code 0
41        syscall                            # invoke operating system to exit
42
43loopControlVariable:
44        .quad    8
45messageConsequent:
46        .ascii  "Consequent\n"
47messageAlternative:
48        .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

 1        .global _start
 2
 3        .text
 4_start:
 5        # assume we begin with the number 5
 6        mov     $5, %rax                   # move 5 into the register rax
 7
 8test:
 9        # we then test to see if the number is greater than 4
10        cmp     $4, %rax                   # subtract 4 from the contents of register rax
11
12        # evaluate test condition, jump conditionally (less-than or equal)
13        jle      alternative
14
15consequent:
16        # write(1, messageConsequent, 10)
17        mov     $1, %rax                   # system call 1 is write
18        mov     $1, %rdi                   # file handle 1 is stdout
19        mov     $messageConsequent, %rsi   # address of string to output
20        mov     $10, %rdx                  # number of bytes
21        syscall                            # invoke operating system to do the write
22        jmp     afterAlternative
23
24alternative:
25        # write(1, messageAlternative, 11)
26        mov     $1, %rax                   # system call 1 is write
27        mov     $1, %rdi                   # file handle 1 is stdout
28        mov     $messageAlternative, %rsi  # address of string to output
29        mov     $11, %rdx                  # number of bytes
30        syscall                            # invoke operating system to do the write
31
32afterAlternative:
33        # exit(0)
34        mov     $60, %rax                  # system call 60 is exit
35        xor     %rdi, %rdi                 # we want return code 0
36        syscall                            # invoke operating system to exit
37
38messageConsequent:
39        .ascii  "Consequent"
40messageAlternative:
41        .ascii  "Alternative"


CoderMerlin™ proudly recommends:
HintIcon.png

Your purchase via these links helps keep  Coder Merlin™ 's orbuculum ablaze. As an Amazon Associate we earn from qualifying purchases. Thank you for your support!


Designed with pride in Silicon Valley, CA, USA