Introduction: First Assembler Program
Assembler can be harder to write than most other languages, but that doesn't mean it's not worth learning. There are some things that are easier in assembler such as writing ones and zeros to a certain USB controller. Or performing operations faster than other systems and languages.
Here is a 1.44 MB GUI Operating System written entirely in assembler code. This system can fit on a floppy disk.
Check it @ http://www.menuetos.net/
If you run it, you will find the system to be lightening fast(especially if you don't actually run it from a floppy disk :P)
So let's experiment with 80x86 Assembler(this program will work on anything). Though our code will work on any system, the program to build the code will not work on any system.
We are going to use my girlfriend's windows machine because so many people use Windows. She has Windows XP.
If you have a newer version of windows, you will have to download the appropriate software for your computer from this link:
I would get the SDK version.
You will be using ntsd or cdb instead of "debug" if you are not running good old XP.
Teachers! Did you use this instructable in your classroom?
Add a Teacher Note to share how you incorporated it into your lesson.
Step 1: Open Up a Command Window
Hold the windows key on your keyboard. Then tap the letter "R." A "Run" dialogue should pop up in the lower left corner of your screen(pic 1).
Type "cmd" and hit enter. "CMD" stands for ComManD window.
You should now get a nice black window(pic 2).
Step 2: The Debugger
The fastest way that we can write 80x86 assembler code on a windows machine is to use the debugger.
If you're in windows XP, type "debug" inside of that black window and hit enter.
You should get a "tack" or a "minus sign" on the line below(pic 1). Here is where we will type our debug commands.
Let's type the debug command to begin writing assembler code.
type "a" and hit enter
Now we see a few hexadecimal numbers which represent the address in memory that we're writing code. We can write assembler code right here!
Step 3: Writing a Little Bit of Code
When we use an assembler language, each command has its own number. We're going to be using special commands called interrupt requests. The interrupt request will do different things based on what parameters we send it. All of our parameters are stored in something called the registry. The registry is a VERY high-performance place to store information.
Let's go ahead and type "mov ah, 2"
This "mov"es a "2" into the upper(or "h"igher) half of the 16 bits of the "a" register
That "2" is going to tell the processor what command we want to run when we call the 21st interrupt request. The "2" command is the command to print a character to the screen.
So how do we know what character the processor is going to write to the screen?
That's contained in the "dl" register. Can you guess what the "dl" register is before going to the next page?
Step 4: Finishing the Code
If you said that "dl" was the lower part of the "d" register, you were correct! You can see the registers by typing "r" after we finish with this program. Don't do it yet, it wont work yet. I typed it for you in a new window. You can see the screenshot below. You'll notice that there are 4 main registers(AX-DX).
Now let's set what character we want to display:
I want to display the smiley face(character 1)
For a list of characters and their corresponding numbers, you can look at the ASCII chart here:
type "mov dl, 1" and hit enter to set the character we intend to display
Now we have to tell the computer to actually run with our two parameters.
type "int 21" and hit enter.
Now let's run our code. It's going to type a smiley and then normally end.
Simply hit enter again and you'll be back to a minus sign. See pic 2
Type "g" next to the minus sign and hit enter.
You should see something like Picture 3.
Step 5: Adding Some Fun
You can type "a" and follow it by a number to insert assembler code at a specific address.
"a 106" allows us to continue writing code where we left off.
"jmp 100" says to go to the beginning of the code(address "100") and continue running from there.
What do you think this new code will output to the screen when we run it?
Step 6: Woah!
The new code runs a bunch of smiley faces across our screens!
Hold the "Ctrl" button and press "C" to stop it!
You can run it again by typing "g" and then enter.
You will notice that the computer is able to display the characters a lot faster if you go into full screen mode by holding down "Alt" and tapping "Enter"
Hold down "Alt" and tap "Enter" to get out of full screen mode.
That's it for this lesson. You've got your first glimpse into assembler. Check out other instructables that use Debug for a Hello World program.