Given this processor hardware design, add control states to the
following to implement an average instruction (as decoded by the
when below), such that avg $rd,$rs,$rt gives
rd the average of the other two values. You're probably
used to averaging by adding and then dividing by 2, but that
actually doesn't work -- because the add can go out of range.
Instead, use the algorithm that
rd=((rs&rt)+((rs^rt)>>1)), which is a trick
from Prof. Dietz's MAGIC algorithms page.
You can test your code with:
MEM[0]=op(1)+rd(8)+rs(9)+rt(10)
MEM[4]=0
$8=42
$9=32
$10=64
Register $8 should end-up holding the value 48, which is 0x00000030.