简体   繁体   English

MSVC编译器从if / else语句反汇编代码

[英]MSVC compiler disassembly code from if/else statement

I have a general question about how MSVC generates machine code regarding to else statement. 我有一个关于MSVC如何生成关于else语句的机器代码的一般问题。

A simple exmaple here: 这里有个简单的例子:

1 bool is_zero(int num) {
2    if (num)
3        return false;
4    else
5        return true;
6 }

and its disassembly code looks like 它的反汇编代码看起来像

; Listing generated by Microsoft (R) Optimizing Compiler Version 19.20.27508.1 

; Function compile flags: /Odtp
num$ = 8
bool is_zero(int) PROC ; is_zero
; File C:\Users\ContainerAdministrator\AppData\Local\Temp\compiler-explorer-compiler11943-18164-1cmj5fb.ujww\example.cpp
; Line 1
  mov DWORD PTR [rsp+8], ecx
; Line 2
  cmp DWORD PTR num$[rsp], 0
  je SHORT $LN2@is_zero
; Line 3
  xor al, al
  jmp SHORT $LN1@is_zero
; Line 4
  jmp SHORT $LN3@is_zero
$LN2@is_zero:
; Line 5
  mov al, 1
$LN3@is_zero:
$LN1@is_zero:
; Line 6
  ret 0
bool is_zero(int) ENDP ; is_zero

Question is: will the line jmp SHORT $LN3@is_zero (corresponding to the 4th line else keyword) get executed? 问题是:将执行jmp SHORT $LN3@is_zero (对应于第4行else关键字)吗?

is there any good reason MSVC generates such code? MSVC生成此类代码有什么充分的理由吗?

It's unoptimized code. 这是未优化的代码。 That line 4 jmp corresponds to the jump from the if body past the else body. 第4行jmp对应于从if主体到else主体的跳转。 In this case it is never executed. 在这种情况下,它永远不会执行。 Enable optimizations and it will go away. 启用优化,它将消失。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM