Simple programs for teaching integers and overflow

Leave a comment

Click here to download

While teaching unsigned and signed binary integers, I wrote these programs so that the students can run them and see overflow in action. There are four programs- addu, adds, subu and subs- for adding and subtracting 8-bit unsigned and signed integers. The numbers to be added are given as arguments when running the program. The result is printed on the screen and also saved in a file, which can be opened with a hex editor to see the result in binary format.

While the students where running the programs they obviously had questions about the the dot-slash. That was a good opportunity to tell them that all the commands they run, ls or cd or chmod or anything for that matter, are all executable files residing somewhere in the file system. I made them type which ls and find that ls is actually /bin/ls.

I pointed out the interesting fact that ls, which was somewhere else in the filesystem, could be run by just typing ls and not necessarily /bin/ls, while the addu program which was right here in this folder you needed to specify that it is in the current folder (they knew that dot stands for the current folder). Then I showed them how by adding the current directory to the PATH variable, you could run it by just typing addu, like any other command in the system.

Advertisements

Teaching Computer Science Bottom-Up

11 Comments

When I started teaching the new batch of Computer Applications students this year (class 9), I had it in my mind to do it bottom up, to try and give them a good sense of how computers actually work, how the 0’s and 1’s get so many things done. I wanted them to not only be able to write java programs in the prescribed IDE, but also have a good command over the machine, visualize what happens when they run a java program and easily learn other programming languages, work with microcontrollers and so on.

I started with teaching them how to use the GNU/Linux command line interface, to carry out the various tasks they normally do by clicking, dragging and dropping. Typing commands and making things happen turned out to be an exciting thing for them to learn. I had soon taught them to navigate the file system, copy and move files and folders, find out and change file permissions, open files using appropriate applications etc. It helped that I have just 4 students, all of whom are quite excited learning about computers and working on them.

Making them open files of different types with different applications gave me a context to introduce binary numbers and the different ways in which binary digit sequences can be interpreted. Usually it’s just stated that computers can work with only 0’s and 1’s, and that the 0’s and 1’s refer to different voltage levels etc. Then the focus turns to learning the binary number system, and the procedures to convert decimal numbers to binary and back, and some binary arithmetic.

But I saw the whole world of binary numbers in a new way, for the first time. What struck me was that computers interpret binary digit sequences in a variety of ways, not just as numbers. We want the computer to interpret binary numbers in several ways, as different kinds of data- text, numbers, images, audio- and instructions. I felt it would be exciting to try and pass on this understanding to the students.

I gave them a simple introduction to the binary number system, and then went on to making them edit text files and bitmap images using a hex editor. This gave them an idea of how the same set of bytes can be interpreted differently. As a project they even drew a 10×10 pixel image and typed in the hex code referring to the bitmap file format and saw the picture in an image viewer!

I then spent a few classes discussing the different kinds of numerical interpretations of binary numbers- unsigned integers, signed integers, fixed point numbers and floating point numbers. They also learnt about the problem of overflow because of the limited range of numbers. To illustrate overflow I wrote a few programs in C and made them run the executable from the terminal. That also gave me a context to talk to them about source code, compilation and the executable. (I was able to even explain to them the difference between proprietary and free software.) This was very useful, because the next interpretation of binary numbers I wanted to show them was that of instructions. I could easily tell them that the executable file contained bytes which the processor interprets as instructions.

And to actually show them this, I used the Hack CPU emulator developed by the authors of the book/course “The Elements of Computing Systems”. It uses a-simple-to-understand-yet-fully-functional hypothetical processor. I explained the architecture and instruction set to them, and even made them write an assembly program to add two numbers, to compare two numbers and to multiply two numbers by repeated addition. This made them learn important concepts like sequential execution of instructions, memory access, usage of registers, rudimentary arithmetic and logical operations, conditional and unconditional jumps- together they cover most of the concepts in elementary programming. And for each assembly program, I kept showing them the equivalent C program, and they were exclaiming how easy and intuitive the C programs are!

I’ll write in more detail about each of these later. But it’s been exciting for me to discover how children of 14 can learn computers at a level of detail that one would normally think is too advanced. And I strongly believe that this what’s-under-the-hood understanding will stand them in good stead whatever they learn in computers.