45
ADD Instruction ADD destination,source destination = destination + source ADD AX,BX ADD SUM,EAX ADD EDX,ARRAY[EBX][ESI] ADD CL,5 ADD DL,[BX]

ADD Instruction - LSU 3.pdfADD Instruction ADD destination ... MOV BL,14H MOV AL,47H ADD AL ... Dot commands are available for MASM

Embed Size (px)

Citation preview

Page 1: ADD Instruction - LSU 3.pdfADD Instruction ADD destination ... MOV BL,14H MOV AL,47H ADD AL ... Dot commands are available for MASM

ADD InstructionADD destination,source

destination = destination + source

ADD AX,BXADD SUM,EAXADD EDX,ARRAY[EBX][ESI]ADD CL,5ADD DL,[BX]

Page 2: ADD Instruction - LSU 3.pdfADD Instruction ADD destination ... MOV BL,14H MOV AL,47H ADD AL ... Dot commands are available for MASM

ADC InstructionADC destination,source

destination = destination + source + carry

ADC DX,BXADC COUNT,ECXADC EAX,ARRAY[EBX][ESI]

Page 3: ADD Instruction - LSU 3.pdfADD Instruction ADD destination ... MOV BL,14H MOV AL,47H ADD AL ... Dot commands are available for MASM

XADD InstructionXADD destination,source

destination = destination + sourceSource = original destination

Assume:BX = 0AHDX = 11H

After XADD BX,DX is executed:BX = 1BHDX = 0AH

80486 and Pentium instruction.

Page 4: ADD Instruction - LSU 3.pdfADD Instruction ADD destination ... MOV BL,14H MOV AL,47H ADD AL ... Dot commands are available for MASM

INC Instruction

INC operandoperand = operand + 1

INC BXINC COUNTINC DWORD PTR [EBX]

Page 5: ADD Instruction - LSU 3.pdfADD Instruction ADD destination ... MOV BL,14H MOV AL,47H ADD AL ... Dot commands are available for MASM

SUB InstructionSUB destination,source

destination = destination - source

SUB AX,BXSUB SUM,EAXSUB EDX,ARRAY[EBX][ESI]SUB CL,5SUB DL,[BX]

Page 6: ADD Instruction - LSU 3.pdfADD Instruction ADD destination ... MOV BL,14H MOV AL,47H ADD AL ... Dot commands are available for MASM

SBB InstructionSBB destination,source

destination = destination - source - carry

SBB DX,BXSBB COUNT,ECXSBB EAX,ARRAY[EBX][ESI]

Page 7: ADD Instruction - LSU 3.pdfADD Instruction ADD destination ... MOV BL,14H MOV AL,47H ADD AL ... Dot commands are available for MASM

DEC InstructionDEC operand

operand = operand - 1

DEC BXDEC COUNTDEC DWORD PTR [EBX]

Page 8: ADD Instruction - LSU 3.pdfADD Instruction ADD destination ... MOV BL,14H MOV AL,47H ADD AL ... Dot commands are available for MASM

CMP InstructionCMP operand1,operand2

operand1 - operand2Flags are updated and the result is discarded.

CMP AL,BLCMP BX,0ABCHCMP DL,[BX]

Page 9: ADD Instruction - LSU 3.pdfADD Instruction ADD destination ... MOV BL,14H MOV AL,47H ADD AL ... Dot commands are available for MASM

CMPXCHG InstructionCMPXCHG operand1,operand2

If operand1 = accumulator thenaccumulator = operand2

Elseaccumulator = operand1

CMPXCHG BL,CLCMPXCHG DATA,EDX

CMPXCHG8B allows the comparison of quad words

Page 10: ADD Instruction - LSU 3.pdfADD Instruction ADD destination ... MOV BL,14H MOV AL,47H ADD AL ... Dot commands are available for MASM

MUL and IMUL InstructionsMUL operand (unsigned product)IMUL operand (signed product)

