MASM

(Home)

Top 3 Resources
Resource David Parker Library
Resource MASM
Resource MASM32
Quick Referencek Reference
     
Code
Moves ; Immediate value moves
        mov     ax, 7       ; Immediate to register
        mov     mem, 7      ; Immediate to memory direct
        mov     mem[bx], 7  ; Immediate to memory indirect

; Register moves
        mov     mem, ax     ; Register to memory direct
        mov     mem[bx], ax ; Register to memory indirect
        mov     ax, bx      ; Register to register
        mov     ds, ax      ; General register to segment register

; Direct memory moves
        mov     ax, mem     ; Memory direct to register
        mov     ds, mem     ; Memory to segment register

; Indirect memory moves
        mov     ax, mem[bx] ; Memory indirect to register
        mov     ds, mem[bx] ; Memory indirect to segment register

; Segment register moves
        mov     mem, ds     ; Segment register to memory
        mov     mem[bx], ds ; Segment register to memory indirect
        mov     ax, ds      ; Segment register to general register
Two Instruction Moves

The following example shows several common types of moves that require two instructions.

; Move immediate to segment register
        mov     ax, DGROUP  ; Load AX with immediate value
        mov     ds, ax      ; Copy AX to segment register

; Move memory to memory
        mov     ax, mem1    ; Load AX with memory value
        mov     mem2, ax    ; Copy AX to other memory

; Move segment register to segment register
        mov     ax, ds      ; Load AX with segment register
        mov     es, ax      ; Copy AX to segment register

Exchanging Integers       xchg    ax, bx       ; Put AX in BX and BX in AX
      xchg    memory, ax   ; Put "memory" in AX and AX in "memory"
      xchg    mem1, mem2   ; Illegal- can't exchange memory locations
converts signed integers using CBW, CWD, CWDE, and CDQ.       .DATA
mem8    SBYTE   -5
mem16   SWORD   +5
mem32   SDWORD  -5
        .CODE
        .
        .
        .
        mov     al, mem8    ; Load 8-bit -5 (FBh)
        cbw                 ; Convert to 16-bit -5 (FFFBh) in AX
        mov     ax, mem16   ; Load 16-bit +5
        cwd                 ; Convert to 32-bit +5 (0000:0005h) in DX:AX
        mov     ax, mem16   ; Load 16-bit +5
        cwde                ; Convert to 32-bit +5 (00000005h) in EAX
        mov     eax, mem32  ; Load 32-bit -5 (FFFFFFFBh)
        cdq                 ; Convert to 64-bit -5
                            ;   (FFFFFFFF:FFFFFFFBh) in EDX:EAX
The next example shows 8-bit signed and unsigned addition and subtraction.        .DATA
mem8    BYTE    39
        .CODE
       
; Addition
                         ;                    signed    unsigned
        mov     al, 26   ; Start with register   26       26
        inc     al       ; Increment              1        1
        add     al, 76   ; Add immediate         76     + 76
                         ;                     ----     ----
                         ;                      103      103
        add     al, mem8 ; Add memory            39     + 39
                         ;                     ----     ----
        mov     ah, al   ; Copy to AH          -114      142
                                               +overflow
        add     al, ah   ; Add register                  142
                         ;                              ----
                         ;                                28+carry

; Subtraction
                         ;                   signed    unsigned
        mov     al, 95   ; Load register         95       95
        dec     al       ; Decrement             -1       -1
        sub     al, 23   ; Subtract immediate   -23      -23
                         ;                     ----     ----
                         ;                       71       71
        sub     al, mem8 ; Subtract memory     -122     -122
                         ;                     ----     ----
                         ;                      -51      205+sign

        mov     ah, 119  ; Load register        119
        sub     al, ah   ;   and subtract       -51
                         ;                     ----
                         ;                       86+overflow
The following example illustrates multiple-register addition and subtraction. You can also use this technique with 64-bit operands on the 80386/486 processors.

       .DATA
mem32   DWORD   316423
mem32a  DWORD   316423
mem32b  DWORD   156739
        .CODE
        .
        .
        .
