Re: A Macro to Generate Jump Tables
- From: "Charles A. Crayne" <ccrayne@xxxxxxxxxx>
- Date: Thu, 7 Jul 2005 23:43:10 -0700
On 7 Jul 2005 15:32:49 -0700
randyhyde@xxxxxxxxxxxxx wrote:
:Creating a
:true switch statement using an assembler's macro facilities is
:relatively difficult and requires a *very* powerful macro subsystem.
Although your macro is indeed powerful, and I can see some value in using
it in an HLA context, I believe that those of us who prefer to program in
the more traditional assembler languages will find very little of value in
a such a macro. However, I will not be upset to be proven wrong, so please
consider the following analysis, and respond as you will.
To begin with, you have described your macro as "A Macro to Generate Jump
Tables", and I am evaluating it in that context -- since one certainly does
not need a macro to generate a "jmp [jmptbl+ebx*4]" statement.
Next, you wrote "you need to allow switches within switches
within switches within... nested to any level". Although this is true
(albeit trivial) in the code section (for which we have already agreed that
no macro is necessary), it is certainly false for the generated jump
tables. Each jump table is, and must always be, a separate array, at the
same program level.
In addition, you stated "You need the ability to store up all the case
values in a compile-time array so you can emit the jump table *after*
processing all the cases present in the switch/case statement." This is
only true if one's goal is to emulate an HLL switch syntax, but is
certainly false if one's goal is merely to generate jump tables, as is the
usual case in assembly languages, and as your choice of subject suggests.
And finally, you argued that "You need a sophisticated enough compile-time
language that will let you sort the array of case values (and corresponding
destination addresses) prior to emiting the jump table (as the entries must
be sorted by index before being output)." At first glance, the automated
sorting of the jump table seems desirable -- since an out of order table is
clearly disastrous. However, since the programmer must enter the switch
value in the case statement, and since most programmers will enter the case
statements in numerical sequence, it takes fewer keystrokes for a
programmer to create a jump table in the correct sequence, to begin
with, and not have to deal with case statements at all.
For example, you ask the programmer to enter
case( 0 )
<some nameless routine> );
which you state will generate
L820__case0_0___hla_:
<some nameless routine>
jmp L824__0329__switch_endcase____hla_
in the code section, and
dd L820__case0_0___hla_
in the jump table. [Incidentally, I note that this differs significantly
from the C/C++ standard].
In contrast, to take a real life example, my switch statement is coded
[in Fasm] as
jmp [jmptbl+eax*4]
there are no case statements, and my jump table is coded as
maxsect equ 16 ;largest valid section number
jmptbl dd buildx ;0 - end of file
dd vocab ;1 - vocabulary
dd text ;2 - room names
dd text ;3 - long descriptions
dd text ;4 - short descriptions
dd motion ;5 - movement table
dd text ;6 - object descriptions
dd text ;7 - miscellaneous messages
dd osetup ;8 - init obj loc & wt & clone
dd actdef ;9 - action defaults
dd brmatr ;10 - room attributes
dd text ;11 - class messages
dd oadj ;12 - object differentiation
dd text ;13 - administrative msgs
dd rdmsg ;14 - read message table
dd rdmsg ;15 - search message table
dd ovride ;16 - default override values
Of course, one still has to write the various routines listed in the jump
table, but, in contrast to embedding them in case statements, I get to
give them meaningful names, optimise their placement in the code section,
and document the jump table with useful comments.
In short, for a skilled assembly language programmer, my code is easier to
write, easier to read, easier to maintain, and is likely to provide better
run-time performance.
-- Chuck
.
- Follow-Ups:
- Re: A Macro to Generate Jump Tables
- From: randyhyde
- Re: A Macro to Generate Jump Tables
- References:
- A Macro to Generate Jump Tables
- From: randyhyde
- Re: A Macro to Generate Jump Tables
- From: randyhyde
- A Macro to Generate Jump Tables
- Prev by Date: Re: London
- Next by Date: Re: London
- Previous by thread: Re: A Macro to Generate Jump Tables
- Next by thread: Re: A Macro to Generate Jump Tables
- Index(es):
Relevant Pages
|