⬆️ ⬇️

How to write in assembler in 2018





The article is devoted to the language of the assembler, taking into account the current realities. Advantages and differences from JAVA are presented, a small comparison of compilers is made, a significant amount of the best subject literature is meticulously collected.



1. Language. Advantages and differences from HLL



Assembly (Assembly) - a programming language, the concepts of which reflect the architecture of an electronic computer. Assembly language - the symbolic form of writing machine code, the use of which simplifies the writing of machine programs. For the same computer, different assembly languages ​​can be developed. In contrast to high-level abstraction languages, in which many problems of the implementation of algorithms are hidden from developers, assembly language is closely related to the microprocessor command system. For an ideal microprocessor, in which the command system corresponds exactly to the programming language, the assembler produces one machine code for each language operator. In practice, real microprocessors may require several machine instructions to implement one language operator.



An assembly language provides access to registers, an indication of addressing methods, and a description of operations in terms of processor commands. An assembly language can contain tools of a higher level of abstraction: built-in and defined macros, corresponding to several machine instructions, automatic selection of a command depending on the types of operands, means of describing data structures. The main advantage of assembly language is “proximity” to the processor, which is the basis of a computer programmer, and the main disadvantage is too small a division of typical operations, which is perceived by most users with difficulty. However, the assembler language to a much greater extent reflects the very functioning of the computer than all other languages.

')

Although drivers and operating systems are now written in C, C, for all its merits, is a high-level language of abstraction, hiding various subtleties and nuances of hardware from a programmer, and an assembler is a low-level language of abstraction that directly reflects all these subtleties and nuances.



To successfully use assembler, three things are needed at once:





As a result, it turns out that for writing even a simple program in assembler a very large amount of preliminary knowledge is required - the “threshold of entry” here is much higher than for high-level languages.



The best 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 minor expenses. Ideally, an assembly language should have a set of characteristics that would allow to obtain programs that satisfy as many of the listed qualities as possible.



In assembly language, programs or their fragments are written in cases when they are critically important:





In addition, knowledge of assembly language makes it easier to understand the architecture of a computer and the operation of its hardware, something that knowledge of high-level abstraction languages (REL) cannot provide. 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, it is often necessary to face such situations when the most powerful and effective functioning of separate 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 repeated cyclical procedures, be it cycles of mathematical calculations or graphics output, it is advisable to group the most time-consuming operations into submodules programmed in assembler language. All packages of modern high-level abstraction programming languages ​​allow this, and the result is always a significant increase in the speed of programs.



Programming languages ​​of a high level of abstraction were developed with the aim of approximating as closely as possible the method of recording programs to those familiar to users of computers of various forms of recording, in particular mathematical expressions, and also not to take into account the specific technical features of individual computers in programs. The assembly language is developed taking into account the specifics of the processor, so for a competent writing of a program in assembly language it is required, in general, to know the architecture of the processor of the computer used. However, bearing in mind the preferential distribution of PC-compatible personal computers and ready-made software packages for them, you should not worry about this, since such concerns are assumed by development companies of specialized and universal software.



2. About compilers



Which assembler is better?



For the x86-x64 processor, there are more than a dozen different assembler compilers. They differ in different sets of functions and syntax. Some compilers are more suitable for beginners, some for experienced programmers. Some compilers are well documented, others have no documentation at all. For some compilers, developed by many programming examples. For some assemblers, textbooks and books are written in which the syntax is discussed in detail, others have nothing. Which assembler is better?



Given the many dialects of assemblers for x86-x64 and a limited amount of time to study them, we will limit 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 you must answer first. The most multifunctional assembler will not bring you any benefit if it is not designed to work under the operating system that you plan to use.



WindowsDosLinuxBSDQNXMacOS working on

Intel / AMD processor
Fasmxxxx
Gasxxxxxx
GoAsmx
HLAxx
MASMxx
NASMxxxxxx
RosAsmx
TASMxx


