If this is your first visit, be sure to
check out the FAQ by clicking the
link above. You may have to register
before you can post: click the register link above to proceed. To start viewing messages,
select the forum that you want to visit from the selection below.

Partial Tutorial: The basics of 80x86 assembly with MASM 5.0

The difficult part of learning this language is not actually learning it, but getting past the common misconception that it is difficult. This misconception was introduced by high level programmers, in some cases it is actually easier to program in assembly language than in a high level language because it allows you to address certain things that languages such as C and BASIC fail to have libraries for. This tutorial will not cover binary, hex, and decimal conversions and mathematics. If you need a tutorial on how to do conversions please email or PM me. Also if you feel that I need to continue this with a tutorial on looping, conditional statements, and the PUSH and POP instructions let me know as I am currently at work and a little pressed with my time.

First of all lets understand that assembly language lets you address hardware with very few limits. The first piece of hardware we are going to talk about is your CPU/Processor. The CPU has built in registers which you can address, the first 4 we are going to talk about can store any value and are 16 bits each. These four registers are AX,BX,CX, and DX.

The AX Register is sometimes called the accumulator register. This register is basically a general purpose register used mainly for math operations. This register can be separated into two 8 bit segments AH and AL or (A-high and A-low). The maximum value possible for the AX register is 0xFFFF, or 65535. The maximum value the AH and AL are 255 each or 0xFF.

The BX register often called the Base register is much like itís brother the AX register, it is used mainly for storing memory address though, but can still be used to preform any other operation. Just like the AX register the BX register is 16 bits and will hold a value of up to 0xFFFF, and can also be split into two smaller "registers" BH and BL.

The CX register is also called the Culmunitive register, it is normally used for counting and for looping conditions, again this is just the recommended use of this register and it can be used for anything you choose to do with it. This is yet again another 16 bit register and just like its brothers and sisters it can hold a value of up to 0xFFFF.

The DX register is also called the Data register, it is used for most data operations, and holding what interrupt functions we want to use on most interrupts. It is yet again another 16 bit register and can hold a value of up to 0xFFFF.

Now that we have something to work with lets start with a few code examples, (these will start with debug then move into MASM format). First of all, let us open the debug program, open a command prompt and type Ďdebugí. This will display a - prompt. At this prompt do the following:

Now if you use the Ďtí command to trace through the program you will see what it does, first it moves the value of 100h into ax (pay attention to the register states during the trace). Then it moves the value of 50h into bx. Next it adds ax(100h) and bx(50h) and stores the value into the ax register. Resulting in ax=0150. "but why the ret command?" I can hear you all asking well try the program without it and see what you get? you are either going to be getting a blue screen, or illegal operation popup box.

Now that we have created a simple yet very informative program, letís learn a few opcodes (operations, much like functions in higher languages). First and most importantly is the Ďmoví instruction, used above, this moves the second specified value into the first (ie, mov ah,09 would move 09 into the ah register). You can also mov two registers, like: mov ax,bx. Moving bx into ah is not a logical way to handle things as moving 16 bits into 8 bits is not going to happen, it overfills the coffee cup which does not make the maid happy, so donít do it.

The next opcode will be add, since we have already used it and all. ADD does EXACTLY what it implies, it adds the first and second operands (thingies after the instruction) and stores the resulting value into the first operand. This instruction should be no problem as the example basically explains itís use.

The INT opcode calls a DOS/OS or BIOS interrupt. These are EXREMELY helpful, that is unless you are the type of person that ENJOYS talking with your video card manually (which really isnít THAT hard but I will SOOO not go into). The most popular interrupt you will use in 80x86 assembly is going to be INT 21h. This is a BIOS call. The first function of interrupt 21h you will learn to use is function 09h, which allows the printing of a string to the standard output (most of the time the console). For a full list of the interrupts and functions contained in them go to ralf brown's interrupt list page at http://www-2.cs.cmu.edu/afs/cs.cmu.e...ralf-home.html so I will not go into *any* detail on them here. So here is our next program this time in MASM format.

