Lab 5 - Binary Fibonacci
This lab you will write binary code (using our Toy ISA) you will run your code on our online simulator .
The Instructions
The instructions from our machine are below for reference.
icode
Behaviors:
icode | operation | ||||||||||
---|---|---|---|---|---|---|---|---|---|---|---|
0 |
| ||||||||||
1 |
| ||||||||||
2 |
| ||||||||||
3 |
| ||||||||||
4 | write | ||||||||||
5 | do different things for different values of
| ||||||||||
6 | do different things for different values of
In all 4 cases, increase | ||||||||||
7 | Compare |
Running programs
You should create two files
-
One you work with, that has comments and notes to keep you sane. Call this anything you like.
-
One you run and submit, which contains nothing by hex bytes separated by white space. You’ll submit this as a file named
fib.binary
To our online simulator and click the file upload button at the top of the page to load your
fib.binary
into the simulator’s memory.
Your Task
Create a binary program (i.e., a file containing hex bytes) that runs in this language; name the file fib.binary
.
When run in a simulator (../homework/files/toy-isa-sim.html)), fib.binary
should change the contents of memory for all addresses i
≥ C016, placing in address i
the i-0xC0
th Fibonacci number (modulo 256, since these are bytes).
Once 0xC0
through 0xFF
are set, halt by running an instruction with the reserved
bit set.
The file fib.binary
itself must not contain more than C016 (19210) hexadecimal bytes.
It should be the case that running your simulator on fib.binary
for many cycles should result in output ending with the following:
0xc0-cf: 01 01 02 03 05 08 0d 15 22 37 59 90 e9 79 62 db
0xd0-df: 3d 18 55 6d c2 2f f1 20 11 31 42 73 b5 28 dd 05
0xe0-ef: e2 e7 c9 b0 79 29 a2 cb 6d 38 a5 dd 82 5f e1 40
0xf0-ff: 21 61 82 e3 65 48 ad f5 a2 97 39 d0 09 d9 e2 bb
Your code should not change the byte stored in address 0x00 during its run. Your code must finish within 2048 clock cycles.
Hints, tips, and suggestions
How to compute Fibonacci numbers
- Keep track of two numbers, current and previous. Start them both off at 1.
- Let next be the sum of current and previous.
- Rename current → previous, next → current (in that order)
- Repeat
You definitely want to make sure you can write working code for this in some language you know well before trying to convert that code into binary.
How to write binary
We suggest following these steps, carefully, saving the result of each in a file so you can go back and fix them if they were wrong:
- Write pseudocode that does the desired task
- Convert any
for
loops towhile
loops with explicit counters - Change any
if
orwhile
guards to the formsomething <= 0
a <= b
becomesa-b <= 0
a < b
becomesa+1 <= b
becomesa+1-b <= 0
a >= b
becomes0 >= b-a
becomesb-a <= 0
a > b
becomes0 > b-a
becomesb+1-a <= 0
a == b
becomesa-b == 0
becomes!(a-b) == 1
becomes!!(a-b) <= 0
a != b
becomesa-b != 0
becomes!(a-b) == 0
becomes!(a-b) <= 0
- Add more variables to split multi-operation lines into a series of single-operation lines
- Add more operations to convert ones not in the instruction set into ones in the instruction set
- Change each loop into a pair of instructions, opening with “
spot1
=pc
” and closing with “if …, gotospot1
” - Count the number of variables needed
- Pick a memory address for each variable. Make these big enough your code is unlikely to get that big; for example, you might pick
0x80
though0x80
+ number of variables - Convert each statement that uses variables into a. register ← load variable’s memory b. original statement c. store variable’s memory ← register
- translate each instruction into numeric (
icode
,a
,b
) triples, possibly followed by aM[pc+1]
immediate value - turn (
icode
,a
,b
) into hex - Write all the hex into
fib.binary
Debugging binary is hard. That’s part of why we don’t generally write code in binary. If you get stuck, you should probably try pulling just the part you are stuck on separate from the rest and test it until it works, then put it back in the main solution. Please know that your TAs are here in lab to help as well!
Check-off
Upload the following to gradescope:
- Your binary ISA code fib.binary
To check-off this lab, show a TA
- Once you submitted your files check in with TA so that they can give you attendence credit.