The assembly language command structure contains: Command structure in assembly language level programming. Data format and command structure of assembly language

Assembly language commands (Lecture)

LECTURE PLAN

1. Main groups of operations.

Pentium.

1. Main groups of operations

Microprocessors execute a set of commands that implement the following main groups of operations:

Forwarding operations

Arithmetic operations,

Logical operations

Shift operations

Comparison and testing operations

Bit operations

Program management operations;

Processor control operations.

2. Mnemonic codes of processor commands Pentium

When describing commands, their mnemonic designations (mnemonic codes) are usually used, which are used to specify the command when programming in Assembly language. For different versions of Assembler, the mnemonic codes of some commands may differ. For example, for the command to call a subroutine, the mnemonic code is usedCALL or JSR (“Jump to SubRoutine"). However, the mnemonic codes for most commands for the main types of microprocessors are the same or differ slightly, since they are abbreviations of the corresponding English words that define the operation being performed. Let's look at the command mnemonic codes adopted for processors Pentium.

Forwarding commands. The main team of this group is the teamMOV , which provides data transfer between two registers or between a register and a memory cell. Some microprocessors implement transfers between two memory cells, as well as bulk transfers of the contents of several registers from memory. For example, microprocessors of the 68 family Motorola xxx execute the commandMOVE , providing transfer from one memory cell to another, and the commandMOVEM , which writes to memory or loads from memory the contents of a specified set of registers (up to 16 registers). TeamXCHG mutually exchanges the contents of two processor registers or a register and a memory cell.

Input commands IN and output OUT implement sending data from a processor register to an external device or receiving data from an external device to a register. These commands specify the number of the interface device (input/output port) through which data is transferred. Note that many microprocessors do not have special commands for accessing external devices. In this case, input and output of data in the system is performed using the commandMOV , which specifies the address of the required interface device. Thus, the external device is addressed as a memory cell, and a certain section is allocated in the address space in which the addresses of interface devices (ports) connected to the system are located.

Arithmetic operations commands. The main commands in this group are addition, subtraction, multiplication and division, which have a number of options. Addition commands ADD and subtraction SUB perform the corresponding operations withcpossessed by two registers, a register and a memory location, or using an immediate operand. Teams AD C , S.B. B perform addition and subtraction taking into account the value of the attributeC, set when forming a transfer during the execution of the previous operation. Using these commands, sequential addition of operands is implemented, the number of bits of which exceeds the processor capacity. Team N.E.G. changes the sign of the operand, converting it to two's complement.

Multiplication and division operations can be performed on signed numbers (commandsI MUL, I DIV ) or unsigned (commands MUL, DIV ). One of the operands is always located in a register, the second can be in a register, a memory cell, or be an immediate operand. The result of the operation is located in the register. When multiplying (commandsMUL , IMUL ) the result is double-bit, for which two registers are used. When dividing (commandsDIV , IDIV ) as a dividend, a double-bit operand is used, placed in two registers, and as a result, the quotient and remainder are written to two registers.

Logical Operation Commands . Almost all microprocessors perform logical operations AND, OR, Exclusive OR, which are performed on the same bits of operands using commands AND, OR, X OR . Operations are performed on the contents of two registers, a register and a memory location, or using an immediate operand. Team NOT inverts the value of each bit of the operand.

Shift Commands. Microprocessors perform arithmetic, logical and cyclic shifts of addressed operands by one or more bits. The operand to be shifted can be in a register or memory location, and the number of shift bits is specified by the immediate operand contained in the instruction or determined by the contents of the specified register. The transfer sign is usually involved in the implementation of the shiftCin the status register (S.R. or EFLAGS), which contains the last bit of the operand removed from the register or memory cell.

Comparison and testing commands . Comparison of operands is usually done using the commandCMP , which subtracts operands and sets feature values N, Z, V, C in the status register according to the result obtained. In this case, the result of the subtraction is not saved, and the values ​​of the operands do not change. Subsequent analysis of the obtained feature values ​​allows us to determine the relative value (>,<, =) операндов со знаком или без знака. Использование различных способов адресации позволяет производит сравнение содержимого двух регистров, регистра и ячейки памяти, непосредственно заданного операнда с содержимым регистра или ячейки памяти.

Some microprocessors execute the test command TST , which is a single-operand version of the compare instruction. When this command is executed, the signs are set N, Z according to the sign and value (equal or non-zero) of the addressed operand.

Bit Operation Instructions . These commands set the value of the attributeCin the status register in accordance with the value of the bit being testedbn in the addressed operand. In some microprocessors, based on the result of bit testing, the attribute is setZ. Test bit numbernis specified either by the contents of the register specified in the command, or by the immediate operand.

The commands of this group implement different options for changing the bit being tested. Command BT keeps the value of this bit unchanged.Command B T S post-test sets the value bn=1, and the command B T C - meaning bn=0.Team B T C inverts the value of bit bn after testing it.

Program management operations. To control the program, a large number of commands are used, among which are:

- unconditional control transfer commands;

- conditional jump commands;

- teams for organizing program cycles;

- interrupt commands;

- commands for changing attributes.

Unconditional transfer of control is performed by the commandJMP , which loads into the program counterPCnew content that is the address of the next command to be executed. This address is either directly specified in the commandJMP (direct addressing), or calculated as the sum of the current contentsPCand the offset specified in the command, which is a signed number (relative addressing). BecausePCcontains the address of the next program command, the latter method specifies the jump address, offset relative to the next address by a specified number of bytes. With a positive offset, the transition is made to subsequent commands of the program, with a negative offset - to the previous ones.

A subroutine is also called by unconditionally transferring control using the commandCALL (or JSR ). However, in this case, before loading intoPC new content that specifies the address of the first command of the subroutine, it is necessary to save its current value (the address of the next command) in order to ensure a return to the main program after execution of the subroutine (or to the previous subroutine when nesting subroutines). Conditional jump commands (program branches) load intoPCnew content if certain conditions are met, which are usually set according to the current value of various attributes in the status register. If the condition is not met, then the next program command is executed.

Feature control commands provide writing - reading the contents of the status register in which features are stored, as well as changing the values ​​of individual features. For example, Pentium processors implement the commands LAHF And SAHF , which load the low byte, which contains the signs, from the status register EFLAG to the low byte of the register EAX and padding the low byte EFLAGS from register E AX.. Teams CLC, STC carry out setting the values ​​of the transfer sign CF=0, CF=1, and the command CMC causes the value of this attribute to be inverted. Since attributes determine the flow of program execution during conditional transitions, attribute change commands are usually used to control the program.

Processor control commands . This group includes stop commands, no operation commands, and a number of commands that determine the operating mode of the processor or its individual blocks. TeamHLT stops program execution and puts the processor into a stop state, which is exited when an interrupt or restart signal is received ( Reset). Team NOP (“empty” command), which does not cause any operations to be performed, is used to implement program delays or fill gaps formed in the program.

Special teams CLI, STI prohibit and enable servicing of interrupt requests. In processors Pentium a control bit (flag) is used for thisIF in the register EFLAGS.

Many modern microprocessors issue an identification command that allows the user or other devices to obtain information about the type of processor used in a given system. In processors Pentuim the command for this is CPUID , during which the necessary data about the processor enters the registers EAXEBXECXEDX and can then be read by the user or the operating system.

Depending on the operating modes implemented by the processor and the specified types of data being processed, the set of executed commands can be significantly expanded.

Some processors perform arithmetic operations with binary-decimal numbers or execute special instructions to correct the result when processing such numbers. Many high-performance processors include FPU - number processing unit c "floating point".

A number of modern processors implement group processing of several integers or numbers c “floating point” using one command according to the principle SIMD (“Single Instruction – Multiple Data” ”) - “One command – Lots of data.” Simultaneous execution of operations on multiple operands significantly improves processor performance when working with video and audio data. Such operations are widely used for processing images, audio signals and other applications. To perform these operations, special blocks have been introduced into the processors that implement the corresponding sets of instructions, which in various types of processors ( Pentium, Athlon) got the nameMMX (“ Milti- Media Extension ”) – Multimedia Extension,SSE(“Streaming SIMD Extension”) – Streaming SIMD - extension, “3 DExtension– Three-dimensional Expansion.

A characteristic feature of the company’s processors Intel , starting with the 80286 model, is priority control when accessing memory, which is provided when the processor operates in protected virtual addresses mode - “ Protected Mode ” (protected mode). To implement this mode, special groups of commands are used, which serve to organize memory protection in accordance with the adopted priority access algorithm.

Command structure in assembly language Programming at the level of machine commands is the minimum level at which computer programming is possible. The machine command system must be sufficient to implement the required actions by issuing instructions to the machine equipment. Each machine instruction consists of two parts: an operational one, which determines “what to do” and an operand, which determines the processing objects, that is, “what to do”. A microprocessor machine instruction, written in Assembly language, is a single line that has the following form: label command/directive operand(s); comments The label, command/directive, and operand are separated by at least one space or tab character. The operands of the command are separated by commas.

Assembly Language Command Structure An assembler command tells the translator what action the microprocessor should perform. Assembly directives are parameters specified in the program text that affect the assembly process or the properties of the output file. The operand specifies the initial value of the data (in the data segment) or the elements on which the command action is performed (in the code segment). An instruction may have one or two operands, or no operands. The number of operands is implicitly specified by the instruction code. If a command or directive needs to be continued on the next line, the backslash character is used: "" . By default, assembler does not distinguish between upper and lower case letters when writing commands and directives. Examples of directives and commands Count db 1 ; Name, directive, one operand mov eax, 0 ; Command, two operands

Identifiers are sequences of valid characters used to denote variable names and label names. The identifier may consist of one or more of the following characters: all letters of the Latin alphabet; numbers from 0 to 9; special characters: _, @, $, ? . A dot can be used as the first character of the label. Reserved assembler names (directives, operators, command names) cannot be used as identifiers. The first character of the identifier must be a letter or a special character. Maximum length The identifier has 255 characters, but the translator accepts the first 32 and ignores the rest. All labels that are written on a line that does not contain an assembler directive must end with a colon ":". The label, command (directive), and operand do not have to start at any particular position in the line. It is recommended to write them in a column for greater readability of the program.

Labels All labels that are written on a line that does not contain an assembler directive must end with a colon ":". The label, command (directive), and operand do not have to start at any particular position in the line. It is recommended to write them in a column for greater readability of the program.

Comments Using comments in a program improves its clarity, especially where the purpose of a set of commands is unclear. Comments begin on any line in the source module with a semicolon (;). All characters to the right of "; " to the end of the line are a comment. A comment can contain any printable characters, including space. A comment can span the entire line or follow a command on the same line.

Assembly Language Program Structure A program written in assembly language can consist of several parts called modules, each of which can define one or more data, stack, and code segments. Any complete program in assembly language must include one main, or main, module from which its execution begins. A module can contain program, data, and stack segments, declared using appropriate directives.

Memory models Before declaring segments, you need to specify the memory model using a directive. MODEL modifier memory_model, calling_convention, OS_type, stack_parameter Basic assembly language memory models: Memory model Code addressing Data addressing Operating system Code and data interleaving TINY NEAR MS-DOS Acceptable SMALL NEAR MS-DOS, Windows No MEDIUM FAR NEAR MS-DOS, Windows No COMPACT NEAR FAR MS-DOS, Windows No LARGE FAR MS-DOS, Windows No HUGE FAR MS-DOS, Windows No NEAR Windows 2000, Windows XP, Windows Acceptable FLAT NEAR NT,

Memory Models The tiny model only works in 16-bit MS-DOS applications. In this model, all data and code are located on one physical segment. The size of the program file in this case does not exceed 64 KB. The small model supports one code segment and one data segment. Data and code are addressed as near when using this model. The medium model supports multiple code segments and one data segment, with all references in code segments considered far by default, and references in a data segment considered near. The compact model supports several data segments that use far data addressing (far), and one code segment that uses near addressing (near). The large model supports multiple code segments and multiple data segments. By default, all references to code and data are considered far. The huge model is almost equivalent to the large memory model.

Memory models The flat model assumes an unsegmented program configuration and is used only in 32-bit operating systems. This model is similar to the tiny model in that the data and code are located in a single segment, but it is 32-bit. To develop a program for the flat model before the directive. model flat should place one of the directives: . 386, . 486, . 586 or. 686. The choice of processor selection directive determines the set of instructions available when writing programs. The letter p after the processor selection directive means protected operating mode. Data and code addressing is near, with all addresses and pointers being 32-bit.

Memory models. MODEL modifier memory_model, calling_convention, OS_type, stack_parameter The modifier parameter is used to define segment types and can take the following values: use 16 (segments of the selected model are used as 16-bit) use 32 (segments of the selected model are used as 32-bit). The calling_convention parameter is used to determine the method of passing parameters when calling a procedure from other languages, including high-level languages ​​(C++, Pascal). The parameter can take the following values: C, BASIC, FORTRAN, PASCAL, SYSCALL, STDCALL.

Memory models. MODEL modifier memory_model, calling_convention, OS_type, stack_parameter The OS_type parameter is OS_DOS by default, and is currently the only supported value for this parameter. The stack_parameter parameter is set to: NEARSTACK (SS register is equal to DS, data and stack areas are located in the same physical segment) FARSTACK (SS register is not equal to DS, data and stack areas are located in different physical segments). The default value is NEARSTACK.

An example of a program that does nothing. 686 P. MODEL FLAT, STDCALL. DATA. CODE START: RET END START RET - microprocessor command. It ensures that the program terminates correctly. The rest of the program concerns the operation of the translator. . 686 P - Pentium 6 (Pentium II) protected mode commands are allowed. This directive selects the supported set of assembler instructions, indicating the processor model. . MODEL FLAT, stdcall - flat memory model. This memory model is used in the operating room Windows system. stdcall - the procedure calling convention used.

An example of a program that does nothing. 686 P. MODEL FLAT, STDCALL. DATA. CODE START: RET END START. DATA is a program segment containing data. This program does not use the stack, so the segment. STACK is missing. . CODE is a program segment containing code. START - label. END START - the end of the program and a message to the compiler that program execution should begin with the START label. Every program must contain an END directive to mark the end source code programs. All lines that follow the END directive are ignored. The label specified after the END directive tells the translator the name of the main module from which program execution begins. If the program contains one module, the label after the END directive can be omitted.

Assembly language translators A translator is a program or technical tool that converts a program presented in one of the programming languages ​​into a program in the target language, called object code. In addition to supporting machine instruction mnemonics, each translator has its own set of directives and macro tools, often incompatible with anything else. The main types of assembly language translators: MASM (Microsoft Assembler), TASM (Borland Turbo Assembler), FASM (Flat Assembler) - a freely distributed multi-pass assembler written by Tomasz Gryshtar (Polish), NASM (Netwide Assembler) - a free assembler for the Intel x architecture 86, was created by Simon Tatham with Julian Hall and is currently being developed by a small team of developers at Source. Forge. net.

Src="https://present5.com/presentation/-29367016_63610977/image-15.jpg" alt="Translating a program in Microsoft Visual Studio 2005 1) Create a project by selecting the File->New->Project menu And"> Трансляция программы в Microsoft Visual Studio 2005 1) Создать проект, выбрав меню File->New->Project и указав имя проекта (hello. prj) и тип проекта: Win 32 Project. В дополнительных опциях мастера проекта указать “Empty Project”.!}

Src="https://present5.com/presentation/-29367016_63610977/image-16.jpg" alt="Translating the program in Microsoft Visual Studio 2005 2) In the project tree (View->Solution Explorer) add"> Трансляция программы в Microsoft Visual Studio 2005 2) В дереве проекта (View->Solution Explorer) добавить файл, в котором будет содержаться текст программы: Source. Files->Add->New. Item.!}

Translating the program into Microsoft Visual Studio 2005 3) Select the Code C++ file type, but specify the name with extension. asm:

Translating the program into Microsoft Visual Studio 2005 5) Set compiler parameters. Right-click the Custom Build Rules menu in the project file...

Translate the program into Microsoft Visual Studio 2005 and select Microsoft Macro Assembler in the window that appears.

Translation of the program in Microsoft Visual Studio 2005 Check with the right button in the file hello. asm project tree of the Properties menu and install General->Tool: Microsoft Macro Assembler.

Src="https://present5.com/presentation/-29367016_63610977/image-22.jpg" alt="Translating the program in Microsoft Visual Studio 2005 6) Compile the file by selecting Build->Build hello. prj."> Трансляция программы в Microsoft Visual Studio 2005 6) Откомпилировать файл, выбрав Build->Build hello. prj. 7) Запустить программу, нажав F 5 или выбрав меню Debug->Start Debugging.!}

