search

# 4-Year Strategic Management Plan.

User Generated

Accounting

Homework

### Rating

Showing Page:
1/4
4 digit HEX number in MIPS
The first one is an hex number it is straightforward to convert it into binary. Every HEX digit can be converted in 4 binary digit as in the
following example
Ex \$\$_7=2^2+2^1+2^0=0111_\$\$
For your hex number 0x71014802 is
0x71014802 => 0111 0001 0000 0001 0100 1000 0000 0010
7 1 0 1 4 8 0 2
Regarding to a conversion from decimal to binary I suggest you converting the decimal in hex and then hex to binary, let's do an
example with a decimal (I choose 71014802 DEC)
71014802 => 0x43B9992 => 0000 0100 0011 1011 1001 1001 1001 0010
0 4 3 B 9 9 9 2
another way to convert a decimal number into a binary one is by dividing by two and picking the remainder... but it is a longer procedure
(you can find it here)
Anyway if the opcode is in a fixed position (26-31) the opcode can be simply obtained through a mask and shift right as follow:
opcode=(reg>>26)&0x3F
An algorithm to print a binary number can be like this:
void print_binary(int n) {
while (n) {
char bit = n & 0x1;
putchar (bit+'0');
n >>= 1;
}
putchar('\n');
}
char bits = (num >> (sizeof(num)*8 - 6)) & 0x3F;

0x3F = 00111111 that's six bits pushed all the way to the left, giving you the result you're looking for.
EDIT: An more detailed explanation. What we are trying to do is to get the six leftmost bits of an integer. To determine if a bit is flipped or
not (i.e. 1 or 0) we and it with 1. Getting six bits of a number is done by constructing an integer with six flipped bits, 00111111,
or 0x3F, and anding it with our number like so: num & 0x3F. This will give the six righmost bits of the integer, to get the bits on the left
side, we shift our number all the way to the left, and run the same and operation: (num >> (sizeof(num)*8 - 6)) & 0x3F.
\$ cat temp.c
#include <stdio.h>
#include <stdint.h>
int main() {
uint32_t num = 0x71014802;
char c = (num >> (sizeof(num) * 8 - 6)) & 0x3F;
printf("0x%X\n", c);
return 0;
}
\$ gcc temp.c && ./a.out
0x1C



 !
"
## 


\$
""%""&""'
' ()
"!*+,-*'(
./(.
 )
""0 10 1
2./2.3

45! 
""'' ()
"!*+,-*'(

66
5) !5) !(6
787 !(
2\$80\$//

80\$//) !80\$//) !
"/
2/
4!83)
.
!9!9
/#-/#-

!!:



\$  !;


""
!\$<=

User generated content is uploaded by users for the purposes of learning and should be used following Studypool's honor code & terms of service.
Review
Review

Anonymous
Really useful study material!

Studypool
4.7
Trustpilot
4.5
Sitejabber
4.4