[英]MSVC compiler disassembly code from if/else statement
我有一個關於MSVC如何生成關於else
語句的機器代碼的一般問題。
這里有個簡單的例子:
1 bool is_zero(int num) {
2 if (num)
3 return false;
4 else
5 return true;
6 }
它的反匯編代碼看起來像
; 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
問題是:將執行jmp SHORT $LN3@is_zero
(對應於第4行else
關鍵字)嗎?
MSVC生成此類代碼有什么充分的理由嗎?
這是未優化的代碼。 第4行jmp
對應於從if
主體到else
主體的跳轉。 在這種情況下,它永遠不會執行。 啟用優化,它將消失。
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.