C++ 什么是跳表?
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/48017/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
What is a jump table?
提问by JeffV
Can someone explain the mechanics of a jump table and why is would be needed in embedded systems?
有人能解释一下跳转表的机制吗?为什么在嵌入式系统中需要它?
回答by Josh Segall
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.
跳转表可以是指向函数的指针数组或机器代码跳转指令数组。如果您有一组相对静态的函数(例如系统调用或类的虚函数),那么您可以创建此表一次并使用数组中的简单索引调用这些函数。这意味着根据所使用的表类型检索指针并调用函数或跳转到机器代码。
The benefits of doing this in embedded programming are:
在嵌入式编程中这样做的好处是:
- Indexes are more memory efficient than machine code or pointers, so there is a potential for memory savings in constrained environments.
- For any particular function the index will remain stable and changing the function merely requires swapping out the function pointer.
- 索引比机器代码或指针的内存效率更高,因此在受限环境中具有节省内存的潜力。
- 对于任何特定的函数,索引将保持稳定,改变函数只需要换出函数指针。
If does cost you a tiny bit of performance for accessing the table, but this is no worse than any other virtual function call.
如果访问该表确实会降低您的性能,但这并不比任何其他虚函数调用差。
回答by Adam Davis
A jump table, also known as a branch table, is a series of instructions, all unconditionally branching to another point in code.
跳转表,也称为分支表,是一系列指令,都无条件地跳转到代码中的另一个点。
You can think of them as a switch (or select) statement where all the cases are filled:
您可以将它们视为一个 switch(或选择)语句,其中填充了所有 case:
MyJump(int c)
{
switch(state)
{
case 0:
goto func0label;
case 1:
goto func1label;
case 2:
goto func2label;
}
}
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.
请注意,没有返回 - 它跳转到的代码将执行返回,并且它将跳转回调用 myjump 的任何地方。
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.
这对于根据状态变量执行某些代码的状态机很有用。还有很多很多其他用途,但这是主要用途之一。
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.
它用于您不想浪费时间摆弄堆栈并希望节省代码空间的地方。它特别适用于速度非常重要的中断处理程序,并且导致中断的外设仅由单个变量知道。这类似于带有中断控制器的处理器中的向量表。
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.
一种用途是使用 0.60 美元的微控制器并为视频应用生成复合 (TV) 信号。微型并不强大 - 事实上,它的速度几乎不足以写入每条扫描线。跳转表将用于绘制字符,因为从内存加载位图需要很长时间,并使用 for() 循环将位图推出。相反,有一个单独的跳转到字母和扫描线,然后是 8 条左右的指令,实际上将数据直接写入端口。
-Adam
-亚当
回答by Eric Haskins
In computer programming, a branch table (sometimes known as a jump table) is a term used to describe an efficient method of transferring program control (branching) to another part of a program (or a different program that may have been dynamically loaded) using a table of branch instructions. The branch table construction is commonly used when programming in assembly language but may also be generated by a compiler.
A branch table consists of a serial list of unconditional branch instructions that is branched into using an offset created by multiplying a sequential index by the instruction length (the number of bytes in memory occupied by each branch instruction). It makes use of the fact that machine code instructions for branching have a fixed length and can be executed extremely efficiently by most hardware, and is most useful when dealing with raw data values that may be easily converted to sequential index values. Given such data, a branch table can be extremely efficient; it usually consists of the following steps: optionally validating the input data to ensure it is acceptable; transforming the data into an offset into the branch table, this usually involves multiplying or shifting it to take into account the instruction length; and branching to an address made up of the base of the table and the generated offset: this often involves an addition of the offset onto the program counter register.
在计算机编程中,分支表(有时称为跳转表)是一个术语,用于描述使用以下方法将程序控制(分支)转移到程序的另一部分(或可能已动态加载的不同程序)的有效方法分支指令表。分支表构造通常在用汇编语言编程时使用,但也可由编译器生成。
分支表由无条件分支指令的串行列表组成,使用通过将顺序索引乘以指令长度(每个分支指令在内存中占用的字节数)创建的偏移量分支到该列表中。它利用了这样一个事实,即用于分支的机器代码指令具有固定长度,并且可以被大多数硬件极其高效地执行,并且在处理可以轻松转换为顺序索引值的原始数据值时最有用。给定这些数据,分支表可以非常高效;它通常包括以下步骤: 可选地验证输入数据以确保它是可接受的;将数据转换为分支表的偏移量,这通常涉及乘以或移位以考虑指令长度;并跳转到由表的基址和生成的偏移量组成的地址:这通常涉及将偏移量添加到程序计数器寄存器上。
回答by Jim Buck
A jump table is described here, 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.
此处描述了一个跳转表,但简而言之,它是 CPU 应根据某些条件跳转到的地址数组。例如,C switch 语句通常被实现为一个跳转表,其中每个跳转条目都将转到特定的“case”标签。
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).
在内存使用率很高的嵌入式系统中,使用跳转表而不是更多内存密集型方法(如大量的 if-else-if)可以更好地为许多结构提供服务。
回答by Jason Etheridge
Wikipediasums it up pretty well:
维基百科总结得很好:
In computer programming, a branch table (sometimes known as a jump table) is a term used to describe an efficient method of transferring program control (branching) to another part of a program (or a different program that may have been dynamically loaded) using a table of branch instructions. The branch table construction is commonly used when programming in assembly language but may also be generated by a compiler.
... Use of branch tables and other raw data encoding was common in the early days of computing when memory was expensive, CPUs were slower and compact data representation and efficient choice of alternatives were important. Nowadays, they are commonly used in embedded programming and operating system development.
在计算机编程中,分支表(有时称为跳转表)是一个术语,用于描述使用以下方法将程序控制(分支)转移到程序的另一部分(或可能已动态加载的不同程序)的有效方法分支指令表。分支表构造通常在用汇编语言编程时使用,但也可由编译器生成。
...在计算的早期,当内存昂贵、CPU 速度较慢、紧凑的数据表示和有效的替代选择很重要时,分支表和其他原始数据编码的使用很常见。如今,它们通常用于嵌入式编程和操作系统开发。
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.
换句话说,当您的系统内存和/或 CPU 非常有限时,这是一个有用的构造,这在嵌入式平台中经常发生。
回答by Brian Gianforcaro
Jump tables, more often known as a Branch table, are usually used only by the machine.
跳转表,通常称为分支表,通常仅供机器使用。
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.
编译器在汇编程序中创建所有标签的列表,并将所有标签链接到内存位置。跳转表几乎是一个参考卡,用于说明函数或变量或标签可能存储在内存中的位置。
So as a function executes, on finishing it jumps back to it's previous memory location or jumps to the next function, etc.
因此,当一个函数执行时,在完成时它会跳回到它之前的内存位置或跳转到下一个函数,等等。
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.
如果你谈论我认为你是什么,你不仅在嵌入式系统中需要它们,而且在任何类型的编译/解释环境中都需要它们。
Brian Gianforcaro
布赖恩·吉安福卡罗
回答by Mawg says reinstate Monica
Jump tables are commonly (but not exclusively) used in finite state machinesto make them data driven.
跳转表通常(但并非唯一)用于有限状态机以使其数据驱动。
Instead of nested switch/case
而不是嵌套的开关/案例
switch (state)
case A:
switch (event):
case e1: ....
case e2: ....
case B:
switch (event):
case e3: ....
case e1: ....
you can make a 2d array or function pointers and just call handleEvent[state][event]
你可以创建一个二维数组或函数指针,然后调用 handleEvent[state][event]