; Addition
        mov     ax, 43981               ; Load immediate     43981
        sub     dx, dx                  ;   into DX:AX
        add     ax, WORD PTR mem32[0]   ; Add to both     + 316423
        adc     dx, WORD PTR mem32[2]   ;   memory words    ------
                                        ; Result in DX:AX   360404

; Subtraction
        mov     ax, WORD PTR mem32a[0]  ; Load mem32        316423
        mov     dx, WORD PTR mem32a[2]  ;   into DX:AX
        sub     ax, WORD PTR mem32b[0]  ; Subtract low    - 156739
        sbb     dx, WORD PTR mem32b[2]  ;   then high       ------
                                        ; Result in DX:AX   159684

 

 

For 32-bit registers on the 80386/486 processors, only two steps are necessary. If your program needs to be assembled for more than one processor, you can assemble the statements conditionally, as shown in this example:

        .DATA
mem32   DWORD   316423
mem32a  DWORD   316423
mem32b  DWORD   156739
p386    TEXTEQU (@Cpu AND 08h)
        .CODE
        .
        .
        .
; Addition
        IF      p386
        mov     eax, 43981  ; Load immediate
        add     eax, mem32  ; Result in EAX
        ELSE
        .
        .       ; do steps in previous example
        .
        ENDIF

; Subtraction
        IF      p386
        mov     eax, mem32a ; Load memory
        sub     eax, mem32b ; Result in EAX
        ELSE
        .
        .       ; do steps in previous example
        .
        ENDIF

 

Since the status of the carry flag affects the results of calculations with ADC and SBB, be sure to turn off the carry flag with the CLC (Clear Carry Flag) instruction or use ADD or SUB for the first calculation, when appropriate.

This example illustrates multiplication of signed 16- and 32-bit integers.

        .DATA
mem16   SWORD   -30000
        .CODE
        .
        .
        .
; 8-bit unsigned multiply
        mov     al, 23     ; Load AL                     23
        mov     bl, 24     ; Load BL                   * 24
        mul     bl         ; Multiply BL              -----
                           ; Product in AX              552
                           ;   overflow and carry set

; 16-bit signed multiply
        mov     ax, 50     ; Load AX                     50
                           ;                         -30000
        imul    mem16      ; Multiply memory          -----
                           ; Product in DX:AX      -1500000
                           ;   overflow and carry set

 

A nonzero number in the upper half of the result (AH for byte, DX or EDX for word) sets the overflow and carry flags.

 

On the 80186–80486 processors, the IMUL instruction supports three additional operand combinations. The first syntax option allows for 16-bit multipliers producing a 16-bit product or 32-bit multipliers for 32-bit products on the 80386/486. The result overwrites the destination. The syntax for this operation is:

IMUL register16, immediate

 

The second syntax option specifies three operands for IMUL. The first operand must be a 16-bit register operand, the second a 16-bit memory (or register) operand, and the third a 16-bit immediate operand. IMUL multiplies the memory (or register) and immediate operands and stores the product in the register operand with this syntax:

 

IMUL register16,{ memory16 | register16}, immediate

 

 

For the 80386/486 only, a third option for IMUL allows an additional operand for multiplication of a register value by a register or memory value. The syntax is:

IMUL register,{register | memory}

 

The destination can be any 16-bit or 32-bit register. The source must be the same size as the destination.

 

In all of these options, products too large to fit in 16 or 32 bits set the overflow and carry flags. The following examples show these three options for IMUL.

        imul    dx, 456     ; Multiply DX times 456 on 80186-80486
        imul    ax, [bx],6  ; Multiply the value pointed to by BX
                            ;   by 6 and put the result in AX

        imul    dx, ax      ; Multiply DX times AX on 80386
        imul    ax, [bx]    ; Multiply AX by the value pointed to
                            ;   by BX on 80386

 

The IMUL instruction with multiple operands can be used for either signed or unsigned multiplication, since the 16-bit product is the same in either case. To get a 32-bit result, you must use the single-operand version of MUL or IMUL.