CIS-261 Home http://www.c-jump.com/bcc/

Language Components of MASM and General-Purpose Registers


  1. General-Purpose Registers
  2. Typical Uses of General-Purpose Registers
  3. ESP Stack Pointer Register
  4. EIP Instruction Pointer Register
  5. EFLAGS Register
  6. EFLAGS Bit Labels
  7. EFLAGS Individual Bit Flags
  8. Write Fast Code
  9. Language Components of MASM
  10. Data-Related Operators and Directives
  11. Identifiers
  12. Integer Constants
  13. EQU Directive and Symbolic Integer Constants
  14. TYPE, LENGTHOF and SIZEOF Operators
  15. masm_operators.ASM
  16. Operand Addressing Mode Types
  17. Register Operands
  18. Immediate Operands
  19. The OFFSET Operator and LEA Instruction
  20. More about LEA Instruction
  21. Ambiguous moves: PTR directive
  22. OFFSET and PTR Example
  23. ADDR and OFFSET
  24. Direct Memory Operands
  25. Plus, Minus, and Index
  26. Directives BYTE PTR, WORD PTR, DWORD PTR
  27. Pointers
  28. Pointer Types
  29. NEAR Pointers
  30. The TYPEDEF Operator

1. General-Purpose Registers


  • The EAX, EDX, ECX, EBX, EBP, EDI, and ESI registers are 32-bit general-purpose registers, used for temporary data storage and memory access.

  • The AX, DX, CX, BX, BP, DI, and SI registers are 16-bit equivalents of the above, they represent the low-order 16 bits of 32-bit registers.

  • The AH, DH, CH, and BH registers represent the high-order 8 bits of the corresponding registers.

  •   16-bit general-purpose registers

  • Since the processor accesses registers more quickly than it accesses memory, you can make your programs run faster by keeping the most-frequently used data in registers.


2. Typical Uses of General-Purpose Registers



3. ESP Stack Pointer Register



4. EIP Instruction Pointer Register



5. EFLAGS Register



6. EFLAGS Bit Labels



7. EFLAGS Individual Bit Flags

Bit Label EFLAGS Flag Description
 0 CF Carry Flag: Set by arithmetic instructions which generate either a carry or borrow. Set when an operation generates a carry to or a borrow from a destination operand.
 2 PF Parity flag: Set by most CPU instructions if the least significant (aka the low-order bits) of the destination operand contain an even number of 1's.
 4 AF Auxiliary Carry Flag: Set if there is a carry or borrow involving bit 4 of EAX. Set when a CPU instruction generates a carry to or a borrow from the low-order 4 bits of an operand. This flag is used for binary coded decimal (BCD) arithmetic.
 6 ZF Zero Flag: Set by most instructions if the result an operation is binary zero.
 7 SF Sign Flag: Most operations set this bit the same as the most significant bit (aka high-order bit) of the result. 0 is positive, 1 is negative.
 8 TF Trap Flag: (sometimes named a Trace Flag.) Permits single stepping of programs. After executing a single instruction, the processor generates an internal exception 1. When Trap Flag is set by a program, the processor generates a single-step interrupt after each instruction. A debugging program can use this feature to execute a program one instruction at a time.
 9 IF Interrupt Enable Flag: when set, the processor recognizes external interrupts on the INTR pin. When set, interrupts are recognized and acted on as they are received. The bit can be cleared to turn off interrupt processing temporarily.
10 DF Direction Flag: Set and cleared using the STD and CLD instructions. It is used in string processing. When set to 1, string operations process down from high addresses to low addresses. If cleared, string operations process up from low addresses to high addresses.
11 OF Overflow Flag: Most arithmetic instructions set this bit, indicating that the result was too large to fit in the destination. When set, it indicates that the result of an operation is too large or too small to fit in the destination operand.
12-13 IOPL Input/Output privilege level flags: Used in protected mode to generate four levels of security.
14 NT Nested Task Flag: Used in protected mode. When set, it indicates that one system task has invoked another via a CALL Instruction, rather than a JMP.
16 RF Resume Flag: Used by the debug registers DR6 and DR7. It enables you to turn off certain exceptions while debugging code.
17 VM Virtual 8086 Mode flag: Permits 80386 to behave like a high speed 8086.

8. Write Fast Code



9. Language Components of MASM



10. Data-Related Operators and Directives



