MIPS: Calculating BEQ into Hexadecimal Machine Code - mips

I have an assignment where I have to convert MIPS instructions into its hexadecimal machine code. I know how to convert the add, addi, lw, etc. instructions just fine, but when it gets to instructions like beq, I get confused. How would I convert this beq to hex?
0x00400108 beq $t3, $t5, NEXT
0x0040010C j END
where the address of NEXT is
0x0040011C
?
What I've tried:
beq opcode = 4
$t3 = register 11
$t5 = register 13
NEXT = 0x0040011C - 0x0040010C = 10 (hex) = 16 (decimal)
4 11 13 16 (decimal)
000100 01011 01101 0000 0000 0000 1000 (convert to binary)
0001 0001 0110 1101 0000 0000 0000 1000 (group into fours)
1 1 6 D 0 0 0 8 (hexadecimal)
but it's wrong...

After spending a long time being dumb, I've found the correct answer.
The summarized code:
beq $t3, $t5, NEXT
[instruction 1]
[instruction 2]
[instruction 3]
[instruction 4]
NEXT: [instruction 5]
As Michael said, the offset is the number of words from the instruction following the branch instruction. Instruction 1 is the following instruction after beq, so start counting from there till NEXT. There are 4 instructions from instruction 1 and NEXT, so the format for beq is now:
op | rs | rd | 16-bit constant or address
000100 | 01011 | 01101 | 0000 0000 0001 0000
Where rs is $t3 and rd is $t5.
Regrouped and converted into hex:
0001 | 0001 | 0110 | 1101 | 0000 0000 0001 0000
1 | 1 | 6 | D | 0 0 1 0
So the hexadecimal representation is 116D0010. Cheers.
Edit:Instructions are Words, 4 Instructions * 4 Bytes = 16 bytes

Since the instructions have a offset of 3 commands from where the pc put in 3 and not 4 into the offset. Thus the binary rep is 0001 | 0001 | 0110 | 1101 | 0000 0000 0000 0011 and not what the edited answer says about multiplying by 4.
Check out the example in this pdf: https://ai.berkeley.edu/~cs61c/sp17/lec/11/lec11.pdf

Related

Binary subset rank with maximum k consecutive 0

Consider N = 6 bits
We have a set S = 2^6 = 64 binary combinations.
We want to consider a subset T of the set S based on the following criteria -
There must be <= k consecutive 0s in the subset T.
Accordingly,
If the binary number is present in subset T, calculate its rank (in binary format)
If the binary number is not present in subset T, return 0 (in binary format)
Note: We want to calculate the rank only by iterating the N bits of the given binary number, and NOT by iterating 2^N numbers.
Here is the pseudo-code -
string GetBinaryRank(string strBinaryNumber, int max_k_0s)
{
string strRankBinary = "";
if(strBinaryNumber.Contains(<= max_k_0s))
{
//calculate strRankBinary by iterating over the N bits of strBinaryNumber
for(int i = 0; i < strBinaryNumber.Length; i++)
{
if(max_k_0s == 1)
{
//calculate rank
//binary rank will be 5 bits
strRankBinary = "xxxxx";
}
else if(max_k_0s == 2)
{
//calculate rank
//binary rank will be 6 bits
strRankBinary = "yyyyyy";
}
}
}
else
{
strRankBinary = "000000";
}
return strRankBinary;
}
We want to do the rank and unrank operations for the binary number, and determine the maximum number of bits required for "strRankBinary"
Also, we want to generalize the code for "N"-bit numbers and maximum "k" consecutive 0s.
Depending on the maximum rank value, the strRankBinary must contain only the required number of maximum bits.
In the examples given below,
If max_k_0s = 1, maximum rank will be 21, which is 10101 (5 bits) in
binary.
If max_k_0s = 2, maximum rank will be 44, which is 101100 (6 bits) in
binary.
Example 1 (max 1 consecutive 0s in 6-digit binary value):
Number Rank Binary rank
-------------------------------
000000 0 00000
000001 0 00000
000010 0 00000
000011 0 00000
000100 0 00000
000101 0 00000
000110 0 00000
000111 0 00000
001000 0 00000
001001 0 00000
001010 0 00000
001011 0 00000
001100 0 00000
001101 0 00000
001110 0 00000
001111 0 00000
010000 0 00000
010001 0 00000
010010 0 00000
010011 0 00000
010100 0 00000
010101 1 00001
010110 2 00010
010111 3 00011
011000 0 00000
011001 0 00000
011010 4 00100
011011 5 00101
011100 0 00000
011101 6 00110
011110 7 00111
011111 8 01000
100000 0 00000
100001 0 00000
100010 0 00000
100011 0 00000
100100 0 00000
100101 0 00000
100110 0 00000
100111 0 00000
101000 0 00000
101001 0 00000
101010 9 01001
101011 10 01010
101100 0 00000
101101 11 01011
101110 12 01100
101111 13 01101
110000 0 00000
110001 0 00000
110010 0 00000
110011 0 00000
110100 0 00000
110101 14 01110
110110 15 01111
110111 16 10000
111000 0 00000
111001 0 00000
111010 17 10001
111011 18 10010
111100 0 00000
111101 19 10011
111110 20 10100
111111 21 10101
Example 2 (max 2 consecutive 0s in 6-digit binary value):
Number Rank Binary rank
-------------------------------
000000 0 000000
000001 0 000000
000010 0 000000
000011 0 000000
000100 0 000000
000101 0 000000
000110 0 000000
000111 0 000000
001000 0 000000
001001 1 000001
001010 2 000010
001011 3 000011
001100 4 000100
001101 5 000101
001110 6 000110
001111 7 000111
010000 0 000000
010001 0 000000
010010 8 001000
010011 9 001001
010100 10 001010
010101 11 001011
010110 12 001100
010111 13 001101
011000 0 000000
011001 14 001110
011010 15 001111
011011 16 010000
011100 17 010001
011101 18 010010
011110 19 010011
011111 20 010100
100000 0 000000
100001 0 000000
100010 0 000000
100011 0 000000
100100 21 010101
100101 22 010110
100110 23 010111
100111 24 011000
101000 0 000000
101001 25 011001
101010 26 011010
101011 27 011011
101100 28 011100
101101 29 011101
101110 30 011110
101111 31 011111
110000 0 000000
110001 0 000000
110010 32 100000
110011 33 100001
110100 34 100010
110101 35 100011
110110 36 100100
110111 37 100101
111000 0 000000
111001 38 100110
111010 39 100111
111011 40 101000
111100 41 101001
111101 42 101010
111110 43 101011
111111 44 101100

