2017-01-07 - In IL: Grade Analyser (switch)
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.
2016-12-17 - Programs aren't Permanent
After a certain age people begin to understand that objects still exist when you put them away. If you put a hammer on a shelf today it will likely still be there in a week and still be the same hammer. Programs on the other hand don't keep when you stop using them, at least not in the same way.
This is because programs don't contain components the way we would normally think of them. A hammer is made up of pieces of metal and plastic. The hammer is a hammer because of the specific shape of those pieces and how they are put together. A program isn't made up of stuff, it is made up of instructions.
When a program is launched it starts executing the instructions stored within it. These instructions generally start by building up data structures in memory that store a program's state. When a program is closed those data structures are destroyed. The information stored within them may be persisted into another form but the exact data structures the program was using to run no longer exist.
Imagine having to rebuild the hammer every time you needed to use it. The hammer would change each time based on the current circumstances. Maybe there wasn't a lot of free plastic one time so you ended up with a very short hammer, or there was an issue getting the metal so you ended up with just a handle.
Because a program is just instructions it is very dependant on the results of those instructions. If the results can change from one run to another then the program's behaviour can change. Some times it launches fine and other time it crashes immediately. It's all up to the circumstances in which the program is being launched.
This is why restarting a program can make it run better.
2016-11-26 - Parts of Speech: Introduction
I enjoy learning. I enjoy gaining a better understanding of how things work. Right now a lot of my time is spent learning about programming languages because they are important to my career and my hobbies, but I want to branch out into learning about language in general.
With this series I hope to learn more about the parts of speech. The different classifications of words, forms of words, usage of words. My hope is that this will improve my writing and may also help me learn another language.
Next time we will start off by learning about nouns.
2016-11-06 - In IL: Switch instruction
Along with the branching instructions that we have already seen there's a special multi-branching instruction. It is used in situations where there are multiple possible outcomes based the value of a single input.
switch
Instruction | Binary Format |
---|---|
switch | 0x45 <uint32> <int32> ... <int32> |
The switch instruction is unique because its encoding is variable length. The encoded instruction contains a value, the uint32, that indicates the number of subsequent int32 values. These int32 values are the targets of the instruction and are the same as the int32 values that showed up when we looked at branching instructions. In IL assembly the uint32 would be computed based on the number of arguments, usually labels, to the switch instruction.
The switch instruction works by popping a value off of the stack and then using that value as an index into the instruction's target list. The instruction then jumps to the instruction indicated by the target at that index. If the value is 0 then the instruction jumps to the instruction indicated by the first target. If the value is 1 then the instruction jumps to the instruction indicated by the second target. If the value is greater than the number of targets then the instruction does nothing and execution moves on to the next instruction.
Next time we will look at some uses of the switch instruction.