Programming in Windows OS Programming in Windows OS is based on the use of API functions (Application Program Interface, i.e., software application interface). Their number reaches 2000. The Windows program largely consists of such calls. All interactions with external devices and operating system resources occurs, as a rule, through such functions. The Windows operating system uses a flat memory model. The address of any memory cell will be determined by the contents of one 32-bit register. There are 3 types of program structures for Windows: dialog (the main window is dialog), console or windowless structure, classic structure (windowed, frame).

Call Windows functions API In the help file, any API function is presented as type function_name (FA 1, FA 2, FA 3) Type – return value type; FAx – a list of formal arguments in the order they appear. For example, int Message. Box(HWND h. Wnd, LPCTSTR lp. Text, LPCTSTR lp. Caption, UINT u. Type); This function displays a window with a message and an exit button (or buttons). Meaning of parameters: h. Wnd is a descriptor of the window in which the message window will appear, lp. Text - text that will appear in the window, lp. Caption - text in the window title, u. Type - window type; in particular, you can determine the number of exit buttons.

Calling Windows API int Message functions. Box(HWND h. Wnd, LPCTSTR lp. Text, LPCTSTR lp. Caption, UINT u. Type); Almost all API function parameters are actually 32-bit integers: HWND is a 32-bit integer, LPCTSTR is a 32-bit pointer to a string, UINT is a 32-bit integer. The suffix "A" is often added to the function name to move to newer versions of the function.

Calling Windows API int Message functions. Box(HWND h. Wnd, LPCTSTR lp. Text, LPCTSTR lp. Caption, UINT u. Type); When using MASM, you must add @N N to the end of the name - the number of bytes that the passed arguments occupy on the stack. For Win 32 API functions, this number can be defined as the number of arguments n multiplied by 4 (bytes in each argument): N=4*n. To call a function, use the assembler CALL instruction. In this case, all function arguments are passed to it via the stack (PUSH command). Direction of passing arguments: LEFT TO RIGHT - BOTTOM UP. The argument u will be pushed onto the stack first. Type. The call to the specified function will look like this: CALL Message. Box. A@16

Calling Windows API int Message functions. Box(HWND h. Wnd, LPCTSTR lp. Text, LPCTSTR lp. Caption, UINT u. Type); The result of executing any API function is usually an integer that is returned in the EAX register. The OFFSET directive represents an "offset in a segment", or, translated into high-level language terms, a "pointer" to the beginning of a line. The EQU directive, like #define in SI, defines a constant. The EXTERN directive tells the translator that the function or identifier is external to this module.

Example of a “Hello everyone!” program . 686 P. MODEL FLAT, STDCALL. STACK 4096. DATA MB_OK EQU 0 STR 1 DB "My first program", 0 STR 2 DB "Hello everyone!", 0 HW DD ? EXTERN Message. Box. A@16: NEAR. CODE START: PUSH MB_OK PUSH OFFSET STR 1 PUSH OFFSET STR 2 PUSH HW CALL Message. Box. A@16 RET END START

