home comics writing pictures archive about

2018-03-25 - In IL: Print the Alphabet (while)

The while loop is the simplest form of loop. It simply repeats the specified operations as long as the specified condition is true. To show off the while loop we are going to use a program that prints out the letters of the alphabet.

Program.cs
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
namespace CsWhile
{
class Program
{
static void Main(string[] args)
{
char ch = 'A';
while(ch <= 'Z')
{
Console.Write(ch);
ch++;
}
Console.WriteLine();
}
}
}
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22

As you can see this is a very simple program. We start at the letter A and then print and increment it until it's the letter Z. This works because characters are actually just numbers and in the encoding used by C# all capital letters are continuous and in order.

Now let's compile this program and see what we get.

Main
.method private hidebysig static void Main(string[] args) cil managed
{
.entrypoint
// Code size 27 (0x1b)
.maxstack 2
.locals init ([0] char ch)
IL_0000: ldc.i4.s 65
IL_0002: stloc.0
IL_0003: br.s IL_0010
IL_0005: ldloc.0
IL_0006: call void [mscorlib]System.Console::Write(char)
IL_000b: ldloc.0
IL_000c: ldc.i4.1
IL_000d: add
IL_000e: conv.u2
IL_000f: stloc.0
IL_0010: ldloc.0
IL_0011: ldc.i4.s 90
IL_0013: ble.s IL_0005
IL_0015: call void [mscorlib]System.Console::WriteLine()
IL_001a: ret
} // end of method Program::Main
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81

As you can see there's no special looping instructions. Instead we just get a couple of branching instructions. We start by initializing our variable to 65 ('A') and then we jump to a point near the end of the function. There we find a second branch that tests if the local variable is less than or equal to 90 ('Z') and jumps backwards in the function if it is. There we find the innards of our loop where we print out and increment the local variable. Then we find ourselves at the second branch point again and perform the test again. If it's true we repeat or loop again and if it's false we continue on and the function ends.

Next time we will look at other kinds of loops.

2018-03-04 - The easier road

I'm currently working on converting the writings section into a hierarchy of pages instead of the simple category-page system it has now. This allows me the freedom to have as many or a few levels as I need instead of trying to force all pages into a fixed arrangement. One of the main requirements for the new version is that it should support using Open Live Writer, the program I use to write blog posts, to create and edit pages. That way I have a consistent and simple way to maintain the content on the site which should encourage me to write more.

I started down the road of duplicating the interface I had for the blog into the writing section. The plan was to use the title as the path to the page. For example I would have a page with a title of "Programming/Languages/C++" where "C++" would be the actual title of the page and "Languages" and "Programming" would be the titles of parent pages. The API would split the title into parts based on slashes and use that to identify the page being created, edited or deleted. There would also have been logic to create any missing pages in the path and delete any pages that were empty as a result of the changes. That is until I discovered an easier road.

While looking into what information Open Live Writer encodes in a post when sending it to the website I found mentions of "Supports pages" and "Supports parent" options. Pages and parents? That's exactly what I'm trying to implement. After some investigation I discovered that Open Live Writer had a second editor mode for dealing with a hierarchies of pages. All I had to do was tell it that my website supported Pages and it would enable that functionality for me.

I was very happy to find this because it made things a lot simpler. Instead of abusing the title to give more information that it was intended to give I could get that information from places specifically decided to contain that information. On the other hand I was also slightly saddened to find this. I had spent a lot of time planning out how to handle the title as a path and had already started writing code for some of the methods. Now I had to throw all of that away.

The easier road is nice but sometimes it's annoying when you don't notice it until after you've been walking down the hard road for a while.

2018-02-10 - Parts of Speech: Articles

Articles are used to indicate which entity a noun refers to. The direct article (The in English) is used to refer to a specific thing while the indirect article (A or An) is used to indicate a generic thing. A is used when the noun begins with a consonant sound while An is used when the noun begins with a vowel sound. For example it would be "A house" but "An H".

Articles are actually a type of adjective but I wanted to talk about articles specifically before talking about adjectives in general because articles allow us to be able to completely describe a simple sentence. The main parts of a sentence are the noun and the verb. The noun said what did something and the verb says what that something did. The article helps to indicate which something is being referred to and although they aren't required they do show up often.

The cat jumps
Article Noun Verb

Acardrives
ArticleNounVerb

catsjump
NounVerb

Next we will go back a bit and talk more about adjectives.

2018-01-20 - In IL: Loop Instructions

This post will be really simple because there are no IL loop instructions. As we shall see loops in IL are implemented using branching instructions just like the conditional statements we saw previously. The main difference is that conditionals tend to jump forward while loops tend to jump backwards so that instructions can be re-ran.

So the question is why isn't there any loop instructions? Well let's think about what these loop instructions would need to do. There is a large variety of programing languages out there and those languages have very different ideas about how they want their loops to work. In order for the IL loop instructions to be useful they would need to meet the majority of these scenarios which would require a large number of very complex looping instructions. So instead IL provides simple branching instructions that can be combined to create complex looping statements. Keeping IL simple and providing more power to languages that want to target IL.

Next time we will look at the simple while loop.

Prev page

9 10 11 12 13

Prev page