W1154-Annex-1
From Coder Merlin
Revision as of 22:44, 23 January 2020 by Chukwuemeka-tinashe (talk | contribs) (→Assembly Language Listing)
Within these castle walls be forged Mavens of Computer Science ...
— Merlin, The Coder
Memory Sequences[edit]
Flowcharts[edit]
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!"