Monday, January 14, 2013

One last step into assembly.

Lets play with assembly language programming one last time.  This time let’s use dos assembly language programming. A program is a set of instructions much like a recipe. With this recipe, we will have to use commands like you might use stir in a recipe.To make this recipe, we will use which is sort of a simple interactive assembler/disassembler. Our goal with the program is to print out the letter A. Now that seems a bit simple, but you have to start somewhere. So fire up debug from the command prompt. We are going to use dosbox (dos simulator) to make it easy to capture images. Thall shalt document. But first let us look at the letter A. If we went to an ascii  (american standard code for information interchange) table we can see the letter A is 65. So far so good.. But the computer does not recognize that nunber as the letter A. You remember base 10 arithmetic. where you have ten fingers to work with to do math. Well the computer in this case has 16 fingers or base 16,.so we have to translate that base 10 A to a base 16 A. Base 16 is also known as hexadecimal. Fortunately there is a chart to do that so we do not have to calculate it. So base 10 A is 65, therefor base 16 A is 41. So we will have to use 41 to let the computer know we want to print out the letter A. Seemed like a lot of work, but it is not really.
Screenshot from 2013-01-14 03:21:09
The chart confirms the 41 is the letter A. Your computer's brain has temporary storage locations known as regisiers. You could equate them as internal mail boxes.  We have two registers we will use dl and ah for the values we need to use.  We also want to use some commands not unlike stir for a recipe. In this case we will use a mov command to put data in the registers or mail boxes we just talked about. Lastly we will use the int command. That tells the computer to stop what you are doing and go do some special commands. What happens is Dos has some built in instructions that we can use to save a lot of programming.  We will let dos do all the hard all we have to do is to let Dos know what we want done. Lets put those instructions together.
mov dl, 41  ; So we are moving the letter A into a register, so the computer knows what to print out.
mov ah, 02 ; We are letting the computer know we want the letter A to be printed to the screen. 02 stands for the screen
int 21 : there is that command that says hey dos, print out what is in register dl using the output location in register ah.
int 20 : Says stop this program and go back to where I was before I started the program.
That is all there is to the program. if you have started debug you should see a dash prompt. Type a100 to let the computer know you want to enter some instructions. So enter the instructions.and then hit enter on a blank line. you should get the dash again. Let’s see if our program works!. press g and then return. You should the the letter A on a separate line, and you should get the dash prompt again. Neat!! You entered and assembled your first program. Tada!!!
Screenshot from 2013-01-14 03:07:44
Now lets see what memory looks like where you entered your program.  Type d100 and return. What is there seems unreadable except  the 41, 02, 21 and 20 you typed in. b2 stands for mov into ah and b4 stands for move into dl. cd stand for int.  If that seems hard to remember you can always translate back what you keyed in. (aka unasemble. So type in u100, 107 and you should see your program again.
Screenshot from 2013-01-14 03:11:59
You can display memory again with the d100  use the g command to run the program. if everything was typed correctly, you should see the letter see nothing has changed. If you turned off or reset the machine the program would be gone.
Screenshot from 2013-01-14 03:14:44
Now do not be afraid, but we are going to wipe out the e100 and return. You are show the old numbers but you want to replace each one of them with 0. Once you are done, do the d100 and look at the memory. so your program is gone. No problem. We can do the e100 again and type in the numbers again b2 41 b4 02 cd 21 cd 20. when you get back to a prompt Type a g and return. you should see your program run again. Cool. What that means is you can enter the assembly code or the hexadecimal numbers for your computer to display the A.
Screenshot from 2013-01-14 03:15:17.
If you type the u100, 107 you can see the original code you typed in. Cool. If you wanted to have sentences print out you will use different code as doing thoie three lines over and over can be a challenge. Doing assembly on most machines is very much alike what you have just done, but the commands may be a dit different.
Some of the early computer cartoons dealt with programmers who were really thick glasses because of all the numbers they had to look at. Things have changed. Enough of assembly, so you do not have to get the thick programmer glasses.

No comments:

Post a Comment