25
UNIT 2 Assembly Language Programming -By Prof. K. U. Sharma

Unit 2 assembly language programming

Embed Size (px)

DESCRIPTION

Assembly Language Programming - Instruction Set

Citation preview

Page 1: Unit 2   assembly language programming

UNIT 2 – Assembly Language Programming

-By

Prof. K. U. Sharma

Page 2: Unit 2   assembly language programming

Instruction Set of 8086/8088

• Instruction set is classified into

1. Data copy/transfer. Ex: Load, store, mov etc.

2. Arithmetic and Logical. Ex: INC, DEC etc.

3. Branch inst. Ex: Call, jump, interrupt etc.

4. Loop instruction. These inst. Have REP as their prefix and

CX is used as a counter register.

5. Machine Control Inst. Ex: NOP, HLT, Lock etc.

6. Flag Manipulation Inst. Ex: CLI, STI, STD, CLD etc.

7. String Manipulation Inst. EX: Load, compare etc.

Prof. K. U. Sharma, PRMCEAM, Contact: [email protected], 9096996329

4/1/2014

Page 3: Unit 2   assembly language programming

Data Copy/Transfer Inst.

1. Mov Inst:

Used to transfer data from one register/memory location to another.

Source can be any of the segment register or other GPR’s or any

memory location and other register or memory location may act as

destination.

Note: In case of immediate addressing mode, a segment register

cannot be a destination register. We cannot load segment register with

immediate data.

Hence

Mov DS, 5000h; is not permitted.

In mov inst. both the source and destination cannot be memory locations.

Prof. K. U. Sharma, PRMCEAM, Contact: [email protected], 9096996329

4/1/2014

Page 4: Unit 2   assembly language programming

• Push Inst. (Push to Stack)

This inst. pushes the contents of the register/memory location on the

stack.

The stack pointer is decremented by 2 (as it starts with FFFFh), after

each execution of the inst.

The actual current stack top is occupied by the previously pushed data.

The higher byte is pushed first and then the lower byte.

Example: Let SS = 2000h, SP = FFFF

PUSH AX;

Prof. K. U. Sharma, PRMCEAM, Contact: [email protected], 9096996329

4/1/2014

Page 5: Unit 2   assembly language programming

• POP Inst. (Pop from stack)

It loads the specific register/memory location with the contents of the

stack that is calculated by SS:SP.

Here SP gets incremented by 2.

POP is exactly opposite of PUSH inst.

Example: Let SS = 2000h SP = FFFF

POP AX;

Prof. K. U. Sharma, PRMCEAM, Contact: [email protected], 9096996329

4/1/2014

Page 6: Unit 2   assembly language programming

• XCHG Inst.

It exchanges the contents of the specified source and destination

operands which may be register or one of them may be memory

location.

Exchange of contents of two memory locations is not permitted.

Immediate data is also not allowed in the inst.

Example: XCHG [5000h], AX;

Prof. K. U. Sharma, PRMCEAM, Contact: [email protected], 9096996329

4/1/2014

Page 7: Unit 2   assembly language programming

• In Inst. (Input the Port)

Used for reading the input port.

Address of the input port may be specified in the inst. directly or

indirectly.

AL, AX registers are only allowed destination for 8-bit and 16-bit.

DX is the only address allowed to carry the port address.

Example:

IN Al, 03h;

IN AX, DX;

Mov Dx, 0900h;

IN AX, DX;

Prof. K. U. Sharma, PRMCEAM, Contact: [email protected], 9096996329

4/1/2014

Page 8: Unit 2   assembly language programming

• XLAT Inst. (Translate)

Used for finding out the codes in case of code conversion problem using the look up

table technique.

This inst. performs the direct table lookup technique which is often used to convert one

code to another.

An XLAT inst. first adds the contents of AL to BX in order to form a memory address

within the data segment.

Basically XLAT performs ((AL)+(BX)+(DS)0)(AL)

Example:

Assume (DS) = 0300H, (BX)=0100H, and (AL)=0DH

XLAT replaces contents of AL by contents of memory location with

PA=(DS)0 +(BX) +(AL)

= 03000H + 0100H + 0DH = 0310DH

