- •Table of Contents
- •Foreword
- •Do Not Pass GO
- •Counting in Martian
- •Octal: How the Grinch Stole Eight and Nine
- •Hexadecimal: Solving the Digit Shortage
- •From Hex to Decimal and from Decimal to Hex
- •Arithmetic in Hex
- •Binary
- •Hexadecimal as Shorthand for Binary
- •Switches, Transistors, and Memory
- •The Shop Foreman and the Assembly Line
- •The Box That Follows a Plan
- •DOS and DOS files
- •Compilers and Assemblers
- •The Assembly Language Development Process
- •DEBUG and How to Use It
- •Chapter 5: NASM-IDE: A Place to Stand Give me a lever long enough, and a place to stand, and I will move the Earth.
- •NASM-IDE's Place to Stand
- •Using NASM-IDE's Tools
- •NASM-IDE's Editor in Detail
- •Other NASM-IDE Features
- •The Nature of Segments
- •16-Bit and 32-Bit Registers
- •The Three Major Assembly Programming Models
- •Reading and Changing Registers with DEBUG
- •Assembling and Executing Machine Instructions with DEBUG
- •Machine Instructions and Their Operands
- •Reading and Using an Assembly Language Reference
- •Rally Round the Flags, Boys!
- •Using Type Specifiers
- •The Bones of an Assembly Language Program
- •Assembling and Running EAT.ASM
- •One Program, Three Segments
- •Last In, First Out via the Stack
- •Using DOS Services through INT
- •Boxes within Boxes
- •Using BIOS Services
- •Building External Libraries of Procedures
- •Creating and Using Macros
- •Bits Is Bits (and Bytes Is Bits)
- •Shifting Bits
- •Flags, Tests, and Branches
- •Assembly Odds 'n Ends
- •The Notion of an Assembly Language String
- •REP STOSW, the Software Machine Gun
- •The Semiautomatic Weapon: STOSW without REP
- •Storing Data to Discontinuous Strings
- •Chapter 12: The Programmer's View of Linux Tools and Skills to Help You Write Assembly Code under a True 32-Bit OS
- •Prerequisites-Yukkh!
- •NASM for Linux
- •What's GNU?
- •The make Utility and Dependencies
- •Using the GNU Debugger
- •Your Work Strategy
- •Genuflecting to the C Culture
- •A Framework to Build On
- •The Perks of Protected Mode
- •Characters Out
- •Characters In
- •Be a Time Lord
- •Generating Random Numbers
- •Accessing Command-Line Arguments
- •Simple File I/O
- •Conclusion: Not the End, But Only the Beginning
- •Where to Now?
- •Stepping off Square One
- •Notes on the Instruction Set Reference
- •AAA Adjust AL after BCD Addition
- •ADC Arithmetic Addition with Carry
- •ADD Arithmetic Addition
- •AND Logical AND
- •BT Bit Test (386+)
- •CALL Call Procedure
- •CLC Clear Carry Flag (CF)
- •CLD Clear Direction Flag (DF)
- •CMP Arithmetic Comparison
- •DEC Decrement Operand
- •IMUL Signed Integer Multiplication
- •INC Increment Operand
- •INT Software Interrupt
- •IRET Return from Interrupt
- •J? Jump on Condition
- •JMP Unconditional Jump
- •LEA Load Effective Address
- •MOV Move (Copy) Right Operand into Left Operand
- •NOP No Operation
- •NOT Logical NOT (One's Complement)
- •OR Logical OR
- •POP Pop Top of Stack into Operand
- •POPA Pop All 16-Bit Registers (286+)
- •POPF Pop Top of Stack into Flags
- •POPFD Pop Top of Stack into EFlags (386+)
- •PUSH Push Operand onto Top of Stack
- •PUSHA Push All 16-Bit GP Registers (286+)
- •PUSHAD Push All 32-Bit GP Registers (386+)
- •PUSHF Push 16-Bit Flags onto Stack
- •PUSHFD Push 32-Bit EFlags onto Stack (386+)
- •RET Return from Procedure
- •ROL Rotate Left
- •ROR Rotate Right
- •SBB Arithmetic Subtraction with Borrow
- •SHL Shift Left
- •SHR Shift Right
- •STC Set Carry Flag (CF)
- •STD Set Direction Flag (DF)
- •STOS Store String
- •SUB Arithmetic Subtraction
- •XCHG Exchange Operands
- •XOR Exclusive Or
- •Appendix C: Web URLs for Assembly Programmers
- •Appendix D: Segment Register Assumptions
- •Appendix E: What's on the CD-ROM?
- •Index
- •List of Figures
- •List of Tables
Conclusion: Not the End, But Only the Beginning
Overview
You never really learn assembly language.
You can improve your skills over time, by reading good books on the subject, by reading good code that others have written, and most of all, by writing lots and lots of code yourself. But at no point will you be able to stand up and say, I know it.
You shouldn't feel bad about this. In fact, I take some encouragement from occasionally hearing that Michael Abrash, author of Zen of Assembly Language, Zen of Code Optimization, and his giant compendium Michael Abrash's Graphics Programming Black Book, has learned something new about assembly language. Michael has been writing high-performance assembly code for almost 20 years and has evolved into one of the two or three best assembly language programmers in the Western hemisphere.
If Michael is still learning, is there hope for the rest of us?
Wrong question. Silly question. If Michael is still learning, it means that all of us are students and will always be students. It means that the journey is the goal, and as long as we continue to probe and hack and fiddle and try things we never tried before, that over time we will advance the state of the art and create programs that would have made the pioneers in our field catch their breath in 1977.
For the point is not to conquer the subject, but to live with it, and grow with your knowledge of it. The journey is the goal, and with this book I've tried hard to help those people who have been frozen with fear at the thought of starting the journey, staring at the complexity of it all and wondering where the first brick in that Yellow Brick Road might be.
It's here, with nothing more than the conviction that you can do it.
I got out of school in recession year 1974 with a B.A. in English, summa cum laude, and not much in reliable prospects outside of driving a cab. I finessed my way into a job with Xerox Corporation, repairing copy machines. Books were fun, but paperwork makes money-so I picked up a tool bag and had a fine old time for several years, before finessing my way into a computer programming position.
But I'll never forget that first awful moment when I looked over the shoulder of an accomplished technician at a model 660 copier with its panels off, to see what looked like a bottomless pit of little cams and gears and drums and sprocket chains turning and flipping and knocking switch actuators back and forth. Mesmerized by the complexity, I forgot to notice that a sheet of paper had been fed through the machine and turned into a copy of the original document. I was terrified of never learning what all the little cams did and missed the comforting simplicity of the Big Picture-that a copy machine makes copies.
That's Square One-discover the Big Picture. Ignore the cams and gears for a bit. You can do it. Find out what's important in holding the Big Picture together (ask someone if it's not obvious) and study that before getting down to the cams and gears. Locate the processes that happen. Divide the Big Picture into subpictures. See how things flow. Only then should you focus on something as small and as lost in the mess as an individual cam or switch.
That's how you conquer complexity, and that's how I've presented assembly language in this book. Some might say I've shorted the instruction set, but covering the instruction set was never the real goal here.
The real goal was to conquer your fear of the complexity of the subject, with some metaphors and some pictures and some funny stories to bleed the tension away.
Did it work? You tell me. I'd really like to know.
Where to Now?
If you've followed me so far, you've probably lost your fear of assembly language, picked up some skills and a good part of the instruction set, and are ready to move on. What's next? Ideally, you need an intermediate book on assembly language. The bad news is, assembly language has had a bad couple of years in the book industry, and most of the useful books I've found are now out of print. Tom Swan's masterful Mastering Turbo Assembler (Howard W. Sams & Co., 1995) is the most significant exception, and is still in print as of early 2000.
Worse, every single one of them focuses on DOS. There has never been an x86 assembly language book focusing on Linux, as best I know. This isn't to say there will never be one, but I don't see one on the immediate horizon.
On the other hand, the Internet has made it much easier to find out-of-print books. There are two sites that you simply must bookmark, and visit regularly, if you want to find books that are no longer available from the publisher or through Amazon.com. Both of these sites are brokers of used books, and what they do is direct you to an independent used bookstore somewhere that contributed listings of books to their master Web database. Here they are:
Alibris has better shipping but a more limited database. You deal directly with Alibris itself rather than the separate used book dealers. It's at www.alibris.com.
Bibliofind is less direct, but its listings are larger and I've found a number of things listed here that Alibris does not list. You send your order to Bibliofind via the Web, and Bibliofind forwards it to the bookstore that listed the book you want. You then conclude the order by sending payment to the bookstore rather than Bibliofind. It's at www.bibliofind.com.
I've used both firms very successfully and I endorse them both without hesitation.
Mastering Turbo Assembler
Tom Swan
HowardW. Sams & Co., 1995
ISBN 0-672-30526-7
Tom's intermediate-level assembly volume is a natural next step if you're working with the Borland tools. I have never seen a better intermediate-level text. It has gone through a couple of editions and is reasonably abundant on the used market. The downside, of course, is that it was published some years back and focuses on DOS real mode segmented model. The TASM assembly code is easily converted to NASM, and the principles Tom teaches apply well to Linux assembly as well, even though Tom does not mention Linux or NASM.
Mastering Turbo Debugger
Tom Swan
Howard W. Sams & Co, 1990
ISBN 0-672-48454-4
For my money, this is the only good book on debugging ever published, and for what I consider an advanced topic, it's remarkably approachable. Again, it focuses on DOS and the Borland tools, but Tom's higher-level strategies for finding and nuking bugs in your code are absolutely essential reading, no matter what assembler you're using, now or at any time in the future. It's been out of print for some time, but you can find it regularly on the used book market.
PC Magazine Programmer's Technical Reference: The Processor and Coprocessor
Robert L. Hummel
Ziff-Davis Press, 1992
ISBN 1-562-76016-5
This is not a tutorial but a reference on Intel's x86 processors through the 486, and it's by far the best one ever written or likely to be written for some time. It has the best discussion of that mysterious protected mode that I've ever seen, and its description of the individual assembly instructions is wonderfully crafted. I'm tempted to have my own copy taken apart and rebound as hardcover-if I don't, it's going to fall to pieces any day now! Alas, out of print but you should grab it if you find it.
Michael Abrash's Graphics Programming Black Book
Michael Abrash
Coriolis Group Books, 1997
ISBN 1-576-10174-6
This is a huge book (1,300+ pages) covering code optimization, largely for graphics applications (where it matters the most) but explained in a way that can be applied to almost anything. Some of it involves C programming, but much of it is pure, expert-level assembly-and on the CD-ROM is the original text of Michael's 1989 classic Zen of Assembly Language, which was barely off press when its publisher went under. The book was thus lost in the crush of a big business reorganization, and it never recovered. You'll need to get some practice and some context before all of this book will be completely comprehensible, but it's beautifully written and whether you can read it now, grab it if you see it so it'll be there on your shelf when you're ready for it. (Alas, it went out of print in early 2000, just as I am completing the book you're now reading.)
Stepping off Square One
Okay-with a couple of new books in hand and good night's sleep behind you, strike out on your own a little. Set yourself a goal, and try to achieve it: something tough, say, an assembly language utility that locates all files anywhere on a hard disk drive with a given ambiguous file name. That's ambitious for a newcomer and will take some research and study and (perhaps) a few false starts. But you can do it, and once you do it you'll be a real journeyman assembly language programmer.
Becoming a master takes work, and time. Michael Abrash's massive Graphics Programming Black Book (recently out of print but still in some stores) is a compilation of the secret knowledge of a programming master. It's not easy reading, but it will give you a good idea where your mind has to be to consider yourself an expert assembly language programmer.
Keep programming. Michael can show you things that would have taken you years to discover on your own, but they won't stick in your mind unless you use them. Set yourself a real challenge, something that has to be both correct and fast: Rotate graphics objects in 3-D, transfer data through a serial port at 19,200 bits per second, things like that.
You can do it.
Coming to believe the truth in that statement is the essence of stepping away from Square One-and the rest of the road, like all roads, is taken one step at a time.
Appendix A: Partial 8086/8088 Instruction Set
Reference
Overview
Instruction |
Reference Page |
Text Page |
|
|
|
|||||
AAA |
|
535 |
|
310 |
|
|
|
|
|
|
ADC |
|
536 |
|
201 |
|
|
|
|
|
|
ADD |
|
538 |
|
11 |
|
|
|
|
|
|
AND |
|
540 |
|
67 |
|
|
|
|
|
|
BT |
|
542 |
|
466 |
|
|
386 |
+ |
||
CALL |
|
543 |
|
454 |
|
|
|
|
|
|
CLC |
|
544 |
|
|
|
|
|
|
|
|
CLD |
|
545 |
|
374 |
|
|
|
|
|
|
CMP |
|
546 |
|
183 |
|
|
|
|
|
|
DEC |
|
548 |
|
21 |
|
|
|
|
|
|
IMUL |
|
549 |
|
309 |
|
|
|
|
|
|
INC |
|
551 |
|
11 |
|
|
|
|
|
|
INT |
|
552 |
|
86 |
|
|
|
|
|
|
IRET |
|
553 |
|
263 |
|
|
|
|
|
|
J? |
|
554 |
|
Only in |
Appendix A |
|
||||
JCXZ |
|
556 |
|
389 |
|
|
|
|
|
|
JECXZ |
|
557 |
|
422 |
|
|
|
|
|
|
JMP |
|
558 |
|
190 |
|
|
|
|
|
|
LEA |
|
559 |
|
Only in |
Appendix A |
|
||||
LOOP |
|
560 |
|
276 |
|
|
|
|
|
|
LOOPNZ/LOOPNE |
|
561 |
|
389 |
|
|
|
|
|
|
LOOPZ/LOOPE |
|
562 |
|
422 |
|
|
|
|
|
|
MOV |
|
563 |
|
75 |
|
|
|
|
|
|
NEG |
|
564 |
|
212 |
|
|
|
|
|
|
NOP |
|
565 |
|
Only in |
Appendix A |
|
||||
NOT |
|
566 |
|
213 |
|
|
|
|
|
|
OR |
|
567 |
|
67 |
|
|
|
|
|
|
POP |
|
568 |
|
214 |
|
|
|
|
|
|
POPA |
|
569 |
|
253 |
|
|
286 |
+ |
||
POPAD |
|
|
|
253 |
|
|
386 |
+ |
||
POPF |
|
570 |
|
253 |
|
|
|
|
|
|
POPFD |
|
571 |
|
465 |
|
|
386 |
+ |
||
PUSH |
|
572 |
|
115 |
|
|
|
|
|
|
PUSHA |
|
573 |
|
251 |
|
|
286 |
+ |
||
PUSHAD |
|
574 |
|
251 |
|
|
386 |
+ |
||
PUSHF |
|
575 |
|
85 |
|
|
|
|
|
|
PUSHFD |
|
576 |
|
465 |
|
|
386 |
+ |
RET |
577 |
|
263 |
|
|
|
ROL |
578 |
|
316 |
|
|
|
ROR |
580 |
|
316 |
|
|
|
SBB |
582 |
|
11 |
|
|
|
SHL |
583 |
|
316 |
|
|
|
SHR |
585 |
|
316 |
|
|
|
STC |
587 |
|
Only in |
Appendix A |
||
STD |
588 |
|
376 |
|
|
|
STOS |
589 |
|
596 |
|
|
|
SUB |
590 |
|
140 |
|
|
|
XCHG |
592 |
|
198 |
|
|
|
XOR |
593 |
|
86 |
|
|
|