Skip to main content Link Menu Expand (external link) Document Search Copy Copied

Lab 5 - Binary Fibonacci

This lab builds on our previous lab and Homework 3, in which you will continue to write binary code (using our Toy ISA) for the simple machine we discussed in class and for which you created a simulator last week.

The Instructions

The instructions from our machine are below for reference. They match those in Lab 3 and Homework 3.

icode Behaviors:

icode operation

rA = rB


rA += rB


rA &= rB


rA = read from memory at address rB


write rA to memory at address rB


do different things for different values of b:

b action
0 rA = ~rA
1 rA = -rA
2 rA = !rA
3 rA = pc

do different things for different values of b:

b action
0 rA = read from memory at pc + 1
1 rA += read from memory at pc + 1
2 rA &= read from memory at pc + 1
3 rA = read from memory at the address stored at pc + 1

In all 4 cases, increase pc by 2, not 1, at the end of this instruction


Compare rA (as an 8-bit 2’s-complement number) to 0; - if rA <= 0, set pc = rB - otherwise, increment pc like normal.

Running programs

You should create two files

  1. One you work with, that has comments and notes to keep you sane. Call this anything you like.

  2. 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 test your code, do one of

python3 fib.binary


java SimBase fib.binary

or going 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 (yours or ours), fib.binary should change the contents of memory for all addresses i ≥ C016, placing in address i the i-0xC0th 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 0 during its run.

Hints, tips, and suggestions

How to compute Fibonacci numbers

  1. Keep track of two numbers, current and previous. Start them both off at 1.
  2. Let next be the sum of current and previous.
  3. Rename current → previous, next → current (in that order)
  4. 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:

  1. Write pseudocode that does the desired task
  2. Convert any for loops to while loops with explicit counters
  3. Change any if or while guards to the form something <= 0
    • a <= b becomes a-b <= 0
    • a < b becomes a+1 <= b becomes a+1-b <= 0
    • a >= b becomes 0 >= b-a becomes b-a <= 0
    • a > b becomes 0 > b-a becomes b+1-a <= 0
    • a == b becomes a-b == 0 becomes !(a-b) == 1 becomes !!(a-b) <= 0
    • a != b becomes a-b != 0 becomes !(a-b) == 0 becomes !(a-b) <= 0
  4. Add more variables to split multi-operation lines into a series of single-operation lines
  5. Add more operations to convert ones not in the instruction set into ones in the instruction set
  6. Change each loop into a pair of instructions, opening with “spot1 = pc” and closing with “if …, goto spot1
  7. Count the number of variables needed
    • If1 it is ≤ 4, skip to step 10
    • else2, continue with next step
  8. 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 though 0x80 + number of variables
  9. Convert each statement that uses variables into a. register ← load variable’s memory b. original statement c. store variable’s memory ← register
  10. translate each instruction into numeric (icode, a, b) triples, possibly followed by a M[pc+1] immediate value
  11. turn (icode, a, b) into hex
  12. 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!


To check-off this lab, show a TA your fibonacci binary code and run your (or our) simulator to display the final results. If you have not completed your code, explain how the code you have written works.

  1. depending on how you write your original code, this is possible for this task … 

  2. … some solutions are in this case instead. 

Copyright © 2023 John Hott, portions Luther Tychonievich.
Released under the CC-BY-NC-SA 4.0 license.
Creative Commons License