Loop storing 1s complement

Suppose a computer uses 4-bit one’s complement numbers. Ignoring overflows, what value will be stored in the variable j after the following pseudocode routine terminates?
0 → j // Store 0 in j
-3 → k // Store -3 in k
while k ≠ 0
j = j + 1
k = k - 1
end while
'''
I thought the loop would continue forever, but the answer in the back of the book says:
J (Binary) K (Binary)
0 0000 -3 1100
1 0001 -4 1011 (1100 + 1110) (where last carry is added to sum doing 1's complement addition)
2 0010 -5 1010 (1011 + 1110)
3 0011 -6 1001 (1010 + 1110)
4 0100 -7 1000 (1001 + 1110)
5 0101 7 0111 (1000 + 1110) (This is overflow -- but you can ignore)
6 0110 6 0110
7 0111 5 0101
-7 1000 4 0100
-6 1001 3 0011
-5 1010 2 0010
-4 1011 1 0001
-3 1100 0 0000
I do not understand how the k can flip to positives and the j to negatives. I do understand how they got the rows of where j =0,1,2,3,4

How to understand IR checksum

I'm trying to understand how crc is calculated of below data packets of unbranded air con remote controller. it's last 4 bits seem to be crc and data packet is 8 bytes long. I can understand how to extract the data packet but not crc. First 3 bits are fixed.
data pattern eg 1) 0010 0100 0100 0011 0000 0000 0000 0000 0000 0000 1110
data pattern eg 2) 0010 1000 0101 0011 0000 0000 0000 0000 0000 0000 1101
data pattern eg 3) 0010 1000 0110 0011 0000 0000 0000 0000 0000 0000 1100
data pattern eg 4) 0010 1000 0111 0011 0000 0000 0000 0000 0000 0000 1011
data pattern eg 5) 0010 1000 1000 0011 0000 0000 0000 0000 0000 0000 1010
data pattern eg 6) 0010 1000 1001 0011 0000 0000 0000 0000 0000 0000 1001
data pattern eg 7) 0010 1000 1010 0011 0000 0000 0000 0000 0000 0000 1000
data pattern eg 8) 0010 1000 1011 0011 0000 0000 0000 0000 0000 0000 0111
data pattern eg 9) 0010 1000 0000 0011 0000 0000 0000 0000 0000 0000 0010
data pattern eg 10) 0010 1000 0001 0011 0000 0000 0000 0000 0000 0000 0001
data pattern eg 11) 0010 1000 0010 0011 0000 0000 0000 0000 0000 0000 0000
data pattern eg 12) 0010 1000 0011 0011 0000 0000 0000 0000 0000 0000 1111
sample image
I think this might be just an inverted sum.
Adding up all but last 4 bits, and then ((x&1111)^1111):
0010 + 0100 + 0100 + 0011 = 1101 -> 0010*
0010 + 1000 + 0101 + 0011 = 10010 -> 1101
0010 + 1000 + 0110 + 0011 = 10011 -> 1100
0010 + 1000 + 0111 + 0011 = 10100 -> 1011
0010 + 1000 + 1000 + 0011 = 10101 -> 1010
0010 + 1000 + 1001 + 0011 = 10110 -> 1001
0010 + 1000 + 1010 + 0011 = 10111 -> 1000
0010 + 1000 + 1011 + 0011 = 11000 -> 0111
0010 + 1000 + 0000 + 0011 = 1101 -> 0010
0010 + 1000 + 0001 + 0011 = 1110 -> 0001
0010 + 1000 + 0010 + 0011 = 1111 -> 0000
0010 + 1000 + 0011 + 0011 = 10000 -> 1111
Note that first sample is incorrect (should be 1110), but the rest match. I'm not sure if that's an error in my algorithm, or error in input data (looking at the photo, I don't see where 0010 0100 appears).
Also note that samples provided are very similar (only bits 8,9,10,11 differ, if we exclude first sample), so there are probably many ways to end up with "a solution".

01 1011 - 11 1101 = ? use 2's complement

I'm trying to figure out this doozie: 01 1011 - 11 1101
use 2's complement to solve, 6bits signed.
This is what I tried:
range of 6bits: -32 to 31
01 1011 = 27
11 1101 = -29
27 -(-29) = 56 (overflow)
11 1101 -- 2s complement --> 10 0011
so
01 1011 + 10 0011 = (missing bit)11 1110 = -2!
Any luck?
Ok so I think I figured it out:
my first mistake was that 11 1101 is not -29 but -3.
SO:
01 1011 = 27
11 1101 = -3
27 -(-3) = 30
I can do a reverse 2's complement of -3 which is:
11 1101 - 1 = 11 1100
then I flip 11 1100 which gives me 00 0011.
so
01 1011 + 00 0011 = 01 1110 = 30
:)

Essential Prime Implicants and Minterm Expressions

I have an exam for a university course shortly, and upon reviewing one of my assignments I have come to realize that I don't understand why I have lost marks/how to do a couple of questions. Hopefully someone can shed some light on the subject for me! The questions were as follows:
Use K-Maps to simplify the following boolean functions (Note that d() represents a don't care minterm):
1.) F(w, x, y, z) = ∑(1,3,5,7,11,12,13,15)
My answer:
Prime Implicants: yz, w'z, xz, wxy'
Essential Prime Implicants: yz, w'z, wxy'
Possible Minimal Expression(s): yz + w'z + wxy'
Answer sheet (professor's answer):
Prime Implicants: yz, w'z, xz, wxy'
Essential Prime Implicants: Same as prime implicants
Possible Minimal Expression(s): yz + w'z + xz + wxy'
2.) F(w, x, y, z) = ∑(1,2,5,7,12) + d(0,9,13)
My answer:
Prime Implicants: w'x'z', y'z, w'xz, wxy', w'x'y'
Essential Prime Implicants: w'x'z', w'xz, wxy'
Possible Minimal Expression(s): w'x'z' + w'xz + wxy'
Answer sheet (professor's answer):
Prime Implicants: w'x'z', y'z, w'xz, wxy', w'x'y'
Essential Prime Implicants: w'x'z', w'xz, wxy'
Possible Minimal Expression(s): w'x'z' + w'xz + wxy' + y'z
I suppose I should add that I asked my professor after he returned my assignment to me if he had made a mistake and explained my point of view. He seemed pretty certain that he was correct, but couldn't really explain why because he speaks poor English (well, that's university for you..).
Thanks in advance to anyone who can help! This has been quite a task to try to figure out on my own!
1.) You are correct: XY is no essential prime implicant. It does not cover any minterm which is not covered by other prime implicants. Thus, is can be removed from the solution.
The Karnaugh map might help to see this more clearly:
wx
00 01 11 10
+---+---+---+---+
00 | 0 | 0 | 1 | 0 |
+---+---+---+---+
01 | 1 | 1 | 1 | 0 |
yz +---+---+---+---+
11 | 1 | 1 | 1 | 1 |
+---+---+---+---+
10 | 0 | 0 | 0 | 0 |
+---+---+---+---+
I am not sure what is meant by "possible minimal expressions". If you enumerate all potential encircled blocks in the map, XY would also be one.
2.) Your solution and the official solution are the same.
Again - like in 1.) - the solution sheet also includes the non-essential terms as "possible minimal expressions".
F = w x y' + w' x z + w' x' z' + w' x' y'
1.) F(w, x, y, z) = ∑(1,3,5,7,11,12,13,15)
wx
00 01 11 10
+---+---+---+---+
00 | 0 | 0 | 1 | 0 |
+---+---+---+---+
01 | 1 | 1 | 1 | 0 |
yz +---+---+---+---+
11 | 1 | 1 | 1 | 1 |
+---+---+---+---+
10 | 0 | 0 | 0 | 0 |
+---+---+---+---+
note : here essential prime implicants are the prime implicants which are formed by
wxyz
1100
1101
result is wxy'
if you compute the prime implicant which is formed 3, 7, 11 and 15
wxyz
0011
0111
1111
1011
result is yz
if you compute the prime implicant which is formed 1, 5, 3 and 7
wxyz
0001
0101
0011
0111
result is w'z
so essential prime implicants are wxy', yz and w'z
xz is not a essential prime implicant because prime implicant which is formed by 5, 13, 7, and 15 is redundant prime implicant