??? 05/12/09 16:46 Read: times |
#165272 - I would not do this in 'C' Responding to: ???'s previous message |
I should preface this comment with the statement that I'm not one who uses 'C' or other HLL's for tasks small enough to fit in an 8-bit MCU. That's because I've always used ASM, with many different CPU architectures, and have never become totally "happy" with HLL's for small machines.
There are features in the 805x instruction set that make this sort of thing VERY straightforward in ASM. You receive one of 8 commands, presumably via serial port. That command is then in 'A' when you retrieve it from SBUF. If your "main dispatch loop" has the address of a "default" jump table loaded into DPTR, then the JMP @A+DPTR instruction quite useful, as it then transfers control to a program segment presumably dedicated to that specific command. Before you can use that instruction, you must left-shift the byte so that there's room for two bytes of address in the table entry. This approach is both very fast and very reliable, as it avoids using stack or other memory resources, aside from nonvolatile code memory. Since there are only eight basic commands, it would probably be useful to use a lookup table to reduce the size of the integer in A. To accomplish that, you'd use the MOVC A, @A+DPTR to perform the reduction. MOVC A, @A+PC might be useful in order to produce a smaller table. If you think about how these simple instructions work, you'll quickly see that they're intended for exactly what you need to do. If you process multi-byte commands, each one will best be reduced in some way, either by code or by table lookup, and the code segment that processes the first byte will vector to the code segment that processes the second byte, and so on. Programming this sort of thing in 'C' is as difficult as doing it in ASM. If you're really uncomfortable in ASM, then 'C' can do it too, albeit much more slowly and with considerably more code space consumed. That may not be a particularly big penalty for you, however. RE |