Types of instructions

49
Types of Instructions

description

Types of instruction inside CPU.

Transcript of Types of instructions

Page 1: Types of instructions

Types of Instructions

Page 2: Types of instructions

Types of Instructions• Different assembly language instructions are

mainly categories into the following main types:

1.Data transfer instructions

2.Arithmetic instructions

3.Logical and program control instructions

Page 3: Types of instructions

1)Data trtansfer instruction:

• These instructions are responsible of transfer of data among operands.

• i) MOV instruction: copy data from one operand to another. general Syntax :- MOV destination ,source

Page 4: Types of instructions

i)Mov instruction (conti..)• Data is copies from source to destination in

such a way that source operand is not changed.

• The source operand may be immediate data(constant) a register

or a memory operand

Page 5: Types of instructions

• The following data transfers are possible: register to register register to memory memory to register

• limitations on types of operands:i. CS and IP may never be destination.

ii. immediate data and segment registers may not be moved to segment registers.

Page 6: Types of instructions

iii. The source and destination operand must be of same size.

iv. If the source operand is immediate data, it must not exceed the destination operand size.

255 (FFh) for a 8-bit destination or 65’535 (FFFFh) for 16-bit destination

Page 7: Types of instructions

• MOV doesn't allow memory to memory transfer.

• data must be transfer to a register first.

• e.g to copy var1 to var2, we have to write: mov ax, var1 mov var2, ax

Page 8: Types of instructions

Types of operands:

i) Register operand: A mov that involves only registers is the fastest

type that take only two clock cycles. clock cycles:-o CPU’s smallest unit of time measurement o It varies from 50 to 210 nano secondso it depends upon type of CPU processor.

Page 9: Types of instructions

• Register should be used when an instruction must execute quickly.

• Any register may be used as a source operand.

• Any register, except CS and IP may be a destination operand.

Page 10: Types of instructions

Examples:

• Mov ax, bx

• Mov dl, al

• Mov bx ,cs

Page 11: Types of instructions

ii) Immediate operands:

• An immediate value i.e. Integer constant may be moved to any register

( except a segment register or IP).

• And may also be moved to any memory operand (variable).

Page 12: Types of instructions

• In immediate value can not be larger then the destination operand.

• Examples: mov bl , 01 mov ax, 1000h mov total ,1000h

Page 13: Types of instructions

iii)Memory operand:

• a variable may be coded as one of the operands in a mov instruction.

• This causes the contents of memory at the variable’s address to be used.

Page 14: Types of instructions

• If an 8-bit variable name “count” contains the number ‘1’ ,

• then the following MOV will copy the contents of count into AL:

Mov al, count

Page 15: Types of instructions

01 01

In similar way ,BL register’s contents may be copied into the variable count as: Mov al, count AL Count

Page 16: Types of instructions

mov count, bl

• BL Count

01 01

Page 17: Types of instructions

Some illegal Mov operations are:1. Moves between two memory operands.

2. Moves of segment registers or immediate values to segment registers.

3. Moves to CS and IP.

4. Moves between register of different sizes.

5. Moves to immediate operands (constant).

Page 18: Types of instructions

iii)Loading instructions :-

• specific register instructions.• used to load bytes or chains of bytes onto a

register.a) LEA INSTRUCTION (Load Effective Address): • Purpose: To load the address of the source

operator.• Syntax: LEA destination, source

Page 19: Types of instructions

LEA instruction (conti..)

• The illustration of one of the facilities we have with this command :

MOV SI, OFFSET VAR1Is equivalent to: LEA SI, VAR1

• MOV......OFFSET instruction is more efficient because the offset of operand is calculated by MASM at assembly time.

Page 20: Types of instructions

LEA instruction (conti..)

• LEA calculate offset of operand at runtime.• Slows the program down.

• but LEA instruction may be used effectively with more advanced addressing modes such as “BASED MODE”.

• It is very probable that for the programmer it is much easier to create extensive programs by using LEA instruction.

Page 21: Types of instructions

iii)XCHG instruction

• The xchg (exchange) instruction swaps two values.

• This instruction is used to exchange the contents of two registers or of a register and a variable .

• The general form is xchg operand1 operand2

Page 22: Types of instructions

EXCHG instruction (conti..)

DIFFERENT FORMS OF REGISTER AND MEMORY OPERANDS ARE:-

• Xchg reg , reg• Xchg reg , mem • Xchg ax , reg16• Xchg eax, reg32 ( Available only on 80386 and later processors)

Page 23: Types of instructions

EXCHG instruction (conti..)

• Provides the most efficient way to exchange two 8-bit ,16-bit OR 32-bit operands.

• Because we don’t need a third register or variable to hold a temporary value.

• Quit suitable in sorting applications where it provides an advantage of speed.

Page 24: Types of instructions

• EXCHG instruction (conti..)

• One or both operand may be register, OR • a register may be combined with a memory

operand ,

• But two memory operands may not be used together .

Page 25: Types of instructions

EXCHG instruction (conti..)

MOV AL, VAL1 ; LOAD THE AL REGISTER

XCHG VAL2 ,AL

; EXCHANGE THE AL AND VAL2

MOV VAL1 , AL ; STORE AL BACK INTO VAL1

Page 26: Types of instructions

14

14

14 14

00

00

00

0A

0A

0A

0A

AX

AX

val2

val1

val2

Val1

AXval1

val2

MEMORYEXCHG instruction (conti..)

Page 27: Types of instructions

EXCHG instruction (conti..)

Some valid example:• XCHNG EAX, EBX (;exchange two 32-bit registers)

• XCHNG AX, BX (;exchange two 16-bit registers)

Page 28: Types of instructions

EXCHG instruction (conti..)

• XCHNG ah , al ; exchange two 8-bit registers

• XCHG var , bx ; exchange 16-bit memory operand with

BX

Page 29: Types of instructions

EXCHG instruction (conti..)

• Note that the order of the xchg's operands does not matter.

• Both operands must be the same size.

• The xchg instruction does not modify any flags

Page 30: Types of instructions

.MODEL SMALL

.STACK 100H

.DATAVAL1 DB 0AHVAL2 DB 14H

.CODEMAIN PROC MOV AX, @DATA ;INITIALIZE DS REGISTER MOV DS,AX MOV AL, VAL1 ; LOAD THE AL REGISTER XCHG VAL2 ,AL ; EXCHANGE THE AL AND VAL2 MOV VAL1 , AL ; STORE AL BACK INTO VAL1 MOV AX,4C00H INT 21HMAIN ENDPEND MAIN

Page 31: Types of instructions

2)Arithematic Instructions

• The Intel instruction set has instructions for integer arithmetic using 8,16 or 32-bits operands.

• The basic arithematic instructions are given below:

i. INC and DEC instructionsii. ADD instructionsiii. SUB instructionsiv. MUL and IMUL instructions

Page 32: Types of instructions

i. INC and DEC instructions

• These INC and DEC are actually unary operators and their operands may be either 8,16, 32-bits register or memory variables.

• INC is used to add 1 to the value of a single operand.

• DEC is used to subtract 1 from the value of a single operand.

Page 33: Types of instructions

• These instructions have the following general form:

INC destination DEC destination

• These instructions are faster than ADD and SUB instructions

INC and DEC instructions conti..

Page 34: Types of instructions

ii) ADD instruction:

• This instruction adds 8 ,16 or 32-bits source operand to a destination operand of the same size.

• The syntax of this instruction is: ADD destination, source

Page 35: Types of instructions

ADD instruction conti..The source operand is unchanged by the

operation .• Source operand may be a register, memory operand or immediate operand • whereas destination may be a register or memory operand.

Page 36: Types of instructions

ADD instruction conti..

• A segment register may not be the destination and only one memory operand may be used.

• The size of source and destination operand must be the same, in this binary instruction, Add is a binary operator.

Page 37: Types of instructions

iii)Sub Instruction:

• This instruction subtracts a source operand from a destination operand.

• The syntax of this instruction is: SUB destination, source

Page 38: Types of instructions

SUB instruction conti..

• The sizes of both operands must match and only one may be the memory operand.

• A segment register may not be the Destination operand.

• All status flags are affected by this binary instructing SUB is a binary operand.

Page 39: Types of instructions

iv)MUL and IMUL instructions:• These instructions are used to multiply an 8-

bit or 16-bit operand by AL or AX.

• If an 8-bit source operand is supplied ,if will automatically multiplied and the result will be stored in AX.

Page 40: Types of instructions

MUL and IMUL instructions conti..

• If a 16-bit operand is source operand is supplied it will be automatically multiplied by AX and the result will be stored in DX and AX so that the higher bits are in DX.

Page 41: Types of instructions

• The syntax of these instructions are: MUL source IMUL source• The source operand may be a register or

memory operand.

mul reg mul mem

• but may not be an immediate data.

Page 42: Types of instructions

• The IMUL (integer multiply) instruction multiplies signed binary values.

• Its sign extends the result through highest bit of AX or AX:DX ,depending on size of the source operand.

Page 43: Types of instructions

• A result of -10h ,for example ,would be extended into AH if an 8-bits operation had taken place or into DX for accomplishing a 16-bit operation.

Page 44: Types of instructions

• DIV INSTRUCTION Purpose: Division without sign.• Syntax: DIV source

• The divider can be a byte or a word and it is the operator which is given the instruction.

• If the divider is 8 bits, the 16 bits AX register is taken as dividend and

Page 45: Types of instructions

• if the divider is 16 bits the even DX:AX register will be taken as dividend, taking the DX high word and AX as the low.

• If the divider was a byte then the quotient will be stored on the AL register and the residue on AH, if it was a word then the quotient is stored on AX and the residue on DX.

Page 46: Types of instructions

IDIV INSTRUCTION• Purpose: Division with sign.• Syntax:• IDIV source

• It basically consists on the same as the DIV instruction,

• the only difference is that this one performs the operation with sign.

• For its results it used the same registers as the DIV instruction.

Page 47: Types of instructions

2)LOGICAL INSTRUCTIONS:• generates their result either as true or false.

• Zero ,Carry and Sign flags are particularly associated with showing the results of Boolean and comparison instructions.

• Boolean instructions are based on boolean algebra operations .

• these operations allow modification of individual bits in binary numbers.

Page 48: Types of instructions

AND results into 1 when both the input bit are 1.

OR results into 1 when either input bit is 1.

XOR results into 1 when the input bits are different and is known as exclusive OR.

NOT results into the reverse of input bit i.e. 1 becomes 0 and 0 becomes 1.

Page 49: Types of instructions

i. The AND instructionii. The OR instructioniii.The XOR instructioniv.The NOT instructionv. The NEG instruction