Click here to Skip to main content
Click here to Skip to main content
 
Add your own
alternative version

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

, 19 Dec 2012 CPOL
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\switch3.cpp
	.686P
	.XMM
	include listing.inc
	.model	flat

INCLUDELIB LIBCMTD
INCLUDELIB OLDNAMES

PUBLIC	?main3@@YAHXZ					; main3
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\switch3.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 ?main3@@YAHXZ
_TEXT	SEGMENT
tv64 = -208						; size = 4
_i$ = -8						; size = 4
?main3@@YAHXZ PROC					; main3, 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], 700		; 000002bcH
	jg	SHORT $LN14@main3
	cmp	DWORD PTR tv64[ebp], 700		; 000002bcH
	je	SHORT $LN5@main3
	cmp	DWORD PTR tv64[ebp], 250		; 000000faH
	jg	SHORT $LN15@main3
	cmp	DWORD PTR tv64[ebp], 250		; 000000faH
	je	SHORT $LN7@main3
	cmp	DWORD PTR tv64[ebp], 100		; 00000064H
	je	SHORT $LN9@main3
	cmp	DWORD PTR tv64[ebp], 200		; 000000c8H
	je	SHORT $LN8@main3
	jmp	SHORT $LN1@main3
$LN15@main3:
	cmp	DWORD PTR tv64[ebp], 500		; 000001f4H
	je	SHORT $LN6@main3
	jmp	SHORT $LN1@main3
$LN14@main3:
	cmp	DWORD PTR tv64[ebp], 750		; 000002eeH
	je	SHORT $LN4@main3
	cmp	DWORD PTR tv64[ebp], 800		; 00000320H
	je	SHORT $LN3@main3
	cmp	DWORD PTR tv64[ebp], 900		; 00000384H
	je	SHORT $LN2@main3
	jmp	SHORT $LN1@main3
$LN9@main3:

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

	call	?f1@@YAXXZ				; f1
	jmp	SHORT $LN10@main3
$LN8@main3:

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

	call	?f2@@YAXXZ				; f2
	jmp	SHORT $LN10@main3
$LN7@main3:

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

	call	?f2@@YAXXZ				; f2
	jmp	SHORT $LN10@main3
$LN6@main3:

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

	call	?f1@@YAXXZ				; f1
	jmp	SHORT $LN10@main3
$LN5@main3:

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

	call	?f2@@YAXXZ				; f2
	jmp	SHORT $LN10@main3
$LN4@main3:

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

	call	?f2@@YAXXZ				; f2
	jmp	SHORT $LN10@main3
$LN3@main3:

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

	call	?f2@@YAXXZ				; f2
	jmp	SHORT $LN10@main3
$LN2@main3:

; 16   :         case 900: f1(); break;

	call	?f1@@YAXXZ				; f1
	jmp	SHORT $LN10@main3
$LN1@main3:

; 17   : 
; 18   :         default: f3(); 

	call	?f3@@YAXXZ				; f3
$LN10@main3:

; 19   :     }
; 20   : 
; 21   :     return 0;

	xor	eax, eax

; 22   : }

	pop	edi
	pop	esi
	pop	ebx
	add	esp, 208				; 000000d0H
	cmp	ebp, esp
	call	__RTC_CheckEsp
	mov	esp, ebp
	pop	ebp
	ret	0
?main3@@YAHXZ ENDP					; main3
_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)

Share

About the Author

Zuoliu Ding
Software Developer
United States United States
An Adjunct Faculty and Software Developer in Los Angeles and Orange County, CA
 
* Typical articles published in Dr. Dobb’s Journal and Windows Developer Magazine:

- A Silent Component Update for Internet Explorer
- Silent Application Update
- An MDI-Style Web Browser and Load Spy Monitor
- Implementing Wireless Print for WinNT/Win2K
- Multi-State Checkbox Tree Views
- A Generic Tool Tip Class
- An Easy Way to Add Tool Tips to Any MFC Control

- More from Google...

| Advertise | Privacy | Mobile
Web04 | 2.8.141022.1 | Last Updated 19 Dec 2012
Article Copyright 2010 by Zuoliu Ding
Everything else Copyright © CodeProject, 1999-2014
Terms of Service
Layout: fixed | fluid