Sadda.ru Ironetcart Андроид Ассемблер MASM32 Linux Все статьи Table of Contents


 

The simplest assembly program (beeper)

  Max Petrov May 2013

      You can see below the program on assembler language MASM32, which outputs on the system speaker the first eight cycles of waltz of Griboyedov. The program can be compiled into an executable file (.exe). For compilation, please create a new text document in the directory on the same drive where you have installed MASM32. Rename the file to beeper.asm, copy into it the text of the program. Then open this file using MASM32 Editor, then in the menu of MASM32 Editor select the option Project >> Assemble & Link. In the folder where beeper.asm is located, the compiled program will also can be found, it will be called beeper.exe.

.386 ; 32-bit mode .model flat, stdcall ; Compilation into file .exe with possibility to call API option casemap :none ; Non-distinction between uppercase and lowercase characters include <\masm32\include\kernel32.inc> ; Connect a file of prototypes of functions includelib <\masm32\lib\kernel32.lib> ; Connect a library file .data ; Start the data segment (and complete the previous segment) ; Here it could be placed a description of the variables .code ; Start the code segment (and complete the previous segment) ; Here it could be placed a description of the procedures start: ; Here the operating system will transfer the control invoke Beep, 3951, 200 ; B (Si) invoke Beep, 4186, 200 ; C (Do) invoke Beep, 3951, 200 ; B (Si) invoke Beep, 3136, 200 ; G (Sol) invoke Beep, 2637, 200 ; E (Mi) invoke Beep, 3951, 200 ; B (Si) invoke Beep, 3136, 200 ; G (Sol) invoke Beep, 3951, 200 ; B (Si) invoke Beep, 1975, 200 ; B (Si) invoke Beep, 3951, 200 ; B (Si) invoke Beep, 3136, 200 ; G (Sol) invoke Beep, 3951, 200 ; B (Si) invoke Beep, 3440, 200 ; A (La) invoke Beep, 3951, 200 ; B (Si) invoke Beep, 3440, 200 ; A (La) invoke Beep, 3015, 200 ; F (Fa) invoke Beep, 2489, 200 ; D# (Re#) invoke Beep, 3440, 200 ; A (La) invoke Beep, 3015, 200 ; F (Fa) invoke Beep, 3440, 200 ; A (La) invoke Beep, 1975, 200 ; B (Si) invoke Beep, 3440, 200 ; A (La) invoke Beep, 3015, 200 ; F (Fa) invoke Beep, 3440, 200 ; A (La) invoke Beep, 3136, 200 ; G (Sol) invoke Beep, 3440, 200 ; A (La) invoke Beep, 3136, 200 ; G (Sol) invoke Beep, 2637, 200 ; E (Mi) invoke Beep, 1975, 200 ; B (Si) invoke Beep, 3136, 200 ; G (Sol) invoke Beep, 3015, 200 ; F (Fa) invoke Beep, 3136, 200 ; G (Sol) invoke Beep, 3015, 200 ; F (Fa) invoke Beep, 2098, 200 ; C (Do) invoke Beep, 1720, 200 ; A (La) invoke Beep, 3015, 200 ; F (Fa) invoke Beep, 2637, 200 ; E (Mi) invoke Beep, 3015, 200 ; F (Fa) invoke Beep, 2637, 200 ; E (Mi) invoke Beep, 2217, 200 ; C#, (Do#) (2217,40) invoke Beep, 1568, 200 ; G (Sol) invoke Beep, 2637, 200 ; E (Mi) invoke Beep, 2489, 250 ; D# (Re#) invoke Beep, 1975, 250 ; B (Si) invoke Beep, 3729, 250 ; A# (La#) (3729,20) invoke Beep, 1975, 250 ; B (Si) invoke Beep, 3951, 250 ; B (Si) invoke Beep, 1975, 250 ; B (Si) invoke ExitProcess, 0 ; Inform Windows about the end of the program end start ; Complete the code segment

      This simplest assembler program has a set of directives that will be present in any program for MASM32. The first string contains the instruction to the compiler about the type of processor for which the program was created. 386 is the third generation (1985) of Intel processors, whose registers, internal and external data channels for the first time became 32-bit. Thus, the directive .386 means that the program is written for 32-bit Intel processors. Since Intel processors are compatible from up to down, the program code will work on all versions of Intel processors released after 1985.

      Other possible variants of the directive indicating the processor type are the following: .8086, .186, .286 (16 bit), .486, .586, .686 (32 bit). In addition (in a separate string), can be indicated the extensions of the system of processor commands (in uppercase or lowercase characters): .MMX, .XMM.

      A comment is any character set, which starts with a semicolon and ends in the end of string. For example, in the first string of our program there is a record
      ; 32-битный режим