16 bit support



If the assembler supports DOS, then it also supports 16-bit instructions. All assemblers provide the ability to write code that uses 16-bit operands. 16-bit support means the ability to create code that works in 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 has cooled in the middle of zero, and which even 32-bit programs do not fully support, 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 encounter portability issues. For example, if you intend to compile and use your programs in assembler under different operating systems. NASM and FASM can be used in those 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 the “recompilation” of the source code? This function is supported by the HLA dialect. Do you intend to be able to create Windows and Linux applications in assembly language with minimal effort for this? Although, if you are working 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 extended syntax that provides language high-level control structures (such as IF, WHILE, FOR, and so on). Such constructions can facilitate learning to assembler and help to 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 control structures or high-level data types if you prefer to work at the encoding level of machine instructions. High-level constructs are an extension of the basic machine language that you can use if you find them convenient.



Quality of documentation



Ease of use of the assembler is directly related to the quality of its documentation. Given the amount of work that is spent to create an assembler dialect, the compiler authors practically do not bother creating documentation for this dialect. Authors, expanding their language, forget to document these extensions.



The following table describes the quality of the assembler reference manual that is attached to the product:



DocumentationComments
FasmGoodMost of the free time the author gives to the development of innovative FASMG. Nevertheless, the author provides support for FASM from time to time, updates manuals, and describes new functions on its own forum. Documentation can be considered quite good. Documentation webpage .

Gas

The bad

poorly documented and documentation, rather, has a "general view". gas is an assembler that was designed so that you can easily write code for different processors. The documentation that exists basically describes pseudo codes and assembler directives. In the "intel_syntax" mode of operation, the documentation is almost absent. Books using the AT & T syntax: Jonathon Bartlett's “Programming from scratch” and Richard Blum’s Professional Assembly Language, 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 and posted on the site ( http://www.godevtool.com/ ). Several GoAsm textbooks:



HLA

Extensive

HLA has a 500-page reference manual. The site contains dozens of articles and HLA documentation.

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 to be written “later”. NASM has been around for quite a long time, so several authors wrote a manual for NASM Jeff Duntemann “Assembly Language Step-by-Step: Programming with Linux”, Jonathan Leto “ Writing A Useful Program With NASM ”, in Russian there is the book of Stolyarov ( Site AV Stolyarov ).

RosAsm

Weak

not very interesting "online tutorials".

TASM

Good

Borland produced excellent reference manuals in its time; reference manuals for non-Borland enthusiastic authors were written for TASM. But Borland no longer supports TASM, so most of the documentation intended for TASM is not printed and it becomes harder and harder to find.





Textbooks and study materials



The documentation in the assembler itself is of course very important. Even more interesting for newbies and others who are learning the assembly language (or additional features of this assembler) is the availability of documentation outside the reference manual for the language. Most people want a tutorial that explains how to program in assembly language, not only provides the syntax of machine instructions and expects the reader to explain how to combine these instructions to solve real problems.



MASM is the leader among a huge amount of books describing how to program in this dialect. There are dozens of books that use MASM as their assembler to train in assembler.



Most of the MASM / TASM assembler textbooks continue to teach MS-DOS programming. Although gradually there are textbooks that teach programming in Windows and Linux.



Comments
FasmSeveral textbooks that describe programming on FASM:



Gas

Tutorial using AT & T syntax

Tutorial assembler in Linux for programmers C

HLA

The 32-bit version of “The Art of Assembly Language Programming” (exists in both electronic and printed form), programming under Windows or Linux

MASM

A large number of books on learning DOS programming. There are not very many books about programming under Win32 / 64 Pirogov, Yurov, Zubkov, Flenov

NASM

many books devoted to programming in DOS, Linux, Windows. Jeff Duneman’s book "Assembly Language Step-by-Step: Programming with Linux" uses NASM for Linux and DOS. The Paul Carter tutorial uses NASM (DOS, Linux).