Thus

[0310DH] AL

Prof. K. U. Sharma, PRMCEAM, Contact: [email protected], 9096996329

4/1/2014

Page 9: Unit 2   assembly language programming

• LEA Inst. (Load Effective Address)

This instruction is used to load the offset of the source memory

operand into one of the registers.

Suppose in ALP, one label named KS is used and we want to access the

offset value of this label then the inst. LEA AX, KS; will perform the

given task.

It is not necessary that the destination is always a segment register.

Syntax: LEA D, S;

Prof. K. U. Sharma, PRMCEAM, Contact: [email protected], 9096996329

4/1/2014

Page 10: Unit 2   assembly language programming

• LDS/LES (Load Pointer using DS/ES)

These inst. are used to load two 16-bit registers from a 4-byte memory

block.

The first two bytes are copied into the destination registers and next

two bytes are copied into the corresponding segment register (DS/ES).

These inst. are useful when we want to load segment and offset values by

using a single inst.

Usually these inst. are used in string inst. in order to load source address

(DS:SI) and destination string address (ES:DI) using single inst.

Example: Let KS be the string that points to the where we have the offset and

segment value of source string. Hence our aim is to load DS:SI. And let the

offset value of KS be 2000h and DS = 4000h.

Prof. K. U. Sharma, PRMCEAM, Contact: [email protected], 9096996329

4/1/2014

Page 11: Unit 2   assembly language programming

Program:

Two code-conversion tables starting with offsets TABL1 and TABL2 in the

current data segment are to be accessed. Write an instruction sequence that

initializes the needed registers and then replaces the contents of the

memory locations MEM1 and MEM2 (offsets in the current data segment)

by the equivalent converted codes from the respective codes from the

respective code conversion table.

Note: Whenever a program is asked it requires proper explanation for it. Only

then u expect full marks.

Prof. K. U. Sharma, PRMCEAM, Contact: [email protected], 9096996329

4/1/2014

Page 12: Unit 2   assembly language programming

Answer:

Mov AX, Data_Seg;

Mov DS, AX;

Mov Al, [MEM1];

Mov BX, TABL1;

XLAT;

Mov [MEM1], Al;

Mov Al, [MEM2];

Mov BX, TABL2;

XLAT;

Mov [MEM2], Al;

HLT

Prof. K. U. Sharma, PRMCEAM, Contact: [email protected], 9096996329

4/1/2014

Page 13: Unit 2   assembly language programming

Arithmetic Instructions

• ADD (To add byte or word)

Both source and destination

cannot be memory locations at

the same time.

Contents of segment register

cannot be added. (Applied for

ADD, ADC, SUB, SBB).

Example:

If Ax= 2347h, Bx= 3254h then

what is AX+BX

• ADC (To add byte or word

with carry)

ADC D, S;

Dest = Dest + Source + Carry

Example:

Add 7654AC59 with 24479678.

Mov Ax, AC59;

ADD Ax, 9678;

MOV Bx, 7654;

ADC Bx, 2447;

Prof. K. U. Sharma, PRMCEAM, Contact: [email protected], 9096996329

4/1/2014

Page 14: Unit 2   assembly language programming

• INC (Increment)

• DEC (Decrement)

• SUB

Dest = Dest – Source

If source is greater than destination, then the resulting borrow is indicated by setting the carry flag.

Example:

AX = 1234, BX= 4321.

• SBB (Subtract with Carry)

SBB D,S

Dest = Dest – Source – Carry

Example:

Sub 576829A5 and 267FE542

• MUL (Unsigned Multiplication)

MUL source;

If source 8-bit * Al, then the result is in AX.

If source 16-bit * AX, the result is in DX:AX.

Example: Al = 80h, Bl = 20h

• IMUL (Signed Multiplication)

IMUL source;

Same as Above except that the operands are treated as signed numbers.

Example: Al = 80h, Bl = 20h

Here 80 is treated as 2’s comp of 128 i.e; -128*32 = -4096 whose 2’s comp is F000 which is stored in AX. Prof. K. U. Sharma, PRMCEAM, Contact:

[email protected], 9096996329 4/1/2014

