Arithmetic Instructions

• The add instruction can operate with any register

 ``` add DEST, SRC ```

Effect:

 DEST = DEST + SRC

Note:

 Make sure that the size of the source and destination operands are equal

• Examples:

 ``` i dd 89 add eax, 12 add eax, ebx add eax, i ```

• Sub

• The sub instruction can operate with any register

 ``` sub DEST, SRC ```

Effect:

 DEST = DEST - SRC

Note:

 Make sure that the size of the source and destination operands are equal

• Examples:

 ``` i dd 89 sub eax, 12 sub eax, ebx sub eax, i ```

• Multiply: Mul and IMul

• Signed and unsigned multiplication:

 imul = signed multiplication (we only learned to use signed multiplication in M68000 and SPARC) mul = unsigned multiplication

• Note:

 There are many formats of the multiply instruction I will only discuss the single operand format. (This format works in all versions of Intel CPUs, even in 8080 :-))

• Imul syntax: (for the single operand imul)

 ``` imul SRC ```

Effect:

 ``` DEST = DEST * SRC DEST is some part of the EAX register Which part will depend on the SRC operand Byte size SRC operand: AX = AL * SRC(8) Word size SRC operand: DX:AX = AX * SRC(16) Double word size SRC operand: EDX:EAX = EAX * SRC(32) ```

• Example: EDX = BX (16 bit value) * AX (16 bit value)

 ``` mov ax, 10000 mov bx, 8888 imul bx ;; Result DX:AX = ax * bx ;; DX AX ;; +---------+ +---------+ ;; |xxxxxxxxx| |yyyyyyyyy| ;; +---------+ +---------+ ;; ROL EDX, 16 ;; Rotate EDX 16 bits to the LEFT ;; ;; Before: ;; EDX ;; +---------+---------+ ;; |?????????|xxxxxxxxx| ;; +---------+---------+ ;; ;; After: ;; EDX ;; +---------+---------+ ;; |xxxxxxxxx|?????????| ;; +---------+---------+ ;; ;; This will put lower EDX in upper EDX mov dx, ax ;; Result: ;; EDX ;; +---------+---------+ ;; |xxxxxxxxx|yyyyyyyyy| ;; +---------+---------+ ```

• Divide: Div and IDiv

• Signed and unsigned division:

 idiv = signed division (we only learned to use signed division in M68000 and SPARC) div = unsigned division

• Note:

 There are many formats of the divide instruction I will only discuss the single operand format. (This format works in all versions of Intel CPUs, even in 8080 :-))

• Idiv syntax: (for the single operand imul)

 ``` idiv SRC ```

Effect:

• Just like the M68000 divide instruction, the idvi instruction computes the quotient and the remainder

• The destination register of the divide instruction is always the accumulator

• Effect for different SRC operand sizes

 ``` idiv SRC ---> DEST = DEST / SRC Remainder: Quotient Byte size SRC: AH : AL = AX / SRC(8) Word size SRC: DX : AX = DX:AX / SRC(16) Double word size SRC: EDX : EAX = EDX:EAX / SRC(32) -----------------+-----------------+-------------+---------+--------- |Must use this | | | Size of Operand |Dividend Register| Size Divisor| Quotient|Remainder -----------------+-----------------+-------------+---------+--------- 16 bits | AX | 8 bits | AL | AH -----------------+-----------------+-------------+---------+--------- 32 bits | DX:AX | 16 bits | AX | DX -----------------+-----------------+-------------+---------+--------- 64 bits | EDX:EAX | 32 bits | EAX | EDX -----------------+-----------------+-------------+---------+--------- ```

• Example:

 ``` mov ax, 18 ;; AX(16 bits) = 18 cwd ;; DX:AX(32 bits) = 18 mov bx, 7 ;; Prepare for division idiv bx ;; Divide by 16 bit source... ;; ;; Operation performed: ;; DX:AX / BX ;; ;; Result: ;; AX = quotient ;; DX = remainder ```

• Example Program: (Demo above code)