C语言 --- switch语句的原理(goto版本)

本文源自《Computer Systems A Programmer’s Perspective》第三版的3.6.8节,探讨C语言switch语句的实现,尤其是跳转表的概念。跳转表是一个数组,用于高效实现根据switch索引执行相应操作。通过汇编代码分析,解释了如何使用跳转表进行非直接跳转,提高程序执行效率。
摘要由CSDN通过智能技术生成

阅读之前:

  • 本文copy自 《Computer Systems A Programmer’s Perspective》 ,第三版。的 3.6.8 节 switch语句。如果汇编代码部分看不懂,需要把前面的部分全部读懂。

C语言的switch的代码:

void switch_eg(long x, long n, long *dest)
{
    long val = x;
    switch (n)
    {
    case 100:
        val *= 13;
        break;
    case 102:
        val += 10;
    /* Fall through */
    case 103:
        val += 11;
        break;
    case 104:
    case 106:
        val *= val;
        break;
    default:
        val = 0;
    }
    *dest = val;
}

C语言—将switch语句翻译为扩展的C语言:

  • 跳转表(jump table):Not only do they make the C code more readable, but they also allow an efficient implementation using a data structure called a jump table. A jump table is an array where entryi is the address of a code segment implementing the action the program should take when the switch index equals i. The code performs an array reference into the jump table using the switch index to determine the target for a jump instruction.
  • &在C语言里是取址符(创建一个指向数据的指针),GCC的作者创造了一个新的操作符 &&(创建一个指向代码所在位置的指针)
void switch_eg_impl(long x, long n, long *dest)
{
    /* Table of code pointers */
    static void *jt[7] = {
        &&loc_A, &&loc_def, &&loc_B,
        &&loc_C, &&loc_D, &&loc_def,
        &&loc_D};
    unsigned long index = n - 100;
    long val;

    if (index > 6)
        goto loc_def;
    /* Multiway branch */
    goto *jt[index];

loc_A: /* Case 100 */
    val = x * 13;
    goto done;
loc_B: /* Case 102 */
    x = x + 10;
/* Fall through */
loc_C: /* Case 103 */
    val = x + 11;
    goto done;
loc_D: /* Cases 104, 106 */
    val = x * x;
    goto done;
loc_def: /* Default case */
    val = 0;
done:
    *dest = val;
}

上面代码用GCC进行运行的结果如图:

在这里插入图片描述

翻译为汇编代码(Part 1):

  • jmp指令前的操作数,带了一个 *号,表示一个非直接跳转。并且该操作数指的是一个存储器的位置,地址的值在%eax里(and the operand specifies a memory location indexed by register %eax, which holds the value of index )。

注:jmp的地址 8*%rsi + .L4

		void switch_eg(long x, long n, long *dest)
		x in %rdi, n in %rsi, dest in %rdx
1 	switch_eg:
2       subq  $100, %rsi               Compute index = n-100
3       cmpq  $6, %rsi                Compare index:6
4       ja    .L8                     If >, goto loc_def
5       jmp   *.L4(,%rsi,8)           Goto *jg[index]
6     .L3:                             loc_A:
7       leaq (%rdi,%rdi,2), %rax        3*x
8       leaq  (%rdi,%rax,4), %rdi      val = 13*x
9       jmp   .L2                        Goto done
10    .L5:                             loc_B:
11      addq  $10, %rdi               x = x + 10
12    .L6:                            loc_C:
13      addq  $11, %rdi               val = x + 11
14      jmp  .L2                       Goto done
15    .L7:                            loc_D:
16      imulq %rdi, %rdi              val = x * x
17      jmp .L2                      Goto done
18    .L8:                           loc_def:
19      movl $0, %edi                 val = 0
20    .L2:                          done:
21      movq %rdi, (%rdx)            *dest = val
22      ret                          Return

汇编代码跳转表部分(Part 2):

  • .rodata (表示 “read-only data”)
  • 有着连续的7个quad(8字节)words,每个word的值都是声明了的指令的地址(比如:.L3)。.L4标记了定位的开始,该标签的地址是非直接跳转的基地址(part 1的第五行)。(there should be a sequence of seven “quad” (8- byte) words, where the value of each word is given by the instruction address associated with the indicated assembly-code labels (e.g., .L3). Label .L4 marks the start of this allocation. The address associated with this label serves as the base for the indirect jump (line 5).)
  • 可以看到:在有很多个分支的情况下,使用跳转表是很高效的,可以一步到位。
1 	.section 	.rodata
2 	.align 			8 Align address to multiple of 8
3  .L4:
4 	.quad .L3 		Case 100: loc_A
5 	.quad .L8 		Case 101: loc_def
6 	.quad .L5 		Case 102: loc_B
7 	.quad .L6 		Case 103: loc_C
8 	.quad .L7 		Case 104: loc_D
9 	.quad .L8 		Case 105: loc_def
10 	.quad .L7 		Case 106: loc_D
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值