Page 15: Unit 2   assembly language programming

• DIV (Divide word or byte) 8-bit can divide 16-bit value. For

instance AX/8-bit value, then the quotient is found in Al and the remainder is found in AH.

16-bit can divide 32-bit value. For instance DX:AX/16-bit value, then the quotient is found in AX and the remainder is found in DX.

The destination always contains an accumulator which is inbuilt.

Example:

Div Bl; if AX= 2710h and Bl= 32h (use calci)

• IDIV (Signed Division) IDIV source;

In this inst. both the dividend and divisor are considered as signed numbers. So if one operand is negative then result will be –ve.

Rest remains the same.

Example:

IDIV BX;

If AX= 7960h, DX= FFFEh and

BX = 1388h.

• NEG (Negate byte or word) NEG Dest.

Finds the 2’s comp of a number.

Example: Al = 01h;

• CBW/CWD (Convert Byte to word and Word to Double Word) Syntax = CBW/CWD;

These inst are used to extend the contents of a lower storage to higher storage without changing the values.

For instance if we want to divide the value present in AL by BL, if we use IDIV BL then it will divide the contents of AX register.

Hence we need to extend the data present in Al to AX without changing the contents.

Prof. K. U. Sharma, PRMCEAM, Contact: [email protected], 9096996329

4/1/2014

Page 16: Unit 2   assembly language programming

Example:

CBW is performed when AL

= 89h then it generates FF89h

And when CWD is

performed when AX = D456 then

it generates DX:AX = FFFF D456

• DAA (Decimal adjust after

Addition)

Syntax = DAA

Sometimes we want to add two

decimal numbers and assume the

result as decimal. For instance we

want to add 23 and 18, the processor

treats them as hex numbers and gives

the answer as 3B. Which is not we

wanted.

DAA inst does it for us and execution

of DAA gives 41 what we want.

But this inst adjusts only the lower

byte of AX.

DAA inst internally performs the

following functions.

Step 1: Check lower nibble of AL,

If it is greater than 9,

Add 6 to this nibble. AC will be set.

Else

No change. AC will be clear.

Step 2: Check upper nibble of AL,

If it is greater than 9,

Add 6 to upper nibble with AC.

Else

Add upper nibble with AC.

Prof. K. U. Sharma, PRMCEAM, Contact: [email protected], 9096996329

4/1/2014

Page 17: Unit 2   assembly language programming

Example:

Add 2987 and 3565,

Mov Al, 87;

ADD Al, 65;

DAA ;

Mov Bl, Al;

Mov Al, 29;

ADC Al, 35;

DAA;

Mov BH, AL;

• DAS (Decimal Adjust after SUB)

Similar to DAA inst and internally

performs the following steps.

Step 1: Check lower nibble of AL;

If it is greater than 9;

Subtract 6 from this nibble;

Else No Change.

Step 2: Check upper nibble of AL;

If it is greater than 9;

Subtract 6 from this nibble;

Else

No change.

Example:

Mov Al, 10;

SUB Al, 3;

DAS;

Prof. K. U. Sharma, PRMCEAM, Contact: [email protected], 9096996329

4/1/2014

Page 18: Unit 2   assembly language programming

• AAA (ASCII Adjust after Addition) Syntax = AAA

The ASCII values of 0 to 9 are from 30h to 39h.

Addition of two ASCII codes does not give correct results.

For instance if we add 34h (ASCII code for 4) and 38h (ASCII code for 8), the result is 6Ch which is not the correct answer.

The AAA inst works on the lower byte of AX.

The Contents of AH must be ZERO before executing AAA inst.

The steps performed by AAA internally are as follows

Step1: Check lower nibble of AL;

If it is greater than 9;

Add 6 to this nibble and the AC generated is stored into AH.

Else No Change

Step 2: Clear upper nibble of AL.

Example:

Mov AX, 0034;

ADD Al, 38h;

AAA;

In order to get the correct answer we need to add 3030 to the above obtained answer.

• AAS (ASCII Adjust after Subtraction) Same as AAA except that this inst is

applied after subtraction.

The steps performed by AAS are as follows.

Step 1: Check the lower nibble of AL,

If it is greater than 9,

