2017-01-07 - In IL: Grade Analyser (switch)
In IL
- Part 1 - Introduction
- Part 2 - Variables and Types
- Part 3 - Variables in Visual Basic .NET
- Part 4 - Instructions and the Stack
- Part 5 - Volume of a Cylinder (Operations)
- Part 6 - Branching Instructions
- Part 7 - Largest of Two Numbers (if-else)
- Part 8 - Largest of Three Numbers (If-ElseIf-Else)
- Part 9 - Switch instruction
- Part 10 - Grade Analyser (switch)
- Part 11 - Prize Calculator (switch-2)
- Part 12 - VB Grade Analyser (Select)
- Part 13 - Loop Instructions
- Part 14 - Print the Alphabet (while)
- Part 15 - Print the Alphabet (do while, for)
- Part 16 - Print the Alphabet (Do Until)
- Part 17 - Print the Alphabet (break, continue)
- Part 18 - Array Instructions
- Part 19 - Summing Arrays
- Part 20 - Other Instructions
- Part 21 - Assemblies
- Part 22 - Class Definitions
- Part 23 - C# Classes and Structs
- Part 24 - VB Classes, Modules and Structures
- Part 25 - Field Definitions
- Part 26 - Field Declarations
Last time we looked at the IL switch instruction. Today we are going to look at the switch statement in C# and see how it uses the IL switch instruction.
We are going to use a program that tests a student's grade and prints out a message.
Now if we compile the above program we get.
Looking at the IL we can clearly see the code writing text to the console, setting the needToImprove variable, and then doing some branching for each of the cases. This is all stuff we've seen before but what's going on before that?
The program starts by loading the value 66 into the grade variable. 66 is the Unicode value for the capital B letter. We then set the needToImprove variable to 0 which represents false. Then we push the grade variable onto the stack and subtract 65 from it. Why does it do that?
65 is the Unicode value for A. Since Unicode letters are in alphabetical order subtracting 65 from the grade variable converts the character value into an offset from A. So A becomes 0, B becomes 1, C becomes 2 and so on. As you will recall from last time switch instructions are 0 based so having the first option be 0 and subsequent options incrementing from there simplifies the switch instruction. Otherwise we would need to specify branching targets for 0 to 64 that all point to the default case.
So now let's look at the switch instruction. The following table shows the switch instruction branch index, the branch target, and the corresponding case statement.
Index | Branch Target | Case |
---|---|---|
0 ('A' - 65) | IL_0020 | case 'A' |
1 ('B' - 65) | IL_002e | case 'B' |
2 ('C' - 65) | IL_003c | case 'C' |
3 ('D' - 65) | IL_004a | case 'D' |
If you look at the instructions at each of the branch targets you can see they line up with the case statements perfectly.
There's only one thing left, the default case. Recall that if the value on the stack doesn't correspond to a target of the switch instruction then nothing happens and executions continues at the next instruction. Thus if the grade value isn't A, B, C, or D, execution will move on to the unconditional branch to IL_0058. This is our default case.
Next time we will try and bust the switch statement.
Comments: