Differences

This shows you the differences between two versions of the page.

Link to this comparison view

Both sides previous revisionPrevious revision
Next revision
Previous revision
en:multiasm:papc:chapter_6_12 [2025/11/14 14:20] – [Conditional assembly directives] ktokarzen:multiasm:papc:chapter_6_12 [2026/02/27 02:10] (current) – [Statements] jtokarz
Line 1: Line 1:
-====== MASM basics ======+====== MASM Basics ======
 An assembler, understood as software that translates assembler source code into machine code, can be implemented in various ways. While the processor's instructions remain constant, other language elements may be implementation-specific. In this chapter, we present the most important language elements specific to the MASM assembly implementation. For a detailed description of MASM assembler language implementation, please refer to the Microsoft© website((https://learn.microsoft.com/en-us/cpp/assembler/masm/microsoft-macro-assembler-reference?view=msvc-170)). An assembler, understood as software that translates assembler source code into machine code, can be implemented in various ways. While the processor's instructions remain constant, other language elements may be implementation-specific. In this chapter, we present the most important language elements specific to the MASM assembly implementation. For a detailed description of MASM assembler language implementation, please refer to the Microsoft© website((https://learn.microsoft.com/en-us/cpp/assembler/masm/microsoft-macro-assembler-reference?view=msvc-170)).
 ===== Alphabet ===== ===== Alphabet =====
Line 122: Line 122:
  
  
-===== Important directives =====+===== Selected directives =====
  
 The **ORG** directive sets the location counter to the specified value x. It is used to align the parts of the program to a specific address. The **ORG** directive sets the location counter to the specified value x. It is used to align the parts of the program to a specific address.
Line 133: Line 133:
 The **LABEL** directive creates a new label by assigning the current location-counter value and the given type to the defined name. Usually in a program, the **:** (colon) sign is used for label definition, but the **LABEL** directive enables specifying the type of element which the label points to. The **LABEL** directive creates a new label by assigning the current location-counter value and the given type to the defined name. Usually in a program, the **:** (colon) sign is used for label definition, but the **LABEL** directive enables specifying the type of element which the label points to.
  
 +===== Data definition directives =====
 There is a set of directives for defining variables. They enable the assignment of a name to a variable and the specification of its type. They are summarised in a table There is a set of directives for defining variables. They enable the assignment of a name to a variable and the specification of its type. They are summarised in a table
 {{ref>masmdatadefine}}. {{ref>masmdatadefine}}.
Line 190: Line 191:
   loop clear   loop clear
 </code> </code>
 +
 +===== Constants =====
 +Constants in an assembler program define the name for the value that can't be changed during normal program execution. It is the assembly-time assignment of the value and its name. Although their name suggests that their value can't be altered, it is true at the program run-time. Some forms of constants can be modified during assembly time. Usually, constants are used to self-document the code, parameterise the assembly process, and perform assembly-time calculations.
 +The constants can be integer, floating-point numeric, or text strings.\\
 +Integer numeric constants can be defined with the data assignment directives, **EQU** or the equal sign **=**. The difference is that a numeric constant defined with the EQU directive can’t be modified later in the program, while a constant created with the equal sign can be redefined many times in the program. Numeric constants can be expressed as binary, octal, decimal or hexadecimal values. They can also be a result of an expression calculated during assembly time. It is possible to use a previously defined constant in such an expression.
 +<code asm>
 +int_const1 EQU 5                ; no suffix by default decimal value
 +int_const_dec = 7               ; finished with "d", "D", "t", "T", or 
 +                                ; by default without suffix
 +int_const_binary = 100100101b   ; finished with "b", "B", "y", or "Y"
 +int_const_octal = 372o          ; finished with "o", "O", "q", or "Q"
 +int_const_hex = 0FFA4h          ; finished with "h", or "H"
 +int_const_expr = int_const_dec * 5
 +</code> 
 +Floating-point numeric constants can be defined with the **EQU** directive only. The number can be expressed in decimal or scientific notation.
 +<code asm> 
 +real_const1 EQU 3.1415          ; decimal
 +real_const2 EQU 6.28e2          ; scientific
 +</code>
 +Text string constants can be defined with **EQU** or **TEXTEQU** directives. Text constants assigned with the **EQU** or **TEXTEQU** directive can be redefined later in the program. The **TEXTEQU** is considered a text macro and is described in the section about macros.
 +<code asm>
 +text_const1 EQU 'Hello World!'
 +text_const2 EQU "Hello World!"
 +</code>
 +
 +
 ===== Conditional assembly directives ===== ===== Conditional assembly directives =====
  
 The condition assembly directives have the same functionality as in high-level language compilers. They control the assembly process by checking the defined conditions and enabling or disabling the process for fragments of the source code. The condition assembly directives have the same functionality as in high-level language compilers. They control the assembly process by checking the defined conditions and enabling or disabling the process for fragments of the source code.
-  * **IF** expression, **IFE** expression - test the value of the expression and perform (or do not) assemble according to the result (0-false)+  * **IF** expression, **IFE** expression - tests the value of the expression and performs (or do not) assemble according to the result (0-false),
   * **IFDEF** symbol - tests whether a symbol is defined,   * **IFDEF** symbol - tests whether a symbol is defined,
-  * **IFNDEF** symbol - tests whether a symbol is undefined.+  * **IFNDEF** symbol - tests whether a symbol is undefined,
   * **IFB** <argument> - tests whether the string argument is empty,    * **IFB** <argument> - tests whether the string argument is empty, 
   * **IFNB** <argument> - tests whether the string argument is empty.    * **IFNB** <argument> - tests whether the string argument is empty. 
-The conditional assembly statement, together with possible **ELSEIF** and **ELSE** statements, is shown in the following code.+The conditional assembly statement, together with optional **ELSEIF** and **ELSE** statements, is shown in the following code.
 <code asm> <code asm>
 IF expression1 IF expression1
Line 216: Line 243:
 All fields in a statement are optional. A statement can be composed of a label only (ended with a colon), an operation only (if it doesn't require operands), or a comment only. A few examples of proper statements are presented in the following code. All fields in a statement are optional. A statement can be composed of a label only (ended with a colon), an operation only (if it doesn't require operands), or a comment only. A few examples of proper statements are presented in the following code.
 <code asm> <code asm>
-; name    ; operation ; operands ; comment+; name ; operation ; operands ; comment
  
-cns_y      EQU         134       ; definition of a constant named cns_y with the value 134+cns_y   EQU         134       ; definition of a constant named cns_y with the  
 +                              ;   value 134
  
-           .DATA                 ; operation only - directive to start data section +        .DATA                 ; operation only - directive to start data section 
-var_x      DB          123       ; definition of a variable named var_x with init value 123+var_x   DB          123       ; definition of a variable named var_x with init  
 +                              ;   value 123
  
-           .CODE                 ; operation only - directive to start code section +        .CODE                 ; operation only - directive to start code section 
-begin:                           ; name only - label that represents an address +begin:                        ; name only - label that represents an address 
-           mov         rax, rbx  ; operation and corresponding operands +        mov         rax, rbx  ; operation and corresponding operands 
-                                 ; comment only statement +                              ; comment only statement 
-           END                   ; operation only - end of the source file+        END                   ; operation only - end of the source file
 </code> </code>
en/multiasm/papc/chapter_6_12.1763122806.txt.gz · Last modified: by ktokarz
CC Attribution-Share Alike 4.0 International
www.chimeric.de Valid CSS Driven by DokuWiki do yourself a favour and use a real browser - get firefox!! Recent changes RSS feed Valid XHTML 1.0