The INVOKE directive The MASM language translator also allows you to simplify function calls using a macro tool - the INVOKE directive: INVOKE function, parameter1, parameter2, ... There is no need to add @16 to the function call; parameters are written exactly in the order in which they are given in the function description. By macro means of the translator, parameters are placed on the stack. To use the INVOKE directive, you must have a description of the function prototype using the PROTO directive in the form: Message. Box. A PROTO: DWORD, : DWORD If a program uses many Win 32 API functions, it is advisable to use the include directive C: masm 32includeuser 32. inc

Structures in assembly language

The arrays we considered above are a collection of elements of the same type. But often in applications there is a need to consider a certain set of data different types as some single type.

This is very important, for example, for database programs, where it is necessary to associate a collection of data of different types with one object.

For example, we previously looked at Listing 4, in which we worked with an array of three-byte elements. Each element, in turn, consisted of two elements of different types: a one-byte counter field and a two-byte field, which could carry some other information necessary for storage and processing. If the reader is familiar with one of the high-level languages, then he knows that such an object is usually described using a special data type - structures.

In order to improve the usability of the assembly language, this data type was also introduced into it.

A-priory structure is a data type consisting of a fixed number of elements of different types.

To use structures in a program, you must perform three steps:

    Set structure template .

    In essence, this means defining a new data type, which can subsequently be used to define variables of this type.

    Define structure instance .

    This stage involves initializing a specific variable with a predefined (using template) structure.

    Organize accessing structure elements .

