CIS-77 Home http://www.c-jump.com/CIS77/CIS77syllabus.htm

Data Types and Memory Allocation


  1. Integer Data Types
  2. Allocating Memory for Integer Variables
  3. Data Organization: DB, DW, and EQU
  4. Endianness: Byte Ordering in Computer Memory
  5. Little Endian Example
  6. Big and Little Endian
  7. Data Allocation Directives
  8. Abbreviated Data Allocation Directives
  9. Multi-byte Definitions
  10. Symbol Table
  11. Correspondence to C Data Types
  12. Data Allocation Directives, Cont.
  13. Size of an Integer
  14. Integer Formats
  15. Data Allocation Directives for Uninitialized Data
  16. Working with Simple Variables, PTR operator
  17. Copying Data Values
  18. The MOV Instruction
  19. More MOV Instruction Types
  20. XCHG Instruction, Exchanging Integers
  21. The XCHG Examples
  22. Memory-to-memory exchange
  23. BSWAP Instruction Swap Bytes
  24. Extending Signed and Unsigned Integers
  25. Sign Extending Signed Value
  26. Sign Extending Unsigned Value
  27. Sign Extending with MOVSX and MOVZX
  28. The XLATB Instruction

1. Integer Data Types



2. Allocating Memory for Integer Variables



3. Data Organization: DB, DW, and EQU


  • Representing data types in assembly source files requires appropriate assembler directives.

  • The directives allocate data and format x86 little-endian values.

  • Bytes are allocated by define bytes DB.

  • Words are allocated by define words DW.

  • Both allow more than one byte or word to be allocated.

  • Question marks specify uninitialized data.

  • Strings allocate multiple bytes.

  • Labels in front of the directives remember offsets from the beginning of the segment which accommodates the directive.

  • DUP allows to allocate multiple bytes. The following two lines produce identical results:

        DB ?, ?, ?, ?, ?
        DB 5 DUP(?)
    
  • Note that EQU directive does not allocate any memory: it creates a constant value to be used by Assembler:

        CR EQU 13
        DB CR
        .
        mov al, CR
    
  •   allocation directives


4. Endianness: Byte Ordering in Computer Memory



5. Little Endian Example


  • At the beginning:

      OllyDbg before excution

  • At the end:

      OllyDbg after excution


6. Big and Little Endian


  • Different processors store multibyte integers in different orders in memory.

  • There are two popular methods of storing integers: big endian and little indian.

  • Big endian method is the most natural:

    • the biggest (i.e. most significant) byte is stored first, then the next biggest, etc.

  • IBM mainframes, most RISC processors and Motorola processors all use this big endian method.

  • However, Intel-based processors use the little endian method, in which the least significant byte is stored first.

  • Normally, the programmer does not need to worry about which format is used, unless

    1. Binary data is transfered between different computers e.g. over a network.

      • All TCP/IP headers store integers in big endian format (called network byte order.)

    2. Binary data is written out to memory as a multibyte integer and then read back as individual bytes or vise versa.

  • Endianness does not apply to the order of array elements.

  • See also: wikipedia article about endianness .

      Byte sequence order
    Data type Value(*) Big endian Little endian
    WORD
    1234
    12 34
    34 12
    DWORD
    47D5A8
    00 47 d5 a8
    a8 d5 47 00
    DWORD
    56789ABC
    56 78 9a bc
    bc 9a 78 56
  • (*) All values shown in base 16.

  • Big Endian:

      Big Endian

  • Little Endian:

      Little Endian


7. Data Allocation Directives



8. Abbreviated Data Allocation Directives



9. Multi-byte Definitions



10. Symbol Table



11. Correspondence to C Data Types



12. Data Allocation Directives, Cont.

Keyword Description
BYTE, DB (byte) Allocates unsigned numbers from 0 to 255.
SBYTE (signed byte) Allocates signed numbers from –128 to +127.
WORD, DW (word = 2 bytes) Allocates unsigned numbers from 0 to 65,535 (64K).
SWORD (signed word) Allocates signed numbers from –32,768 to +32,767.
DWORD, DD (doubleword = 4 bytes) Allocates unsigned numbers from 0 to 4,294,967,295 (4 megabytes)
SDWORD (signed doubleword) Allocates signed numbers from –2,147,483,648 to +2,147,483,647.
FWORD, DF (farword = 6 bytes) Allocates 6-byte (48-bit) integers. These values are normally used only as pointer variables on the 80386/486 processors.
QWORD, DQ (quadword = 8 bytes) Allocates 8-byte integers used with 8087-family coprocessor instructions.
TBYTE, DT (10 bytes) Allocates 10-byte (80-bit) integers if the initializer has a radix specifying the base of the number.

13. Size of an Integer


  •  

    Data Type Bytes
    BYTE, SBYTE 1
    WORD, SWORD 2
    DWORD, SDWORD 4
    FWORD 6
    QWORD 8
    TBYTE 10

  • Storing different data types in register:

      storing different data types in register

14. Integer Formats


  • The data types SBYTE, SWORD, and SDWORD tell the assembler to treat the initializers as signed data.

  • It is important to use these signed types with high-level constructs such as .IF, .WHILE, and .REPEAT, and with PROTO and INVOKE directives.

  • For descriptions of these directives, refer to the sections

    • Loop-Generating Directives

    • Declaring Procedure Prototypes

    • Calling Procedures with INVOKE

    in MASM Programmer's Guide.

  •   integer formats


15. Data Allocation Directives for Uninitialized Data



16. Working with Simple Variables, PTR operator



17. Copying Data Values



18. The MOV Instruction



19. More MOV Instruction Types



20. XCHG Instruction, Exchanging Integers



21. The XCHG Examples



22. Memory-to-memory exchange



23. BSWAP Instruction Swap Bytes



24. Extending Signed and Unsigned Integers



25. Sign Extending Signed Value



26. Sign Extending Unsigned Value



27. Sign Extending with MOVSX and MOVZX



28. The XLATB Instruction