Hi - I'm working on writing a program that will communicate with a computer through the serial port and the UART. I'm trying to figure out what the best way to write it would be. Commands will be multiple letters, some different numbers of letters than others, and some will have multiple digit ascii coded decimal numbers that will need to be converted to binary. All in all - quite the headache!
So what I'm thinking is I'll have three primary subroutines. So the first subroutine would essentially contain the other three, and some other code as well. Let's just call it "primary". The first thing it'd do would be to call subroutine "receive". Receive would first pop the return address into two registers, then it would watch the UART for commands received and keep on pushing every byte received through the uart onto the stack after converting all ascii capitol letters to lowercase, or the other way around. Then, once it received a enter keystroke it would push the registers loaded with the return address onto the stack, and return.
The next part of the "primary" subroutine would interpret the code. The nasty part of this is that the first letter popped from the stack would be the last digit in the command. So it could be a number or letter - essentially the command would have to be interpreted backwards. So what I'm thinking is first the code would recognize if the first digit after the enter was a number or a letter, and make a jump based on that.
If it were a number, the program would push that last number back onto the stack and jump to a bit of code that would convert the ascii decimal to binary - which shouldn't be too hard (subtract ascii "0" from each number to get them to BCD, then multiply the digits by their respective powers of 10 and add them together.) Once it shifted in a space, it would load this number into a set of registers (it would be 2 bytes long) and then jump to the same place that the program jumps to if there are no numbers in the command.
This bit of code would just go letter by letter, backwards like everything else. So this is where I just can't figure out a way to make the coding really pretty, and pretty much the primary reason for my post. My thoughts were that it would essentially check to see if the first letter was an A, then it would make a jump if it was, othewise it would check to see if it was a B, a C, etc. Over and over. Then the placed where it jumped to would do the exact same thing, over and over. Naturally - It wouldn't have to check for every letter, only the letters which would be possible at each point (so if the first letter was an A there may be 5 different letters possible for the second letter, if the first letter was B there may only be 3 possible letters, etc.), and since there will only be 10 or so commands there won't be too many, but still this seems clunky to me and I must admit it bothers me. Anyways the code would then jump to the location in the code necessary to complete the command received. That code would then use the 2 byte number if that command needed to do so.
Lastly - upon completion of the received command, the program would push something like ".eonD" ("Done." backwards) into the stack, then call the last major subroutine, "transmit". This would pop the return address into some temporary registers, then pop and send each byte in the stack, then push the return address back onto the stack, and return. The program would then jump back up to primary - and start all over.
Sorry for writing such a book - I tried to keep it as short as possible, honest! So - how does this look? Could anybody suggest a better way to do any of this? Thanks!