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

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.

Related

Reverse function

I have been trying to reverse a quite simple looking function.
the function is presented in assembly:
(Argument is loaded into AX)
AND AX, 0xFFFE (round down to even number)
MUL AX (Multiply AX by AX ; the result is represented as DX:AX)
XOR AX,DX
The function can be described as: H(X) = F(X & 0xFFFE); F(X) = ((X * X) mod 2^16) xor ((X * X) div 2^16)
Calculated all of the values from 1 to 2^16 and plotted on matlab in order to "see" some function.
Can anyone help me find an answer to this? (when given y what is the argument x).
It might be that for some values there is more than one answer, so narrowing it down is my goal.
Thanks,
Or.
It's a hash function.
You can't reverse a hash function, because the whole point of it is that it's a one way function.
The multiply is clearly reversible, it's the xor that's not. By combining the low and high part of the multiplication you lose information.
As you can see in the plot there are some white spaces, because there are 2^16 spaces in that plot that means there are also different input values that hash to the same value.
This is common in a hash function.
The only way to 'reverse' it is to build a lookup table that translates output values into possible input values. However you will find that for every output values that be 1 or more input values.
An even number x an even number is always a multiple of 4.
So the low 2 bits are always 0, ergo the low 2 bits of the result are bits 16+17 of the multiplication.
Bits 2..15 are a mix of bits 2..15 xor bits 18..31.
A quick simulation shows 24350 unique outputs ergo on average 1.34 0.34 duplicates for every input value, not bad.
The maximum number of collisions is 6, but most numbers don't collide.
For all those numbers that don't collide you can uniquely lookup your input value in the lookup table (all this disregarding odd input values obviously).

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!

Why am I geting automatic broadcasting warning here?

function [theta, J_history] = gradientDescent(X, y, theta, alpha, num_iters)
m = length(y);
J_history = zeros(num_iters, 1);
for iter = 1:num_iters
## warning: product: automatic broadcasting operation applied
theta = theta - sum(X .* (X * theta - y))' .* (alpha / (m .* 2));
J_history(iter) = computeCost(X, y, theta);
end
end
This is my homework, but I don't ask you to do it for me (I actually think that I've either done it or am close to). I've red the manual where it mentions boradcasting, but I don't understand still, why am I getting a warning here?
The problem is that size(theta') is 1 2 and size(X) is m 2.
When you multiply them, Octave starts by multiplying X(1,1) by theta'(1,1) and X(1,2) by theta'(1,2). Then it moves to the second row of X and tries to multiply X(2,1) by theta'(2,1). But theta' doesn't have a second row so the operation makes no sense.
Instead of just crashing, Octave guesses that you meant to extend theta' so that it has as many rows as X does before beginning the multiplication. However, because it's guessing something, it feels that it should warn you about what it's doing.
You can avoid the warning by explicitly extending the length of theta before you start the multiplication with the repmat function.
repmat(theta',m,1) .* X
Since the warning says that broadcasting comes from a product operation, it will come from any of .* in the offending line. Which one I can't say without knowing the input values you're given to the function but assuming that:
X is a vector;
alpha is a scalar;
theta is a scalar.
my guess is the warning comes from X .* (X * theta - y))' specially since you're transposing the second part. Try to remove the transpose operator (which may cause an error if there's another bug on it -- I'm assuming that you do not want to perform broadcasting).

PTX - get value/address

I don't understand how the mov instruction works in PTX..
mov.type d, a
this moves a in d if a is a register or immediate value. By the way this can move into d the address of a if a is a variable in global, local or shared state space.
Let's suppose that a is a variable in global memory and points to an u64 with value 0x1... how do I store 0x1 into d since I can only get a's address??
I'm not sure on how to get a value instead of an address.. something like intel ASM's mov eax, ebx for address and mov eax, [ebx] for value (dereferencing ebx)
This answer should help you. It shows a worked example of using the mov and ld instructions to load a value from a pointer.