Click here to Skip to main content
15,891,757 members
Articles / Programming Languages / ASM

Something You May Not Know About the Switch Statement in C/C++

Rate me:
Please Sign up or sign in to vote.
4.86/5 (152 votes)
19 Dec 2012CPOL13 min read 374.1K   463   146  
A discussion on how switch/case is executed, by reverse engineering in VC++
; Listing generated by Microsoft (R) Optimizing Compiler Version 16.00.30319.01 

	TITLE	D:\Articles\CodeProject\switch\CppExample\switch6.cpp
	.686P
	.XMM
	include listing.inc
	.model	flat

INCLUDELIB LIBCMTD
INCLUDELIB OLDNAMES

PUBLIC	?main6@@YAHXZ					; main6
EXTRN	?f3@@YAXXZ:PROC					; f3
EXTRN	?f2@@YAXXZ:PROC					; f2
EXTRN	?f1@@YAXXZ:PROC					; f1
EXTRN	__RTC_CheckEsp:PROC
EXTRN	__RTC_Shutdown:PROC
EXTRN	__RTC_InitBase:PROC
;	COMDAT rtc$TMZ
; File d:\articles\codeproject\switch\cppexample\switch6.cpp
rtc$TMZ	SEGMENT
__RTC_Shutdown.rtc$TMZ DD FLAT:__RTC_Shutdown
rtc$TMZ	ENDS
;	COMDAT rtc$IMZ
rtc$IMZ	SEGMENT
__RTC_InitBase.rtc$IMZ DD FLAT:__RTC_InitBase
; Function compile flags: /Odtp /RTCsu /ZI
rtc$IMZ	ENDS
;	COMDAT ?main6@@YAHXZ
_TEXT	SEGMENT
tv64 = -208						; size = 4
_i$ = -8						; size = 4
?main6@@YAHXZ PROC					; main6, COMDAT

; 4    : {

	push	ebp
	mov	ebp, esp
	sub	esp, 208				; 000000d0H
	push	ebx
	push	esi
	push	edi
	lea	edi, DWORD PTR [ebp-208]
	mov	ecx, 52					; 00000034H
	mov	eax, -858993460				; ccccccccH
	rep stosd

; 5    :     int i =1;

	mov	DWORD PTR _i$[ebp], 1

; 6    : 
; 7    :     switch (i)

	mov	eax, DWORD PTR _i$[ebp]
	mov	DWORD PTR tv64[ebp], eax
	cmp	DWORD PTR tv64[ebp], 200		; 000000c8H
	jg	SHORT $LN15@main6
	cmp	DWORD PTR tv64[ebp], 200		; 000000c8H
	je	$LN6@main6
	mov	ecx, DWORD PTR tv64[ebp]
	sub	ecx, 3
	mov	DWORD PTR tv64[ebp], ecx
	cmp	DWORD PTR tv64[ebp], 97			; 00000061H
	ja	$LN1@main6
	mov	edx, DWORD PTR tv64[ebp]
	movzx	eax, BYTE PTR $LN17@main6[edx]
	jmp	DWORD PTR $LN18@main6[eax*4]
$LN15@main6:
	cmp	DWORD PTR tv64[ebp], 750		; 000002eeH
	jg	SHORT $LN16@main6
	cmp	DWORD PTR tv64[ebp], 750		; 000002eeH
	je	SHORT $LN3@main6
	cmp	DWORD PTR tv64[ebp], 250		; 000000faH
	je	SHORT $LN5@main6
	cmp	DWORD PTR tv64[ebp], 700		; 000002bcH
	je	SHORT $LN4@main6
	jmp	SHORT $LN1@main6
$LN16@main6:
	cmp	DWORD PTR tv64[ebp], 900		; 00000384H
	je	SHORT $LN2@main6
	jmp	SHORT $LN1@main6
$LN10@main6:

; 8    :     {
; 9    :         case 3: f1(); break;

	call	?f1@@YAXXZ				; f1
	jmp	SHORT $LN11@main6
$LN9@main6:

; 10   :         case 5: f2(); break;

	call	?f2@@YAXXZ				; f2
	jmp	SHORT $LN11@main6
$LN8@main6:

; 11   :         case 7: f1(); break;

	call	?f1@@YAXXZ				; f1
	jmp	SHORT $LN11@main6
$LN7@main6:

; 12   :         case 100: f1(); break;

	call	?f1@@YAXXZ				; f1
	jmp	SHORT $LN11@main6
$LN6@main6:

; 13   :         case 200: f2(); break;

	call	?f2@@YAXXZ				; f2
	jmp	SHORT $LN11@main6
$LN5@main6:

; 14   :         case 250: f2(); break;

	call	?f2@@YAXXZ				; f2
	jmp	SHORT $LN11@main6
$LN4@main6:

; 15   :         case 700: f2(); break;

	call	?f2@@YAXXZ				; f2
	jmp	SHORT $LN11@main6
$LN3@main6:

; 16   :         case 750: f2(); break;

	call	?f2@@YAXXZ				; f2
	jmp	SHORT $LN11@main6
$LN2@main6:

; 17   :         case 900: f2(); break;

	call	?f2@@YAXXZ				; f2
	jmp	SHORT $LN11@main6
$LN1@main6:

; 18   : 
; 19   :         default: f3(); 

	call	?f3@@YAXXZ				; f3
$LN11@main6:

; 20   :     }
; 21   : 
; 22   :     return 0;

	xor	eax, eax

; 23   : }

	pop	edi
	pop	esi
	pop	ebx
	add	esp, 208				; 000000d0H
	cmp	ebp, esp
	call	__RTC_CheckEsp
	mov	esp, ebp
	pop	ebp
	ret	0
