Question: HW 3 - Binary Modulo icode Behaviors: icodeoperation 0 rA = rB 1 rA + = rB 2 rA & = rB 3 rA =

HW3- Binary Modulo
icodeBehaviors:
icodeoperation0rA = rB
1rA += rB
2rA &= rB
3rA =read from memory at addressrB
4writerAto memory at addressrB
5do different things for different values ofb:
baction0rA = ~rA1rA =-rA2rA =!rA3rA = pc
6do different things for different values ofb:
baction0rA =read from memory atpc +11rA +=read from memory atpc +12rA &=read from memory atpc +13rA =read from memory at the address stored atpc +1
In all 4 cases, increasepcby 2, not 1, at the end of this instruction
7ComparerA(as an 8-bit 2s-complement number) to0; - ifrA <=0, setpc = rB- otherwise, incrementpclike normal.
Task
code should
Load the values in memory at addresses0x01and0x03into registersNote: 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.
Compute the modulo of those values (i.e.,(whats at 0x01) mod (whats at 0x03))
Store the product at address 0xD0
Halt once it is done
For example, ifmod.binarybegins__10__06then when it is finished it should have04in address 0xD0; in decimal, that is 16 mod 6, which is 4. Ifmod.binarybegins__7F __0Athen when it is finished it should have07in 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,amodnora % n, is defined as the remainder when dividingabyn. As in our examples above with16 mod 6,16/6=62+4
, where the quotient is 2 and the remainder is 4. Therefore, 16 mod 6=4. TheWikipedia pageon modulo has a more formal definition.
Hint: For the division operation, think along the lines of how many times can
fit into
? You may wish to consider multiples of
.
Youdefinitelywant 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 anyforloops towhileloops with explicit counters
Change anyiforwhileguards to the formsomething <=0
a <= bbecomesa-b <=0
a < bbecomesa+1<= bbecomesa+1-b <=0
a >= bbecomes0>= b-abecomesb-a <=0
a > bbecomes0> b-abecomesb+1-a <=0
a == bbecomesa-b ==0becomes!(a-b)==1becomes!!(a-b)<=0
a != bbecomesa-b !=0becomes!(a-b)==0becomes!(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 neededIf1it is <=4, skip to step 10
else2, continue with next step
Pick a memory address for each variable. Make these big enough your code is unlikely to get that big; for example, you might pick0x80though0x80+ number of variables
Convert each statement that uses variables intoregister load variables memory
original statement
store variables 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 intomod.binary
Debugging binary is hard. Thats part of why we dont 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.
Code:
010101010203030200040703010004070501020201 D0 FF
Still producting errors.
Hints and Comments:
looped forever (for example, try 127 mod 3)
wrong result (for example, try 55 mod 23)
Please note the values are displayed in decimal, not hexadecimal.
Zero 1(6/6)
Correct! 0 mod 5 gives 0
Zero 2(6/6)
Correct! 4 mod 0 gives 0
Even divide (6/6)
Correct! 6 mod 2 gives 0
Big difference (0/5)
127 mod 3 runs forever
127 mod 3 gives 0 but should give 1
Gives 9(0/5)
55 mod 23 gives 0 but should give 9
Random 1(0/5)
72 mod 70 gives 0 but should give 2
Random 2(0/5)
94 mod 84 gives 0 but should give 10
Random 3(0/5)
37 mod 4 gives 0 but should give 1
Random 4(0/5)
114 mod 70 runs forever
114 mod 70 gives 0 but should give 44
Random 5(0/5)
56 mod 5 gives 0 but should give 1
Random 6(0/5)
56 mod 35 gives 0 but should give 21
Random 7(0/5)
97 mod 88 gives 0 b

Step by Step Solution

There are 3 Steps involved in it

1 Expert Approved Answer
Step: 1 Unlock blur-text-image
Question Has Been Solved by an Expert!

Get step-by-step solutions from verified subject matter experts

Step: 2 Unlock
Step: 3 Unlock

Students Have Also Explored These Related Programming Questions!