linux-assembly.vger.kernel.org archive mirror
 help / color / mirror / Atom feed
From: Nicolas Bock <nicolasbock@gmail.com>
To: Brian Raiter <breadbox@muppetlabs.com>
Cc: linux-assembly@vger.kernel.org
Subject: Re: writing a jump table
Date: Wed, 16 Mar 2011 20:12:59 -0600	[thread overview]
Message-ID: <4D816E2B.2000409@gmail.com> (raw)
In-Reply-To: <19840.863.574550.774103@eidolon.muppetlabs.com>

[-- Attachment #1: Type: text/plain, Size: 1642 bytes --]

I think I figured it out now. I used gcc to compile PIC for the C switch
statement and checked what it does. I don't fully understand it to be
honest, but it seems to do the job also for non PIC code. For posterity,
here is the code:

  .text
  .global jump_table
  .type jump_table, @function

jump_table:
  # Push stack pointer so we can make room for local storage.
  push %rax

  mov $0x02, %rax # Move index into rax; 2 is supposed to end up at
label_02.

  lea 0(,%rax, 4), %rdx
  lea table(%rip), %rax
  mov (%rdx, %rax), %edx
  movslq %edx, %rdx
  lea table(%rip), %rax
  lea (%rdx, %rax), %rax
  jmp *%rax

  .section .rodata
  .align 4
table:
  .long label_00-table
  .long label_01-table
  .long label_02-table
  .long label_03-table

  .text
label_00:
  jmp done

label_01:
  jmp done

label_02:
  jmp done

label_03:
  jmp done

done:
  pop %rax
  ret

  .size jump_table, .-jump_table


On 03/15/11 18:25, Brian Raiter wrote:
>> I am trying to write a jump table, but unfortunately with limited
>> success. When I compile the code and disassemble it, the offset of
>> "table" is 0, which I guess means that something didn't work out.
> 
> Actually, table appears immediately following your indirect jump
> instruction, so I would assume that the offset would be zero. Did you
> actually try this code to verify that it doesn't do what you expect?
> 
> b
> --
> To unsubscribe from this list: send the line "unsubscribe linux-assembly" in
> the body of a message to majordomo@vger.kernel.org
> More majordomo info at  http://vger.kernel.org/majordomo-info.html


[-- Attachment #2: OpenPGP digital signature --]
[-- Type: application/pgp-signature, Size: 262 bytes --]

  parent reply	other threads:[~2011-03-17  2:12 UTC|newest]

Thread overview: 10+ messages / expand[flat|nested]  mbox.gz  Atom feed  top
2011-03-16  0:10 writing a jump table Nicolas Bock
2011-03-16  0:25 ` Brian Raiter
2011-03-16 14:13   ` Nicolas Bock
2011-03-16 14:26     ` Frank Kotler
2011-03-16 16:48       ` Nicolas Bock
2011-03-16 16:57   ` Nicolas Bock
2011-03-17  2:12   ` Nicolas Bock [this message]
2011-03-17  2:23     ` Brian Raiter
2011-03-17  2:26       ` Nicolas Bock
     [not found]         ` <AANLkTinzgyzwN3Zj7bmqw7tDF0QKDSJiJj7MQt7vFx-h@mail.gmail.com>
2011-03-17 10:11           ` Fwd: " Hendrik Visage

Reply instructions:

You may reply publicly to this message via plain-text email
using any one of the following methods:

* Save the following mbox file, import it into your mail client,
  and reply-to-all from there: mbox

  Avoid top-posting and favor interleaved quoting:
  https://en.wikipedia.org/wiki/Posting_style#Interleaved_style

* Reply using the --to, --cc, and --in-reply-to
  switches of git-send-email(1):

  git send-email \
    --in-reply-to=4D816E2B.2000409@gmail.com \
    --to=nicolasbock@gmail.com \
    --cc=breadbox@muppetlabs.com \
    --cc=linux-assembly@vger.kernel.org \
    /path/to/YOUR_REPLY

  https://kernel.org/pub/software/scm/git/docs/git-send-email.html

* If your mail client supports setting the In-Reply-To header
  via mailto: links, try the mailto: link
Be sure your reply has a Subject: header at the top and a blank line before the message body.
This is a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox;
as well as URLs for NNTP newsgroup(s).