Interpreting string commands

Go To Last Post
3 posts / 0 new
Author
Message
#1
  • 1
  • 2
  • 3
  • 4
  • 5
Total votes: 0

Using EVK1100 with the UC3A processor, would the following code be efficient for decoding text:

union cmd_link_t {
  char input[4];   // Argument storage
  unsigned output; // Corresponding numeric code word
}link;

The idea is to put text like "STOP" or "RUN" into the character part of the union and then get an integer to use further in the program.

The question is how the GCC compiler and the processor is handling such unions, is the characters stored directly into the memory, or are there any intermediate calculations for these 8 bit values?

  • 1
  • 2
  • 3
  • 4
  • 5
Total votes: 0

I'd bet it is not going to work for you this way, unfortunately...

"char input[4];" will result in 4 subsequent 32-bit-wide locations allocated in "linear" memory space, otherwise e.g. pointer arithmetic and array operations would require rather twisted logic to work (i.e. if they were "packed" without any "padding" - BTW __attribute__ ((packed)) won't work this way either).

of course you can use all 32 bit in memory, if you insist, but you would need to aggregate/combine the values "manually" into 32-bit variable (by means of adding and bit-shifting, for example), so perhaps that's a method to save some RAM memory at expense of program memory.

  • 1
  • 2
  • 3
  • 4
  • 5
Total votes: 0

Sounds reasonable. Actually the goal is to save execution time so the "command decoding" would not interrupt a time critical process. An alternative to storing each character of course is to add and shift as the user is typing in the terminal, then pass on the integer code when he hits enter.