For people who are not in computing, and even for programmers who have always worked with high-level languages, this article is very enlightening. It helps us become aware of and appreciate the effort made by the first programmers and designers to get us to where we are today. Read the original article at this link. Here is the translation:
This is a brief introduction to Assembly language. Assembly is the most basic programming language available for any processor. With Assembly language, a programmer works only with operations performed directly on the physical CPU. Assembly lacks high-level conveniences such as variables and functions, and it is not portable across different processor families. However, Assembly is the most powerful programming language available on a computer, and it gives programmers the insight they need to write efficient code in high-level languages. Learning Assembly is a way to appreciate the time and effort of all serious programmers.
The Basics
Before we can explore the process of writing computer programs, we have to go back to the basics and understand exactly what a computer is and how it works. Every computer, no matter how simple or complex, has exactly two things at its core: a CPU and some memory. Together, these two things make it possible for your computer to run programs.
At the most basic level, a computer program is nothing more than a collection of numbers stored in memory. Different numbers tell the CPU to do different things. The CPU reads the numbers one at a time, decodes them, and performs what they say. For example, if the CPU reads the number 64 as part of a program, it will add 1 to the number stored in a special location called AX. If the CPU reads the number 146, it will swap the number stored in AX with the number stored in another location called BX. By combining several simple operations like these in a program, a programmer can make the computer do many amazing things.
As an example, here are the numbers for a simple computer program: 184, 0, 184, 142, 216, 198, 6, 158, 15, 36, 205, 32. If you type these numbers into your computer’s memory and run them in MS-DOS, you will see a dollar sign ($) placed in the bottom-right corner of the screen, because that is what these numbers tell the computer to do.
Assembly Language
Although the program’s numbers make sense to a computer, they are not clear to a human being. Who could guess that the code would put a dollar sign on the screen? Clearly, manipulating numbers directly is a terrible way to write a program.
It doesn’t have to be this way. Some time ago, someone came up with the idea that computer programs could be written with words instead of numbers. A special program called an assembler would take the programmer’s words and convert them into numbers the computer could understand. This new method consists of writing a program in Assembly language. It saved programmers thousands of hours of effort: they no longer had to look up hard-to-remember numbers at the back of programming books, and could simply use ordinary words instead.
The program above, written in Assembly language, looks like this:
MOV AX, 47104
MOV DS, AX
MOV [3998], 36
32 INT
When an assembler reads this example program, it converts each line of code into a CPU-level instruction. This program uses two kinds of instructions, MOV and INT. On Intel processors, the MOV instruction moves data, while the INT instruction transfers control of the processor to the device drivers or operating system.
The program still isn’t completely clear, but it’s much easier to understand than before. The first instruction, MOV AX, 47104, tells the computer to copy the number 47104 into location AX. The next instruction, MOV DS, AX, tells the computer to copy the number in AX into location DS. The next instruction, MOV [3998], 36, tells the computer to put the number 36 in memory location 3998. Finally, INT 32 ends the program by returning control to the operating system.
Before we continue, I’d like to explain how this program works. Inside the CPU there are several locations, called registers, that can store a number. Some registers, like AX, are general-purpose and do nothing special. Other registers, like DS, control how the processor works. DS acts as a segment register and is used to choose which area of memory the CPU can write to. In our program, we put the number 47104 in DS, which tells the CPU to access the memory on the video card. The next thing our program does is put the number 36 at location 3998 in the video memory. Since 36 is the code for the dollar sign “$” and 3998 is the memory location at the bottom-right of the screen, a dollar sign appears on the screen a few microseconds later. Finally, our program tells the CPU to perform what is called an interrupt. An interrupt is used to stop one program and run another in its place. In our case, we use interrupt 32, which ends our program and returns to MS-DOS, or whatever other program was used to start our program.
Running the Program
Let’s run this program now. First, make sure to write these instructions down somewhere else as a reference while you type the code. Then click your Start menu and run the program called “MS-DOS Prompt.” A black screen with white text should appear. We are now in MS-DOS, the way computers were used 20 years ago. MS-DOS existed before the mouse was used, so you have to type commands on the keyboard to make the computer do things.
First, type the word “debug” and press Enter. The cursor will move down a line, and you should see the Debug prompt, which is a simple dash. We are now in a program called Debug. Debug is a powerful utility that lets you directly access your computer’s registers and memory for various purposes. In our case, we want to put our program in memory and run it, so we’ll use a Debug command to assemble it. Go ahead and type “a100” now. The cursor will move down another line, and you will see something like “1073:0100”. This is the memory location where we will put the Assembly instructions. The first number is the segment, and the second number is the memory location within the segment. Your Debug program will probably choose a different segment from mine, so don’t worry if it is different. Another thing to note is that Debug only understands hexadecimal numbers, which are a kind of computer shorthand. Hexadecimal numbers can contain letters and digits, so if you see something like “63AF”, don’t worry.
Let’s enter our program now. Type each of the instructions below into Debug exactly as they appear, and press Enter after each one. When you have entered the last instruction, press Enter twice to tell Debug that we’ve finished entering instructions.
mov ax, B800
mov ds, ax
mov byte [0F9E], 24
int 20
As you can see, I converted all the numbers to hexadecimal and made a few changes so Debug could understand what was going on. If you make a mistake while entering the program above, press Enter twice, type “a100” again, and start entering the instructions from the beginning.
Once you’ve entered the program, you can go ahead and run it. Just type “g” and press Enter when you’re ready to start the program. You should see a dollar sign in the bottom-right corner of the screen, and the words “Program terminated normally.” These words are displayed by Debug to tell you that the program ended normally. Congratulations! You’ve just entered and run your Assembly program!
Let’s return to Windows now. Go ahead and type “q” to exit Debug. Now type “exit” to leave MS-DOS. You should now be back in Windows.