# HW3 - Binary Modulo

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.

## The instructions

`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 but hex bytes separated by white space. You’ll submit this as a file named
`mod.binary`

(or`mod.binary.txt`

if needed)

To test your code, do one of

```
python3 sim_base.py mod.binary
```

or

```
java SimBase mod.binary
```

or going to our online simulator and click the file upload button at the top of the page to load your `mod.binary`

into the simulator’s memory.

## Your task

Your code should

- Load the values in memory at addresses
`0x01`

and`0x03`

into registers- Note: since we have not discussed ways to get other data into our memory yet, we will directly modify your code so that the second (0x01) and fourth (0x03) bytes are the input values.
*This means your first (0x00) and third (0x02) bytes (instructions) should be very specific operations.*

- Note: since we have not discussed ways to get other data into our memory yet, we will directly modify your code so that the second (0x01) and fourth (0x03) bytes are the input values.
- Compute the modulo of those values (i.e., (what’s at 0x01) mod (what’s at 0x03))
- Store the product at address 0xD0
- Halt once it is done

For example, if `mod.binary`

begins `__ 10 __ 06`

then when it is finished it should have `04`

in address 0xD0; in decimal, that is 16 mod 6, which is 4. If `mod.binary`

begins `__ 7F __ 0A`

then when it is finished it should have `07`

in address 0xD0; in decimal, that is 127 mod 10, which is 7.

*Note: We should be able to change the second and fourth bytes of your program to do other modulo calculations too.*

**You may assume that neither value will be negative, but either may be zero.** If either operand is 0, your code should set the value at 0xD0 to 0x00.

## Hints, tips, and suggestions

### How to compute modulo

Modulo, *a* mod *n* or `a % n`

, is defined as the remainder when dividing *a* by *n*. As in our examples above with **16 mod 6**, \(16 / 6 = 6 * 2 + 4\), where the quotient is 2 and the remainder is 4. Therefore, 16 mod 6 = 4. The Wikipedia page on modulo has a more formal definition.

*Hint: For the division operation, think along the lines of “how many times can \(n\) fit into \(a\)?” You may wish to consider multiples of \(n\).*

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 to`while`

loops with explicit counters - 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`

- 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 …, goto`spot1`

” - Count the number of variables needed
- If
^{1}it is ≤ 4, skip to step 10 - else
^{2}, continue with next step

- If
- 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 - Convert each statement that uses variables into
- register ← load variable’s memory
- original statement
- store variable’s memory ← register

- translate each instruction into numeric (
`icode`

,`a`

,`b`

) triples, possibly followed by a`M[pc+1]`

immediate value - turn (
`icode`

,`a`

,`b`

) into hex - Write all the hex into
`mod.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.

## Submit

Submit your `mod.binary`

file via Gradescope.