ICode9

精准搜索请尝试: 精确搜索
首页 > 其他分享> 文章详细

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

2021-12-23 12:58:00  阅读:221  来源: 互联网

标签:Case loc goto val long C语言 --- switch rdi


阅读之前:

  • 本文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

标签:Case,loc,goto,val,long,C语言,---,switch,rdi
来源: https://blog.csdn.net/lsllll44/article/details/122104455

本站声明: 1. iCode9 技术分享网(下文简称本站)提供的所有内容,仅供技术学习、探讨和分享;
2. 关于本站的所有留言、评论、转载及引用,纯属内容发起人的个人观点,与本站观点和立场无关;
3. 关于本站的所有言论和文字,纯属内容发起人的个人观点,与本站观点和立场无关;
4. 本站文章均是网友提供,不完全保证技术分享内容的完整性、准确性、时效性、风险性和版权归属;如您发现该文章侵犯了您的权益,可联系我们第一时间进行删除;
5. 本站为非盈利性的个人网站,所有内容不会用来进行牟利,也不会利用任何形式的广告来间接获益,纯粹是为了广大技术爱好者提供技术内容和技术思想的分享性交流网站。

专注分享技术,共同学习,共同进步。侵权联系[81616952@qq.com]

Copyright (C)ICode9.com, All Rights Reserved.

ICode9版权所有