Thread: Need some help with C program writing

  1. #1
    Registered User
    Join Date
    Feb 2008
    Posts
    5

    Need some help with C program writing

    Hello, Im in need of some help getting started on writing a C program.

    A quick description of what It must do:

    I must Write a function "function (A, S)" which will accept the values for registers A and S and return the value of result Y to the main line. This function can use only bitwise AND, OR and shift instructions. Shift instruction should simulate connecting the output pins of one register into different input pins of another register. The main line program will call the " function (A, S)" from four different loops supplying the correct values for A and S and print the result Y.

    An example of the loop output:
    Loop1:
    Testing input line A, set B = 0
    Input = 0, S = 15, Y = 0
    Input = 1, S = 15, Y = 1
    Input = 2, S = 15, Y = 2
    Input = 3, S = 15, Y = 3
    ............................ (omitted lines)
    Input = 11, S = 15, Y = 11
    Input = 12, S = 15, Y = 12
    Input = 13, S = 15, Y = 13
    Input = 14, S = 15, Y = 14
    Input = 15, S = 15, Y = 15


    Loop 2:
    Testing input line A, set B = 15
    Input = 240, S = 15, Y = 0
    Input = 241, S = 15, Y = 1
    Input = 242, S = 15, Y = 2
    ..............................
    Input = 255, S = 15, Y = 15

    Loop 3:
    Testing input line B, set A = 0
    Input = 0, S = 240, Y = 0
    Input = 16, S = 240, Y = 1
    .............................
    Input = 240, S = 240, Y = 15

    Loop 4:
    Testing input line B, set A = 15
    Input = 15, S = 240, Y = 0
    Input = 31, S = 240, Y = 1
    .............................
    Input = 239, S = 240, Y = 14
    Input = 255, S = 240, Y = 15

    I have written the printvalues function for each loop:
    Code:
    void print_val()
    {
      register int i0;   	/* Input */
      register int i1;   	/* S */
      register int i2;	/* Y */
    
      printf ("\nInput = %d", i0);
      printf (" S = %d", i1);
      printf (" Y = %d", i2);
    
    }
    I just need some help getting started with the main program of the 4 loops. The values should already be stored in the program, I dont need any user input.

    A rough outline of what I need to do:
    Code:
    int S
    
    s =15
    
      for (i=0 to 255)
        {
          C=i&S;
          D=C>>4;
          C=C&15;
          Y=C|D;
        }
    Any suggestions to get me going?

    Thanks.

  2. #2
    and the Hat of Guessing tabstop's Avatar
    Join Date
    Nov 2007
    Posts
    14,336
    So, what is function(A, S) supposed to do, exactly? Why is there a B all the time? Where does Input go? I could guess, but it's more fun to make you figure it out.

  3. #3
    Registered User
    Join Date
    Feb 2008
    Posts
    5
    Quote Originally Posted by tabstop View Post
    So, what is function(A, S) supposed to do, exactly? Why is there a B all the time? Where does Input go? I could guess, but it's more fun to make you figure it out.
    the function (a,S) should create the 4 loops, and using the printvalues, print them out. The B is one of the 2nd set of the values, can be explained using this:

    The truth table is as follows:
    Code:
    Line 	A3  A2  A1  A0       B3  B2  B1  B0     S    Y3   Y2    Y1    Y0
    0	0     0      0     0       x    x     x    x      0      0     0     0     0
    1	0     0      0     1       x    x     x     x     0      0     0     0     1
    2	0     0      1     0       x    x     x     x     0      0     0     1     0
    	...................................................................................
    15	1    1      1      1       x    x     x     x     0      1     1      1     1
    
    16	x    x      x      x       0    0     0     0     1      0     0      0     0
    17	x    x      x      x       0    0     0     1     1      0     0      0     1
    	...................................................................................
    31	x    x      x      x       1    1     1     1      1     1     1      1     1
    A and B are inputs, Y is output, S is Select. x means “do not care”

    The S and Y's got a little left-shifted in my example.

    And as I mentioned, there is no User input needed. It should just run thru the loops and print the values.

  4. #4
    and the Hat of Guessing tabstop's Avatar
    Join Date
    Nov 2007
    Posts
    14,336
    Quote Originally Posted by The_PC_Gamer View Post
    I must Write a function "function (A, S)" which will accept the values for registers A and S and return the value of result Y to the main line.
    Quote Originally Posted by The_PC_Gamer View Post
    the function (a,S) should create the 4 loops, and using the printvalues, print them out.
    If I ask another time, will I get a third answer?

    If we take it as read then that A is really the low four bits of the parameter input, and B is really the high four bits of that parameter, do you understand:
    (1) How to build the parameter input out of A and B?
    (2) How to compute Y given the parameter input and S? (Note: this part is writing function(a,s).)
    (3) How to write a loop to get A or B through all of its possible values?
    (4) How to set up the loops in (3) to get the specific values of the input parameter that you're supposed to get in your examples?

  5. #5
    Registered User
    Join Date
    Feb 2008
    Posts
    5
    Quote Originally Posted by tabstop View Post
    If I ask another time, will I get a third answer?

    If we take it as read then that A is really the low four bits of the parameter input, and B is really the high four bits of that parameter, do you understand:
    (1) How to build the parameter input out of A and B?
    (2) How to compute Y given the parameter input and S? (Note: this part is writing function(a,s).)
    (3) How to write a loop to get A or B through all of its possible values?
    (4) How to set up the loops in (3) to get the specific values of the input parameter that you're supposed to get in your examples?
    ah arlight, this already seems to make more sense to me.

    As for your notes, (1) I just set the parameters of A and B to a value (1-15) and let it run, while the second value remains constant.
    (2) to get Y with input S, Lines 0 – 3: A = 0, 1, 2, 3; S = 3; Lines 4 – 7: A = 0*4, 1*4, 2*4, 3*4; S = 3*4 = 12
    (3) Would this not be the same as #1?
    (4) To get the specific values, I'd just set the loop to run through 1-15 for each case.

  6. #6
    and the Hat of Guessing tabstop's Avatar
    Join Date
    Nov 2007
    Posts
    14,336
    Quote Originally Posted by The_PC_Gamer View Post
    ah arlight, this already seems to make more sense to me.

    As for your notes, (1) I just set the parameters of A and B to a value (1-15) and let it run, while the second value remains constant.
    (2) to get Y with input S, Lines 0 – 3: A = 0, 1, 2, 3; S = 3; Lines 4 – 7: A = 0*4, 1*4, 2*4, 3*4; S = 3*4 = 12
    (3) Would this not be the same as #1?
    (4) To get the specific values, I'd just set the loop to run through 1-15 for each case.
    (1) Not at all. For instance if A=15 and B=2, your input parameter (the first number you have to feed to your function) is supposed to be 47. Do you know how to get 47 out of 15 and 2 (and remember, you are only allowed bit operations)?
    (2) You can't be thinking in terms of a loop; your function doesn't know anything about a loop! It just takes the two numbers (e.g., 47 and 15) and needs to get the output (2).
    (3) and (4) ok -- you'll have a loop on one number and the other the same.

  7. #7
    Registered User
    Join Date
    Feb 2008
    Posts
    5
    alright... well with that in mind, was I going in the right direction with my general code outline, which I posted in the very first post?

    Also, is this a good start for demonstrating the bitwise operations?

    Code:
    #include <stdio.h>
    
    int main()
    {
      int mask = 15, value, result, select;
    
      while(1)
        {
          printf ("enter value: ");
          scanf ("%d", &value);
          
          printf ("enter selection: ");
          scanf ("%d", &select);
    
          switch (select)
    	{
    	case 1: {
    	  result = value & mask;  /* bitwise AND */
    	  printf ("value is: %d\t result is: %d\n", value, result);
    	  break;
    	}
    	case 2: {
    	  result = value | mask;  /* bitwise OR */
    	  printf ("value is: %d\t result is: %d\n", value, result);
    	  break;
    	}
    	case 3: {
    	  result = value << 1;  /* left shift one bit */
    	  printf ("value is: %d\t result is: %d\n", value, result);
    	  break;
    	}
    	case 4: {
    	  result = value >> 1;  /* right shift one bit */
    	  printf ("value is: %d\t result is: %d\n", value, result);
    	  break;
    	}
    	case 5: return 0;
          }
        }
    }

  8. #8
    and the Hat of Guessing tabstop's Avatar
    Join Date
    Nov 2007
    Posts
    14,336
    Quote Originally Posted by The_PC_Gamer View Post
    alright... well with that in mind, was I going in the right direction with my general code outline, which I posted in the very first post?

    Also, is this a good start for demonstrating the bitwise operations?
    Well, the code proves you know what the bitwise operators look like. As to your outline, I think you'll be able to use the second line somewhere.

    Let's go over again and see what's what. This is how I am interpreting what you've posted your assignment to be (let me know if this is really wrong): you are to write a function that takes two inputs: a byte and another byte. The first byte is composed of two nybbles, B and A (B in the high order bits, A in the low); so for instance if B=2 and A=F, then our byte would be 2F. It appears that our second byte is always a mask with either all four low bits on or all four high bits on.

    So: first we need to construct our input bytes from B and A (this is where 2 plus 15 gets 47). Then we need to call the function with our input byte and our mask; then we need to perform whatever operation that function is supposed to do (presumably involving the bitmask), and return the value.

    Does all this match your idea of the assignment?

  9. #9
    Registered User
    Join Date
    Feb 2008
    Posts
    5
    I have created a diagram to help me show you what I mean

    Image

    The code has to simulate the functionality of a digital circuit called multiplexer.

    The Select line selects one of the n Inputs into the Output. All of the lines can be one or more bits.

  10. #10
    and the Hat of Guessing tabstop's Avatar
    Join Date
    Nov 2007
    Posts
    14,336
    Maybe I've triggered on something that is a side issue, but: your pictures and your loops keep talking about A and B and S, but your function only takes two parameters. Unless the assignment you gave at the beginning got mangled in the posting, you need to figure out how to combine A and B into one register ("Shift instruction should simulate connecting the output pins of one register into different input pins of another register.") to get it into your function, and then split the same way once you're there (or maybe take care of it in a different way). Once you get the split, then yes it's two ands and two ors and a shift (not necessarily in that order).

    This goes back to the example I quoted from you earlier: in the loop where A is fixed at 15, when B is 2 the input is 47. Do you see how to get that 47 using 15, 2, and no arithmetic? That will allow you to build the "input register."

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Writing a program to make a calendar
    By Northstar in forum C Programming
    Replies: 17
    Last Post: 11-07-2007, 11:34 AM
  2. writing a calendar program help please!!!
    By Newbie2006 in forum C Programming
    Replies: 7
    Last Post: 11-20-2002, 07:36 PM
  3. Replies: 5
    Last Post: 11-19-2002, 09:36 PM
  4. Help with a file writing program
    By pritesh in forum C Programming
    Replies: 3
    Last Post: 11-11-2001, 01:56 AM