Now to compile this using masm 5.0 type 'masm filename' after masm makes the obj file, run 'link filename' to make an exe. Running that EXE will display "Hello everyone!" to the screen without the quotes. Just understand for small programs like you are likely to create while learning assembly the model small will work, for other options and what they mean goto the MASM documentation or to www.microsoft.com. The .data section is actually a segment, it is where the program stores values for your "variables". The SEG and OFFSET instructions are just for EASE of use with MASM, if you remember back to debug the address the code started was formed as XXXX:XXXX this is basically a SEG:OFFSET, all addresses you deal with are going to be made up like this. Since we know that msg is stored somewhere in memory (had we used debug we would have had to tell it what memory address to put it at) we know it has a segment and an offset. In order to print text to the screen using function 9 of int 21 we need to have the string (ended with a $), it's SEGMENT address stored into DS (or the Datasegment register) and its OFFSET stored into DX. If you messed this up and threw the program to a random address in memory it would continue displaying ASCII characters until it found a $ then stop normally.

You may have also noticed I used a LOT of ;'s up there, well everything to the right of the semi-colon is a comment, ignored by MASM. This is equivilent to // in C or # in perl or ' in basic or REM in the OLD basic. I think I have now written WAY too much too explain what a comment is so I shall stop...

We also see other things in this program we did not see with debug. such as the "main proc" and "main endp" instructions. Like perl, C, and Pascal MASM allows us to use procedures and make calls to procedures. All programs need to have a main procedure (it actually doesn't HAVE to be named main, although it is a good programming practice). As you have already guessed the first line in the code segment started the main procedure (ie. main proc) the format is "nameofproc proc". The end of the procedure is notated by "nameofproc endp". The end of the program MUST have the end statement followed by the procedure the program needs to start with, in this case "end main".

Our next program will use the call instruction to call the procedures in the program, hey a demonstration sounds good to me!

.model small

.data
db msg "Hello everyone!", "$" ;message we are storing (msg)

.code
main proc
call printmsg ;call procedure to print MSG to the screen
call endall ;to return control back to DOS
main endp

This program just demostrated the use of the call instruction to call different procedures, note the ret statement at the end of each procedure, this forces the program to go back up one level (ie. after it runs int 21h in the printmsg proc it returns to the call endall instruction). Without the ret instruction at the end of the procedure the program would crash...quite hard actually.

Well..I am quite tired and do not have time to really fully write a 100% complete tutorial on 80x86 assembly, if I get enough of a responce from this I will write more stuff. If anyone has any questions about this stuff email me or PM me and I will respond as soon as possible.

Sorry for the poor formatting on some of this as the code just did not copy paste from notepad to the form that well. Sorry.
Cheeseball

sorry to post again ...but cheese can u tell me from where i will get masm download and how exactly i can use it.

what u have explained is very useful ..but for me a bit confusing..as i am very new to assembly language ....but i am very very interested in learning assembly. can u pls teach a little more. pls give more focus on how exactly we can compile using masm ...and how to compile. pls tell me step by step...

First of all, just do a search on www.google.com for masm and you should be able to find a link to either the program and linker, or to the microsoft site where you can download the SDK (which has masm and the linker in it). You will need (with masm 5.0) masm.exe and link.exe (this is the DOS version of masm I use). To assemble a program using masm type 'masm programname.asm' into the commandline, and it will construction you an object file. to switch the object file over to an exe type 'link objectfilename.obj' and you will have an exe.

just a note on the trace command with debug, it "traces" or "steps thru the program, when you only did one 't' it just executed the first line of your program, (which is why AX = 100). if you pressed t 2 more times it would have displayed down to the addition step. Also it displays what instruction it is on above the register states.

hey thanks a lot cheeseball..i got the answer....and now i am trying to learn...it also...well the masm i will try to search from google.com.
and now i am waiting for one more tutorial from u..on assembly...so when are u going to post one more....this was great one...good job...

and there is one request...if u have any e-book on assembly for newbies or for dummies..then if u can forward..me...i will be very grateful to u...

The art of assembly is an online book about assembly, unforunately Randall Hyde has moved towards HLA (High Level Assembly) which isn't IMHO the best thing in the word, but I believe he still has his old 16bit dos days book online...Someone gave the link above...I think I will write another tutorial in a few mins and post it sometime....
Cheeseball