Partial Tutorial: The basics of 80x86 assembly with MASM 5.0
Page 1 of 2 12 LastLast
Results 1 to 10 of 19

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

  1. #1
    Senior Member
    Join Date
    Sep 2001
    Posts
    138

    Post 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:
    -a 100
    XXXX:0100 mov ax,100
    XXXX:0103 mov bx,50
    XXXX:0106 add ax,bx
    XXXX:0108 ret
    XXXX:0109
    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.

    .model small

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

    .code
    main proc
    mov ax,SEG msg ;move the segment address of msg into ax
    mov dx,OFFSET msg ;move the offset of msg into ax
    mov ds, ax ;mov the SEG of msg into DS
    mov ah,09h ;calling function 09h
    int 21h ;of interrupt 21h (this displays the string stored in msg)

    mov ax,4c00h ;put 4c00h into ax
    int 21h ;interrupt 21h, return to DOS
    main endp

    end main
    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

    ;#################################################
    printmsg proc
    mov ax,SEG msg ;move the segment address of msg into ax
    mov dx,OFFSET msg ;move the offset of msg into ax
    mov ds, ax ;mov the SEG of msg into DS
    mov ah,09h ;calling function 09h
    int 21h ;of interrupt 21h
    ret
    printmsg endp
    ;#################################################
    endall proc
    mov ax,4c00h ;put 4c00h into ax
    int 21h ;interrupt 21h, return to DOS
    ret
    endall endp
    ;#################################################
    end main ;end program, start prog with main proc.
    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
    http://www25.brinkster.com/cheeseball

    -- Do not dwell in the past, do not dream of the future, concentrate the mind on the present moment--

  2. #2
    Old-Fogey:Addicts founder Terr's Avatar
    Join Date
    Aug 2001
    Location
    Seattle, WA
    Posts
    2,007
    Yaaay, Assembly examples that are not subconsciously trying to mimic the exact emotional mood of an Aliens movie! Kudos.
    [HvC]Terr: L33T Technical Proficiency

  3. #3
    Senior Member
    Join Date
    Sep 2001
    Posts
    534
    hey cheese i have done that as a practise...but i am getting this...can u pls tell me where exactly it is adding the ax and bx registers....as i cannot see the value ax = 0150 anywhere....pls help...


    C:\>debug
    -a 100
    117F:0100 mov ax,100
    117F:0103 mov bx,50
    117F:0106 add ax,bx
    117F:0108 ret
    117F:0109
    -t

    AX=0100 BX=0000 CX=0000 DX=0000 SP=FFEE BP=0000 SI=0000 DI=0000
    DS=117F ES=117F SS=117F CS=117F IP=0103 NV UP EI PL NZ NA PO NC
    117F:0103 BB5000 MOV BX,0050
    -




    pls help me....intruder...
    A laptop, internet connection and beer.

  4. #4
    Senior Member
    Join Date
    Sep 2001
    Posts
    534
    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...

    thank u ...for the information..

    intruder...
    A laptop, internet connection and beer.

  5. #5
    Senior Member
    Join Date
    Sep 2001
    Posts
    138
    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.

    Hope this answers the questions
    Cheeseball
    http://www25.brinkster.com/cheeseball

    -- Do not dwell in the past, do not dream of the future, concentrate the mind on the present moment--

  6. #6
    Senior Member
    Join Date
    Dec 2001
    Posts
    319
    Good tutorial. For more in-depth information, I suggest the Art Of Assembly :
    http://webster.cs.ucr.edu/Page_asm/ArtOfAsm.html

  7. #7
    Senior Member
    Join Date
    Sep 2001
    Posts
    534
    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...

    if u have any e-books .then pls mail me on pok_pok_007@yahoo.com

    i will be very very thankful to u...

    thanks once again...

    intruder...
    A laptop, internet connection and beer.

  8. #8
    Senior Member
    Join Date
    Nov 2001
    Posts
    157
    This is a great start! Please post again with more.
    =-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-==-==-=
    Noah built the ark BEFORE it rained.


    http://ld.net/?rn
    =-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-==-==-=

  9. #9
    Senior Member
    Join Date
    Sep 2001
    Posts
    138
    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
    http://www25.brinkster.com/cheeseball

    -- Do not dwell in the past, do not dream of the future, concentrate the mind on the present moment--

  10. #10
    Senior Member
    Join Date
    Sep 2001
    Posts
    534
    that is a great idea cheeseball ...but pls explain step by step....
    i am waiting for your tutorial....

    thank u ....

    intruder....
    A laptop, internet connection and beer.

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  

 Security News

     Patches

       Security Trends

         How-To

           Buying Guides