A quick overview of computers, machine code, assembly language, editing, compilers, compiling and debugging

Computers are actually pretty dumb. They only do what they’re told and you can only tell them in very simple language called machine code. A computer is made up of one or more CPUs though generally we call them cores and CPU often means the whole chip.

A core is the bit that executes these simple programs and it runs incredibly fast. My desktop PC can run at 3.5 billion ticks per second and an instruction takes several ticks. As instructions vary in length, it’s probably doing between 500 million and a billion instructions every second. When you move the mouse, click on an icon etc. that is all under the control of one or more cores. From the moment you switch on your computer it is executing instructions.

At the start it gets instructions from hardwired memory- called ROMs. These instructions when executed check if there are any hardware disks (hard disk, pen sticks or SSDs) attached and whether it can boot from them. Once it finds a device with an operating system on it, it loads the instructions and graphics from disk into RAM and starts running it. Operating systems like Windows, Linux, Mac OS and others are huge programs made up of many smaller programs. These start running and that’s what we call the boot up process.

Programming

Every progra  that runs on your computer including those in ROM as well as the ones on disk were written by one or more programmers. There were probably hundreds or maybe thousands of people involved in creating and testing the programs. All operating systems have hundreds or thousands of man-years of work in creating and maintaining them.

If you have a smartphone it is a computer plus a radio for you to make phone calls.

All of these programs were created in mostly the same manner.

Someone creates a computer program in a programming language. This is called source code and consists of instructions in a form of highly structured English. It could be in C, C++, Python, Delphi, Java, Kotlin, Swift, PHP or many others. This for example is what a very simple program in C that prints “Hello, World” looks like:

#include <stdio.h>
  int main() {
  printf("Hello, World!");
  return 0;
}

The source code is edited by a text editor to add new code to it or modify or even delete instructions. Once the programmer is satisfied that the program will work, he has it converted into machine code so the computer can run it. This is called compilation and a program called a compiler translates it line by line into machine code that can be executed by the computer it is targetted for.

There have been many different CPUs created in the past and they each have different instructions in their machine code. Unless they are part of the same family, instructions on one CPU will not work on another. Desktop and server PCS use x86-64 family chips manufacturer by Intel and AMD and smartphones mostly use the ARM family of chips. ARM is a licensing and design outfit and many manufacturers make ARM chips under licence. Apple for instance do this and have recently switched to making desktop and laptop computers powered by ARM chips (that they designed and had manufactured) instead of the X86-64 chips they previously used.

This shows part of a C program with source code, machine code and assembly language. The numbers like 81 EC C0 00 00 00 in the 3rd line are the machine code and sub esp,0C0h is the assembly language that is the equivalent.  The lines a = 2.0; etc. are the source code and the two lines after that are the machine code and assembly language of that instruction.

009B1910 55                   push        ebp  
009B1911 8B EC                mov         ebp,esp  
009B1913 81 EC C0 00 00 00    sub         esp,0C0h  
009B1919 53                   push        ebx  
009B191A 56                   push        esi  
009B191B 57                   push        edi  
009B191C 8D BD 40 FF FF FF    lea         edi,[ebp-0C0h]  
009B1922 B9 30 00 00 00       mov         ecx,30h  
009B1927 B8 CC CC CC CC       mov         eax,0CCCCCCCCh  
009B192C F3 AB                rep stos    dword ptr es:[edi]  
009B192E B9 03 C0 9B 00       mov         ecx,offset _6271C709_calc@c (09BC003h)  
009B1933 E8 DE F9 FF FF       call        @__CheckForDebuggerJustMyCode@4 (09B1316h)  
	a = 2.0;
009B1938 F2 0F 10 05 50 7B 9B 00 movsd       xmm0,mmword ptr [__real@4000000000000000 (09B7B50h)]  
009B1940 F2 0F 11 05 F0 A4 9B 00 movsd       mmword ptr [_a (09BA4F0h)],xmm0  
	b = 4.5;
009B1948 F2 0F 10 05 60 7B 9B 00 movsd       xmm0,mmword ptr [__real@4012000000000000 (09B7B60h)]  
009B1950 F2 0F 11 05 E8 A4 9B 00 movsd       mmword ptr [_b (09BA4E8h)],xmm0  
	c = 1.9;
009B1958 F2 0F 10 05 40 7B 9B 00 movsd       xmm0,mmword ptr [__real@3ffe666666666666 (09B7B40h)]  
009B1960 F2 0F 11 05 98 A1 9B 00 movsd       mmword ptr [_c (09BA198h)],xmm0  
	printf("ax^2+bx+c=%6.3f\n", calc(4.8));
009B1968 83 EC 08             sub         esp,8  
009B196B F2 0F 10 05 70 7B 9B 00 movsd       xmm0,mmword ptr [__real@4013333333333333 (09B7B70h)]  
009B1973 F2 0F 11 04 24       movsd       mmword ptr [esp],xmm0  
009B1978 E8 C6 F9 FF FF       call        _calc (09B1343h)  
009B197D DD 1C 24             fstp        qword ptr [esp]  
009B1980 68 18 7C 9B 00       push        offset string "ax^2+bx+c=%f8.3\n" (09B7C18h)  
009B1985 E8 43 F7 FF FF       call        _printf (09B10CDh)  
009B198A 83 C4 0C             add         esp,0Ch  

As well as high level languages like the ones I’ve described there is an English language equivalent of machine code instructions called Assembly language. Back when computers were still in their infancy this was the only type of programming language available. A program called an assembler turned assembly language into machine code just like a compiler compiles higher-level programs into machine code.  People still learn assembly language but it is limited to very small devices, or programming device drivers for peripherals, or even games. When manufacturers create devices they have to include drivers so operating systems can use them. A driver is just a specialized type of computer program and your computer probably has dozens.

Debugging

Many programs do not work properly and programmers have to fix their mistakes which are known as bugs. They (programmers) use a program called a debugger which lets you examine a program as it runs and look at its internals and help you try to fix the bugs. This is known as debugging.

 

(Visited 21 times, 1 visits today)