Difference between revisions of "W1154-Annex-1"
From Coder Merlin
Line 34: | Line 34: | ||
'''FLOWCHART 3''' | '''FLOWCHART 3''' | ||
{{ResponsiveImage|[[File:Flowchart-2.png]]}} | {{ResponsiveImage|[[File:Flowchart-2.png]]}} | ||
}} | |||
== Assembly Language Listing == | |||
{{Box| | |||
'''Listing 2''' | |||
<syntaxhighlight lang="gas" highlight="8,15,29,37" line> | |||
.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" | |||
</syntaxhighlight> | |||
}} | |||
{{Box| | |||
'''Listing 3''' | |||
<syntaxhighlight lang="gas"> | |||
.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!" | |||
</syntaxhighlight> | |||
}} | }} |
Revision as of 21:46, 23 January 2020
Within these castle walls be forged Mavens of Computer Science ...
— Merlin, The Coder
Memory Sequences[edit]
Flowcharts[edit]
Assembly Language Listing[edit]
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!"