this is a comment. The compiler does not process the comments.

      The directive .model in the record flat tells the compiler that the program should be created in a flat memory model, with that in one 32-bit segment both the program and its data will be contained. Just in this format Windows operating system perceive the executable files, respectively, the indication to the compiler on a flat memory model implies the creation after all the file .exe. The flat memory model is applicable only to 32-bit and higher processors.

      All memory models perceived by MASM32 are the following: tiny, small, compact, medium, large, huge, flat.

      In standard function call, the parameters are passed through the stack. It is just through the stack that the built-in functions of the Windows operating system (API functions) exchange the data with the programs, so if we plan to use such functions in our program, we need to write in the .model directive the option stdcall.

      The directive of option casemap :none instructs the compiler to make no distinction between uppercase and lowercase characters in the program written by us. All variants of the option casemap: are all, none, notpublic.

      The directive include instructs the compiler to insert into the project the assembler code from the file specified in it. The path to the file can be specified in the directive not completely, in this case the compiler will make into consideration the current folder. The angle brackets are necessary if the path to the file contains a backslash, a semicolon, single or double quote marks.

      Similarly, the directive includelib connects a library file to our project.

      The directives .data and .code start the data segment and the segment of executable code respectively and end the previous segment, if any exists.

      The directive invoke instructs the compiler to call the function. In our case, the called function Beep is an API function of Windows, and it is described as follows:
      Function Beep Lib "kernel32" Alias DWORD "Beep" (DWORD dwFreq, DWORD dwDuration).
From the description it can be seen that this function is contained in the system file kernel32.dll (which is a part of Windows operating system). Therefore, before in the program we have included the files kernel32.inc and kernel32.lib, which contain the instructions to assembler for a work with the library kernel32.dll.

      To the function Beep it must be transmitted two values: DWORD dwFreq and DWORD dwDuration. The record DWORD (double word) specifies the memory size which is occupied by the variable. Double word is two machine words (four bytes or 32 bits). The specification of the size of the memory allocated to a variable is important because the data exchange with API functions is performed through the stack, and to the stack the program must place before calling the API function such a quantity of bytes as will be extracted by API function after. Otherwise, call of API function or execution of the program after call may be ended by fail.

      And, finally, dwFreq is the frequency of sound in Htz, dwDuration is the duration of the sound in milliseconds. In assembler, the parameters passed to the function are written after its name, separated by commas, in the same order as in the function description. For example, the record
      invoke Beep, 784, 900
after compilation will give a note G (Sol) of second octave (its frequency is 784 Htz) with a sound duration of 0.9 seconds.

     


Send a comment

E-mail:
*


Insert the digits:

   Вовка    12.11.2016    20:38
Ну наконец то нашёл нормальный код, удалось без проблем скомпилировать и отлинковать пример кода ассемблера в masm32. Спасибо конечно большое, особенно спасибо за подробный комментарий в коде.


   Макс    13.11.2016    08:53
Спасибо за отзыв, рад, что удалось помочь.


   1    08.01.2017    16:27
Хорошие статьи, спасибо :)


   MASM32    29.05.2017    03:02
=)




Table of Contents

Assembler MASM32

      The simplest assembly program (beeper)
      Variables and Data Types of Assembler
      Registers of Processor IA32
      Numerical Systems, conversion of number
      Negative numbers

Other articles

      Frontend protection against Stored XSS Attacks (Javascript kills XSS)
      How to enforce Firefox to update favicon
      How to lock CD autorun in Windows XP
      Javascript Progress Bar for PHP program
      Schultz's tables (Shultzstables.exe)
      Speed of Forum Engine. Files or Database
      The best free programs for reading txt-files on Android smartphones
      Why a stupid forum needs a search option?

     


© Max Petrov При использовании материалов ссылка на sadda.ru обязательна