Pseudo Code

advertisement
Software Assignment 1
The Fibonacci Sequence
Valdemar Örn Erlingsson
Pseudo Code
I Started out writing a simple version of the software in a language that I know very well,
C++. I made sure it did what it was supposed to and then altered the code to be more
language-independent. Obviously some parts must be changed as Assembly does not
directly have some of the functionality I use (like while-loops and if-statements).
Main:
// for calculating the nth fibonacci number
// This is the final outcome, f = fibonacci(n)
int n
int f
print "Calculate fibonacci number: "
n = [input from user]
f = Fibo(n)
print f
// Call subroutine fibo, pass n to it, and return the number f
// Print f to the screen
Fibo:
int
int
int
int
ai = 1
ah = 1
store = 0
i = 0
//
//
//
//
while i ≤ n
if i < 2
ai = 1
i = i + 1
// Calculate within this region
// fibonacci numbers 0 and 1 equal one.
// increment to the next place in sequence
else
store = ai
ai = ah + ai
ah = store
i = i + 1
the ith number in the sequence. Start at one for reference
the i-1th number. Start at one for reference
Placeholder value.
The number to which we have calculated the fibonacci number.
//
//
//
//
//
Keep old number for reference
sum the current number with the old one.
We have now calculated the next place in the sequence,
insert [store] as old number
increment to the next place in sequence
return ai
Assembly Code
*----------------------------------------------------------* Program
: Fibonacci Sequence
* Written by : Valdemar Erlingsson
* Date
: 28.09.2008
* Description: Short and simple program that finds the n'th
*
fibonacci number in the sequence
*----------------------------------------------------------START
ORG
$1000
LEA
MOVE.B
TRAP
output,A1
#14,D0
#15
* Load output for use in the Trap function
* Set the action of the Trap function to print
* Display message
MOVE.B
TRAP
MOVE.B
BSR
#4,D0
#15
D1,n
FIBO
*
*
*
*
*
Set the action of the Trap function to input
Get input
Store input value in a safe location
Branch to subroutine, pass n through register D1
the outcome is accessible through f and D2
LEA
MOVE.B
TRAP
output2,A1
#14,D0
#15
MOVE.L
MOVE.B
TRAP
f,D1
#3,D0
#15
* Move f do D1 for displaying
BRA
END
* End program
MOVE.L
MOVE.L
MOVE.L
MOVE.L
#1,D2
#1,D3
#0,D4
#0,D5
*
*
*
*
*
CMP.L
BGT
D1,D5
COMPLETE
* compare i and n
* Inverse of the pseudocode, branch if i>n
CMP.L
BGE
#2,D5
OVERTWO
* Is 2 < i ? Should the program branch?
* i is greater than 2, goto OVERTWO
MOVE.L
ADDI.L
BRA
#1,D2
#1,D5
CHECK
* ai = 1
* i++
* Go back to loop requirement
MOVE.L
ADD.L
MOVE.L
ADDI.L
BRA
D2,D4
D3,D2
D4,D3
#1,D5
CHECK
*
*
*
*
*
MOVE.L
RTS
D2,f
* Store output value in a safe location
* Return back to lower stack
DS.L
DS.L
DC.B
DC.B
1
* Reserve space for n and f
1
'Calculate fibonacci number: ',0
'The number is: ',0
MOVE.B
TRAP
#9,D0
#15
END
START
* Print second message
* Print f
FIBO
Use
Use
Use
Use
Use
D1
D2
D3
D4
D5
Register
Register
Register
Register
Register
for
for
for
for
for
n
ai
ah
store
i (iterator)
CHECK
OVERTWO
store = ai
ai = ah + ai
ah = store
i++
Go back to loop requirement
COMPLETE
n
f
output
output2
END
Halt Simulator
About the program
Had some problems at first getting values higher than 65536 ( = 2 16), but this was due to
the fact that I forgot to add a .L parameter to the Add statement. This was easily
rectified. Other than that, I was able to collect everything I needed to know to complete
the assignment from easy68.com and Karl's lectures.
The program itself is very simple. It starts out asking the user for a number, n, which
represents that nth number in the Fibonacci sequence. The first two numbers equal zero,
but all numbers above that are the sum of the two previous numbers. hopefully the
pseudocode and the commented assembly code are enough to explain the execution of
the program.
Execution:
Calculate fibonacci number: 7
The number is: 21
Calculate fibonacci number: 27
The number is: 317811
Differences between Assembly and Pseudo-code:
 Definition of variables. I define n and f at the bottom of the program, as data
storage of size Long (I used long variables allow greater outcomes than 2 16 =
65536).
 Registers are used to hold variables within the Fibo() function. This makes the
program run faster and simplifies it.
 one quirk of transporting the project from pseudo to assembly were the
compare statements:
CMP.L
BGE
#2,D5
OVERTWO
* Is 2 < i ? Should the program branch?
* i is greater than 2, go to OVERTWO
Here it was easier to reverse the if-statement (originally written if(i < 2) ),
because a BRANCH must be used if the condition is not met (if it's met we simply
execute the code that follows).
Sidenote
In the assignment the Fibonacci sequence is defined as:
𝐹(𝑛) = {
1
𝐹(𝑛 − 1) + 𝐹(𝑛 − 2)
𝑛 = 0,1
𝑛>1
This is actually not the usual way to define the sequence. Rather, it is defined:
0
𝑛=0
1
𝑛
= 1,2
𝐹(𝑛) = {
𝐹(𝑛 − 1) + 𝐹(𝑛 − 2) 𝑛 ≥ 3
This doesn't really affect our assignment, but I thought it was a fact to notice.
Download