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.
2016-10-09 - Nightmares
I occasionally have what people might call nightmares. That is to say I have dreams that are about scary things. I remember having a dream a while ago where I was running around an industrial plant at night and being chased by zombies. The thing is though, I don't find them to be scary. I actually find them rather enjoyable. I sometimes get startled awake while having these dreams but I never feel afraid to go back to sleep afterwards.
I think playing video games helps me to accept dreams as not being real. I'm used to controlling a character in a virtual world which makes it easier to separate myself from the dream world while still being a part of it. I can be impacted by what's happening in the dream but I know it can't actually hurt me.
What I find really terrifying is when you wake up but are unable to move. You lay there yelling at your muscles to do something. Trying to force yourself to fully wake up. That sense of dread that you are trapped in your own body. That's a nightmare.
2016-09-18 - In IL: Largest of Three Numbers (If-ElseIf-Else)
Last time we looked at a program which used an If statement to determine which of two numbers was the largest. If statements can actually get a lot more complicated. They can be extended with additional else if clauses which allow alternative conditions to be tested. The condition can also contain multiple comparisons combined using logical operators.
Continuing from last time let's look at a program that reports which of three numbers is the largest using if, else if, else statements in Visual basic .NET.
Before we look at the compiled output I want to point out one thing. The If condition uses the AndAlso operator (&& in C#) while the ElseIf uses the And operator (& in C#). We will be able to see the difference between these two operators when we look at the compiled code. So let's do that now.
As expected we have three sets of instructions which print text to the console. There are also three branching instructions used to implement the if-else chain.
Branching instructions are generally of the form Value1 Operation Value2 where Value1 is the second value popped off the stack, Value2 is the first value popped off the stack, and Operation is the condition specified by the branch instruction. Last time we determined Value1 and Value2 by running through the program and determining the values on the stack at the point of the branch instruction. This will become very time consuming as our programs get more and more complicated so I'd like to find an easier way.
The value at the top of the stack will be the last value pushed onto the stack and not removed. The value second from the top of the stack will be the second to last value pushed onto the stack and not removed. This means we can determine Value1 and Value2 by working backwards from the branch instruction and looking at what gets pushed onto the stack.
Looking at the first branch instruction on line 90 we see the first instruction before it loads local variable 1 while the second instruction before it loads local variable 0. This means Value1 is NumberA and Value2 is NumberB. The instruction is ble so the condition is NumberA <= NumberB. Looking at the code this matches up with the first part of the if condition (Remember the compiler likes to inverse branch conditions compared to if conditions). If the condition is true we jump to line 110 which is the start of the ElseIf block.
The second branch instruction will only be encountered if the first condition is false. It is also a ble instruction but this time it compares local variable 0 and local variable 2. So the condition is NumberA <= NumberC which matches up with the second part of the if condition. If the condition is true we again jump to line 110. If the condition is false we execute the If block and print that NumberA is the largest.
If either of those conditions are true we branch to the ElseIf Condition. This time though we have two greater-than compare instructions and an and instruction followed by a brfalse instruction. The process for determining comparison values is the same as branching instructions so the full condition is NumberB > NumberA And NumberB > NumberC which is exactly the ElseIf condition. If the result is false we branch to the Else block otherwise we execute the ElseIf block and print that NumberB is the largest.
Why does the ElseIf condition perform the full comparison while the If condition had two branch instructions? As I mentioned earlier the ElseIf uses And while the If uses AndAlso. The difference between these two operators is that AndAlso is lazy. Since logical and requires all of its arguments to be true in order for the result to be true it's possible to know that the result is false after only checking some of the arguments if one of them turns out to be false. AndAlso uses this fact to skip the other comparison if it determines that the first is false. And on the other hand performs all comparisons.
AndAlso is able to make less comparisons which in some ways makes it more efficient. And always executes all comparisons which is important if some of those comparisons have side effects.
Next time we are going to look at the IL Switch instruction.



![[Valid RSS]](/images/valid-rss-rogers.png)