accumulator = accumulator * operand

MUL BLAX = AL * BL

MUL CX<DX>:<AX> = AX * CX

MUL EBX<EDX>:<EAX> = EAX * EBX

Page 11: ADD Instruction - LSU 3.pdfADD Instruction ADD destination ... MOV BL,14H MOV AL,47H ADD AL ... Dot commands are available for MASM

MUL and IMUL InstructionsMUL BYTE PTR TEMP

AX = AL * TEMPMUL WORD PTR [DI]

<DX>:<AX> = AX * [DI]MUL DWORD PTR [EBX]

<EDX>:<EAX> = EAX * [EBX]

Page 12: ADD Instruction - LSU 3.pdfADD Instruction ADD destination ... MOV BL,14H MOV AL,47H ADD AL ... Dot commands are available for MASM

Special Immediate 16 bit Product

IMUL reg,immIMUL reg,reg,immIMUL reg,mem,imm

IMUL CX,16CX = CX * 16

IMUL DX,DATA,2DX = DATA * 2

Page 13: ADD Instruction - LSU 3.pdfADD Instruction ADD destination ... MOV BL,14H MOV AL,47H ADD AL ... Dot commands are available for MASM

DIV and IDIV InstructionsDIV operand (unsigned division)IDIV operand (signed division)DIV BL

AL (quotient) = AX / BLAH (remainder) = AX / BL

DIV CXAX (quotient) = <DX>:<AX> / CXDX (remainder) = <DX>:<AX> / CX

Page 14: ADD Instruction - LSU 3.pdfADD Instruction ADD destination ... MOV BL,14H MOV AL,47H ADD AL ... Dot commands are available for MASM

DIV and IDIV InstructionsDIV EBX

EAX (quotient) = <EDX>:<EAX> / EBXEDX (remainder) = <EDX>:<EAX> / EBX

DIV BYTE PTR TEMPDIV WORD PTR [DI]DIV DWORD PTR [EBX]

Page 15: ADD Instruction - LSU 3.pdfADD Instruction ADD destination ... MOV BL,14H MOV AL,47H ADD AL ... Dot commands are available for MASM

BCD ArithmeticInstructions that use packed BCD operands.DAA - Decimal adjust after addition.DAS - Decimal adjust after subtraction.

MOV BL,14HMOV AL,47HADD AL,BLDAA

Page 16: ADD Instruction - LSU 3.pdfADD Instruction ADD destination ... MOV BL,14H MOV AL,47H ADD AL ... Dot commands are available for MASM

ASCII ArithmeticInstructions that use unpacked BCD operands.AAM – Adjust after multiplication.

MOV AL,5MOV BL,8MUL BLAAM

AAD – Adjust before division.MOV AL,12MOV BL,3AADDIV BL

Page 17: ADD Instruction - LSU 3.pdfADD Instruction ADD destination ... MOV BL,14H MOV AL,47H ADD AL ... Dot commands are available for MASM

ASCII Arithmetic

Instructions that use ASCII operands.AAA – Adjust after addition.AAS – Adjust after subtraction.

MOX AX,31HADD AL,39HAAAADD AX,3030H

Page 18: ADD Instruction - LSU 3.pdfADD Instruction ADD destination ... MOV BL,14H MOV AL,47H ADD AL ... Dot commands are available for MASM

AND Instruction

AND destination,sourcedestination = destination · source

AND AX,BXAND SUM,EAXAND EDX,ARRAY[EBX][ESI]AND CL,5AND DL,[BX]

Page 19: ADD Instruction - LSU 3.pdfADD Instruction ADD destination ... MOV BL,14H MOV AL,47H ADD AL ... Dot commands are available for MASM

OR Instruction

OR destination,sourcedestination = destination + source

OR AX,BXOR SUM,EAXOR EDX,ARRAY[EBX][ESI]OR CL,5OR DL,[BX]

Page 20: ADD Instruction - LSU 3.pdfADD Instruction ADD destination ... MOV BL,14H MOV AL,47H ADD AL ... Dot commands are available for MASM

XOR Instruction

XOR destination,sourcedestination = destination ⊕ source

XOR AX,BXXOR SUM,EAXXOR EDX,ARRAY[EBX][ESI]XOR CL,5XOR DL,[BX]

Page 21: ADD Instruction - LSU 3.pdfADD Instruction ADD destination ... MOV BL,14H MOV AL,47H ADD AL ... Dot commands are available for MASM

NOT and NEG Instructions

NOT operand – 1’s complementNEG operand – 2’s complement

operand = operand’

NOT BXNEG SUMNOT ECXNEG CL

Page 22: ADD Instruction - LSU 3.pdfADD Instruction ADD destination ... MOV BL,14H MOV AL,47H ADD AL ... Dot commands are available for MASM

TEST InstructionTEST operand1, operand2

operand1 · operand2Flags are updated and the result is discarded.

TEST AX,BXTEST SUM,EAXTEST CL,5TEST DL,[BX]

Page 23: ADD Instruction - LSU 3.pdfADD Instruction ADD destination ... MOV BL,14H MOV AL,47H ADD AL ... Dot commands are available for MASM

Shift InstructionsThese instructions perform the logical and arithmetic shifts.SHL destination,countSAL destination,countSHR destination,countSAR destination,count

Count can be an immediate value or the CX register.

SHL AX,CXSAR DL,1

Page 24: ADD Instruction - LSU 3.pdfADD Instruction ADD destination ... MOV BL,14H MOV AL,47H ADD AL ... Dot commands are available for MASM

Rotate InstructionsThese instructions perform the logical and arithmetic shifts.RCL destination,countROL destination,countRCR destination,countROR destination,count

Count can be an immediate value or the CX register.

ROL EDX,16RCR BH,CL

Page 25: ADD Instruction - LSU 3.pdfADD Instruction ADD destination ... MOV BL,14H MOV AL,47H ADD AL ... Dot commands are available for MASM

Conditional TransfersThese instructions conditionally modify the EIP register to be one of two addresses defined as follows:

An address or displacement following the instruction (label);The address of the instruction following the conditional jump.

Ex:JE SUMSUB EAX,EBX

.

.SUM:

Page 26: ADD Instruction - LSU 3.pdfADD Instruction ADD destination ... MOV BL,14H MOV AL,47H ADD AL ... Dot commands are available for MASM

Conditional TransfersUsed with unsigned integers

JA/JNBE – Jump if above – Z=0 and C=0JAE/JNB – Jump if above or equal – C=0JB/JNA – Jump if below – C=1JBE/JNA – Jump if below or equal – Z=1 and C=1

CMP AL,BLJA NEXTMOV CL,0

.

.NEXT:

Page 27: ADD Instruction - LSU 3.pdfADD Instruction ADD destination ... MOV BL,14H MOV AL,47H ADD AL ... Dot commands are available for MASM

Conditional TransfersUsed with signed integers

JG/JNLE – Jump if greater – Z=0 and S=0JGE/JNL – Jump if greater or equal – S=0JL/JNGE – Jump if less – S<>0JLE/JNG – Jump if less or equal – Z=1 and S<>0

CMP AL,BLJLE NEXTMOV CL,0

.

.NEXT:

Page 28: ADD Instruction - LSU 3.pdfADD Instruction ADD destination ... MOV BL,14H MOV AL,47H ADD AL ... Dot commands are available for MASM

Conditional TransfersOther conditions

JE/JZ – Jump if equal – Z=1JNE/JNZ – Jump if not equal – Z=0JC – Jump if carry - C=1JNC – Jump if not carry – C=0JS – Jump if sign – S=1JNS – Jump if not sign – S=0JO – Jump if overflow – O=1JNO – Jump if not overflow – O=0

Page 29: ADD Instruction - LSU 3.pdfADD Instruction ADD destination ... MOV BL,14H MOV AL,47H ADD AL ... Dot commands are available for MASM

Conditional TransfersJP/JPE – Jump if parity/parity even – P=1JNP/JPO – Jump if not parity/parity odd – P=0JCXZ – Jump if CX is zeroJECXZ – Jump if ECX is zero

Page 30: ADD Instruction - LSU 3.pdfADD Instruction ADD destination ... MOV BL,14H MOV AL,47H ADD AL ... Dot commands are available for MASM

Conditional Set

The 80386 and above processors have conditional set instructions. These instructions set a byte to 01H or 00H depending on the value of the flag or condition under test.Example:

SETZ COUNT_ZERO

Page 31: ADD Instruction - LSU 3.pdfADD Instruction ADD destination ... MOV BL,14H MOV AL,47H ADD AL ... Dot commands are available for MASM

Conditional SetUsed with unsigned integers

SETA – Set if above – Z=0 and C=0SETAE – Set if above or equal – C=0SETB – Set if below – C=1SETBE – Set if below or equal – Z=1 and C=1

Used with signed integersSETG – Set if greater – Z=0 and S=0SETGE – Set if greater or equal – S=0SETL – Set if less – S<>0SETLE – Set if less or equal – Z=1 and S<>0

Page 32: ADD Instruction - LSU 3.pdfADD Instruction ADD destination ... MOV BL,14H MOV AL,47H ADD AL ... Dot commands are available for MASM

Conditional SetOther conditions

SETE/SETZ – Set if equal – Z=1SETNE/SETNZ – Set if not equal – Z=0SETC – Set if carry - C=1SETNC – Set if not carry – C=0SETS – Set if sign – S=1SETNS – Set if not sign – S=0SETO – Set if overflow – O=1SETNO – Set if not overflow – O=0

Page 33: ADD Instruction - LSU 3.pdfADD Instruction ADD destination ... MOV BL,14H MOV AL,47H ADD AL ... Dot commands are available for MASM

Conditional SetSETP/JPE – Set if parity/parity even – P=1SETNP/SETPO – Set if not parity/parity odd – P=0

Page 34: ADD Instruction - LSU 3.pdfADD Instruction ADD destination ... MOV BL,14H MOV AL,47H ADD AL ... Dot commands are available for MASM

Controlling the Flow of the Program Using Dot Commands

Dot commands are available for MASM version 6.xx and above.They do not work with Visual C++ inline assembler.When these directives are found the assembler inserts the appropriate instructions that will perform what the directives indicate.

Page 35: ADD Instruction - LSU 3.pdfADD Instruction ADD destination ... MOV BL,14H MOV AL,47H ADD AL ... Dot commands are available for MASM

Controlling the Flow of the Program Using Dot Commands

Commands:.IF, .ELSE, .ELSEIF, and .ENDIF.WHILE, .BREAK, .CONTINUE and .ENDW.REPEAT, .UNTIL, and .UNTILCXZ

Page 36: ADD Instruction - LSU 3.pdfADD Instruction ADD destination ... MOV BL,14H MOV AL,47H ADD AL ... Dot commands are available for MASM

.IF, .ELSE, .ELSEIF, and .ENDIF

Relational operators used with .IF statements

Operator Function== Equal!= Not equal> Greater than

>= Greater than or equal< Less than

<= Less than or equal& Bit test! Logical inversion

&& Logical AND|| Logical OR| OR

Page 37: ADD Instruction - LSU 3.pdfADD Instruction ADD destination ... MOV BL,14H MOV AL,47H ADD AL ... Dot commands are available for MASM

.IF, .ELSE, .ELSEIF, and .ENDIF

INC BL.IF BL >= 205 || BL<= 2ADD BL,CL.ENDIFMOV DX,1

