[PASM] calling the right sub-routine depending on a value
Wossname
Posts: 174
in Propeller 1
I'd like to call one of perhaps 40 different sub-routines based on the value stored in a register...
For instance a "command_ID" value of 1 should cause the CMD_WATCHDOG sub-routine to be called (and in fact this part does work).
The problem is that the code never returns to the point just after :Exec_MODIFIED_JUMP.
I think this is because it's the compiler that deals with the returning address for the CALL opcode, but I'm using MOVS to alter the jump address myself, so perhaps the compiler is actually putting the return address into PLACEHOLDER_ret always.
How can I rewrite this code so that my subroutines know where to return to?
For instance a "command_ID" value of 1 should cause the CMD_WATCHDOG sub-routine to be called (and in fact this part does work).
cmp command_ID, #0 WZ IF_E MOVS :Exec_MODIFIED_JUMP, #CMD_HARD_RESET cmp command_ID, #1 WZ IF_E MOVS :Exec_MODIFIED_JUMP, #CMD_WATCHDOG 'many more of these..... NOP :Exec_MODIFIED_JUMP CALL #PLACEHOLDER '...... PLACEHOLDER NOP PLACEHOLDER_ret ret '...... '----------------------------------------------------------------- ' COMMAND SUB-ROUTINES - these are called by self-modifying ' code at ":Exec_MODIFIED_JUMP" above, see comments! '----------------------------------------------------------------- '...... '----------------------------------------------------------------- CMD_WATCHDOG 'debug mov sertx_byte_out, #"W" call #Ser_TX_Byte mov sertx_byte_out, #"D" call #Ser_TX_Byte mov sertx_byte_out, LINEFEED call #Ser_TX_Byte CMD_WATCHDOG_ret ret '----------------------------------------------------------------- '......
The problem is that the code never returns to the point just after :Exec_MODIFIED_JUMP.
I think this is because it's the compiler that deals with the returning address for the CALL opcode, but I'm using MOVS to alter the jump address myself, so perhaps the compiler is actually putting the return address into PLACEHOLDER_ret always.
How can I rewrite this code so that my subroutines know where to return to?
Comments
change this to (you can get rid of the PLACEHOLDER now. Also don't forget to allocate the return_ptr variable somewhere)
Then, instead of RET, do a JMP return_ptr (WITHOUT THE #).
Also, you might be able to reduce the memory usage by using a jump table, like this
Fantastic! It works well and is efficient in both time and memory. Thank you very much.