11. Identifiers



12. Integer Constants



13. EQU Directive and Symbolic Integer Constants



14. TYPE, LENGTHOF and SIZEOF Operators




TITLE MASM Operators
; CIS-261
; masm_operators.asm
; Demonstration of TYPE, LENGTHOF, SIZEOF operators

.386                ; Tells MASM to use Intel 80386 instruction set.
.MODEL FLAT         ; Flat memory model
option casemap:none ; Treat labels as case-sensitive

.CONST          ; Constant data segment
    byte1    BYTE  10,20,30
    array1   WORD  30 DUP(?),0,0
    array2   WORD  5 DUP(3 DUP(?))
    array3   DWORD 1,2,3,4
    digitStr BYTE  '12345678',0
    myArray  BYTE  10,20,30,40,50,
                   60,70,80,90,100

    ;---------------------------------------------
    ; You can examine the following constant values
    ; by looking in the listing file masm_operators.lst
    ;---------------------------------------------
    X = LENGTHOF byte1     ; 3
    X = LENGTHOF array1    ; 30 + 2
    X = LENGTHOF array2    ; 5 * 3
    X = LENGTHOF array3    ; 4
    X = LENGTHOF digitStr  ; 9
    X = LENGTHOF myArray   ; 10

    X = SIZEOF byte1       ; 1 * 3
    X = SIZEOF array1      ; 2 * (30 + 2)
    X = SIZEOF array2      ; 2 * (5 * 3)
    X = SIZEOF array3      ; 4 * 4
    X = SIZEOF digitStr    ; 1 * 9

    X = TYPE byte1     ; 1
    X = TYPE array1    ; 2
    X = TYPE array2    ; 2
    X = TYPE array3    ; 4
    X = TYPE digitStr  ; 1

.DATA           ; Begin initialized data segment
    
.CODE           ; Begin code segment
_main PROC      ; Beginning of code

    ret
    
_main ENDP
END _main       ; Marks the end of the module and sets the program entry point label

15. Operand Addressing Mode Types



16. Register Operands


  • Register operands refer to data stored in registers. The following examples show typical register operands:

            mov     bx, 10          ; Load constant to BX
            add     ax, bx          ; Add BX to AX
            jmp     di              ; Jump to the address in DI
    
  • An offset stored in a base or index register often serves as a pointer into memory.

  • You can store an offset in one of the base or index registers, then use the register as an indirect memory operand. For example:

            mov     [bx], dl ; Store DL in indirect memory operand
            inc     bx       ; Increment register operand
            mov     [bx], dl ; Store DL in new indirect memory operand
    
  • This example moves the value in DL twice to 2 consecutive bytes of a memory location pointed to by BX.

  • Example shows that changing BX register causes it to point to a different location in memory.

  •   EAX register


17. Immediate Operands



18. The OFFSET Operator and LEA Instruction


The OFFSET operator returns the offset of a data label. The offset represents the distance, in bytes, of the label from the beginning of the data segment. In Protected mode, an offset is always 32 bits long. In Real-address mode, offsets are only 16 bits.


19. More about LEA Instruction



20. Ambiguous moves: PTR directive



21. OFFSET and PTR Example


 
 ; offset_ptr.asm
 ; OFFSET and PTR demo program
 .586P
 ; Flat memory model
 .MODEL FLAT, STDCALL
 ;---------------------------------------
 ; Data segment
 _DATA SEGMENT
         num  DWORD   0
 _DATA ENDS
 ; Code segment
 _TEXT SEGMENT
 START:
         lea     ESI, num           ; Load effective address
         mov     ESI, OFFSET num
         mov     bx, WORD PTR num   ; WORD PTR needed because num declared DWORD
         mov     [ESI], bx          ; Copy a word-size value (BX is 16-bit)
         mov     BYTE PTR [ESI], 5  ; Store 8-bit value
         mov     WORD PTR [ESI], 5  ; Store 16-bit value
         mov     DWORD PTR [ESI], 5 ; Store 32-bit value
         ret                        ; Exit
 _TEXT ENDS
 END START
 
 

22. ADDR and OFFSET



23. Direct Memory Operands



24. Plus, Minus, and Index



25. Directives BYTE PTR, WORD PTR, DWORD PTR



26. Pointers



27. Pointer Types



28. NEAR Pointers



29. The TYPEDEF Operator