It is very important that you understand from the very beginning what the difference is between description structures in the program and its definition.

Describe structure in a program simply means indicating its outline or pattern; memory is not allocated.

This template can only be considered as information for the translator about the location of the fields and their default value.

Define structure means instructing the translator to allocate memory and assign a symbolic name to this memory area.

A structure can be described in a program only once, but defined any number of times.

Description of the structure template

The structure template description has the following syntax:

structure_name STRUC

structure_name ENDS

Here is a sequence of data description directives db, dw, dd, dq And dt.

Their operands determine the size of the fields and, if necessary, the initial values. These values ​​will probably initialize the corresponding fields when defining the structure.

As we already noted when describing the template, memory is not allocated, since this is just information for the translator.

Location The template in the program can be arbitrary, but, following the logic of the one-pass translator, it must be located before the place where the variable with the type of the given structure is defined. That is, when describing a variable with the type of a certain structure in a data segment, its template must be placed at the beginning of the data segment or before it.

Let's consider working with structures using the example of modeling a database about employees of a certain department.

For simplicity, in order to avoid problems of converting information when entering, we will agree that all fields are character fields.

Let's define the record structure of this database with the following template:

Defining data with structure type

To use a structure described using a template in a program, you must define a variable with the type of this structure. For this, the following syntactic construction is used:

[variable name] structure_name

    variable name- identifier of a variable of this structure type.

    Specifying a variable name is optional. If you do not specify it, a memory area of ​​size equal to the sum of the lengths of all structure elements.

    list of values- a list of initial values ​​of structure elements enclosed in angle brackets, separated by commas.

    His assignment is also optional.

    If the list is not fully specified, then all fields of the structure for this variable are initialized with values ​​from the template, if any are specified.

    It is possible to initialize individual fields, but in this case the missing fields must be separated by commas. Omitted fields will be initialized with the values ​​from the structure template. If, when defining a new variable with the type of a given structure, we agree with all the field values ​​in its template (that is, those specified by default), then we just need to write angle brackets.

    Eg: victor worker.

For example, let's define several variables with the type of structure described above.

Methods for working with structure

The idea of ​​introducing a structured type into any programming language is to combine variables of different types into one object.

The language must have a means of accessing these variables within a specific instance of the structure. In order to refer to a field of some structure in a command, a special operator is used - symbol ". " (dot). It is used in the following syntax:

    address_expression- identifier of a variable of some structural type or expression in brackets in accordance with the syntax rules indicated below (Fig. 1);

    structure_field_name- field name from the structure template.

    This, in fact, is also an address, or rather, the offset of the field from the beginning of the structure.

Thus the operator " . " (dot) evaluates expression

Rice. 5. Syntax of an address expression in a structure field access operator

Let us demonstrate using the example of the structure we have defined. worker some techniques for working with structures.

For example, extract to ax field values ​​with age. Since it is unlikely that the age of a working person will be greater than 99 years, after placing the contents of this character field in the register ax It will be convenient to convert it to binary representation with the command aad.

Be careful because due to the principle of data storage “low byte at low address” the highest digit of the age will be placed in al, and the youngest - in ah.

To make adjustments, just use the command xchg al,ah:

mov ax,word ptr sotr1.age ;in al age sotr1