INC BL.IF BL >= 205 || BL<= 2CMP BL,205JAE @C001CMP BL,2JA @C002@C001:ADD BL,CL.ENDIF@C002:MOV DX,1

Page 38: ADD Instruction - LSU 3.pdfADD Instruction ADD destination ... MOV BL,14H MOV AL,47H ADD AL ... Dot commands are available for MASM

.WHILE, .BREAK, .CONTINUE and

.ENDW

The .BREAK statement allows for unconditional exit from loop. The .BREAK statement may be followed by an .IF statement thus allowing for conditional exit from the loop.The .CONTINUE statement behaves in the reverse way as the .BREAK statement. It is always conditional.

Page 39: ADD Instruction - LSU 3.pdfADD Instruction ADD destination ... MOV BL,14H MOV AL,47H ADD AL ... Dot commands are available for MASM

.WHILE, .BREAK, .CONTINUE and

.ENDW.WHILE BL >= 1MOV BL, DATA[SI]MOV COPY[SI],BLINC SI.ENDWMOV AX,DX

.WHILE BL >= 1JMP @C001@C002:MOV BL, DATA[SI]MOV COPY[SI],BLINC SI.ENDW@C001:CMP BL,1JAE @C002MOV AX,DX

Page 40: ADD Instruction - LSU 3.pdfADD Instruction ADD destination ... MOV BL,14H MOV AL,47H ADD AL ... Dot commands are available for MASM

Procedures

Also known as subroutines, these sets of instructions usually perform a single task.They are reusable code, that can be executed as often as needed by calling it.Procedures save memory, but the calling of a procedure takes a small amount of time.

Page 41: ADD Instruction - LSU 3.pdfADD Instruction ADD destination ... MOV BL,14H MOV AL,47H ADD AL ... Dot commands are available for MASM

ProceduresFormat

Name PROC [NEAR or FAR]Subroutine codeRET

ENDP

Global procedures are defined as FAR.Local procedures are defined as NEAR.

Page 42: ADD Instruction - LSU 3.pdfADD Instruction ADD destination ... MOV BL,14H MOV AL,47H ADD AL ... Dot commands are available for MASM

ProceduresCALL destination

Calls a subroutine at location destination.Different addressing modes may be used for destination.

CALL DELAYCALL EBXCALL ARRAY[BX]

RETReturns execution of program to location stored in stack.NEAR or FAR is dependent on procedure definition.

Page 43: ADD Instruction - LSU 3.pdfADD Instruction ADD destination ... MOV BL,14H MOV AL,47H ADD AL ... Dot commands are available for MASM

InterruptsINT typeINTO – Interrupt if overflowIRETThese instructions modify the EIP register to be the address stored at:

The IDT. The interrupt type or number is used to identify which element of the IDT holds the addresses of the desired interrupt service subroutines;The stack. The address stored in the stack by the INT or INTO instruction. This address identifies the return point after the interrupts execution.

Page 44: ADD Instruction - LSU 3.pdfADD Instruction ADD destination ... MOV BL,14H MOV AL,47H ADD AL ... Dot commands are available for MASM

Miscellaneous Control InstructionsWAIT – Delays the execution of the program conditionally to the BUSY’ pin.HLT – It stops execution of software. There are three way to exit a halt instruction:

Interrupt;Hardware reset;DMA operation.

NOP – No operation.LOCK’ Prefix – Causes LOCK’ pin to become logic 0.

Page 45: ADD Instruction - LSU 3.pdfADD Instruction ADD destination ... MOV BL,14H MOV AL,47H ADD AL ... Dot commands are available for MASM

Miscellaneous Control Instructions

ESC – Passes information to the numerical co-processor.BOUND – Comparison that may generate an interrupt call. The comparison is between the contents of two words or double words, an upper and a lower boundary.ENTER and LEAVE – Allow the creation and use of stack frames.