MIPS Pipeline with and without Forwarding - mips

I am looking at the number of stalls in the following MIPS code with and without forwarding. I am trying to get a better understanding of when the data is needed in the datapath.
lw $10, 0($4)
sw $10, 24($5)
With forwarding, I get the following with the understanding that the value going into register 10 from the load word instruction is available after the memory stage, and that value is needed by the store word instruction before its memory stage. Hence, there are zero stalls.
F D E M W
F D E M W
If there is no forwarding, register 10 will not have the correct value from the load word instruction until it is written in the first half of the clock cycle in the write back stage.
Is it correct to say that the store word instruction needs the correct value of register 10 in the second half of the clock cycle in the decode stage, producing the following two stalls:
F D E M W
F F F D E M W
Or is it that the store word instruction needs it in the execute stage producing this sequence of two stalls:
F D E M W
F D D D E M W
I guess I'd like a way of phrasing this in my head to better my understanding.

Without forwarding, the load word instruction will have register 10 updated after the 1st half of the clock cycle in the write back stage. The store word instruction will need to read that value in register 10 in the second half of the clock cycle in the decode stage, producing the following 2 stalls in the decode stage:
F D E M W
F D D D E M W

Related

Determine the function and output from this (very little) assembler code

ax is a register, shl means we shift to left by n values...
z is a memory address and at the beginning it contains the value 4.
mov ax, [z]
shl ax, 3
add ax, [z]
shl ax, 1
mov [q], ax
I'd like to know how can we read the function from this code?
So in the first line we put what is in z into register ax, alright.
Then we shift this value (4) to left by 3, so this operation is basically made: 2^3 * z
To this we add [z], so now we have 2^3 * z + z = 9z
Now we take 9z and shift it to left by 1, so we have 9z * 2^1 = 18z
In the end we take register ax and put it into memory address q, so we have:
q(z) = 18z
The task said that z contains value 4, so in this case the output would be
q(4) = 18*4= 72
Did I do everything correctly, from explanation to solution? Please do tell me as I need to do a task like that in the exam.
So in the first line we put what is in z into register ax, alright.
No, you put content of memory from value (address) z into ax.
So ax = 4, not ax = z. In assembly z is equivalent to symbol or address, ie. some memory cell label like 12340.
("no" - you probably understand it correctly, I just wanted to emphasize how [] is used in x86 intel syntax).
In C = z assigns value of variable z, and to get the address you have to do &z. In Assembly it's the other way around z itself is address, [z] is content. (at least in NASM syntax, MASM/TASM allows for mov ax,z syntax fetching the content, and there you have to write mov ax,offset z to get address, but I advice against this mixed usage, where variables are fetched automatically, but accessing memory trough register requires brackets [rx], IMO it's confusing)
Rest is OK.

fftw library, what is the output size of fftw_plan_dft_r2c_1d?

Im new to fftw library. I have an array of n real data and use fftw_plan_dft_r2c_1d to find fft spectrum. What would be the size of the output. Is it n as same as the input? Also, is the result center around 0 Hz or I have to manually do it?
For a real-to-complex transform you get N / 2 + 1 complex outputs for N real inputs (the redundant symmetric outputs are not generated).
The 0 Hz component is in bin 0.
This is all covered in the FFTW manual.
This is not an answer to your question, but I hope it can be a solution to your problem.
If you only want to find the spectrum of your data , you might use the "halfcomplex" format.
Here is an piece of code:
double *in,*out;
fftw_plan myplan;
in = fftw_malloc (N*sizeof(double));
out = fftw_malloc (N*sizeof(double));
myplan = fftw_plan_r2r_1d(N,in,out,FFTW_R2HC,FFTW_FORWARD);
// Fill in[] with your data.
...
fftw_execute(myplan);
Now out contains r0, r1, r2, ..., rn/2, i(n+1)/2-1, ..., i2, i1 , as it is written in the manual.
r0 ,out[0],is the mean value of your data/signal.
r1 ,out[1],is the real part of the first element of the DFT.
...
i0 is 0 because you're using real data , so it isn't stored in out.
i1 ,out[N-1],is the imaginary part of the first element of the DFT.
i2 ,out[N-2],is the imaginary part of the second element of the DFT.
If N is a even number , then r(N/2) out[N/2] is the Nyquist frequency amplitude.
Im new to fftw library
Remember that FFTW computes only the product of your data by the trigonometric functions, but it don't normalize them.
You can find more info about the halfcomplex here.

What is the correct syntax of MIPS instruction sll?

Should the shift amount be an immediate value, or a value stored in a register? Does both work?
I've had different websites tell me different things and am confused.
Based on my research the sll (shift left logical) instruction should be used like this:
sll $d, $t, h
Which makes $d = $t shifted left h times.
I've been told that h should be a immediate value, but I was wondering if a register could be used as the third argument, and the value inside that register used as the shift amount. Would that also work?
You are correct.
sll is specific in that it is a R-format instruction where only two registers are used, rd and rs (destination and source), and the shamt field is a immediate value (a constant).
There is another instruction sllv which uses a third register where you specify shift by variable (the register).
Let me clear the point Shift left logical in MIPS 32 bit has following syntax:
SLL destination, Target, Shift amount(Should be immediate value)
Where as in 8086 if we want shift amount more than 1 we have to use a register to store the value of shift amount!

Adding subtrees to a tree

Suppose I have a tree X
a
b c
d e f g
and I want to add a long subtree Y to X
a
b
e
u
so X+Y would look like this.
a
b c
d e f g
u
How would one go about implementing such a tree concatenation?
What you're describing sounds to me like you're trying to insert a word into a trie. If that's what you're trying to do, you can start at the root of the trie and the beginning of the word and then process each character x - if there is no edge labeled x from the current node, create a new node and add an edge between them; then, in either case, follow the edge labeled x and move to the next character.

How do I turn this question into a boolean logic problem?

"If you can press a button to get $1M and a random person dies somewhere in the world would you press the button?"
A = press button
B = get $1M
C = random person dies
Here is what I think it should be:
If A, then B AND c
According to the original statement is it:
(If A, then B) AND C
or
If A, then (B AND C)
You've correctly identified the three propositional variables:
P1(x): "x presses a button."
P2(x): "x receives one million dollars."
P3(x): "x causes the death of a random person."
You want to express the sentence Q: "if someone presses the button, then they receive a million dollars and a person dies." At first glance, it seems like P1(x) ⇒ P2(x) ∧ P3(x) correctly expresses this. How can we be sure? Let's draw a truth table:
P1 P2 P3 P2 ^ P3 P1 --> P2 ^ P3
---- ---- ---- --------- ----------------
T T T T T
T T F F F
T F T F F
T F F F F
F T T T T
F T F F T
F F T F T
F F F F T
Notice that "you receive a million dollars and cause a death" is true only when both of the constituent parts are true. This makes sense; if both parts don't come true, the whole is not also true.
Notice also the truth values for the entire statement Q: it's false whenever the second part is false and the first part is true. This makes sense: if you press the button but either (1) the million dollars doesn't appear or (2) nobody dies, the prediction implied by Q is not true. So our assertion is correct.
Think about it. Draw up a truth table for each option.
HINT: If you don't push the button, would the random person die?
In all maths where the operators are the same and no logical grouping is indicated, the expression is read from left to right. Therefore, if you press the button, you will receive $1M and a random person will die.
I changed my mind. True. This is not programming. This is Ethical logic. Go to Community wiki.