什么是跳台?
有人可以解释跳转表的机制吗,为什么嵌入式系统需要跳转表?
解决方案
回答
从维基百科:
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.
回答
这里描述了一个跳转表,但是简单来说,它是CPU根据特定条件应跳转到的地址数组。例如,C switch语句通常被实现为跳转表,其中每个跳转条目都将转到特定的" case"标签。
在内存使用率非常高的嵌入式系统中,使用跳转表而不是使用更多内存密集型方法(例如大规模的if-else-if),可以更好地服务于许多构造。
回答
维基百科很好地总结了这一点:
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受到极大限制时(如在嵌入式平台中经常发生这种情况),使用它是一种有用的构造。
回答
跳转表(通常称为分支表)通常仅由机器使用。
编译器在汇编程序中创建所有标签的列表,并将所有标签链接到内存位置。跳转表几乎是功能,变量或者标签可能存储在内存中的参考卡。
因此,当函数执行时,完成时会跳回到其先前的存储位置或者跳至下一个函数,依此类推。
而且,如果我们谈论的是我的想法,那么我们不仅需要在嵌入式系统中使用它们,还需要在任何类型的编译/解释环境中使用它们。
布赖恩·吉安佛卡罗
回答
跳转表可以是指向函数的指针数组,也可以是机器码跳转指令的数组。如果我们具有一组相对静态的函数(例如系统调用或者某个类的虚函数),则可以创建此表一次,并使用简单的数组索引来调用这些函数。这将意味着要检索指针并调用函数,或者根据所用表的类型跳转到机器代码。
在嵌入式编程中这样做的好处是:
- 索引比机器代码或者指针更有效地利用内存,因此在受限环境中有节省内存的潜力。
- 对于任何特定功能,索引将保持稳定,更改功能仅需要换出功能指针。
如果这样做确实会花费我们一点时间来访问表,但这并不比任何其他虚拟函数调用差。
回答
跳转表(也称为分支表)是一系列指令,所有指令均无条件分支到代码中的另一点。
我们可以将它们视为所有情况都已填充的switch(或者select)语句:
MyJump(int c) { switch(state) { case 0: goto func0label; case 1: goto func1label; case 2: goto func2label; } }
请注意,没有返回,它跳转到的代码将执行返回,并且它将跳回到调用myjump的位置。
这对于在状态机上根据状态变量执行某些代码的状态机很有用。还有许多其他用途,但这是主要用途之一。
它用于我们不想浪费时间摆弄堆栈并希望节省代码空间的地方。它尤其适用于速度非常重要的中断处理程序,并且导致中断的外围设备仅由单个变量知道。这类似于带有中断控制器的处理器中的向量表。
一种用途是采用售价为0.60美元的微控制器,并为视频应用生成复合(TV)信号。实际上,micro并没有强大的功能,它的速度几乎不足以写入每条扫描线。跳转表将用于绘制字符,因为从内存中加载位图花费的时间太长,并使用for()循环将位图推出。取而代之的是,分别跳到字母和扫描行,然后再跳转8条左右的指令,将数据直接直接写入端口。
-亚当