or you can do it like this:

Further work with an array of structures is carried out in the same way as with a one-dimensional array. Several questions arise here:

What to do with the size and how to organize indexing of array elements?

Similar to other identifiers defined in the program, the translator assigns a type attribute to the name of the structure type and the name of the variable with the structure type. The value of this attribute is the size in bytes occupied by the fields of this structure. You can retrieve this value using the operator type.

Once the size of a structure instance is known, organizing indexing in an array of structures is not particularly difficult.

Eg:

How to copy a field from one structure to the corresponding field of another structure? Or how to copy the entire structure? Let's copy the field nam third employee in the field nam fifth employee:

mas_sotr worker 10 dup()

mov bx,offset mas_sotr

mov si,(type worker)*2 ;si=77*2

mov di,(type worker)*4 ;si=77*4

It seems to me that being a programmer sooner or later makes a person look like a good housewife. He, like her, is constantly in search of where to save something, cut down and make a wonderful lunch from a minimum of products. And if this is successful, then the moral satisfaction you get is no less, and maybe even more, than from a wonderful dinner with a housewife. The degree of this satisfaction, it seems to me, depends on the degree of love for one’s profession.

On the other hand, successes in the development of software and hardware somewhat relax the programmer, and quite often a situation similar to the well-known proverb about a fly and an elephant is observed - to solve some minor problem, heavy tools are used, the effectiveness of which, in the general case, is significant only when implementation of relatively large projects.

The presence of the following two types of data in the language is probably explained by the desire of the “housewife” to use the working area of ​​the table (RAM) as efficiently as possible when preparing food or for placing products (program data).

Commands can be distinguished by purpose (examples of mnemonic operation codes of IBM PC assembler commands are given in parentheses):

l performing arithmetic operations (ADD and ADC - addition and addition with carry, SUB and SBB - subtraction and subtraction with borrowing, MUL and IMUL - unsigned and signed multiplication, DIV and IDIV - unsigned and signed division, CMP - comparisons etc.);

l performing logical operations (OR, AND, NOT, XOR, TEST, etc.);

l data transfer (MOV - forward, XCHG - exchange, IN - enter into the microprocessor, OUT - output from the microprocessor, etc.);

l transfer of control (program branches: JMP - unconditional jump, CALL - procedure call, RET - return from procedure, J* - conditional jump, LOOP - loop control, etc.);

l processing character strings (MOVS - transfers, CMPS - comparisons, LODS - loads, SCAS - scans. These commands are usually used with the prefix (repetition modifier) ​​REP;

l program interruptions (INT - software interrupts, INTO - conditional interrupt on overflow, IRET - return from interrupt);

l microprocessor control (ST* and CL* - setting and resetting flags, HLT - stopping, WAIT - waiting, NOP - idling, etc.).

WITH full list Assembly commands can be found in the works.

Data Transfer Commands

l MOV dst, src - data transfer (move - send from src to dst).

Transfers: one byte (if src and dst are in byte format) or one word (if src and dst are in word format) between registers or between a register and memory, and writes a direct value to a register or memory.

The operands dst and src must have the same format - byte or word.

Src can be of the following type: r (register) - register, m (memory) - memory, i (impedance) - immediate value. Dst can be of type r, m. You cannot use the following operands in one command: rsegm together with i; two operands of type m and two operands of type rsegm). The i operand can also be a simple expression:

mov AX, (152 + 101B) / 15

Expression evaluation is performed only during translation. Doesn't change flags.

l PUSH src - pushing a word onto the stack (push - push through; push onto the stack from src). Places the contents of src - any 16-bit register (including segment register) or two memory cells containing a 16-bit word - onto the top of the stack. The flags do not change;

l POP dst - pop a word from the stack (pop - pop; count from stack to dst). Removes a word from the top of the stack and places it in dst - any 16-bit register (including segment register) or in two memory cells. The flags do not change.

Programming at the machine instruction level is the minimum level at which programs can be written. The system of machine instructions must be sufficient to implement the required actions by issuing instructions to the computer hardware.

Each machine command consists of two parts:

  • operational - determining “what to do”;
  • operand - defining processing objects, “what to do with.”

