Results 1 to 6 of 6

Thread: Tutorial: Entering the world of x86 assembly.

Threaded View

  1. #1

    Thread Starter
    Addicted Member
    Join Date
    Aug 2005
    Location
    York
    Posts
    197

    Post Tutorial: Entering the world of x86 assembly.

    Most the tutorial has been written already, but I have not included it all, as I will be tidying bits of it up as I post them in the forum.
    There will be basically two sections two this:

    1.) Introducing assembly language on its own
    2.) Integrating it into your VB6 apps.

    Well here is the start

    Introduction To x86 32-bit Assembly Language

    A FAQ:

    Why?

    A common question asked by many people who have not programmed in assembly language before. Common belief is that assembly programming is pretty much dead, and not useful. This is entirely wrong, as assembly programming has many benefits for a programmer even if he doesn’t use it!

    Following the fact that compilers have grown exceptionally good in compiling, the developers behind the compiler themselves have to know assembly language well in order to implement new optimisation techniques. However, just leaving learning the assembly language to the compiler enthusiasts is folly, as the instruction sets of assembly language grows with each generation of new computers. This enables faster machine codes that are available with newer computers.

    Now, compiler developers do not have all the time in the world to implement all the new instruction sets and provide new optimisations that work 100% efficiently and even some optimisations just cannot be programmed into a compiler. This creates the void between hand-written assembly and compiler output. Hand-written assembly by a moderately experienced assembly programmer can run from 10%-50% faster than compiled code.

    Operating systems also need assembly language as some functionality can only be provided by assembly language. Speed is also of the essence in time critical portions of the system.

    Faster? Aren’t computers fast enough already?

    Many non-assembly programmers have brought this up.

    If you just check the market and see the huge range of different x86 computer processors up for sale, you would notice the processors may be as cheap as £50 or as expensive as £400+. This shows that there is always a need for speed because there is great demand for CPUs that cost £400, to ensure that applications run as fast as possible.

    There is always a ‘need for speed’ in programs, and if it is possible to obtain it without additional hardware costs, it should be done. Many scientific applications, large databases have time constraints and even games has a major need to speed optimisation as they need to provide smooth game play in order to immerse an user into a game. Take for example, a batch processed database file takes 6 hours (ie. One that contains millions and millions of records) will take just over 4 hours if there is a 30% improvement due to assembly optimisations. Just a 30% optimisation in speed may determine the playability of a game on a slow computer.

    A Beginning

    For a ‘dead’ language, there does seem to be a startling number of assemblers. More recently most the assemblers are available free and some are even open-source. The current free x86 assemblers available are:
    MASM, FASM, RosASM, NASM, GoASM, TASM, LZASM, YASM

    MASM (Microsoft Macro Assembler) is definitely the most popular as it has a good reputation as a fully featured assembler and has been available free for about 8 years.
    However, FASM and YASM that have been developed in recent years possibly surpass the MASM feature set now. I, personally, use FASM that is well known as the fastest assembler and has extremely good macro capability. Another interesting fact is that FASM itself is written in assembly language, which can be compiled using itself.

    FASM is available here: http://flatassembler.net/

    The examples given use the FASM variant of the INTEL-syntax, which is slightly different from the MASM variant.

    This tutorial will be focused upon 32-bit assembly programming, and the sample programs will be geared for windows. 16-bit assembly programming is relatively disused today as they have extremely tight memory constraints and require the use of difficult to manage segment registers.

    Numbers

    Basic Type Signed Range Unsigned Range
    8-bit integer (Byte / Char) -128 to 127 0 to 255
    16-bit integer (Short / Word) -32768 to 32767 0 to 65535
    32-bit integer (Long / DWord) -2147483648 to 2147483647 0 to 4294967295

    Hexadecimals
    Decimal numbers are numbers we use in everyday use – i.e. base 10 numbers.
    Hexadecimal numbers are slightly different, they are base 16.
    Decimal Hexadecimal
    0 0
    1 1
    2 2
    3 3
    4 4
    5 5
    6 6
    7 7
    8 8
    9 9
    10 A
    11 B
    12 C
    13 D
    14 E
    15 F
    16 10
    17 11
    18 12
    19 13
    20 14

    The joy of hexadecimal numbers is every two digits can represent any combination of a byte, for example 0 in hex means 0 and FF in hex means 255.
    You can manually change hexadecimals to decimal numbers, but to save tedious math – its easy enough to load up Windows Calc, which will do the conversion for you.

    Ambiguity

    There are various terms and words used in this presentation/tutorial, which have ambiguous definitions, so I will attempt to explain their usage.
    Those highlighted in bold are the definitions used here.

    Word – unsigned 2 byte integer or the Number of bits treated as a single unit by a CPU.
    Dword – unsigned 4 byte integer or double a word


    Statements

    Assembly language makes use of mnemonics that represent a machine code instruction. An example of a mnemonic is ‘add’. Operands are like a parameter that a code instruction can take.

    For example:
    add destination, source

    The instruction takes a number in ‘source’ and adds it to the number in ‘destination’ and the result is in ‘destination’. ‘source’ and ‘destination’ are known as operands. Operands can be registers or memory locations. However, in the x86 instruction set, there can be no more than one memory operand in an instruction.

    Unlike high-level languages, which translate each statement into a number of different machine code instructions, there is a 1 to 1 relationship between an assembly statement and a single machine code instruction, providing full control to the programmer. A mnemonic like add is translated into an opcode (the machine code representation of a ‘operator’ of a instruction). Note: However, a single mnemonic can be translated into different opcodes, either because it translates to another opcode that does the same operation on different operands or that there is an opcode that does the same operation but has a shorter code length overall.
    Last edited by Raedwulf; Sep 22nd, 2006 at 02:55 AM.

Posting Permissions

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



Click Here to Expand Forum to Full Width