🚀 go-pugleaf

RetroBBS NetNews Server

Inspired by RockSolid Light RIP Retro Guy

Article View: comp.lang.asm.x86
Article #170

Re: Jump table without the table?

#170
From: Matt Taylor
Date: Wed, 12 Feb 2003 19:57
73 lines
4494 bytes
You need two steps: an address computation and a jmp.

asm("shll %1, %0"
    "addl _first_case, %0"
    "jmpl %0"
    : /* no output */
    : "r" (offset), "I" (align_size)
    : /* no clobbers */);

Alternatively, to let the compiler potentially reschedule the shift:
asm("addl _first_case, %0"
    "jmpl %0"
    : /* no output */
    : "r" (offset * align_size)
    : /* no clobbers */);

As long as you keep the code smaller than the alignment size, you should be
safe. Be -extremely- careful not to have a case larger than the alignment
size.

-Matt

"Vis Mike" <visionary25@_nospam_hotmail.com> wrote in message
news:b2d1sj$8j4$1@bob.news.rcn.net...
> I found a nice feature in gcc - -malign-jumps=n
>
> > wcc -O3 -malign-jumps=3 vmtest2.cpp
> > a.exe
> Testing stack4...
> &&addi = 0x401180 +4 +8 +16
>
> 0x401180 is the address of the first label, followed by the offset of the
> rest of the labels.  So if I can squeeze the code in, all I need is a "jmp
> (opcode*4)".  This is sooo unportable :)  I'll just use conditional
> compilation.
>
> I don't quite understand gcc's inline assembly syntax, any help for the
jmp?
> Or am I just going overboard?
>
> Cheers!
> Mike
>
> "James Van Buskirk" <not_valid@attbi.com> wrote in message
> news:dCk2a.80786$HN5.307889@rwcrnsc51.ops.asp.att.net...
> >
> > "Matt Taylor" <para@tampabay.rr.com> wrote in message
> > news:h2j2a.61691$AQ3.981480@twister.tampabay.rr.com...
> >
> > > Any good compiler will convert switch/case to a jump table for you. VC
> > does
> > > this; I'm quite sure that GCC does too. Unless you're writing in
> assembly,
> > > don't try to pad things out. The compiler should do this for you. I
> don't
> > > know when specifically VC/GCC do and when they don't.
> >
> > I think I have a different interpretation of Mike's question than
> > you: my take on it is that he has code where all branches are just
> > little snippets no longer than some nice easy number of bytes to
> > multiply by, e.g. if all his branches were less than 32 bytes
> > long and he had 16 possible cases numbered 0:15 he could, assuming
> > the branch number were in EAX do something like:
> >
> > lea EAX, [4*EAX]
> > jmp branch_implementation_start[8*EAX]
> >
> > In this way he could avoid waiting for the address of a previous
> > write to memory to be computed before prefetching the code
> > following the branch.  Maybe ALIGN directives or ORG
> > directives could tell the assembler where to insert padding
> > without having to maintain it by hand every time the code
> > is modified?
Technical Details
Message-ID: <1ox2a.65290$AQ3.1060020@twister.tampabay.rr.com>
Path: arc1-ca.pugleaf.net!archiver1.google.com!news1.google.com!newsfeed.stanford.edu!news-spur1.maxwell.syr.edu!news.maxwell.syr.edu!sn-xit-03!sn-xit-01!sn-post-01!supernews.com!corp.supernews.com!news.crayne.org!clax86!robomod!not-for-mail
References: <b2c5s4$p9p$1@bob.news.rcn.net> <h2j2a.61691$AQ3.981480@twister.tampabay.rr.com> <dCk2a.80786$HN5.307889@rwcrnsc51.ops.asp.att.net> <b2d1sj$8j4$1@bob.news.rcn.net>

Show Full Headers
Complete Article Headers
X-Google-Language: ENGLISH,ASCII-7-bit
X-Google-Thread: 11318c,d7bccf0f48f83a38
X-Google-Attributes: gid11318c,public
X-Google-Thread: 114917,fa3250fb672bd972
X-Google-Attributes: gid114917,public
X-Google-ArrivalTime: 2003-02-12 11:57:35 PST
Path: arc1-ca.pugleaf.net!archiver1.google.com!news1.google.com!newsfeed.stanford.edu!news-spur1.maxwell.syr.edu!news.maxwell.syr.edu!sn-xit-03!sn-xit-01!sn-post-01!supernews.com!corp.supernews.com!news.crayne.org!clax86!robomod!not-for-mail
From: "Matt Taylor" <para@tampabay.rr.com>
Newsgroups: comp.lang.asm.x86,gnu.gcc.help
Subject: Re: Jump table without the table?
Approved: CLAX86 Moderators <ccrayne@crayne.org>
Date: Wed, 12 Feb 2003 19:57:11 +0000 (UTC)
Organization: RoadRunner - Tampa Bay
Message-ID: <1ox2a.65290$AQ3.1060020@twister.tampabay.rr.com>
Sender: clax86@wotan.asgard
References: <b2c5s4$p9p$1@bob.news.rcn.net> <h2j2a.61691$AQ3.981480@twister.tampabay.rr.com> <dCk2a.80786$HN5.307889@rwcrnsc51.ops.asp.att.net> <b2d1sj$8j4$1@bob.news.rcn.net>
X-CLAX86-Policy: http://www.pacificsites.com/~ccrayne/clax86.html
X-CLAX86-Faq: http://www2.dgsys.com/~raymoon/x86faqs.html
X-CLAX86-Info-1: Send submissions to             comp-lang-asm-x86@moderators.isc.org
X-CLAX86-Info-2: Send technical complaints to    ccrayne@crayne.org
X-CLAX86-Info-3: Send complaints about policy to ccrayne@crayne.org
X-Comment: moderators do not necessarily agree or disagree with this article.
X-Priority: 3
X-MSMail-Priority: Normal
X-Newsreader: Microsoft Outlook Express 6.00.2800.1106
X-MimeOLE: Produced By Microsoft MimeOLE V6.00.2800.1106
X-MailScanner: Found to be clean
X-MailScanner-SpamCheck: not spam (whitelisted), SpamAssassin (score=0,
 required 5, NOSPAM_INC, PRIORITY_NO_NAME, QUOTED_EMAIL_TEXT,
 REFERENCES, SPAM_PHRASE_01_02)
X-UIDL: FL8!!#nZ!!4RN!!^*K"!
X-Complaints-To: abuse@supernews.com
Lines: 75