W1154-Annex-1
From Coder Merlin
Memory Sequences[edit]
Flowcharts[edit]
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
9 statements:
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
20 test:
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
27 consequent:
28 # jump to statements at beginning of loop
29 jmp statements # jump unconditionally
30
31 alternative:
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
39 afterAlternative:
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
45 loopControlVariable:
46 .quad 8
47 messageStatement:
48 .ascii "Statement\n"
49 messageAlternative:
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
8 test:
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
15 consequent:
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
29 alternative:
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
37 afterAlternative:
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
43 loopControlVariable:
44 .quad 8
45 messageConsequent:
46 .ascii "Consequent\n"
47 messageAlternative:
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
8 test:
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
15 consequent:
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
24 alternative:
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
32 afterAlternative:
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
38 messageConsequent:
39 .ascii "Consequent"
40 messageAlternative:
41 .ascii "Alternative"