TASM

As with MASM, a large number of DOS-based books have been written for TASM. But, since Borland no longer supports this product, they have stopped writing books about using TASM. Tom Swan wrote a TASM tutorial that contained several chapters on programming under Windows.





3. Literature and web resources



Beginners



  1. Abel P. Assembler language for the IBM PC and programming. - M .: Higher School, 1992. - 447 p.
  2. Bradley D. Programming in assembly language for personal computers of the IBM company. - M .: Radio and communication, 1988. - 448 p.
  3. Galiseev G.V. IBM PC assembler. Tutorial .: - M .: Publishing house "Williams", 2004. - 304 pp .: Il.
  4. Tao L. Microprocessor Programming 8088. - M .: Mir, 1988. - 357 p.
  5. Zhukov A.V., Avdyukhin A.A. Assembler - SPb .: BHV-Petersburg, 2003. - 448 pp., Ill.
  6. Zubkov SV, Assembler for DOS, Windows and UNIX. - M .: DMK Press, 2000. - 608 pp., Ill. (A series for programmers).
  7. Irwin K. Assembly language for Intel processors, 4th edition: trans. from English - M .: Williams Publishing House, 2005. - 912 pp., Ill. - Paral. tit English (see also the latest 7th edition in original)
  8. Norton P., Souhe D. The assembly language for the IBM PC. - M .: Computer, 1992. - 352 p.
  9. Pilshchikov V.N. Programming in assembler language IBM PC.– M .: DIALOG-MEPI, 1994–2014 288 p.
  10. Sklyarov I.S. We study the assembler for 7 days www.sklyaroff.ru




