Introduction to Windows as a Platform
Content Master Ltd
The Visual Studio Express Editions family provides a free, lightweight, and easy-to-learn suite of programming tools that are aimed at the hobbyist, novice, and student developer. Many people in this category will not have had any formal training in computer science, and indeed they may not have any programming experience at all. If you fall into this category, don’t worry – this guide is for you!
This beginner’s guide is designed for people with little or no prior knowledge of computer programming, who want to create computer programs with the Visual Studio Express tools. If you already have significant programming experience, then you will probably not have much to learn from this article.
So, what will you learn by reading through this guide? Well, if you want to write a program that you can run on your Microsoft Windows computer, this guide will introduce you to the fundamental concepts that will help you understand how to do just that. You will learn the answer to questions such as:
How does a computer work?
What’s the difference between hardware and software?
What’s an Operating System?
How does a computer run my program?
What are the component parts of a program?
The purpose of this guide is to help you understand the environment in which a Microsoft Windows program runs. The way that a program works is affected by many factors including the computer that it runs on, the operating system, and the way that people will use the program. The environment of a computer program is called its platform. When you understand the programming environment, you will be able to appreciate how the computer interprets your programs, and as a result, learning to program will become easier and ultimately you’ll write better programs.
This guide describes the Microsoft Windows platform as a programming environment. It applies to programs that run on Windows and are used by people that run Microsoft Windows – these are called Windows applications. If you want to write an application that people will access by using through their browser, then you need to create a Web application, not a Windows application. In that case, you should read the companion guide “Introduction to Web as a platform”, which provides information about the World Wide Web as an application platform.
Although computers consist of advanced technological components, the basic way a computer operates is very simple, and has changed very little since the invention of electronic computers in the middle of the twentieth century. A computer contains three main functional areas:
Input/Output. This refers to any device attached to the computer that provides a means of interaction or communication between the computer and the ‘outside world’ (from the computer’s perspective). Examples of input devices include the keyboard, mouse, and joystick. Examples of output devices include the monitor, printer and soundcard.
Memory. This is the part of the computer that provides information storage. A computer’s memory is divided into many thousands of small units called bytes. All the data that a computer manages is stored as one or more bytes. You can imagine these bytes as slots or pigeonholes, each with its own memory location or address that the computer uses to fetch and store data, similar to a room full of Post Office boxes. Each ‘box’ or byte contains a number that the computer can process.
Central Processing Unit. The Central Processing Unit (CPU) is the ‘brain’ of the computer, which performs all calculations and co-ordinates the operation of the memory and input/output devices.
The Instruction Cycle
A computer operates by copying bytes from the memory or an input device onto the CPU, performing some calculations on those bytes, and then copying the result of the calculations to the memory or an output device. This process continues in a cycle as shown below:
For this cycle of data processing to be useful, the computer needs to have instructions that tell it what to read and write, and which calculations to perform. These instructions tell the CPU what memory location to read from (fetching data from memory) or write to (storing data to memory), and which input or output devices to access. (Later on in this article you will see how computer memory can hold information that represents external entities such as images or sound.)
The instructions that a computer runs are simply numbers like any other data, and they are stored in the computer’s memory. The CPU always keeps an internal record of which memory location (address) contains the next instruction that it will execute. When it completes one instruction, it reads the next instruction from memory, and updates its record to point to the instruction after that. After it has updated its record, it can process the instruction that it has just read from memory.
So a computer program is just a series of bytes that contain instructions for the CPU. When the computer runs the program, it moves sequentially through the set of instruction bytes, loading each instruction onto the CPU, performing that instruction, and then going back to fetch the next instruction onto the CPU. The program itself does not always need to tell the CPU to move to the next instruction – this happens automatically.
However, instructions can also tell the CPU to modify its internal record so that it will read the next instruction from a different address. These instructions, called ‘jump’ instructions, enable the computer to perform different actions depending on the circumstances. Let’s have a look at how instructions work inside a computer, by taking some example instructions and seeing how the computer uses them.
The diagram depicts several bytes of computer memory that contain CPU instructions (in the lower section) and data (in the upper section).
In the lower section of memory depicted, the numerical values of the CPU instructions are displayed in blue text. Alongside the numerical value is a mnemonic representation of the CPU instructions in semi-readable text.
Starting with the lowest memory location (at the bottom of the diagram) and reading upwards, the CPU instructions tell the CPU to perform the following actions:
Read the value from address 8000 into the CPU.
Subtract 100 from the value in the CPU (the result of the subtraction replaces the value just read into the CPU).
Compare the current value in the CPU to 121.
If the CPU value is not equal (jne stands for Jump Not Equal), jump forward by 8 bytes from the current instruction (to address 2020) and continue by executing the instruction contained at that memory location.
If the CPU value is equal to 121, then the CPU would not jump, and it would continue to read its instructions from the next sequential memory location, which is 2016 in this example.
Jump to address 2038 and continue by executing the instruction contained at that memory location.
Write the value in the CPU to memory location 8842.
Phew! It takes quite a leap of imagination to see how all this number processing could enable a computer to perform all the actions that we use from day to day, like word processing or browsing the Internet. Hang on – you’re through the most complicated part, but you still need a bit more information to help you make the leap.
Challenge: As a quick challenge, see if you can work out what will happen in the example above. Assuming that the value at memory location 8000 contains the value 221, work through the CPU instructions and answer the following questions:
What value gets written to memory location 8842?
What value gets written to memory location 8878?
You can find the answers at the end of this article.
Input and Output Devices
If a computer simply processed instructions in memory, it would not serve any useful purpose. In order to be useful, the computer needs to provide a way to enable the user to indicate what calculations to perform, and to display the results of its calculations – to interact with the computer.
Interaction with a computer takes place through components called input/output devices. For example, a keyboard is an input device, because the computer can read from the keyboard to see when a key is pressed. From the computer’s perspective, the key press is information that is coming in. Conversely, a monitor is an output device, because the computer sends information out to the display as a result of its calculations.
You may wonder how the computer can interact with these devices, when all it does is copy bytes from one place to another place. The answer is that all the input and output information can be represented as bytes. For example, each key on the keyboard has its own numeric value so the computer can identify the key and store the number in a memory location.
Less obviously, the information displayed on your monitor right now is just a series of bytes to the computer. Each dot (pixel) on the screen is represented by some bytes that indicate what color to display on the monitor at that pixel.
In fact, everything that a computer displays or stores is a set of bytes, called a digital representation – whether it is a photo, a video, a song, or even a word processing document. If you’ve ever seen the movie The Matrix, you’ll recognize the image of green 0’s and 1’s cascading down a black screen. This image actually has some truth to it, since a computer sees all data as represented by numbers. The translation between what a computer sees and what we see on our monitors or as a printout is done by the input/output devices.
The job of the computer’s input/output devices is to perform these conversions between the digital representation and the actual medium that the user interacts with – sound, light, paper, keystrokes, or even movement (in the case of a mouse). In fact, sometimes the user doesn’t even interact with an input/output device – network cards and modems, for example, are input/output devices that interact with other computers rather than with humans.
Once the information is inside the computer, in digital format, a computer program just manipulates the bytes in various ways, and then sends the results of its calculations to an output device so that a human or another computer can see the results.
Conversion between a digital representation and the external medium is a complex task, and the input/output devices are specialist electronic components that know how to perform their tasks quickly and efficiently. The devices contain electronic circuitry that is adept at performing conversions. For example, a sound card contains a computer chip called a digital/analog converter that takes a computer’s digital representation of sound, and converts it to an analog wave form which the human ear can hear.
The generic term for any electronic circuitry in a computer is hardware, because it has a physical presence, like any tool that you could buy in your local hardware store. Contrast this with software, which is the name given to the programs that run on a computer. Unlike the hardware, the software has no physical presence on a computer; it is merely a series of bytes in the computer’s memory that constitute the CPU’s instructions.
Hardware has the virtue that it can perform its specialist task very well – in computer-speak, it is dedicated to that task. The down side of this specialization is that hardware is usually inflexible; because it is dedicated to one task, it cannot perform any other tasks. For example, your monitor is very good at displaying data on your computer, but it is not so good at connecting to a network. Software, on the other hand, is very flexible, because it can easily perform different tasks just by changing the contents of the computer memory. In fact, this is exactly what happens when you switch from one program to another, for example from your word-processing application to your Internet browser. The computer changes the bytes that are in memory so that it can run the new program.
Hang on a second! Computers just follow instructions, right? How does the computer know to replace the bytes from one program with the bytes from another? The answer is that there is one master program that is always running, which controls the operation of the computer. This program is called the operating system (OS). One familiar example of an operating system is Microsoft Windows. There are many other operating systems, such as Macintosh and Linux, but they all perform a similar role.
One of the jobs of the operating system is to run other programs, like word processors or browsers, when the user asks it to. The operating system loads the program into the computer’s memory and ‘jumps’ to the starting memory location to run it. The operating system also ‘shares’ the CPU between different programs that are running, so that they all appear to be running at the same time. You can think of the OS as a set of traffic lights at a busy intersection. Each road that joins the intersection is a program running on the CPU. The OS has to keep the overall traffic flowing smoothly by stopping all but one of the roads at any given time. In a computer, the CPU is so fast that even though the OS switches from program to program sequentially, it seems to us as if all the programs are being executed simultaneously. It’s as if the traffic lights change back and forth so quickly that all the vehicles at the intersection seem to be slicing through each other without stopping!
The operating system has another important function. It provides standardized tools that all the other programs can use. You may have noticed that most applications have certain standard features, such as menu bars, borders, and buttons to maximize, minimize and close the application. The reason that these features all appear in the same way is not because all software vendors conscientiously ensure that their applications look like each other! They all appear the same because Windows provides a set of tools that applications can use – the menu bars, borders and buttons as well as other features.
It turns out that Windows provides hundreds of tools for applications to use. Whenever applications read data from a hard drive, or connect to the network, or update the screen display, or many other tasks, they use the tools provided by the operating system. Even something as essential as reading and writing memory is controlled by the operating system, so that applications don’t accidentally (or deliberately) modify the memory locations that are in use by another application.
One of the most useful features provided by the operating system is related to the input/output devices that we discussed earlier. The operating system allows programs to make use of these devices in a standard way.
For any type of hardware device – graphics cards, for example – there may be several different manufacturers who produce their own brand of the device. Each manufacturer’s graphics card works in a slightly different way, because it has different features. If the operating system did not provide any tools, and a program used a graphics card, the programmer would have to write special code for each vendor’s hardware. By using the functions provided by the operating system, the programmer’s job is much easier, because the program just uses the operating system tools and lets the operating system deal with each vendor’s hardware.
So far, we have mainly looked at general aspects of computers that apply to all types of computers. As you learned above, operating systems provide tools that programs can use. However, each operating system provides different tools, or provides the same tools in a different way. To a great extent, therefore, programs that are designed to work on one operating system will not necessarily work on other operating systems. In this section we focus more towards the specifics of the Microsoft Windows platform, although the concepts we cover may also apply to other platforms.
What is a Computer Program?
You have already learned that a program consists of many bytes that contain CPU instructions. These instructions perform basic operations that manipulate data bytes and transfer them from one memory location to another, or to and from some input/output devices. When a program runs, the operating system loads the program files into memory and then invokes the program instructions.
It would follow from this, that when you create a program you are simply creating lots of data files that contain bytes of CPU instructions. This is strictly true, but it would be a tedious job to learn all the bytes that represent the CPU instructions! Instead, programmers use a set of tools that enable them to create these program files without having to learn lots of boring numbers.
The foremost tool in any programmer’s toolbox is the computer language. In the same way that a regular language helps humans to communicate between themselves, so a computer language helps humans to communicate with computers. A language is an intuitive or human-readable form of instructions that you can convert into CPU instructions to create a computer program.
Like languages in the real world, there are dozens of computer languages. Some are for specialized tasks and others are more general-purpose. What all languages have in common is that they enable programmers to create instructions for a computer without having to learn the computer’s machine code.
The code example in the box shows a section of an imaginary alarm clock program. Don’t worry about the details – the point is that you can make a good guess as to what the program is doing just by looking at the code. At least, you could make a better guess than if you were reading a long series of bytes!
Sub CheckAlarm() If DateTime.Now >= AlarmTime Then DisplayAlarm() End If End Sub
When you write a program, you create a series of instructions in a computer language, just like in the example. Then the computer itself translates these instructions into a different set of instructions that it can understand.
Challenge: See if you can imagine how the code example relates to the CPU instructions you saw earlier in this article. Try to relate the statement
If DateTime.Now >= AlarmTime
to the earlier example and see if you can connect the computer instructions with the CPU instructions. Hint: imagine that DateTime.Now and AlarmTime are names that represent memory locations. You might also need to use the jge CPU instruction, which performs a jump if the comparison is greater than or equal, instead of the jne instruction.
The Microsoft .NET Framework
For programmers, it is difficult and expensive to manage the differences between different operating systems, if they want their software to work on multiple platforms. To begin with, different operating systems may not provide the same tools. Also, if the program needs to run on a different type of CPU, the numbers that represent the CPU instructions are different between CPU types – and in fact the instructions themselves may not exist on one of the CPU types!
Furthermore, if a programmer wants to re-use some existing code in their program, the existing code usually has to be in the same language as they are using in their program, to avoid difficulties. It can be time-consuming to learn a new computer language when they are already familiar with a different language.
To solve problems like these, Microsoft has produced the .NET Framework. The .NET Framework provides a managed environment for programs to run in – a bit like an operating system within an operating system. The .NET Framework offers many advantages to a programmer:
An extensive and consistent set of tools that are available on any platform and in any computer language that the .NET framework supports.
The ability to create programs that can run on any type of CPU and on any operating system that the .NET Framework supports.
The ability to use existing code from any .NET-supported language.
When you write your first Windows programs, they will likely be simple programs that help you to learn the concepts and tools of programming. When you start to write ‘real-world’ applications, you will need to look beyond these basic tools to ensure that your program is suitable for its intended scenario.
One common problem in a real-world scenario is how to share information among multiple people. In this scenario, the information usually resides in one location and people access it from other locations.
You may have heard the terms “client” and “server” before. In this guide, we won’t go into too much detail since you probably won’t have to worry too much about this until you become a more advanced programmer, but we’d like to familiarize you with the concepts.
Client-server architecture is a common way to share data among multiple people. Data resides on a server, which provides a way that clients can access the data. A computer program runs on the server at all times, listening for connections from clients. On the client computer, another program connects to the server whenever it requires information, either on a schedule or as a result of actions by the computer user. For example, when you check your email, the email program makes a connection to the mail server. In this scenario, the email program is the client application that makes requests to the mail server.
When the client makes a request to the server:
It sends a message to the server over the network that contains details about the request – for example, your email program sends the user name and password as part of the request.
The server then performs some action, usually by processing a database or some files located on the server.
The server then sends the information back to the client as a response.
The client program can then use the information inside the response as appropriate – in your email program, it would display the email on the screen for you to read.
For client and server applications to communicate, they need to have a common understanding of the data that they exchange. The rules that define this common understanding define a protocol for communication. Protocol is the computing term that describes an agreed ‘language’ for communication. Each computer must use the protocol so that other computers can make sense of the data.
As well as defining a protocol, the client and server computers must run code that communicates with other computers by using the protocol. In the .NET Framework, much of this code already exists in the ‘toolbox’ so you don’t need to write it yourself. When you use the .NET Framework, you will discover tools like .NET Remoting that help you write programs that communicate with other programs.
You’ve covered a whole lot of ground by reading this guide. You’ve learned the fundamentals of how computers work, how they store information, and how they send and receive information from the outside world by converting it into a digital representation. You’ve seen how hardware and software interact to make your computer work, and how the Operating System helps the hardware and software work together. You’ve learned how programs work within an operating system to perform their function, and how they use the tools provided by the operating system to help them. Finally, you saw how two or more computer programs can work together, even if they are on different computers, by using a client-server design.
By learning how these different components work together, you now have a better understanding of how a computer program actually works. Fundamentally, all computer programs – email programs, computer games, word processing applications – are performing the same actions ‘under the hood’, even though they may be designed to achieve different results. The only thing that changes the results is the imagination of the programmer.
Obviously you have only scratched the surface of computing in this short article. There are many more directions that your learning can take. If you want to use the Visual Studio Express Editions family of applications to write programs, you could start your next steps by reading the introductory guides to either Visual Basic or Visual C# that are available here on MSDN.
Answers to Challenge
In the CPU instructions challenge, the answers to the questions are:
No value is written to memory location 8842, because this instruction is not executed. The CPU reads the value 221 from memory location 8000 and then subtracts 100, which leaves a value of 121 in the CPU. The jne instruction will therefore not jump to address 2020, and so the jmp instruction is executed, which means that the CPU will entirely skip the instruction at address 2020.
The CPU will write the value 121 to memory location 8878. When the CPU executes the jmp instruction, it jumps to location 2038. Therefore the write 8878 instruction is executed. At this point, the CPU still holds the value 121 as the result of the previous calculations, so it writes that value to address 8878.
If you got the answer right – congratulations! This type of programming is assembly language programming, and even most professional programmers don’t do this!