Can someone tell me what this mips question means in english? - byte

Find the shortest sequence of MIPS instructions that extracts bits 16 down to 11 from register $t0 and uses the value of this field to replace bits 31 down to 26 in register $t1 without changing the other 26 bits of register $t1.
can you help explain this more clearly? or help me to do it?

Find the shortest sequence of MIPS instructions that extracts bits 16 down to 11 from register $t0 and uses the value of this field to replace bits 31 down to 26 in register $t1 without changing the other 26 bits of register $t1. can you help explain this more clearly? or help me to do it?
It says that in a 32-bit MIPS register $t0 where the bits are indexed as 31(most significant bit)-0(least significant bit) take the subset of bits indexed by the range 16-11 (inclusive). So,
31 30 29 28 27 26 25 24 23 22 21 20 19 18 17 (16 15 14 13 12 11) 10 9 8 7 6 5 4 3 2 1 0
And, copy them into the register $t1's bit's indexed by the range "31 down to 26",
(31 30 29 28 27 26) 25 24 23 22 21 20 19 18 17 16 15 14 13 12 11 10 9 8 7 6 5 4 3 2 1 0
The rest of the bit's in $t1 (25-0) should not change.
Can you think of a way of doing it with srl, sll, or (shift right, shift left, or)?
srl $t0, $t0, 11 # t0 = t0 >> 11
sll $t0, $t0, 26 # t0 = t0 << 26
sll $t1, $t1, 5
srl $t1, $t1, 5
or $t1, $t1, $t0
Do you see what we did here?

I don't know MIPS, but this is all about bit manipulation:
Looks like registers in MIPS are 32 bits.
Let's say bits are numbered 0 to 31, the most significant one is the leftmost one and it is bit number 31.
To extract bits 16..11 (6 bits in total) you have to do a 6 bit AND, like $t0 & 0b00000000 00000001 11111000 00000000.
To replace the bits in $t1 it is trickier: first you have to set bits 31..26 to zero, like $t1 & 0b00000011 11111111 11111111 11111111
Next you have to left-shift the source 15 bits (from 16 to 31 there are 15 bits), like $t0 << 15.
And finally you need to OR'em, like $t1 = $t1 | $t0

Related

trying to understand how checksum is calculated

I am looking at this page and I am not sure how the author is calculating the checksum. I would contact the author directly, but don't have his email address (its not listed in github).
This is a simple example of a packet with no variables. The author calculates the checksum to be 120 (I assume this is hex as all his other values are in hex). The sum of all the bytes is 0xBA hex or 186 base(10). His notes say "Checksum Low Bit, This bit is checksum of 1-5 bits (MOD 256, if necessary)" but I am not getting what he is saying and I can't figure out how to get to his answer.
Get Version / Return Name
Byte 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24
Request 16 2 80 20 2 120 16 3
Byte Sample hex Definition
hex (B10)
==== ==== ===== =============================
1 0x16 (22) Preamble 1
2 0x02 (2) Preamble 2
3 0x80 (128) Destination = Chlorinator
4 0x20 (32) Command = Get Name
5 0x02 (2) Not sure. Intellitouch uses 2. Aquarite uses 0. Any of them seem to work.
6 120 Checksum Low Bit, This bit is checksum of 1-5 bits (MOD 256, if necessary)
7 0x16 (22) Post-amble 1
8 0x3 (3) Post-amble 2
Any suggestions would be most appreciated!
Turns out that the commentors were 100% correct: the numbers were express in decimal, not hex as I assumed.

How to count the number of cycles in code by hand?

