Hi all,

I am new to C, and till now have only done python (a years worth at university), and am finding myself running into walls whenever i try to do seemingly simply stuff. One of the main issues in comparison is the lack of a interpreter and Ch is only of limited help. I figured that here at least i could get an idea of why im getting nowhere. Please excuse my long-windedness.

I have been tasked with creating a limited mips simulator in c that will take in >31 8 bit hex numbers (31 initial register values with $0=0 and some instructions) e.g. 00000000 000FF000 00... by reading them as input from another file (i am refering to calc.c < input_numbers), then perform the instructions input and print out the resulting register values. We are only tasked with performing 5 operations addu/subu (all unsigned) and the like and can ignore any others. We have also been advised to make use of uint32_t.

I have beat my head against the wall long enough to come up with a way to read in and then print out the values input using scanf and
a while loop utilizing getchar/EOF and scanf==1. I don't want to be more specific as i doubt my lecturer would want me to go into details on a public forum.

So now that i have the hex numbers read in (i used two arrays, one for the register initial values and another for instructions), how do i go about converting them to binary so that i can then (somehow, i am guessing by using bit masks to cut them down and if statements to match to the addu/subu/) play with them (verification would be nice btw...!) ?

I have a number like 00FF00FF stored in register[1]. I want to convert it to register2[1] = 00000000111111110000000011111111 Using a for loop to iterate over all the elements in the array, i then assign it to a temp variable and WHAT? The lack of the python "for i in string" is painful! I have tried looking at binary/decimal/hex converters here and elsewhere but to no avail. I feel there should be some sort of simple solution - am i overlooking something obvious?

Thanks in advance - and i hope i conveyed that i have been trying to solve it myself, i normally don't have to resort to (posting on) forums.

I should add, as i am also trying it out myself at the moment, how should i go about in c dealing with binary numbers? Lets say i assign 1000111 (bin) to a variable number, how do i tell c not to treat it as the decimal number 1000111? For hex you preface with 0x.

Second question, after assigning number = 0xf, does that allow for treatment as a hex number in any special way? I know you can printf using argument %d to treat it as decimal, but without printing can you do the equivalent?

Thanks

I have crafted a script that will take a hex number and create an array with the binary equivalent. Some issues: it wont work with FFFFFFFF but returns -1. I have no idea how to get the result in a workable format from the array. I would be happy to pm the code... if someone was interested.

I have, but i am guessing by the fact the you are pointing it out that i missed something? I would go through them point by point but there are a lot... Any issue?

Do not post homework problems expecting a quick answer
without showing any effort yourself. This especially pertains
to the software development forums. For easy readability, always
wrap programming code within posts in code (code blocks)
and icode (inline code) tags.

Without posting what you've done, all you've asked us to do is write it for you. If you have a question, post the pertinent code section and give us details of what you're having trouble with.

Edited 3 Years Ago by Reverend Jim: Fixed formatting

Ah. I had hoped to avoid specifics and use whatever "theory" answer i could get to then go away and craft my own code (my issue is with arrays/c rather than with a specific broken program, what i was getting at in the last three paras). I can see how that might be too ambiguous to be practical. Do you mind if i PM you some code (not for a homework answer...)?

Regardless the idea is i have an array with

inst[0] = 0xFFFFF4

How can i convert this to binary, or even treat any number in c as binary to use bit operations on them (like 1<<y). I have written code that returns an array with the answer 101111... as

array[0] =1
array[1] =0.

I want to know a) how to put all of that into a single binary number in a single array entry or b) whatever equivalent way exists to play with binary numbers.

live and learn...

Do not post homework problems expecting a quick answer
without showing any effort yourself[. This especially pertains
to the software development forums. For easy readability, always
wrap programming code within posts in code (code blocks)
and icode (inline code) tags.

Without posting what you've done, all you've asked us to do is write it for you. If you have a question, post the pertinent code section and give us details of what you're having trouble with.

Edited 3 Years Ago by Reverend Jim: Fixed formatting

Do you mind if i PM you some code (not for a homework answer...)?

Against the rules... And it helps no one else. We are not a personal tutor service.

How can i convert this to binary, or even treat any number in c as binary to use bit operations on them (like 1<<y).

It already is binary. Everything in a computer is binary. Only the output is converted to a human-readable format.

if you have an integer with the value 65 and output it as:
Decimal you get 65
Hex you get 41
Octal you get 61
Binary you get 01000001
Character you get A

Once you grasp this concept you will be ready for the next step.

I get there, if slowly...! Thanks for the insight. It means i have been chasing my tail all day as the hex2bin isn't needed. And seeing as thats the case, and now it isn't essential to the project i guess i can post the code i had.

Could you tell me why the code couldn't handle FFFFFFFF (with scanf and printf included of course)? I figure its because of the int type and 32 bits, but i couldn't correct for that nonetheless.

Thanks again!

// Function to raise to power of n, to support hex2bin function from
	// http://www.java2s.com/Code/C/Function/Arecursivepowerfunction.htm
	/* Function to raise x to the power n.*/
	double power(double x, int n)
	{
	  if(n == 0)
	    return 1.0;
	  else
	    return x * power( x , n - 1 );
	}

	// create array of binary numbers from hex number
	int hex2bin(int number, int h)
		{	 int x, y, binary[31];
			 x = y = 0;

			 for(y = 32; y >= 0; y--)
			 {
				 x = number / (power(2, y));
				 number = number - x * (power(2, y));
				 binary[(32-y)] = x;
//			  	 printf("%d", binary[(32-y)]);
				 return binary;

Against the rules... And it helps no one else. We are not a personal tutor service.


It already is binary. Everything in a computer is binary. Only the output is converted to a human-readable format.

if you have an integer with the value 65 and output it as:
Decimal you get 65
Hex you get 41
Octal you get 61
Binary you get 01000001
Character you get A

Once you grasp this concept you will be ready for the next step.

Line 14 coupled with lines 17 and 21 result in a big error. int binary[31]; defines an array of integers with 31 entries in the array indexed as 0 - 30. Certainly not enough to hold a value for each bit in a 32 bit value. for(y = 32; y >= 0; y--) You iterate through the loop 33 times once weach for every value between 32 and 0. Clearly an error when trying to evaluate a 32 bit integer. binary[(32-y)] = x; combined with the previous 2 errors you access array indexes 31 and 32 when y is respectively 1 and 0 both of which are out of bounds array accesses and result in undefined behaviour.

On the algorithm had it been coded correctly. All that raising to powers and reverting to floating point arithmetic is incredibly inefficient not to mention if you were going to do that why not use pow from the standard library rather than write your own.

However the whole affair can be much more simply and efficiently code by using the operators that work directly on bits, this set of operators include

bitwise AND &
bitwise OR |
bitwise NOT ~
bitwise XOR ^
shift left <<
shift right >>

You can use AND to isolate the value of specific bits you can use the shift operators to control which bit you are currently examining.

Hi,

This function convert Hex value to Binary:

int binary_conv(char *str, unsigned int val) {
char *ptr;
char ret_buf[33];
int cnt = 0;


ptr = &ret_buf[sizeof(ret_buf) - 1];
*ptr = '\0';


do {
*--ptr =

Edited 3 Years Ago by happygeek: fixed formatting

Comments
How can you leave just a fraction of the code?? and with no code tags?
This question has already been answered. Start a new discussion instead.