Packing data...

Collapse
This topic is closed.
X
X
 
  • Time
  • Show
Clear All
new posts
  • FMorales

    Packing data...

    Hello all, quick hopefully easy question. Here goes.

    I've started on the Art Of Assembly tutorial(s)
    (http://webster.cs.ucr.edu/Page_asm/0_ArtOfAsm.html)
    and got to the sample projects and kinda got stuck. I'm needing to use a
    language of my choosing (which
    i easily chose C) to do this project where it wants me to pack data into a
    16 bit variable. Now i am
    familiar with C to a pretty decent extent, just not bit manipulation. So
    what i'm needing help on is getting
    this data packed into an unsigned short int, data type.

    The example it gives is : 0100 0001 0101 1000 or 4158h
    0100 being a 4 for the month,
    00010 being 2 for the day, and
    1011000 being 88 for the year.

    These will come into my function as 3 seperate variables.I'm using 2 single
    characters for the month & day, then an array for the year.(they need to
    enter
    in say 1988 for the above example, and i just grab out the 88. Not a
    problem).
    So i would basically get something along the lines of, myFunc('4', '2',
    "1988");
    And i just need help getting those values in their correct "spot" packed
    into my
    16 bit data type. (I can get 1 of them, but after the first one i keep
    messing
    up the way it's packed in by trying to pack the next value in.). As i said
    i'm
    still learning this stuff, so i'm kinda picking up on things as i go. Any
    help,
    and or a comments are definatly welcome. I've been searching google and such
    for
    help but so far have found none. Thought i'd give here a shot. Thx in
    advance all,
    ....Frank M...



  • Nick Austin

    #2
    Re: Packing data...

    On Mon, 25 Aug 2003 13:02:56 GMT, "FMorales" <altf2o@comcast .net>
    wrote:
    [color=blue]
    >Hello all, quick hopefully easy question. Here goes.
    >
    >I've started on the Art Of Assembly tutorial(s)
    >(http://webster.cs.ucr.edu/Page_asm/0_ArtOfAsm.html)
    >and got to the sample projects and kinda got stuck. I'm needing to use a
    >language of my choosing (which
    >i easily chose C) to do this project where it wants me to pack data into a
    >16 bit variable. Now i am
    >familiar with C to a pretty decent extent, just not bit manipulation. So
    >what i'm needing help on is getting
    >this data packed into an unsigned short int, data type.
    >
    >The example it gives is : 0100 0001 0101 1000 or 4158h
    >0100 being a 4 for the month,
    >00010 being 2 for the day, and
    >1011000 being 88 for the year.[/color]

    Bitfield packing is done with the left-shift operator and the
    bitwise inclusive or operator:

    unsigned short month = 4;
    unsigned short day = 2;
    unsigned short year = 88;
    unsigned short dmy;

    dmy = month << 12 | day << 7 | year;

    However it's a good idea to add range checks and/or masking so
    that one field cannot overflow into the adjacent field.
    [color=blue]
    >These will come into my function as 3 seperate variables.I'm using 2 single
    >characters for the month & day, then an array for the year.(they need to
    >enter
    >in say 1988 for the above example, and i just grab out the 88. Not a
    >problem).[/color]

    I'm glad it's "not a problem". You can combine your solution to
    this bit with the packing operation from above.

    Nick.

    Comment

    • Pieter Droogendijk

      #3
      Re: Packing data...

      On Mon, 25 Aug 2003 13:02:56 GMT
      "FMorales" <altf2o@comcast .net> wrote:[color=blue]
      > So what i'm needing help on is getting this data packed into an unsigned short
      > int, data type.[/color]

      bitfields or shifts.
      [color=blue]
      > The example it gives is : 0100 0001 0101 1000 or 4158h
      > 0100 being a 4 for the month,
      > 00010 being 2 for the day, and
      > 1011000 being 88 for the year.
      >
      > So i would basically get something along the lines of, myFunc('4', '2',
      > "1988");[/color]

      what about december 31 2003? myFunc ('12', '31', "03")?
      [color=blue]
      > And i just need help getting those values in their correct "spot" packed
      > into my
      > 16 bit data type. (I can get 1 of them, but after the first one i keep
      > messing
      > up the way it's packed in by trying to pack the next value in.).[/color]

      You can pack it into a data type, but it's printed value will change when you
      run it on a big-endian system.

      union {
      unsigned short int si;
      struct {
      unsigned year:7;
      unsigned day:5;
      unsigned month:4;
      } bf;
      } cram;
      main() {
      cram.bf.month = 4;
      cram.bf.day = 2;
      cram.bf.year = 88;
      printf ("%X\n", cram.si);
      }

      or if you like less readable but much shorter code:

      main () {
      unsigned short int sint = 0;
      sint = 4<<12 | 2<<7 | 88;
      printf ("%X\n", sint);
      }

      I'd pick the first option. It's understandable, and reading the variables is
      easier.

      --
      char*x(c,k,s)ch ar*k,*s;{if(!k) return*s-36?x(0,0,s+1):s ;if(s)if(*s)c=1 0+(c?(x(
      c,k,0),x(c,k+=* s-c,s+1),*k):(x(* s,k,s+1),0));el se c=10;printf(&x( ~0,0,k)[c-~-
      c+"1"[~c<-c]],c);}main(){x(0 ,"^[kXc6]dn_eaoh$%c","-34*1'.+(,03#;+, )/'///*");}

      Comment

      Working...