Advanced



  1. Kaspersky K. Fundamental bases of hacking. The art of disassembling. - M .: SOLON-Press, 2004. 448 p. - (Kodokopatel series)
  2. Kaspersky K. Technique debugging programs without source texts. - SPb .: BHV-Petersburg, 2005. - 832 pp., Ill.
  3. Kaspersky K. Computer viruses inside and out. - SPb .: Peter, 2006. - 527 pp., Ill.
  4. Kaspersky K. Notes of the researcher of computer viruses. - SPb .: Peter, 2006. - 316 pp., Ill.
  5. Knut, D. The Art of Programming, Volume 3. Sorting and Search, 2nd ed .: Per. from English - M .: Publishing house "Williams", 2003. - 832 pp., Ill. - Paral. tit English
  6. Kolisnichenko D.N. Rootkits for Windows. The theory and practice of programming “invisible caps”, which allow to hide data, processes, network connections from the system. - SPb .: Science and Technology, 2006. - 320 pp., Ill.
  7. Lyamin L.V. Macroassembler MASM.– M .: Radio and communications, 1994.– 320 pp.: Il.
  8. Magda Y. Assembler for Intel Pentium processors. - SPb .: Peter, 2006. - 410 pp., Ill.
  9. Maiko G.V. Assembler for IBM PC.– M .: Business-Inform, Sirin, 1997.– 212 p.
  10. Warren G. Algorithmic Tricks for Programmers, 2nd ed .: Per. from English - M .: Publishing house "Williams", 2004. - 512 pp., Ill. - Paral. tit English
  11. Sklyarov I.S. The art of protecting and hacking information. - SPb .: BHV-Petersburg, 2004. - 288 pp., Ill.
  12. Wetzerell C. Etudes for Programmers: Trans. from English - M .: Mir, 1982. - 288 p., Il.
  13. Frolov Brothers Electronic Library www.frolov-lib.ru
  14. Chekatov A.A. The use of Turbo Assembler in the development of programs. - Kiev: Dialectics, 1995.– 288 p.
  15. Yurov V. Assembler: a special reference book. - St. Petersburg: Peter, 2001.– 496 p.: Ill.
  16. Yurov V. Assembler. Workshop. 2nd ed. - SPb .: Peter, 2006. - 399 p.: Il.
  17. Yurov V. Assembler. Textbook for universities. 2nd ed. - SPb .: Peter, 2007. - 637 pp., Ill.
  18. Pirogov V. Assembler training course. 2001 Knowledge
  19. Pirogov V. ASSEMBLER training course 2003 Knowledge-BHV
  20. Pirogov V. Assembler for windows

    1st edition - M .: publishing house Molgacheva SV, 2002

    2nd edition - SPb.:. BHV-Petersburg, 2003 - 684 pp., Il.

    3rd edition - SPb.:. BHV-Petersburg, 2005 - 864 pp., Ill.

    4th edition - SPb.:. BHV-Petersburg, 2012 - 896 pp., Il.
  21. Pirogov V. Assembler on examples. - SPb.:. BHV-Petersburg, 2012 - 416 pp., Ill.
  22. Pirogov V. ASSEMBLER and disassembly. - SPb.:. BHV-Petersburg, 2006. - 464 pp., Ill.
  23. Pirogov V. work on the book '64-bit programming in assembler (Windows, Unix)'. The book covers programming on fasm in 64-bit Windows and Unix
  24. Yurov V., Khoroshenko S. Assembler: training course. - St. Petersburg: Peter, 1999. - 672 p.
  25. Yu-Zheng Liu, Gibson G. Microprocessors of the 8086/8088 family. Architecture, programming and design of microcomputer systems. - M .: Radio and communication, 1987.– 512 p.
  26. Agner Fog: Software optimization resources (assembly / c ++) 1996 - 2017. Web page
  27. Intel® 64 and IA-32 Architectures Optimization Reference Manual
  28. Intel® 64 and IA-32 Architectures Software Developer's Manual Volume 1: Basic Architecture
  29. Intel® 64 and IA-32 Architects Software Developer's Manual Volume 2A: Instruction Set Reference, AM
  30. Intel® 64 and IA-32 Architects Software Developer's Manual Volume 2B: Instruction Set Reference, NZ
  31. Intel® 64 and IA-32 Architects Software Developer's Manual Volume 3A: System Programming Guide, Part 1
  32. ® 64 and IA-32 Architects Software Developer's Manual Volume 3B: System Programming Guide, Part 2
  33. Leiterman JC 32/64-BIT 80x86 Assembly Language Architecture. © 2005, Wordware Publishing, Inc. (568 pages) 2320 Los Rios Boulevard Plano, Texas 75074
  34. Turbo Assembler Version 3.2 User's Guide Borland International. Inc 1800 Green Hills Road PO BOX 660001, Scotts Valley, CA 95067-0001
  35. Articles from the site wasm.in
  36. Articles from the site sasm.narod.ru
  37. MASM32 website and forum
  38. FASM website and forum
  39. NASM website


4. Practice



So, you already know what an assembler is and what it is eaten with. You have stocked up with a pair / three books and web manuals, and you may have 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:





The question remains, in which editor to write the code? Someone writes in a notebook and compiles via the command line, the 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 the opportunity, I dare to suggest that you try your own programming language in assembly language - ASM Visual . In addition to the basic features, it differs from analogs in the presence of refactoring tools, code metrics, in general, more intuitive interfaces and its own project type.



spoiler
It so happened that the environment is not completely free. You can support the project by purchasing a Pro license for a free price. Use the code: HABR_zzdBG1qadG (if free set the slider to position 0).



We wish you, friends, significant achievements and new knowledge in 2018!



respectfully

Michael Smolentsev MiklIrk (Irkutsk State University of Communications),

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 (the question is the answer options) on the knowledge of assembler and computer architecture. Tests will be structured by difficulty level. If you teach assembler or have sufficient knowledge in this area - write us an e-mail express-rus@yandex.ru you can participate by offering your test to the system.

Source: https://habr.com/ru/post/345748/



All Articles