0

Similar to this

I realized that what I am trying to do can be done mostly with bitwise operators, no need for complicated loops or messing with the stack, so that's what I am trying to do now.

Now I need to make a choose box that works as follows:

It chooses the bits based on the value of input E (32 bit integer). If a bit of E=1, the output bit is the corresponding bit of F (another 32 bit integer). If a bit of E is 0, the output bit is the corresponding bit of G. In this way, the bits of F and G are shuffled together based on the value of E.

I guess I could make this with a loop but is there a simpler way like in the link?

Hypercube
  • 89
  • 6
  • 1
    Bitwise operations like `or` do all 32 bits in parallel; that's the entire point. If you can express the formula for one bit as a boolean expression like `majority = (A&B) | (B&C)`, you can use the same operation on 32-bit integers to get 32 results in parallel, one in each bit of the result. For bit blend, see [Merge bit sequences a and b according to a mask](https://stackoverflow.com/q/39282691) - the formula is in the question – Peter Cordes May 26 '20 at 05:58
  • 1
    Also related: [C - Swap a bit between two numbers](https://stackoverflow.com/q/45361419)/ / [Swapping bits at a given point between two bytes](https://stackoverflow.com/a/50839334). Just blending into one is slightly simpler than swapping; you leave out the step that would calculate the other result. – Peter Cordes May 26 '20 at 06:07
  • In general for any boolean function of any number of inputs, you can write down a truth table and start looking for boolean operations that can implement it. It's always possible to implement any boolean truth table of N inputs in terms of MIPS's AND, OR, XOR, and NOR operations, it's just a matter of finding an efficient sequence. – Peter Cordes May 26 '20 at 07:27
  • e.g. https://www.allaboutcircuits.com/textbook/digital/chpt-7/converting-truth-tables-boolean-expressions/ shows a way to derive boolean expressions from truth tables, then simplify them down to something reasonable. – Peter Cordes May 26 '20 at 08:04
  • @PeterCordes Riiight, I remember that from Digital Systems in the first semester. Hadn't seem the connection. – Hypercube May 26 '20 at 15:32
  • @PeterCordes I'm a bit rusty on this stuff. I am trying to make a Karnaugh map for this. I figure it's going to look a bit like a multiplexer but it's confusing me. Can you give me some clues? – Hypercube May 26 '20 at 15:36
  • @PeterCordes I think I got it. Is it (G&!E) || (F&E)? If so it's almost the same as the other one – Hypercube May 26 '20 at 15:49
  • Yes that's one way to blend. But in C notation `!` is logical NOT, producing a boolean 0 or 1 result for the whole integer, not per-bit. `||` is also logical OR, not evaluating the right hand side if any bits in the left are set. In C it would be `(G & ~E) | (F & E)`. Another way to optimize it might involve `(F^G) & E`, then XOR that info F or G. MIPS doesn't have an ANDN instruction so `G & ~E` would take two instructions, not one. – Peter Cordes May 26 '20 at 17:07
  • @PeterCordes I did this `la $t0,E` `la $t1,F` `la $t2,G` `and $t1,$t1,$t0 #(F&E)` `not $t0,$t0 #!E` `and $t2,$t2,$t0 #(G&!E)` `or $t0,$t1,$t2 #(G&!E) || (F&E)` Probably a bit clumsy. – Hypercube May 26 '20 at 18:08
  • @PeterCordes You have used ^in other posts, what does it mean exactly? Not power I assume. – Hypercube May 26 '20 at 18:10
  • 1
    You're still writing your comment with broken notation. Bitwise NOT is `~` not `!`, bitwise OR is `|` not `||`. You want arithmetic (bitwise) operators, not logical (whole value as one boolean): https://en.cppreference.com/w/c/language/expressions. Also, you didn't load values, only addresses; you only used `la` not `lw`. – Peter Cordes May 26 '20 at 18:12
  • 1
    I'm using C notation, as linked in my last comment. `^` is XOR. Many other language use the same conventions as C for bitwise booleans, including Java, Javascript, Python. https://en.cppreference.com/w/c/language/operator_arithmetic – Peter Cordes May 26 '20 at 18:12
  • @PeterCordes Right. Fixed that. Thanks again. – Hypercube May 26 '20 at 18:16

0 Answers0