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


 

Variables and Data Types of Assembler

  Max Petrov May 2013

Bit is a binary digit.
Byte is a sequence of 8 bits.
Word is a sequence of two bytes (16 bits).
Double word is a sequence of four bytes (32 bits).

      Example of declaration of variables in the program for MASM32 (segment .data):

.data ; --------------------------------------------------------------------------------- ; declaration and initialization of variables of type byte (db) ; --------------------------------------------------------------------------------- a byte ? ; variable a without initial value (uninitialized) b byte 0 ; variable b with initial value 0 abc byte 35 ; variable abc with initial value 35 a1b2 db "#" ; variable a1b2 with initial value 35 ; --------------------------------------------------------------------------------- ; declaration and initialization of variables of type word (dw) ; --------------------------------------------------------------------------------- d word ? ; variable d without initial value (uninitialized) e word 0 ; variable e with initial value 0 ee1 word 35 ; variable ee1 with initial value 35 ee2 dw "#" ; variable ee2 with initial value 35 eee1 word 8995 ; variable eee1 with initial value 8995 eee2 dw "##" ; variable eee2 with initial value 8995 efg1 dw "kl" ; variable efg1 with initial value "lk" (27755) ; --------------------------------------------------------------------------------- ; declaration and initialization of variables of type dword (dd) ; --------------------------------------------------------------------------------- var1 dword 0 ; variable var1 with initial value 0 var2 dword "asdf" ; variable var2 with initial value "fdsa" ; --------------------------------------------------------------------------------- ; declaration and initialization of arrays ; --------------------------------------------------------------------------------- dim1 byte "qwerty" ; array of 6 bytes: "qwerty" dim2 byte 100 dup (?) ; array of 100 bytes, uninitialized dim3 byte 150 dup (0) ; array of 150 bytes, filled with zeros dim4 byte 5 dup (0), ; array of 25 bytes: "0000011111##########asdfg" 5 dup (1), 5 dup (35), 5 dup ("#"), "asdfg" dim5 word 5 dup (0), ; array of 15 words: "000000000001010101010#0#0#0#0#" 5 dup (1), 5 dup (35)

      As you can see from the above example, the variables in assembler MASM32 are defined in segment .data of the program with the aid of following structure:
      [name] type value,
where name is an optional parameter. The name (identifier) of variable can contain numbers, letters of the Latin alphabet, character of underlining in any order, but it must necessarily begins with a letter. The length of name of the variable is allowed in the eleventh version of MASM32 up to 247 characters. If the value of variable at the program start is not defined, instead of number or character value after the indication of type, it is written a symbol “?”.

      The character values (character strings) which initialize the variable are framed in quotes. It should be taken into account that the variables of length 2 or more bytes are initialized by means a string with string inversion, as a result of the record

var2 dword "asdf"

the variable var2 will get an inverted value "fdsa". Such an inversion of strings occurs for the following reason. The character in the computer memory (in case of one-byte encoding) takes 1 byte. Assembler sees a character string as a set of bytes, and assigns sequentially a string value to the multi-byte variable, the first (left) character is placed in the first byte of the variable, the second character is placed in the second byte, and so on. During this procedure, assembler considers a multi-byte variable, initialized by a string, yet as a number. Therefore, for printing (on the screen) the bytes (characters) are displayed in the reverse order, the low byte is placed on the right, so it turns out, that the string is printed backwards.

      Integer variables in MASM32 may be:
  1-byte (8 bits or 1 character), denoted as byte or db,
  2-byte (16-bit or 2 characters), denoted as word or dw,
  4-byte (32 bits or 4 characters), denoted as dword or dd,
  6-byte (48 bits or 6 characters), denoted as fword or df,
  8-byte (34 bits or 8 characters), denoted as qword or dq,
10-byte (80 bits or 10 characters), denoted as tword or dt.

      Floating point variables:
  4-byte, denoted as real4,
  8-byte, denoted as real8,
10-byte, denoted as real10.

      When describing an array, you can use the directive

N dup (val)

where N is the number of duplicates of variable in the memory; val is an initializing value. For example, under the directive

byte 5 dup (0)

the assembler will fill with zeros 5 bytes, which follow immediately the previous (if any) variable or from the beginning of the data segment.

      The name (identifier) of the variable, in the assembler's comprehension, is the address of the low-order byte of this variable (or the address of its single byte, if the variable is a single-byte one). The index of element of array is the shift with the corresponding step (byte, word, double word, etc.) relative to the address of the first element of the array. The names of the elements of the array are formed by a combination of the name of the variable and the shift from it, the shift is written in square brackets.

      It is necessary to remember that for a first element there is no shift (it is equal to zero), respectively, the index of the first element of the array is also equal to zero. For example, for an array of six bytes

dim1 byte "qwerty"

names and values of the elements will be as follows:
      dim1[0] = "q"
      dim1[1] = "w"
      dim1[2] = "e"
      dim1[3] = "r"
      dim1[4] = "t"
      dim1[5] = "y".

      Strictly speaking, the data segment of the program is always a single-byte array (a sequence of cells of memory). In assembler, the definition (description) of variable in the form of an array is an optional convention that does not affect the location of data in the computer's memory. With variable names in assembler you can deal liberally, to address any variable as an array, to obtain the values of some variables through the identifiers of others.

variables of assembler

      The figure shows the first eight bytes of data segment from the fragment of the program, located at the beginning of this article. We have six variables, the first four (a, b, abc, a1b2) are single byte variables, the fifth and sixth (d, e) are double byte variables. We can call the values of single-byte variables by the name of single-byte variable preceding them, considering its name as the name of the array, even if such a previous variable was not explicitly described by us in the data segment of the program as array:
      a = a[0] = ?
      b = a[1] = 0
      abc = a[2] = 35
      a1b2 = a[3] = 35.
Similarly with double-byte (and higher) variables:
      e = d[1] = 0.

      You can get the value of each byte of multi-byte variable through the name of preceding single-byte variable, in our example
      a[4] is the low byte of variable d,
      a[5] is the high byte of variable d,
      a[6] is the low byte of variable e,
      a[7] is the high byte of variable e.

     


Send a comment

E-mail:
*


Insert the digits:

   Макс Петров    13.10.2016    08:16
ya lyublyu assembler(!)


   Вакер    15.03.2017    10:38
Ассемблер ван лав


   Максим Чечуркин    20.06.2017    12:36
Обожаю АСМ, но ЦэШарп лучше :3


   кент с Дута    04.10.2017    08:29
Я ненавижу ассемблер. Нафиг он кому-то нужен???


   Всё    19.10.2017    12:09
Ясно


   +    26.10.2017    10:25
+




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 обязательна