CIS-77 Home http://www.c-jump.com/CIS77/CIS77syllabus.htm
Introduction to x86 Assembly Language
1. Advantages of High-Level Languages
In this guide, we describe the basics of 32-bit x86 assembly language programming, covering a small but useful subset of the available instructions and assembler directives. How-ever, real x86 programming is a large and extremely complex universe, much of which is beyond the useful scope of this class. Get Assembly Language for Intel-Based Computers, one of the best books on the subject. Alternatively, you may try Randall Hyde's free, online Art of Assembly Language book as well. Download Masm32 assembler, which you will use to compile your assembly code into executables. And, if you like IDEs, get Winasm as well. It'll simplify code editing.
HOWL (HLA Object Windows Library) has arrived! HOWL makes Win32 assembly language programming easier than ever before. By providing an 'Application Framework' (much like Microsoft's MFC or Borland's VCL), HOWL takes care of all the grunt work required by low-level Win32 API programming and lets you concentrate on writing your applications. If you are new to Win32 API programming then I suggest you download a copy of the Win32 Reference Manual and this help file viewer before going on to the first tutorial on how to make a Basic Window. For further help you could also go to #winprog on Efnet IRC where you can ask questions and talk about anything to do with the Win32 API.
High-level language programs are portable.
(Although some programs could still have a few machine-dependent details, they can be used with little or no modifications on other types of machines.)
High-level instructions:
Program development is faster
Fewer lines of code
Program maintenance is easier
Compiler translates to the target machine language.
2. Why program in Assembly ?
There are some disadvantages...
Assembly language programs are not portable!
Learning the assembly is more difficult than learning Java!
Programming in the assembly language is a tedious and error-prone process.
High-level languages should be natural preference for common applications.
3. Here is why...
I just don't consider a utility program that's 4 megabytes big, and contains all sorts of files that the author didn't create, to be really great software. Do you?
Steve Gibson, Gibson Research Corporation.
Assembly language programs contain only the code that is necessary to perform the given task.
Assembly gives direct and complete control over system hardware:
Writing device drivers.
Operating system design.
Embedded systems programming, e.g. aviation industry.
Writing in-line assembly (mixed-mode) in high-level languages such as C/C++, or hybrid programming in assembly and C/C++.
4. Speed, Efficiency, Debugging, Optimization...
There are areas where speed is everything, for example, internet data encryption, aircraft navigational systems, medical hardware control...
There are also areas where space-efficiency is everything: spacecraft control software...
Understanding disassembly view of an executable program is also useful:
for investigating the cause of a serious bugs or crashes that require understanding of memory dumps and disassembled code.
for optimizing your code.
for practical and educational purposes.
5. Why MASM ?
The 'granddaddy' of all assemblers for the Intel platform, product of Microsoft.
Available since the beginning of the IBM-compatible PCs.
Works in MS-DOS and Windows environments.
It's free: Microsoft no longer sells MASM as a standalone product.
Bundled with the Microsoft Visual Studio product.
Numerous tutorials, books, and samples floating around, many are free or low-cost.
Steve Hutchessen's www.masm32.com
MASM32 development environment incorporates MASM assembler and Win32 API tools.
6. Introduction to 80x86 Assembly Language
Logic gates are used at the hardware level.
What is machine language?
How high-level language concepts, such as if-else statements, are realized at the machine level?
What about interactions with the operating system functions?
How is assembly language translated into machine language?
These fundamental questions apply to most computer architectures.
By using assembly, we gain understanding of how the particular model of computer works.
7. Materials on the Web
Such secrets have been revealed to me that all I have written now appears of little value.
St. Thomas Aquinas, December 6, 1273.
Useful links: Microsoft MASM Programmer's Guide Assembly-Language Development System v6.1, also at another location
MASM Reference Guide can be downloaded there, too.
More here: Assembly Technical Documentation in PDF and MS Word format
Intel and Microsoft MASM 6.1 Documentation
A web page with a variety of assembler source code
Intel 80x86 Conditional and Unconditional Branching Examples
Intel 80x86 Boolean and Arithmetic Instruction Examples
You can get Microsoft's Macro Assembler free: download Microsoft Windows Driver Development Kit (DDK), which contains both assembler and linker. Also, download Microsoft's Debugging Tools for Windows 32-bit Version.
Take a look at Sivarama P. Dandamudi textbook info, Introduction to Assembly Language Programming , From 8086 to Pentium. Homepage includes free downloadable Microsoft assembler, MASM , and student slides.
Last, but not least, Microsoft Macro Assembler Reference MSDN resource.
8. Useful books, in no particular order
Intel Architecture Software Developer's Manual
Volume 1 , Intel Basic Architecture: Order Number 243190 , PDF, 2.6 MB.
Volume 2 , Instruction Set Reference: Order Number 243191 , PDF, 6.6 MB.
Volume 3 , System Programing Guide: Order Number 243192 , PDF, 5.1 MB.
It is highly recommended that you download the above manuals and use them as a reference.
Introduction to 80x86 Assembly Language and Computer Architecture
by Richard C. Detmer,
Professor of Computer Science at Middle Tennessee State University, Tennessee.
DUMPBIN: command-line utility that examines binary files and disassembles programs.
11. Runtime Environment
Program runs on the processor.
Program uses operating system functions and services.
Program uses one of the memory models:
Real mode flat model, 65,536 bytes of addressable memory (ancient MS-DOS .COM files)
Real mode segmented model, 1 megabyte (prime-time MS-DOS)
Protected mode flat model, modern Windows and Linux:
Addressable Memory: 80486 and Pentium - 4 Gigabytes
As far as 32-bit Vista is concerned, the world ends at 4,096 megabytes.
A 32-bit program can address up to 4 gigabytes of memory.
12. Assembly and C Code Compared
Some simple high-level language instructions can be expressed by a single assembly instruction:
13. More Assembly and C Code
Most high-level language instructions need more than one assembly instruction:
14. Assembly vs. Machine Language
Assembly Language uses mnemonics, digital numbers, comments, etc.
Machine Language instructions are just a sequences of 1s and 0s.
Readability of assembly language instructions is much better than the machine language instructions:
15. Controlling Program Flow
Just as in high-level language, you want to control program flow.
The JMP instruction transfers control unconditionally to another instruction.
JMP corresponds to goto statements in high-level languages:
16. Conditional Jumps
Conditional jump is taken only if the condition is met.
Condition testing is separated from branching.
Flag register is used to convey the condition test result.
For example:
17. General-Purpose Registers
The EAX, EDX, ECX, EBX, EBP, EDI, and ESI registers are 32-bit general-purpose registers, used for temporary data storage and memory access.
The AX, DX, CX, BX, BP, DI, and SI registers are 16-bit equivalents of the above, they represent the low-order 16 bits of 32-bit registers.
The AH, DH, CH, and BH registers represent the high-order 8 bits of the corresponding registers.
Since the processor accesses registers more quickly than it accesses memory, you can make your programs run faster by keeping the most-frequently used data in registers.
Similarly, AL, DL, CL, and BL represent the low-order 8 bits of the registers.
18. Typical Uses of General-Purpose Registers
Register
Size
Typical Uses
EAX
32-bit
Accumulator for operands and results
EBX
32-bit
Base pointer to data in the data segment
ECX
32-bit
Counter for loop operations
EDX
32-bit
Data pointer and I/O pointer
EBP
32-bit
Frame Pointer - useful for stack frames
ESP
32-bit
Stack Pointer - hardcoded into PUSH and POP operations
ESI
32-bit
Source Index - required for some array operations
EDI
32-bit
Destination Index - required for some array operations
EIP
32-bit
Instruction Pointer
EFLAGS
32-bit
Result Flags - hardcoded into conditional operations