Reputation: 55
So I have a txt file that I'm taking numbers in like 02324020 which is the machine code from the MIPS instruction: add $t0, $s1, $s2
When it's in binary and cut into sections of 6,5,5,5,5,6 the numbers taken are: 0,17,18,8,0,20
This gives the idea of the instruction as R[rd] = R[rs]+R[rt] and the number from 26-31 is opcode 0 meaning R format, rd=8, rs=17, rt=18
I'm trying to mask the bits so I only see the bits between 21-25 and then receive the number that they are: 0,17,18,8,0,20 because shifting to the right: printf("\n%x\n", code >>26);
gives me the opcode 0 and tells me its the R format.
So now I'm trying to find the 17 but I don't know what to place after & 0b
in:
printf("\n%x\n", code >>21 & 0b____);
I could really use an example to find the remaining numbers from my number 02324020
This is my code so far:
#include <stdio.h>
#include <stdlib.h>
int main(int argc, char* argv[])
{
unsigned int code = 0;
FILE *fp;
fp = fopen(argv[1], "r+");
int result = fscanf(fp, "%x", &code);
printf("\n hex-1: %08X, decimal-1: %d \n", code, code);
printf("\n%x\n", code >>26);
printf("\n%x\n", code >>21 & 0x0b00000011111); //what can i do to change this
while (result != EOF)
{
result = fscanf(fp, "%x", &code);
printf("\n read hex: %08X, decimal: %d \n", code, code);
if(feof(fp))
{
puts("EOF");
break;
}//if
}//while
}
Upvotes: 1
Views: 106
Reputation: 2978
To define a binary number, you'd use 0b___
(as you've mentioned in your answer), not 0x0b0___
.
To extract the number you need, you'd have to bit-shift code
so that the bits you're interested in are at the right-most end. Then OR
only those bits you're interested in. Putting this together, you have:
printf("\n%x\n", code >> 26);
printf("\n%x\n", code >> 21 & 0b11111);
printf("\n%x\n", code >> 16 & 0b11111);
printf("\n%x\n", code >> 11 & 0b11111);
printf("\n%x\n", code >> 6 & 0b11111);
printf("\n%x\n", code & 0b111111);
Upvotes: 1