1

I need to implement BGE instruction in MIPS:

BGE $r1,$r2,despl (jump to PC + 4 + despl(<<) if regA >= reg B)

I need to modify the architecture but I do not know how

This is the model to modify

Peter Cordes
  • 328,167
  • 45
  • 605
  • 847
Dani TR
  • 11
  • 1
  • 1
    First, find an instruction that is similar but already exists. *Then study that instruction so you know exactly how it works, with all the control signals and datapaths.* Then study the new instruction to identify what it needs to do differently. Then implement the differences between the existing instruction and the new one. – Erik Eidt Jun 19 '22 at 20:21
  • I think I need to use sign flag, but I am not sure how to implement that. – Dani TR Jun 19 '22 at 20:24
  • Do you understand this diagram? It doesn't show every detail. Your new instruction needs to show the same level of detail that's in the existing diagram, that's all. – Erik Eidt Jun 19 '22 at 20:43
  • Btw, there is no sign flag on MIPS, as MIPS has no flags. It has a zero control signal: think about how that works, look to `bne` vs. `beq`. – Erik Eidt Jun 19 '22 at 20:47
  • Yes, I understand the diagram, although I am not an expert. I have implemented other instruccions, just adding 1 or 2 "wires" to different positions (to PC or to register bank for example). But with this instruction I am lost. My profesor gave us the next clue "the most significant bit of the ALU is the S flag (sign)" – Dani TR Jun 19 '22 at 21:32
  • FYI, the reason RISC-V already has this instruction, but real MIPS doesn't: [How does MIPS I handle branching on the previous ALU instruction without stalling?](https://stackoverflow.com/q/56586551) - it was designed around a pipeline that kept branch latency low by only having conditional branches that could evaluate quickly, e.g. checking the sign bit or all-zero status of a single register, or exact equality, but not something that would need an adder/subtractor. – Peter Cordes Jun 20 '22 at 00:21
  • do you want signed BGE or unsigned BGE? – old_timer Jun 20 '22 at 00:29
  • generally you just look at the carry out of the msbit. that bit will either tell you lt or gt and if you swap the operands then you get bge or lte. just take four bit operands and walk through the math by hand with pencil and paper 3-1, 3-2, 3-3, 3-4, etc....then do it with signed numbers. – old_timer Jun 20 '22 at 00:32
  • @old_timer: In MIPS, unsigned conditions end with U, like `slt` vs. `sltu`. (And BGE and BGEU already exist as pseudo-instructions using `slt` / `bnez` and `sltu` / `bnez` respectively.) So this is asking for signed compare. – Peter Cordes Jun 20 '22 at 04:26
  • so if you lave slt/bnez then you also have bge with slt and bnez with operands swapped. sorry if that was already mentioned above. sounded like they wanted to change the logic to create a new one. – old_timer Jun 20 '22 at 13:55
  • ahh, okay for signed, sorry about the carry out comment, that is unsigned. It is going ot either be n==v or n!=v for signed. But you already have the instructions you need to do a bge...which is I assume the answer below. – old_timer Jun 20 '22 at 13:57
  • but again if you do 4 bit math by hand you can see this, can even write a trivial program to go through all the cases and confirm... – old_timer Jun 20 '22 at 14:03

1 Answers1

1

It is true that for signed arithmetic, the MSB coming out of the ALU indicates the sign of the answer — but it is only valid when there is no overflow.

Mathematically speaking, adding two 32-bit numbers produces a 33-bit answer with no overflow.  If that 33-bit answer is forced into 32 bits by truncation (truncation means loosing/discard the 33'rd high bit, and there's no other reasonable approach to such downsizing), then there is the possibility of overflow.  When there is overflow, the 32-bit answer will have the sign reversed from what it should be, so the 32-bit answer is just junk — and while predictable junk, it is not directly usable.

However, a bge instruction doesn't need to capture a 33-bit answer in 32 bits of storage, but only tell us if the first operand was >= the second operand.  This can be done in all cases without overflow: we never need to truncate a 33-bit answer to 32 bits, but only need one bit of answer as output (yes/no) so unlike subtraction, no truncation is ever needed.  So, this is the most appropriate way to implement bge.

If, instead, you implement bge by using subtraction in the ALU and then looking at the MSB of a 32-bit result, that won't detect overflow, so this instruction will give wrong answers when the inputs are out of range, such as comparing max signed int to min signed int.  If you choose to detect overflow (possible by comparing signs of the A & B ALU inputs with the sign of the ALU output), what you'd have to do is either cause an exception (like add and sub do), or correct the answer outside the ALU.

To see more clearly, let's look at some examples as if we were doing this in 8 bits: let's see about 1 and -1, where the answer is 1 - (-1) = 2 which is positive so we know that 1 is indeed >= -1.  Using a larger values 63 and -63: 63 - (-63) = 126, also positive so we know >= holds.  But if we do 127 and -127, we get 127 - (-127) which is 254, but truncated back to 8 bits we have -2 (which is overflow) and that would lead us to believe that -127 is larger than 127.

Because this instruction could and can be implemented so as to avoid all issues of overflow, well, it should be done that way!

If it was me, I would add an OPALU value for ge operation to the ALU, and when that, have the ALU simply output Z=1 if >= holds on the operands and Z=0 otherwise.  No overflow, just some custom circuitry in the ALU, and an extra OPALU value to feed the ALU.

However, it seems that maybe your professor is looking for a different answer possibly involving reuse of existing ALU subtraction (OPALU) instead.

For that you would have OPALU call for subtraction, and then pull the 32 bit, the MSB out of the ALU.  You can take that (inverted of course, since MSB=1 means less than and Z=1 usually means take the branch) and then mux that into the Z line on condition of bge, or feed a second line to the UNIDAD DE CONTROL, and let it figure out whether to use Z vs. your new line.  This solution would involve overflow problems, which ought to be mitigated in some way (but perhaps unnecessary for this assignment).


These two potential approaches are not that different.  Assuming you did the OPALU ge value fed to the ALU, the ALU could do either 33-bit arithmetic or 32-bit arithmetic with overflow detection & correction.  The subtraction approach could also do overflow detection & correction, with the main difference being it would occur outside the ALU vs. inside the ALU.

As far as the final hardware is concerned, there is no box boundary between the gates inside the ALU vs. outside the ALU, but at the level of this kind of block diagram, I would put this complexity inside the ALU.

Erik Eidt
  • 23,049
  • 2
  • 29
  • 53
  • BTW, a true MIPS implementation would already have an ALU that supports signed-compare operations, specifically `slt`. – Peter Cordes Jun 20 '22 at 00:26