|
MASM |
| 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 |
|
| 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
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
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
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
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. |
|