We are learning pipeline processing and I have an exercise, where I have to count the number of cycles a MIPS code runs for - by hand. We have the following code in C and MIPS:
0. for (i=0; i<10; i++)
1. som = som + a[i];
0. add $t0, $zero , $zero
1. L:
2 sll $t1 , $t2, 2
3 add $t1 , $a0 , $t1
4 lw $t1, 0($t1)
5 add $v0, $v0, $t1
6 slti $t2, $t0, 9
7 addi $t0, $t0, 1
8 bne $t2, $zero, L
9 nop
The code inside the loop has 7 instructions and it is executed 10 times, therefore we get 70 cycles. Then we have a stall cycles in line 5 because of the load use hazard on $t1. Again the code runs 10 times, thus an additional 10 cycles. In total we now have 80 cycles. Because of the bne, we have 9 flushes (not 10 because after 9 we don't come back down). Therefore, now we have 89 cycles. We then have 1 cycle for the one NOP and 1 cycle for the initialisation of the variable i. This gives us a total of 91 cycles. In the MIPS reference sheet, it says to always add 4, because of the first line of code, thus bringing our total to 95 cycles.
Assuming I'm right, which I should be, is there a faster way to count the number of cycles in any MIPS code? I could use the equation CPI = # of cycles / clock rate but assuming we're not given that information and only have the given code, is there a way? I found that if you use the following equation, you get the right answer for the given code.
# of cycles = 4 + # of instructions x (# of pipeline levels - 1) + # of NOPs
However, I'm not sure if this works in general or I got lucky with my specific example. BTW - # refers to the number of.

Data files did not show the actual output

I have two .dat files. They are world.dat and sensor_data.dat. I have a folder name in D: drive named tutorial. Within this tutorial file, there are two folders data and code. Now in the data folder, there are two files as I mentioned earlier world.dat and sensor_data.dat. In the code folder, there is a file name main.m as it is a Matlab file.
The code that is written on this file(main.m) is
clc;
clear;
close all;
% Read *.dat files containing landmark data
landmarks = fopen('../data/world.dat');
landmarks_data = fread(landmarks);
% Read *.dat files containing odometry and range-bearing sensor data
data = fopen('../data/sensor_data.dat');
data_data = fread(data);
But when I print landmarks_data and data_data they print something other than that is written on those two files(world.dat,sensor_data.dat)
world.dat file contains:
1 2 1
2 0 4
3 2 7
4 9 2
5 10 5
6 9 8
7 5 5
8 5 3
9 5 9
My output:
>> landmarks_data
landmarks_data =
49
32
50
32
49
10
50
32
48
32
52
10
51
32
50
32
55
10
52
32
57
32
50
10
53
32
49
48
32
53
10
54
32
57
32
56
10
55
32
53
32
53
10
56
32
53
32
51
10
57
32
53
32
I don't know where they get those data? The same thing happened for data_data variable.
Need help to fix the problem.
You are getting the ASCII values of the characters in the file.
ASCII value of 1 equals 49.
ASCII value of ' ' (space) equals 32.
ASCII value of 2 equals 50...
fread reads data from binary file, and you are using fread for reading a text file. The binary value of a text character is the ASCII code (it can also be a UNICODE value).
In case you want to read the data as text, and keep the matrix structure, you can use readmatrix function:
landmarks = readmatrix('../data/world.dat');
Result:
landmarks =
1 2 1
2 0 4
3 2 7
4 9 2
5 10 5
6 9 8
7 5 5
8 5 3
9 5 9
Remark: In case your MATLAB version is before R2019a, you can use dlmread instead.

How to convert the contents of a cell array into specific numbers?

I want to convert the contents of column 1 in a cell array into specific numbers as mentioned in the code but doesn't work.
col=1;
[row_patho,col_patho]=size(data);
for i=1:1:row_patho
if Pro_patho_data{i,col}>=70
Pro_patho_data(i,col)=11;
end
end
Input:
73 t2b 7
59 t1c 9
58 t3b 7
83 t3b 8
wanted_output:
11 t2b 7
59 t1c 9
58 t3b 7
11 t3b 8

How can I define a verb in J that applies a different verb alternately to each atom in a list?

Imagine I've defined the following name in J:
m =: >: i. 2 4 5
This looks like the following:
1 2 3 4 5
6 7 8 9 10
11 12 13 14 15
16 17 18 19 20
21 22 23 24 25
26 27 28 29 30
31 32 33 34 35
36 37 38 39 40
I want to create a monadic verb of rank 1 that applies to each list in this list of lists. It will double (+:) or add 1 (>:) to each alternate item in the list. If we were to apply this verb to the first row, we'd get 2 3 6 5 10.
It's fairly easy to get a list of booleans which alternate with each item, e.g., 0 1 $~{:$ m gives us 0 1 0 1 0. I thought, aha! I'll use something like +:`>: #. followed by some expression, but I could never quite get it to work.
Any suggestions?
UPDATE
The following appears to work, but perhaps it can be refactored into something more elegant by a J pro.
poop =: monad define
(($ y) $ 0 1 $~{:$ y) ((]+:)`(]>:) #. [)"0 y
)
I would use the oblique verb, with rank 1 (/."1)- so it applies to successive elements of each list in turn.
You can pass a gerund into /. and it applies them in order, extending cyclically.
+:`>: /."1 m
2
3
6
5
10
12
8
16
10
20
22
13
26
15
30
32
18
36
20
40
42
23
46
25
50
52
28
56
30
60
62
33
66
35
70
72
38
76
40
80
I spent a long time and I looked at it, and I believe that I know why ,# works to recover the shape of the argument.
The shape of the arguments to the parenthesized phrase is the shape of the argument passed to it on the right, even though the rank is altered by the " conjugate (well, that is what trace called it, I thought it was an adverb). If , were monadic, it would be a ravel, and the result would be a vector or at least of a lower rank than the input, based on adverbs to ravel. That is what happens if you take the conjunction out - you get a vector.
So what I believe is happening is that the conjunction is making , act like a dyadic , which is called an append. The append alters what it is appending to what it is appending to. It is appending to nothing but that thing still has a shape, and so it ends up altering the intermediate vector back to the shape of the input.
Now I'm probably wrong. But $,"0#(+:>:/.)"1 >: i. 2 4 5 -> 2 4 5 1 1` which I thought sort of proved my case.
(,#(+:`>:/.)"1 a) works, but note that ((* 2 1 $~ $)#(+ 0 1 $~ $)"1 a) would also have worked (and is about 20 times faster, on large arrays, in my brief tests).

Resources