Author Topic: Jump Tables  (Read 10918 times)

Offline pprocacci

  • Jr. Member
  • *
  • Posts: 11
Jump Tables
« on: May 20, 2010, 06:28:28 AM »
Forgive me if this has been asked before but I can't seem to find a "Nasm" style syntax that works.   I've searched the forums but came up empty. If you have and know of a post detailing this please just provide that and I'll do my own research.  I'm referring to it as a "Jump Table" but perhaps it has a different definition.

Essentially what I'm trying to accomplish is jump to a specific label given a value passed as an arg.

Sudo code (not working)
Code: [Select]
section .data
        JumpTab: DD .T00, T01, .T02, .T03, .T04, .T05, .T06, .T07, .T08, .T09, .T10, .T11, .T12, .T13, .T14, .T15, .T16  

section .text

global byte_zero:function

byte_zero:
        mov     edx, [esp+4]            ; dest
        mov     ecx, [esp+8]            ; count
        xor     eax, eax                ; Set to 0

        cmp     ecx, 16
        ja      .F01
        jmp     [JmpTab+ecx*4]

.T00:
.T01:
.T02:
.T03:
.T04:
.T05:
.T06:
.T07:
.T08:
.T09:
.T10:
.T11:
.T12:
.T13:
.T14:
.T15:
.T16:
.F1:
             ret

Of coarse this only handle values up to a certain number, but that's ok...I am just attempting to test the concept out.

I tried numerous variations of the above, but just can't put my finger on it.  I'm gonna guess and say it's because of the way I've defined my "JumpTab", but an not clear on what would be the proper way to define this.

As a side note, I did read: http://www.nasm.us/doc/nasmdoc9.html which dealt with things similar but not quite like this.

Thanks for any insight.
« Last Edit: May 20, 2010, 06:30:24 AM by pprocacci »

Offline Frank Kotler

  • NASM Developer
  • Hero Member
  • *****
  • Posts: 2667
  • Country: us
Re: Jump Tables
« Reply #1 on: May 20, 2010, 07:44:22 AM »
Hi Paul,

Looks like you've run afoul of Nasm's "local label" mechanism...

http://www.nasm.us/doc/nasmdoc3.html#section-3.9

In short, by starting your labels, ".T01", etc. with a '.', you've made them "local labels", with a "scope" from the preceeding "regular label" (not really "global" - "file scope") - "byte_zero:" to the next "regular label". Unlike "true local labels", Nasm's "local labels", can be referenced outside of their scope by prepending the preceeding "regular label".

Code: [Select]
JumpTab: dd byte_zero.T00, byte_zero.T01...

Or, you could lose the '.' throughout - possibly the best solution, in this case. If "namespace pollution" were an issue ("monofile programming" - I don't advocate it, but I do it), you could put JumpTab within "scope" (since it doesn't have to be writable, it could go in .text), pehaps making it a "local variable" itself...

Code: [Select]
section .text

global byte_zero:function

byte_zero:
        mov     edx, [esp+4]            ; dest
        mov     ecx, [esp+8]            ; count
        xor     eax, eax                ; Set to 0

        cmp     ecx, 16
        ja      .F01
        jmp     [.JumpTab+ecx*4]

.T00:
.T01:
.T02:
.T03:
.T04:
.T05:
.T06:
.T07:
.T08:
.T09:
.T10:
.T11:
.T12:
.T13:
.T14:
.T15:
.T16:
.F01:
             ret

        .JumpTab: DD .T00, .T01, .T02, .T03, .T04, .T05, .T06, .T07, .T08, .T09, .T10, .T11, .T12, .T13, .T14, .T15, .T16 

Nasm will swallow that.

Since a jump through a jump table can't be "predicted", this may be slower than what you've got. Try it and see, I guess...

Best,
Frank


Offline pprocacci

  • Jr. Member
  • *
  • Posts: 11
Re: Jump Tables
« Reply #2 on: May 20, 2010, 08:03:49 AM »
After posting, I naturally DID come across Nasm's label'ing scheme and had thought this to be the issue.  I'm certainly glad you responded in such detail because it certainly clarifies this a ton.  Hopefully this is searchable now for those that need a clear explanation.

I have yet to try quite frankly (no pun intended)...getting kinda early here and I must sleep.  I think the unpredicted branch (being that is just one) will most likely be ok, but I'll certainly let you know what I have planned in other posts and whatnot to keep you informed.

Again, appreciate the help.  I feel like a broken recorder saying that.

Cheers!  Have a great night!