switch语句和汇编代码表示

本文探讨了GCC编译器如何处理switch语句,特别是在开关情况多且值范围较小时使用跳转表的情况。跳转表提供了一种高效的方式,使得执行时间与开关情况数量无关。通过引用GCC文档,解释了jump table的工作原理,并展示了跳转表的汇编代码示例。
摘要由CSDN通过智能技术生成

statement(switch语句)可根据一个整数索引值(integer index)进行多重分支,处理有多种可能结果的测试时,这种语句特别有用。当开关情况数量比较多(如4个以上),并且值得范围跨度比较小时,GCC编译switch语句时会使用跳转表(jump table)这种数据结构;当不满足以上情况时编译方式和编译多个if-else语句的方式一样。这两种方式相比,跳转表的优点是执行开关语句的时间与开关情况的数量无关。
下面是引用GCC文档中的一段文字,具体解释了jump table的用法:

You can get the address of a label defined in the current function (or a containing function) with the unary(一元的) operator ‘&&’. The value has type void *. This value is a constant(常量) and can be used wherever a constant of that type is valid. For example: 
			 1. void *ptr; 
			 2. /* ... */ 
			 3. ptr = &&foo; 
 To use these values, you need to be able to jump to one. This is done with the computed goto statement1, goto *exp;. For example,
			 1. goto *ptr; 
Any expression of type void * is allowed. One way of using these constants is in initializing a sta
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值