$LN18@main6:
	DD	$LN10@main6
	DD	$LN9@main6
	DD	$LN8@main6
	DD	$LN7@main6
	DD	$LN1@main6
$LN17@main6:
	DB	0
	DB	4
	DB	1
	DB	4
	DB	2
	DB	4
	DB	4
	DB	4
	DB	4
	DB	4
	DB	4
	DB	4
	DB	4
	DB	4
	DB	4
	DB	4
	DB	4
	DB	4
	DB	4
	DB	4
	DB	4
	DB	4
	DB	4
	DB	4
	DB	4
	DB	4
	DB	4
	DB	4
	DB	4
	DB	4
	DB	4
	DB	4
	DB	4
	DB	4
	DB	4
	DB	4
	DB	4
	DB	4
	DB	4
	DB	4
	DB	4
	DB	4
	DB	4
	DB	4
	DB	4
	DB	4
	DB	4
	DB	4
	DB	4
	DB	4
	DB	4
	DB	4
	DB	4
	DB	4
	DB	4
	DB	4
	DB	4
	DB	4
	DB	4
	DB	4
	DB	4
	DB	4
	DB	4
	DB	4
	DB	4
	DB	4
	DB	4
	DB	4
	DB	4
	DB	4
	DB	4
	DB	4
	DB	4
	DB	4
	DB	4
	DB	4
	DB	4
	DB	4
	DB	4
	DB	4
	DB	4
	DB	4
	DB	4
	DB	4
	DB	4
	DB	4
	DB	4
	DB	4
	DB	4
	DB	4
	DB	4
	DB	4
	DB	4
	DB	4
	DB	4
	DB	4
	DB	4
	DB	3
?main6@@YAHXZ ENDP					; main6
_TEXT	ENDS
END

By viewing downloads associated with this article you agree to the Terms of Service and the article's licence.

If a file you wish to view isn't highlighted, and is a text file (not binary), please let us know and we'll add colourisation support for it.

License

This article, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)


Written By
United States United States
This member has not yet provided a Biography. Assume it's interesting and varied, and probably something to do with programming.

Comments and Discussions