1
\$\begingroup\$

So I know how to push a register using two instructions, but how can I push a reg onto the stack using one instruction? Here's the single cycle datapath

enter image description here

I'm seriously stuck here

\$\endgroup\$
3
\$\begingroup\$

There's absolutely no way to accurately answer this question without more context, but I'll take a shot in the dark just for the fun of it:

Pushing to the stack involves writing the contents of register Rdata to the memory indicated by the stack pointer register, [Rsp], and then incrementing Rsp so that it continues to point to the top of the stack.

It appears that in order to write to ram, you must set MemWrite in the "Control" block.

Looking at the "Data Memory" block in the bottom right, we need to supply it with "Write Data" and "Address". The "Write Data" signal comes from "Read data 2" from the "Registers" block.

Now it's not 100% clear to me what "Read data 2" is supposed to output, but I think it echoes the contents of the register indicated by "Read register 2", which is Instruction[20-16]. So you could set Instruction[20-16] = 0b00010 to place the value of R2 at "Read register 2". Ok, then Instruction[20-16] should be the id of the register whose contents you want pushed onto the stack.

Now, the "Address" input comes from the ALU. The ALU inputs appear to be "Read data 1" from the "Registers" block, and either "Read data 2" or Instruction[15-0] (after being sign-extended to 32 bits), depending on the AluSrc signal from the Control block.

So it looks like "Read register 1", instruction[25-21] should be the id of your stack pointer register so that the address of the top of the stack is put onto the ALU line. We don't want the value pushed onto the stack to be our other ALU operand, so set AluSrc=1 so that it's MUXed from Instruction[15-0]. Instruction[15-0] should therefore be the size of the write (4 bytes), so that the stack pointer can be incremented to the next address. In order for this to happen, the ALU control block needs to send a signal to the ALU indicating that the operation is an addition. It looks like you can either set ALUOp in Control, via Instruction[26-31] or possibly through Instruction[0-5].

After this, the address you write to will be Rsp+4, and then you need to write this address back into Rsp so that Rsp = Rsp + 4. We see that the output of the ALU is routed to a MUX directed by the MemToReg signal from the "Control" block. I believe we need to set MemToReg=0 in order to expose the address at the MUX output (as opposed to the data at that address). Then Rsp + 4 is routed into "Write data" in the Registers block.

Now we need to set the "Write register" value in the Registers block. Since our stack pointer register was indicated via "Read register 1", as opposed to "Read register 2", we cannot use the MUXed value from the "Read register 2" signal (and there's no way around this, since the data in register 2 is directly tied to the "Write data" line, so we cannot put the stack pointer in register 2). I think you also have to enable the "RegWrite" signal from the Control block to enable writeback.

But that's OK, because we can set the "write register" signal to be instruction[15-11] by making RegDst from the Control block = 1. Thus, instruction[15-11] should again by the id of the stack pointer register. However, you'll notice that this conflicts with what we did earlier, saying that instruction[15-0] should be 4. So, you can either make your stack pointer register 0 in order to avoid this conflict, fill instruction[15-0] with -4 to use register 31 as your stack pointer and have the stack grow downward, or you can look into the documentation for the ALU control block and see if there's a way around this - it looks like data from instruction[5-0] is also sent to the ALU, though it's not clear how this data is used.

Then set the rest of the control block appropriately - "Jump" should be set to 1, and MemRead should probably be deactivated.

Again, you really need to elaborate on this diagram - it's unclear to me, for example, how the output of the Control block is decided (especially considering it has 9 outputs but only 6 bits of input, so unless I'm missing something, not all combinations are possible) or what data "ALU control" sends to the ALU.

\$\endgroup\$
0
\$\begingroup\$

You can do it with an I-type instruction. the $rs register and the immediate value for push have not been specified; so we are free to choose these as needed. Extend the RegDst multiplexer so that it has three inputs: 0, 1 and 2.Inputs 0 and 1 remain as shown, but input 2 is the name of the $rs register (I[25–21]), which will be $sp for this instruction. Also, the immediate field will be -4. We then set RegWrite = ALUSrc = MemWrite= 1, MemRead = MemToReg = PCSrc = 0, and ALUOp = add.

\$\endgroup\$

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.