<p>Can someone explain the mechanics of a jump table and why is would be needed in embedded systems?</p>

[{'answer_id': 48023, 'author': 'Eric Haskins', 'author_id': 100, 'author_profile': 'https://Stackoverflow.com/users/100', 'pm_score': 2, 'selected': False, 'text': '<p><a href="http://en.wikipedia.org/wiki/Jump_table" rel="nofollow noreferrer">From Wikipedia</a>:</p>\n\n<blockquote>\n  <p>In computer programming, a branch\n  table (sometimes known as a jump\n  table) is a term used to describe an\n  efficient method of transferring\n  program control (branching) to another\n  part of a program (or a different\n  program that may have been dynamically\n  loaded) using a table of branch\n  instructions. The branch table\n  construction is commonly used when\n  programming in assembly language but\n  may also be generated by a compiler.</p>\n  \n  <p>A branch table consists of a serial\n  list of unconditional branch\n  instructions that is branched into\n  using an offset created by multiplying\n  a sequential index by the instruction\n  length (the number of bytes in memory\n  occupied by each branch instruction).\n  It makes use of the fact that machine\n  code instructions for branching have a\n  fixed length and can be executed\n  extremely efficiently by most\n  hardware, and is most useful when\n  dealing with raw data values that may\n  be easily converted to sequential\n  index values. Given such data, a\n  branch table can be extremely\n  efficient; it usually consists of the\n  following steps: optionally validating\n  the input data to ensure it is\n  acceptable; transforming the data into\n  an offset into the branch table, this\n  usually involves multiplying or\n  shifting it to take into account the\n  instruction length; and branching to\n  an address made up of the base of the\n  table and the generated offset: this\n  often involves an addition of the\n  offset onto the program counter\n  register.</p>\n</blockquote>\n'}, {'answer_id': 48024, 'author': 'Jim Buck', 'author_id': 2666, 'author_profile': 'https://Stackoverflow.com/users/2666', 'pm_score': 1, 'selected': False, 'text': '<p>A jump table is described <a href="http://en.wikipedia.org/wiki/Jump_table" rel="nofollow noreferrer">here</a>, but briefly, it\'s an array of addresses the CPU should jump to based on certain conditions. As an example, a C switch statement is often implemented as a jump table where each jump entry will go to a particular "case" label.</p>\n\n<p>In embedded systems, where memory usage is at a premium, many constructs are better served by using a jump table instead of more memory-intensive methods (like a massive if-else-if).</p>\n'}, {'answer_id': 48027, 'author': 'Jason Etheridge', 'author_id': 2193, 'author_profile': 'https://Stackoverflow.com/users/2193', 'pm_score': 1, 'selected': False, 'text': '<p><a href="http://en.wikipedia.org/wiki/Jump_table" rel="nofollow noreferrer">Wikipedia</a> sums it up pretty well:</p>\n<blockquote>\n<p>In computer programming, a branch\ntable (sometimes known as a jump\ntable) is a term used to describe an\nefficient method of transferring\nprogram control (branching) to another\npart of a program (or a different\nprogram that may have been dynamically\nloaded) using a table of branch\ninstructions. The branch table\nconstruction is commonly used when\nprogramming in assembly language but\nmay also be generated by a compiler.</p>\n<p>... Use of branch tables and other raw\ndata encoding was common in the early\ndays of computing when memory was\nexpensive, CPUs were slower and\ncompact data representation and\nefficient choice of alternatives were\nimportant. Nowadays, they are commonly\nused in embedded programming and\noperating system development.</p>\n</blockquote>\n<p>In other words, it\'s a useful construct to use when your system is extremely memory and/or CPU limited, as is often the case in an embedded platform.</p>\n'}, {'answer_id': 48030, 'author': 'Brian Gianforcaro', 'author_id': 3415, 'author_profile': 'https://Stackoverflow.com/users/3415', 'pm_score': 0, 'selected': False, 'text': "<p>Jump tables, more often known as a Branch table, are usually used only by the machine.</p>\n\n<p>The compiler creates a list of all labels in a assembly program and links all labels to a a memory location. A jump table pretty much is a reference card to where, a function or variable or what ever the label maybe, is stored in memory. </p>\n\n<p>So as a function executes, on finishing it jumps back to it's previous memory location or jumps to the next function, etc. </p>\n\n<p>And If your talking about what I think you are, you don't just need them in embedded systems but in any type of compiled/interpreted environment. </p>\n\n<p>Brian Gianforcaro</p>\n"}, {'answer_id': 48031, 'author': 'Josh Segall', 'author_id': 2659, 'author_profile': 'https://Stackoverflow.com/users/2659', 'pm_score': 7, 'selected': True, 'text': '<p>A jump table can be either an array of pointers to functions or an array of machine code jump instructions.  If you have a relatively static set of functions (such as system calls or virtual functions for a class) then you can create this table once and call the functions using a simple index into the array.  This would mean retrieving the pointer and calling a function or jumping to the machine code depending on the type of table used.</p>\n\n<p>The benefits of doing this in embedded programming are:</p>\n\n<ol>\n<li>Indexes are more memory efficient than machine code or pointers, so there is a potential for memory savings in constrained environments.</li>\n<li>For any particular function the index will remain stable and changing the function merely requires swapping out the function pointer.</li>\n</ol>\n\n<p>If does cost you a tiny bit of performance for accessing the table, but this is no worse than any other virtual function call.</p>\n'}, {'answer_id': 48033, 'author': 'Adam Davis', 'author_id': 2915, 'author_profile': 'https://Stackoverflow.com/users/2915', 'pm_score': 5, 'selected': False, 'text': "<p>A jump table, also known as a branch table, is a series of instructions, all unconditionally branching to another point in code.</p>\n\n<p>You can think of them as a switch (or select) statement where all the cases are filled:</p>\n\n<pre><code>MyJump(int c)\n{\n   switch(state)\n   {\n      case 0:\n         goto func0label;\n      case 1:\n         goto func1label;\n      case 2:\n         goto func2label;\n   }\n}\n</code></pre>\n\n<p>Note that there's no return - the code that it jumps to will execute the return, and it will jump back to wherever myjump was called.</p>\n\n<p>This is useful for state machines where you execute certain code based on the state variable.  There are many, many other uses, but this is one of the main uses.</p>\n\n<p>It's used where you don't want to waste time fiddling with the stack, and want to save code space.  It is especially of use in interrupt handlers where speed is extremely important, and the peripheral that caused the interrupt is only known by a single variable.  This is similar to the vector table in processors with interrupt controllers.</p>\n\n<p>One use would be taking a $0.60 microcontroller and generating a composite (TV) signal for video applications.  the micro isn't powerful - in fact it's just barely fast enough to write each scan line.  A jump table would be used to draw characters, because it would take too long to load a bitmap from memory, and use a for() loop to shove the bitmap out.  Instead there's a separate jump to the letter and scan line, and then 8 or so instructions that actually write the data directly to the port.</p>\n\n<p>-Adam</p>\n"}, {'answer_id': 29079346, 'author': 'Mawg says reinstate Monica', 'author_id': 192910, 'author_profile': 'https://Stackoverflow.com/users/192910', 'pm_score': 2, 'selected': False, 'text': '<p>Jump tables are commonly (but not exclusively) used in <a href="http://en.wikipedia.org/wiki/Finite_State_Machine" rel="nofollow">finite state machines</a> to make them data driven. </p>\n\n<p>Instead of nested switch/case  </p>\n\n<pre><code>  switch (state)\n     case A:\n       switch (event):\n         case e1: ....\n         case e2: ....\n     case B:\n       switch (event):\n         case e3: ....\n         case e1: ....\n</code></pre>\n\n<p>you can make a 2d array or function pointers and just call <code>handleEvent[state][event]</code></p>\n'}]
['https://Stackoverflow.com/questions/48017', 'https://Stackoverflow.com', 'https://Stackoverflow.com/users/445087/']