then subtract 6 from this nibble and subtract carry from AH.

Else No change

Prof. K. U. Sharma, PRMCEAM, Contact: [email protected], 9096996329

4/1/2014

Page 19: Unit 2   assembly language programming

Step 2: Clear upper nibble of AL.

Example:

Mov Ax, 0035h;

SUB Al, 39h;

AAS

Answer: FF06h

• AAM (ASCII Adjust after Multiplication) Operands used in MUL should be less

than equal to 9 (unpacked BCD).

Contents of AH must be zero before executing AAM.

The steps that take place internally are that the result of MUL which is in AX for 8-bit mul are divided by 10 i.e; ‘A’ in hex.

The quotient will go in AH and remainder will be in AL. And the final answer is obtained by adding 3030 to the obtained answers.

Example:

Mov AX, 0007;

Mov BL, 08;

MUL BL;

AAM;

Answer: 3536h

• AAD (ASCII Adjust BEFORE Division) This inst is used before the DIV inst

in order to adjust the contents of the AX register according to hex.

Operands must follow unpacked BCD format.

Prof. K. U. Sharma, PRMCEAM, Contact: [email protected], 9096996329

4/1/2014

Page 20: Unit 2   assembly language programming

Problem:

1. Write a sequence of inst that perform the following task and assume that

all the values are 16 bit signed numbers. A = B + C/D.

2. What is the result of executing the

Mov AL, A1h;

CBW;

CWD;

Prof. K. U. Sharma, PRMCEAM, Contact: [email protected], 9096996329

4/1/2014

Page 21: Unit 2   assembly language programming

Answers:

1. Mov AX, C;

Mov BX, D;

CWD;

IDIV BX;

ADD AX, B;

2. AX= FFA1h; and DX= FFFFh;

Prof. K. U. Sharma, PRMCEAM, Contact: [email protected], 9096996329

4/1/2014

Page 22: Unit 2   assembly language programming

Program:

1. Write an inst sequence to evaluate the following expression

A = (B-C+4*D)/E

Where A,B,C,D & E are all byte sized operands.

2. Write an inst sequence that show two different ways to increment and

decrement the address pointer in SI by 2.

3. Write an inst sequence that will add the immediate value 111F and the

carry flag to the contents of the data register DX.

Prof. K. U. Sharma, PRMCEAM, Contact: [email protected], 9096996329

4/1/2014

Page 23: Unit 2   assembly language programming

Answers:

1. Mov AL,D

Mov BL,4

MUL BL ; 4*D AL

Mov BL,B

SUB BL,C ; B-C BL

ADD AL,BL ; B-C+4*D

Mov BL,E

DIV BL ; (B-C+4*D)/E

Mov A,AL ; Result in A

2. Increment:

ADD SI, 2

&

INC SI

INC SI

3. ADC DX, 111F.

Prof. K. U. Sharma, PRMCEAM, Contact: [email protected], 9096996329

4/1/2014

Page 24: Unit 2   assembly language programming

Program:

1. Two byte sized BCD integers are stored at symbolic offset addresses

NUM1 and NUM 2 respectively. Write an instruction sequence to generate

their difference and store it at NUM 3. The difference is to be formed by

subtracting the value at NUM 1 and NUM 2. Assume that all storage

locations are in the current data segment.

2. Assuming that [AL] = 10H; [BX] = 0100H and [DS] = 1000H. What

happens if the XLAT inst is executed.

Prof. K. U. Sharma, PRMCEAM, Contact: [email protected], 9096996329

4/1/2014

Page 25: Unit 2   assembly language programming

Answers:

Mov AX, Data_Seg ; Establishing the DS

Mov DS, DX ;

Mov AL, [NUM 2] ; Get the second BCD number

SUB AL, [NUM 1] ; Subtract NUM 2 – NUM 1

DAS ; For getting the BCD answer

Mov [NUM 3], AL ; Result in NUM 3

HLT

=======================================================

XLAT ((AL) + (BX) + (DS)0) AL

Hence the value stored at location 10110H will be moved into AL register.

Prof. K. U. Sharma, PRMCEAM, Contact: [email protected], 9096996329

4/1/2014