How to write in assembler in 2018

The article is devoted to assembler language taking into account current realities. The advantages and differences from Java are presented, a small comparison of compilers is made, a considerable amount of the best subject literature is meticulously collected.
1. Language. Advantages and differences from Java
Assembler (Assembly) - a programming language whose concepts reflect the architecture of a computer. Assembly language is a symbolic form of writing machine code, the use of which simplifies the writing of machine programs. Different assembly languages can be developed for the same computer. Unlike languages of a high level of abstraction, in which many problems of the implementation of algorithms are hidden from developers, the assembler language is closely connected with the microprocessor instruction system. For an ideal microprocessor, whose command system exactly matches the programming language, the assembler generates one machine code for each operator of the language. In practice, real machine microprocessors may require several machine instructions to implement a single language operator.
The assembly language provides access to registers, specifying addressing methods and a description of operations in terms of processor instructions. The assembly language may contain tools of a higher level of abstraction: built-in and defined macros that correspond to several machine instructions, automatic selection of a command depending on the types of operands, means of describing data structures. The main advantage of the assembly language is its "proximity" to the processor, which is the basis of the computer used by the programmer, and the main disadvantage is the too small division of typical operations, which is difficult for most users to perceive. However, assembly language reflects to a much greater extent the very functioning of the computer than all other languages.
Although drivers and operating systems are now written in C, C, for all its advantages, is a high-level abstraction language that hides various subtleties and nuances of iron from a programmer, and assembler is a low-level abstraction language that directly reflects all these subtleties and nuances.
For successful use of assembler, three things are necessary at once:
- knowledge of the syntax of the assembler translator that is used (for example, the syntax of MASM, FASM, and GAS is different), the assignment of assembler directives (statements processed by the translator during translation of the source code of a program);
- understanding of machine instructions executed by the processor while the program is running;
- the ability to work with services provided by the operating system - in this case, this means knowledge of the functions of the Win32 API. When working with high-level languages, very often the programmer does not directly access the system API; he may not even suspect its existence, since the language library hides from the programmer details that depend on a particular system. For example, on Linux, Windows, and any other system in a C / C ++ program, you can print a line to the console using the printf () function or the cout stream, that is, there is no difference for a programmer using these tools, what system the program is made for, although the implementation of these functions will be different in different systems, because the API of the systems is very different. But if a person writes in assembler, he no longer has ready-made functions like printf (), which are thought out for him,
As a result, it turns out that writing even a simple assembler program requires a very large amount of preliminary knowledge - the “entry threshold” is much higher here than for high-level languages.
Optimal can be considered a program that works correctly, as quickly as possible and takes up, possibly a small amount of memory. In addition, it is easy to read and understand; it is easy to change; its creation requires little time and low costs. Ideally, the assembler language should have a combination of characteristics that would make it possible to obtain programs that satisfy as many of these qualities as possible.
Programs or fragments of programs are written in assembly language in cases where they are critically important:
- the amount of memory used (downloaders, embedded software, programs for microcontrollers and processors with limited resources, viruses, software protections, etc.);
- speed (programs written in assembly language run much faster than analog programs written in programming languages of a high level of abstraction. In this case, speed depends on understanding how a particular processor model works, the actual pipeline on the processor, cache size, and the intricacies of work operating system. As a result, the program starts to work faster, but loses portability and versatility).
In addition, knowledge of the assembly language facilitates understanding of the architecture of the computer and the operation of its hardware, something that knowledge of high-level languages of abstraction (YaVU) cannot give . Currently, most programmers develop programs in rapid design environments.(Rapid Application Development) when all the necessary design and control elements are created using ready-made visual components. This greatly simplifies the programming process. However, one often encounters situations where the most powerful and efficient functioning of individual program modules is possible only if they are written in assembly language (assembler inserts). In particular, in any program related to the execution of repeatedly repeating cyclic procedures, whether it is cycles of mathematical calculations or the output of graphic images, it is advisable to group the most time-consuming operations into submodules programmed in assembly language. All packages of modern programming languages of a high level of abstraction allow this,
Programming languages of a high level of abstraction were developed with the aim of approximating as much as possible the way of writing programs to the familiar forms of recording for computer users, in particular mathematical expressions, and also not to take into account the specific technical features of individual computers. The assembly language is developed taking into account the specifics of the processor, therefore, to correctly write a program in assembly language, in general, you need to know the processor architecture of the computer used. However, bearing in mind the predominant distribution of PC-compatible personal computers and ready-made software packages for them, you can not think about it, since such developers take on specialized and universal software companies.
2. About compilers
Which assembler is better?
For the x86-x64 processor, there are more than a dozen different assembler compilers. They differ in various sets of functions and syntax. Some compilers are more suitable for beginners, some for experienced programmers. Some compilers are fairly well documented; others have no documentation at all. A number of programming examples have been developed for some compilers. For some assemblers, textbooks and books have been written that detail syntax, while others have nothing. Which assembler is better?
Given the many dialects of assemblers for x86-x64 and the limited amount of time to study them, we restrict ourselves to a brief overview of the following compilers: MASM, TASM, NASM, FASM, GoASM, Gas, RosAsm, HLA.
What operating system would you like to use?
This is a question that you must answer first. The most versatile assembler will not do you any good if it is not designed to work under the operating system that you plan to use.
| Windows | Dos | Linux | BSD | QNX | MacOS powered by Intel / AMD processor | |
|---|---|---|---|---|---|---|
| Fasm | x | x | x | x | ||
| Gas | x | x | x | x | x | x |
| Goasm | x | |||||
| Hla | x | x | ||||
| MASM | x | x | ||||
| NASM | x | x | x | x | x | x |
| Rosm | x | |||||
| Tasm | x | x |
16 bit support
If the assembler supports DOS, then it supports 16-bit commands. All assemblers provide the ability to write code that uses 16-bit operands. 16-bit support means the ability to create code that runs on a 16-bit segmented memory model (compared to the 32-bit flat memory model used by most modern operating systems).
64 bit support
With the exception of TASM, to which Borland cooled in the mid-zero, and which does not even fully support 32-bit programs, all other dialects support the development of 64-bit applications.
Program portability
Obviously, you are not going to write code in x86-x64 assembler that would run on some other processor. However, even on a single processor, you may run into portability issues. For example, if you intend to compile and use your assembler programs under different operating systems. NASM and FASM can be used on the operating systems that they support.
Do you intend to write an application in assembler and then port this application from one OS to another with "recompiling" the source code? This feature is supported by the HLA dialect. Do you expect to be able to create Windows and Linux applications in assembly language with minimal effort? Although, if you work with one operating system and absolutely do not plan to work in any other OS, then this problem does not concern you.
Support for high level language constructs
Some assemblers provide advanced syntax that provides linguistic, high-level control structures (such as IF, WHILE, FOR, and so on). Such constructs can facilitate assembly training and help write more readable code. Some assemblers have built-in “high-level constructions” with limited capabilities. Others provide high-level macro-level constructs.
No assembler forces you to use any high-level control structures or data types if you prefer to work at the level of machine instruction encoding. High-level constructs are an extension of the basic machine language that you can use if you find them convenient.
Documentation Quality
Ease of use of the assembler is directly related to the quality of its documentation. Considering the amount of work that is spent on creating an assembler dialect, the authors of compilers practically do not bother creating documentation for this dialect. The authors, expanding their language, forget to document these extensions.
The following table describes the quality of the assembler reference guide that comes with the product:
| Documentation | Comments | |
|---|---|---|
| Fasm | Good | The author gives most of his free time to the development of the innovative FASMG. Nevertheless, the author provides support for FASM from time to time updates the manuals, and describes new features in its own forum. The documentation can be considered quite good. Documentation webpage . |
| Gas | Bad | poorly documented and documentation, rather, has a "general view". gas is an assembler that was designed to easily write code for different processors. The documentation that exists basically describes pseudo codes and assembler directives. In the "intel_syntax" operating mode, the documentation is practically absent. Books that use AT&T syntax: “Programming from scratch” by Jonathon Bartlett and “Professional Assembly Language” by Richard Blum, Konstantin Boldyshev asmutils - Linux Assembly . |
| Goasm | Weak | Most of the syntax is described in the manual, and an experienced user will find what he is looking for. Many manuals are also available on the website ( http://www.godevtool.com/ ). A few GoAsm tutorials:
|
| Hla | Scaling | HLA has a 500-page reference guide. The site contains dozens of articles and documentation on the HLA. |
| MASM | Good | Microsoft has written a significant amount of documentation for MASM, there are a large number of directories written for this dialect. |
| NASM | Good | NASM authors write more software for this dialect, leaving the manual for later. NASM has been around for quite some time, so several authors have written a manual for NASM by Jeff Duntemann, “Assembly Language Step-by-Step: Programming with Linux”, Jonathan Leto, “ Writing A Useful Program With NASM, ” in Russian there is a book by Stolyarov ( Website of A.V. Stolyarov ). |
| Rosm | Weak | not very interesting “online tutorials”. |
| Tasm | Good | Borland once produced excellent reference guides; for TASM, reference guides were written by enthusiastic authors not affiliated with Borland. But Borland no longer supports TASM, so most of the documentation for TASM is not printed and it is becoming harder and harder to find. |
Textbooks and training materials
The documentation in assembler itself is, of course, very important. An even greater interest for beginners and others who are learning assembly language (or the additional features of this assembler) is the availability of documentation outside the reference manual for the language. Most people want a tutorial explaining how to program in assembler not only provide the syntax of machine instructions and expect the reader to be explained how to combine these instructions to solve real problems.
MASM is a leader among a vast body of books describing how to program in this dialect. There are dozens of books that use MASM as their assembler to teach assembler.
Most MASM / TASM assembly books continue to teach MS-DOS programming. Although textbooks are gradually appearing that teach programming in Windows and Linux.
| Comments | |
|---|---|
| Fasm | A few tutorials that describe FASM programming:
|
| Gas | AT&T Syntax Tutorial Assembler Tutorial on Linux for C Programmers |
| Hla | 32-bit version of “The Art of Assembly Language Programming” (available in both electronic and print form), Windows or Linux programming |
| MASM | A large number of books on teaching programming for DOS. Not a lot of books on programming for Win32 / 64 Pirogov, Yurov, Zubkov, Flenov |
| NASM | many books on programming in DOS, Linux, Windows. Jeff Dunteman's book, Assembly Language Step-by-Step: Programming with Linux, uses NASM for Linux and DOS. Paul Carter's tutorial uses NASM (DOS, Linux). |
| Tasm | Like MASM, a large number of DOS-based books have been written for TASM. But, since Borland no longer supports this product, they stopped writing books about using TASM. Tom Swan wrote a TASM tutorial that included several chapters on Windows programming. |
3. Literature and web resources
Beginners
- Abel P. Assembler Language for IBM PC and Programming. - M .: Higher school, 1992 .-- 447 p.
- Bradley D. Programming in assembly language for a personal computer from IBM. - M.: Radio and Communications, 1988. - 448 p.
- Galiseev G.V. Assembler IBM PC. Self-instruction.: - M.: Williams Publishing House, 2004. - 304 pp., Ill.
- Tao L. Programming of the microprocessor 8088. - M.: Mir, 1988. - 357 p.
- Zhukov A.V., Avdyukhin A.A. Assembler. - St. Petersburg: BHV-Petersburg, 2003 .-- 448 p.: Ill.
- Zubkov S.V., Assembler for DOS, Windows and UNIX. - M.: DMK Press, 2000 .-- 608 p.: Ill. (Series "For Programmers").
- Irwin K. Assembler Language for Intel Processors, 4th Edition: transl. from English - M.: Williams Publishing House, 2005. - 912 p.: Ill. - Paral. tit. English (see also the latest 7th edition in the original)
- Norton P., Souhe D. Assembly language for IBM PC.– M .: Computer, 1992.– 352 p.
- Pilshchikov V.N. Programming in assembly language IBM PC.– M.: DIALOGUE – MEPhI, 1994–2014 288 p.
- Sklyarov I.S. We study assembler in 7 days www.sklyaroff.ru
Advanced
- Kaspersky K. Fundamentals of hacking. The art of disassembling. - M .: SOLON-Press, 2004.448 s. - (Series "Digger")
- Kaspersky K. Technique for debugging programs without source code. - St. Petersburg: BHV-Petersburg, 2005 .-- 832 p.: Ill.
- Kaspersky K. Computer viruses inside and out. - St. Petersburg: Peter, 2006 .-- 527 p.: Ill.
- Kaspersky K. Notes of the computer virus researcher. - St. Petersburg: Peter, 2006 .-- 316 p.: Ill.
- Knut D. The Art of Programming, Volume 3. Sorting and Search, 2nd ed .: Per. from English - M.: Williams Publishing House, 2003. - 832 p.: Ill. - Paral. tit. English
- Kolisnichenko D.N. Rootkits under Windows. The theory and practice of programming “invisible caps”, allowing you to hide data, processes, network connections from the system. - St. Petersburg: Science and Technology, 2006. - 320 p.: Ill.
- Lyamin L.V. Macroassembler MASM.– M.: Radio and communications, 1994.– 320 pp., Ill.
- Magda J. Assembler for Intel Pentium processors. - St. Petersburg: Peter, 2006 .-- 410 p.: Ill.
- Mayko G.V. Assembler for IBM PC.– M.: Business Inform, Sirin, 1997.– 212 p.
- Warren G. Algorithmic Tricks for Programmers, 2nd ed.: Per. from English - M.: Williams Publishing House, 2004. - 512 p.: Ill. - Paral. tit. English
- Sklyarov I.S. The art of protecting and hacking information. - St. Petersburg: BHV-Petersburg, 2004 .-- 288 p.: Ill.
- Weatherly C. Studies for programmers: Per. from English - M .: Mir, 1982. - 288 p., Ill.
- The Frolov Brothers Electronic Library www.frolov-lib.ru
- Chekatov A.A. Using Turbo Assembler in the development of programs. - Kiev: Dialectics, 1995. - 288 p.
- Yurov V. Assembler: special reference book. - St. Petersburg: Peter, 2001. - 496 pp., Ill.
- Yurov V. Assembler. Workshop 2nd ed. - St. Petersburg: Peter, 2006 .-- 399 p .: ill.
- Yurov V. Assembler. Textbook for high schools. 2nd ed. - St. Petersburg: Peter, 2007 .-- 637 p.: Ill.
- Pirogov V. Assembler Training Course. 2001 Nolige
- Pirogov V. ASSEMBLER training course 2003 Nolidzh-BHV
- Pirogov V. Assembler for windows
1st edition - M.: Publishing house of S. Molgachev, 2002
2nd edition - St. Petersburg.:. BHV-Petersburg, 2003 - 684 pp., Ill.
3rd edition - St. Petersburg.:. BHV-Petersburg, 2005 - 864 p.: Ill.
4th edition - St. Petersburg.:. BHV-Petersburg, 2012 - 896 pp .: ill. - Pirogov V. Assembler on examples. - SPb.:. BHV-Petersburg, 2012 - 416 pp., Ill.
- Pirogov V. ASSEMBLER and disassembly. - SPb.:. BHV-Petersburg, 2006 .-- 464 p.: Ill.
- Pirogov V. work on the book '64-bit assembly language programming (Windows, Unix)'. This book discusses fasm programming on 64-bit Windows and Unix
- Yurov V., Khoroshenko S. Assembler: training course. - St. Petersburg: Peter, 1999. - 672 p.
- Yu-Zheng Liu, Gibson G. Microprocessors of the 8086/8088 family. Architecture, programming and design of microcomputer systems. - M.: Radio and communications, 1987.– 512 p.
- Agner Fog: Software optimization resources (assembly / c ++) 1996 - 2017. Web page
- Intel ® 64 and IA-32 Architectures Optimization Reference Manual
- Intel ® 64 and IA-32 Architectures Software Developer's Manual Volume 1: Basic Architecture
- Intel ® 64 and IA-32 Architectures Software Developer's Manual Volume 2A: Instruction Set Reference, AM
- Intel ® 64 and IA-32 Architectures Software Developer's Manual Volume 2B: Instruction Set Reference, NZ
- Intel ® 64 and IA-32 Architectures Software Developer's Manual Volume 3A: System Programming Guide, Part 1
- Intel ® 64 and IA-32 Architectures Software Developer's Manual Volume 3B: System Programming Guide, Part 2
- Leiterman JC 32/64-BIT 80x86 Assembly Language Architecture. © 2005, Wordware Publishing, Inc (568 pages) 2320 Los Rios Boulevard Plano, Texas 75074
- Turbo Assembler® Version 3.2 User's Guide Borland International. Inc 1800 Green Hills Road PO BOX 660001, Scotts Valley, CA 95067-0001
- Articles from wasm.in
- Articles from sasm.narod.ru
- MASM32 website and forum
- FASM website and forum
- NASM website
4. Practice
So, you already know what assembler is and what it is eaten with. You stocked up a couple / three books and web manuals, you probably decided on the compiler ... Unfortunately, programming lessons are beyond the scope of this article, but for those whose choice fell on MASM / FASM you can use the following layouts:
- MASM64 simple window masm64SimpleWindow.asm
- FASM64 simple window fasm64SimpleWindow.asm
The question remains, in which editor to write the code? Someone writes in notepad and compiles through the command line, more savvy prepare scripts - simplifying the process, while others use special development environments. Yes, there is an IDE in the field of low-level programming; their review is also beyond the scope of the article. However, taking this opportunity, I dare to offer you to try your own environment for programming in assembler - ASM Visual . In addition to the basic capabilities, it differs from analogues by the presence of refactoring tools, code metrics, as a whole, more intuitive interfaces and its own type of project.
We wish you, friends, significant achievements and new knowledge in 2018!
Sincerely,
Mikhail Smolentsev MiklIrk (Irkutsk State Transport University),
Alexey Gritsenko expressrus (Don State Technical University).
Ps1 : Dear, Habrahabr! Add assembler highlighting (Intel syntax) to your editor, this will come in handy for future articles!
Ps2: Soon we will launch a portal with tests (question - answer options) for knowledge of assembler and computer architecture. Tests will be structured by level of difficulty. If you teach assembler or have sufficient knowledge in this area - write to us at [email protected], you can take part by submitting your test to the system.