This is the first of two assignments in which you will write binary code for the simple machine you created a simulator for in lab. It is much simpler than the second one, and will take much less time. Both assume that you have understood that lab’s content well.
icode |
Behavior | ||||||||||
---|---|---|---|---|---|---|---|---|---|---|---|
0 | rA = rB |
||||||||||
1 | rA += rB |
||||||||||
2 | rA &= rB |
||||||||||
3 | rA = read from memory at address rB |
||||||||||
4 | write rA to memory at address rB |
||||||||||
5 | do different things for different values of
|
||||||||||
6 | do different things for different values of
In all 4 cases, increase |
||||||||||
7 | Compare rA (as an 8-bit 2’s-complement number) to 0 ; if rA <= 0 , set pc = rB otherwise, increment pc like normal. |
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 mult.binary
To test your code, do one of
python3 sim_base.py mult.binary
or
java SimBase mult.binary
or going to our online simulator and click the file upload button at the top of the page to load your mult.binary
into the simulator’s memory.
Your code should
You should ignore overflow, so since 0x79 × 0x23 = 0x108B, the answer should be 8B. This is likely to happen automatically without your explicit planning for it. You may assume that neither multiplicand will be negative, but either or both may be zero.
Thus, if mult.binary
begins __ 09 __ 0A
then when it is finished it should have 5A
in address 0xA0; if mult.binary
begins __ 79 __ 23
then when it is finished it should have 8B
in address 0xA0. We should be able to change the second and fourth bytes of your program to do other multiplications too.
You may be familiar with fancier ways, but a simple definition of multiplication that will result in reasonable-to-write code is
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.
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:
for
loops to while
loops with explicit countersif
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
and closing withspot1
=pc
if …, goto spot1
0x80
though 0x80
+ number of variablesicode
, a
, b
) triples, possibly followed by a M[pc+1]
immediate valueicode
, a
, b
) into hexmult.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.
There’s a 10-minute video of Prof Tychonievich3 using the How to write binary
section above to create a program that computes x <<= y
. We expect it will prove helpful in understanding how to solve this assignment.
Submit via the submission site, linked from the top of every course page and also available as https://kytos.cs.virginia.edu/cso1/
depending on how you write your original code, this is possible for this task …↩︎
… some solutions are in this case instead.↩︎
I made an error in the video: 50 seconds in I said multiply
but wrote <<=
instead of *=
. Because that was in the lead-in discussion and not the machine code creation part, I decided not to do another take to fix it.↩︎