The microprocessor machine command, written in assembly language, is one line with the following syntactic form:

command/directive label operand(s) ;comments

In this case, the required field in the line is a command or directive.

The label, command/directive, and operands (if any) are separated by at least one space or tab character.

If a command or directive needs to be continued on the next line, the backslash character is used: \.

By default, assembly language does not distinguish between uppercase and lowercase letters when writing commands or directives.

Example lines of code:

Count db 1 ;Name, directive, one operand
mov eax,0 ;Command, two operands
cbw ; Team

Tags

Label in assembly language can contain the following symbols:

  • all letters of the Latin alphabet;
  • numbers from 0 to 9;
  • special characters: _, @, $, ?.

A period can be used as the first character of a label, but some compilers do not recommend using this character. Reserved Assembler names (directives, operators, command names) cannot be used as labels.

The first character in the label must be a letter or special character (but not a number). The maximum label length is 31 characters. All labels that are written on a line that does not contain an assembler directive must end with a colon: .

Teams

Team tells the translator what action the microprocessor should perform. In a data segment, a command (or directive) defines a field, workspace, or constant. In a code segment, a command specifies an action, such as moving (mov) or adding (add).

Directives

The assembler has a number of operators that allow you to control the process of assembly and listing. These operators are called directives . They act only during the process of assembling the program and, unlike commands, do not generate machine code.

Operands

Operand – an object on which a machine command or programming language statement is executed.
An instruction may have one or two operands, or no operands at all. The number of operands is implicitly specified by the instruction code.
Examples:

  • No operands ret ;Return
  • One operand inc ecx ;Increase ecx
  • Two operands add eax,12 ;Add 12 to eax

The label, command (directive), and operand do not have to start at any particular position in the line. However, it is recommended to write them in a column to make the program easier to read.

The operands can be

  • identifiers;
  • strings of characters enclosed in single or double quotes;
  • integers in binary, octal, decimal or hexadecimal number systems.
Identifiers

Identifiers – sequences of valid characters used to denote program objects such as operation codes, variable names, and label names.

Rules for recording identifiers.

  • The identifier can consist of one or more characters.
  • As symbols you can use letters of the Latin alphabet, numbers and some special characters: _, ?, $, @.
  • An identifier cannot begin with a digit character.
  • The length of the identifier can be up to 255 characters.
  • The translator accepts the first 32 characters of the identifier and ignores the rest.
Comments

Comments are separated from the executable line by a character; . In this case, everything written after the semicolon and up to the end of the line is a comment. Using comments in a program improves its clarity, especially where the purpose of a set of commands is unclear. The comment can contain any printable characters, including spaces. A comment can span the entire line or follow a command on the same line.

Assembly program structure

A program written in assembly language can consist of several parts called modules . Each module can have one or more data, stack, and code segments defined. Any complete assembler program must include one main, or main, module from which its execution begins. A module can contain code segments, data segments, and stack segments, declared using appropriate directives. Before declaring segments, you must specify the memory model using the .MODEL directive.

An example of a “do nothing” program in assembly language:

686P
.MODEL FLAT, STDCALL
.DATA
.CODE
START:

RET
END START

This program contains only one microprocessor command. This command is RET. It ensures that the program terminates correctly. In general, this command is used to exit a procedure.
The rest of the program concerns the operation of the translator.
.686P - Pentium 6 (Pentium II) protected mode commands are allowed. This directive selects the supported set of assembler instructions, indicating the processor model. The letter P indicated at the end of the directive informs the translator that the processor is operating in protected mode.
.MODEL FLAT, stdcall - flat memory model. This memory model is used in operating system Windows. stdcall
.DATA is a program segment containing data.
.CODE is a program block containing code.
START - label. In assembler, tags play a big role, which cannot be said about modern high-level languages.
END START - the end of the program and a message to the translator that program execution should begin with the START label.
Each module must contain an END directive, marking the end of the program's source code. All lines that follow the END directive are ignored. If you omit the END directive, an error is generated.
The label specified after the END directive tells the translator the name of the main module from which program execution begins. If the program contains one module, the label after the END directive can be omitted.




Top