24
LAB 1: BASIC ADDITION & SUBTRACTION QUESTION 1: Write a program to add 2 eight bit numbers and store the sum and carry. ALGORITHM: Initialize the Data Segment. Store 2 eight bit numbers in locations 4000 and 4001. Read the first number in AL register and the second number in BL register. Add the 2 values. Store the sum in location 5000. If carry is produced, then store 01 in location 5001, else store 00 in that location. PROGRAM: Addr. Opcode Operand Mnemonic Comment 2000 BE 00 40 MOV SI, 4000 Loading location 4000 to Source Index 2003 BF 00 50 MOV DI, 5000 Loading location 5000 to Destination Index 2006 B8 00 00 MOV AX, 0000 Loading the value 0000 to AX register 2009 8E D8 MOV DS, AX Initializing Data Segment using AX register 200B 8A 04 MOV AL, [SI] Getting the 1 st value to AL register 200D 46 INC SI Incrementing the Source Index 200E 8A 1C MOV BL, [SI] Getting the 2 nd value to BL register 2010 02 C3 ADD AL, BL Adding & Storing the result in AL register 2012 72 08 JC 201C Jump to 201C if carry is generated 2014 B1 00 MOV CL, 00 Loading 00 to CL register 2016 88 05 MOV [DI], AL Storing the Sum in location 5000 2018 47 INC DI Incrementing the Destination Index 2019 88 05 MOV [DI], CL Storing the Carry in location 5001 201B CC INT 03 End of the Program 201C B1 01 MOV CL, 01 Loading 01 to CL register 201E EB F6 JMP 2016 Unconditional Jump to 2016 OUTPUT: Location Value 4000 A 4001 7 5000 1 5001 1

Av222 lab 1 st part

Embed Size (px)

Citation preview

Page 1: Av222 lab 1 st part

LAB 1: BASIC ADDITION & SUBTRACTION

QUESTION 1:

Write a program to add 2 eight bit numbers and store the sum and carry.

ALGORITHM:

Initialize the Data Segment.

Store 2 eight bit numbers in locations 4000 and 4001.

Read the first number in AL register and the second number in BL register.

Add the 2 values.

Store the sum in location 5000.

If carry is produced, then store 01 in location 5001, else store 00 in that location.

PROGRAM:

Addr. Opcode Operand Mnemonic Comment

2000 BE 00 40 MOV SI, 4000 Loading location 4000 to Source Index

2003 BF 00 50 MOV DI, 5000 Loading location 5000 to Destination Index

2006 B8 00 00 MOV AX, 0000 Loading the value 0000 to AX register

2009 8E D8 MOV DS, AX Initializing Data Segment using AX register

200B 8A 04 MOV AL, [SI] Getting the 1st value to AL register

200D 46 INC SI Incrementing the Source Index

200E 8A 1C MOV BL, [SI] Getting the 2nd value to BL register

2010 02 C3 ADD AL, BL Adding & Storing the result in AL register

2012 72 08 JC 201C Jump to 201C if carry is generated

2014 B1 00 MOV CL, 00 Loading 00 to CL register

2016 88 05 MOV [DI], AL Storing the Sum in location 5000

2018 47 INC DI Incrementing the Destination Index

2019 88 05 MOV [DI], CL Storing the Carry in location 5001

201B CC INT 03 End of the Program

201C B1 01 MOV CL, 01 Loading 01 to CL register

201E EB F6 JMP 2016 Unconditional Jump to 2016

OUTPUT:

Location Value

4000 A

4001 7

5000 1

5001 1

Page 2: Av222 lab 1 st part

QUESTION 2:

Write a program to add 2 sixteen bit numbers using 8 – bit addition and store the sum and

carry.

ALGORITHM:

Initialize the Data Segment.

Store 2 sixteen bit numbers in locations 4000 – 4003.

Read the first number in AX register and the second number in BX register.

Add the 2 values using 8 – bit addition. (Hint: use ADC instruction)

Store the sum in location 5000 and 5001.

If carry is produced, then store 01 in location 5002, else store 00 in that location.

PROGRAM:

Addr. Opcode Operand Mnemonic Comment

2000 BE 00 40 MOV SI, 4000 Loading location 4000 to Source Index

2003 BF 00 50 MOV DI, 5000 Loading location 5000 to Destination Index

2006 B8 00 00 MOV AX, 0000 Loading the value 0000 to AX register

2009 8E D8 MOV DS, AX Initializing Data Segment using AX register

200B 8A 04 MOV AL, [SI] Getting the 1st value (1st 8-bit) to AL register

200D 46 INC SI Incrementing the Source Index

200E 8A 1C MOV BL, [SI] Getting the 2nd value (1st 8-bit) to BL register

2010 46 INC SI Incrementing the Source Index

2011 8A 24 MOV AH, [SI] Getting the 1st value (2nd 8-bit) to AH register

2013 46 INC SI Incrementing the Source Index

2014 8A 3C MOV BH, [SI] Getting the 2nd value (2nd 8-bit) to BH register

2016 02 C3 ADD AL, BL Adding the values of AL, BL registers

2018 12 E7 ADC AH, BH Add with Carry the values of AH, BH registers

201A 72 0B JC 2027 Jump to 2027 if carry is generated

201C B1 00 MOV CL, 00 Loading 00 to CL register

201E 88 25 MOV [DI], AH Storing the Sum (2nd 8-bit) in location 5001

2020 47 INC DI Incrementing the Destination Index

2021 88 05 MOV [DI], AL Storing the Sum (1st 8-bit) in location 5001

2023 47 INC DI Incrementing the Destination Index

2024 88 0D MOV [DI], CL Storing the Carry in location 5002

2026 CC INT 03 End of the Program

2027 B1 01 MOV CL, 01 Loading 01 to CL register

2029 EB F3 JMP 201E Unconditional Jump to 201E

Page 3: Av222 lab 1 st part

OUTPUT:

Location Value

4000 D4

4002 C8

5000 9

5001 C

5002 1

QUESTION 3:

Write a program to add 2 sixteen bit numbers using 16 – bit addition and store the sum

and carry.

ALGORITHM:

Initialize the Data Segment.

Store 2 sixteen bit numbers in locations 4000 – 4003.

Read the first number in AX register and the second number in BX register.

Add the 2 values using 16 – bit addition.

Store the sum in location 5000 and 5001.

If carry is produced, then store 01 in location 5002, else store 00 in that location.

PROGRAM:

Addr. Opcode Operand Mnemonic Comment

2000 BE 00 40 MOV SI, 4000 Loading location 4000 to Source Index

2003 BF 00 50 MOV DI, 5000 Loading location 5000 to Destination Index

2006 B8 00 00 MOV AX, 0000 Loading the value 0000 to AX register

2009 8E D8 MOV DS, AX Initializing Data Segment using AX register

200B 8B 04 MOV AX, [SI] Getting the 1st value to AX register

200D 46 INC SI Incrementing the Source Index

200E 46 INC SI Incrementing the Source Index

200F 8B 1C MOV BX, [SI] Getting the 2nd value to BX register

2011 03 C3 ADD AX, BX Adding & Storing the result in AX register

2013 72 09 JC 201E Jump to 201E if carry is generated

2015 B1 00 MOV CL, 00 Loading 00 to CL register 2017 89 05 MOV [DI], AX Storing the Sum in location 5000

2019 47 INC DI Incrementing the Destination Index

201A 47 INC DI Incrementing the Destination Index

201B 88 0D MOV [DI], CL Storing the Carry in location 5002

201D CC INT 03 End of the Program

201E B1 01 MOV CL, 01 Loading 01 to CL register

2020 EB F5 JMP 2017 Unconditional Jump to 2017

Page 4: Av222 lab 1 st part

OUTPUT:

Location Value

4000 D2

4002 2D

5000 FF

5002 00

QUESTION 4:

Write a program to subtract 2, 16 bit numbers using 8 – bit SUB and store the diff. and

borrow.

ALGORITHM:

Initialize the Data Segment.

Store 2 sixteen bit numbers in locations 4000 – 4003.

Read the first number in AX register and the second number in BX register.

Add the 2 values using 8 – bit subtraction. (Hint: use SBB instruction)

Store the difference in location 5000 and 5001.

If borrow is generated, then store 01 in location 5002, else store 00 in that location.

PROGRAM:

Addr. Opcode Operand Mnemonic Comment

2000 BE 00 40 MOV SI, 4000 Loading location 4000 to Source Index

2003 BF 00 50 MOV DI, 5000 Loading location 5000 to Destination Index

2006 B8 00 00 MOV AX, 0000 Loading the value 0000 to AX register

2009 8E D8 MOV DS, AX Initializing Data Segment using AX register

200B 8A 04 MOV AL, [SI] Getting the 1st value (1st 8-bit) to AL register

200D 46 INC SI Incrementing the Source Index

200E 8A 1C MOV BL, [SI] Getting the 2nd value (1st 8-bit) to BL register

2010 46 INC SI Incrementing the Source Index

2011 8A 24 MOV AH, [SI] Getting the 1st value (2nd 8-bit) to AH register

2013 46 INC SI Incrementing the Source Index

2014 8A 3C MOV BH, [SI] Getting the 2nd value (2nd 8-bit) to BH register

2016 2A C3 SUB AL, BL Subtracting the values of AL, BL registers

2018 1A E7 SBB AH, BH Subtract with Borrow the values of AH, BH

201A 72 0B JC 2027 Jump to 2027 if carry is generated

201C B1 00 MOV CL, 00 Loading 00 to CL register

201E 88 25 MOV [DI], AH Storing the Diff (2nd 8-bit) in location 5001

2020 47 INC DI Incrementing the Destination Index

2021 88 05 MOV [DI], AL Storing the Diff (1st 8-bit) in location 5001

2023 47 INC DI Incrementing the Destination Index

2024 88 0D MOV [DI], CL Storing the Borrow in location 5002

2026 CC INT 03 End of the Program

2027 B1 01 MOV CL, 01 Loading 01 to CL register

Page 5: Av222 lab 1 st part

2029 EB F3 JMP 201E Unconditional Jump to 201E

OUTPUT:

Location Value

4000 84

4002 42

5000 4

5001 2

5002 0

QUESTION 5:

Write a program to subtract 2 sixteen bit numbers using 16 – bit subtraction and store the

difference and borrow.

ALGORITHM:

Initialize the Data Segment.

Store 2 sixteen bit numbers in locations 4000 – 4003.

Read the first number in AX register and the second number in BX register.

Subtract the 2 values using 16 – bit subtraction.

Store the difference in location 5000 and 5001.

If borrow is generated, then store 01 in location 5002, else store 00 in that location.

PROGRAM:

Addr. Opcode Operand Mnemonic Comment

2000 BE 00 40 MOV SI, 4000 Loading location 4000 to Source Index

2003 BF 00 50 MOV DI, 5000 Loading location 5000 to Destination Index

2006 B8 00 00 MOV AX, 0000 Loading the value 0000 to AX register

2009 8E D8 MOV DS, AX Initializing Data Segment using AX register

200B 8B 04 MOV AX, [SI] Getting the 1st value to AX register

200D 46 INC SI Incrementing the Source Index

200E 46 INC SI Incrementing the Source Index

200F 8B 1C MOV BX, [SI] Getting the 2nd value to BX register

2011 2A C3 SUB AX, BX Subtracting & Storing the result in AX register

2013 72 09 JC 201E Jump to 201E if carry is generated

2015 B1 00 MOV CL, 00 Loading 00 to CL register 2017 89 05 MOV [DI], AX Storing the Sum in location 5000

2019 47 INC DI Incrementing the Destination Index

201A 47 INC DI Incrementing the Destination Index

201B 88 0D MOV [DI], CL Storing the Carry in location 5002

201D CC INT 03 End of the Program

201E B1 01 MOV CL, 01 Loading 01 to CL register

2020 EB F5 JMP 2017 Unconditional Jump to 2017

Page 6: Av222 lab 1 st part

OUTPUT:

Location Value

4000 4D

4002 D4

5000 19

5002 1

Page 7: Av222 lab 1 st part

LAB 2: BASIC MULTIPLICATION & DIVISION

QUESTION 1:

Write an ALP IN 8086 to multiply two 16-bit numbers and store the result.

ALGORITHM:

Initialize the Data Segment.

Store 16-bit numbers in locations 4000 - 4003.

Read the first number in AX register and the second number in BX register.

Multiply the 2 values using ‘MUL’ instruction.

Store the product in location 5000 and Carry in 5002.

PROGRAM:

Addr. Opcode Operand Mnemonic Comment

2000 BE 00 40 MOV SI, 4000 Loading location 4000 to Source Index

2003 BF 00 50 MOV DI, 5000 Loading location 5000 to Destination Index

2006 B8 00 00 MOV AX, 0000 Loading the value 0000 to AX register

2009 8E D8 MOV DS, AX Initializing Data Segment using AX register

200B 8B 04 MOV AX, [SI] Getting the 1st value to AX register

200D 46 INC SI Incrementing the Source Index

200E 46 INC SI Incrementing the Source Index

200F 8B 1C MOV BX, [SI] Getting the 2nd value to BX register

2011 F7 E3 MUL BX Multiplying AX with BX, storing Quot. in AX

2013 89 15 MOV [DI], DX Storing the the high word in the location

2015 47 INC DI Incrementing the Destination Index

2016 47 INC DI Incrementing the Destination Index

2017 89 05 MOV [DI], AX Moving Carry to 5002

2019 CC INT 03 End of the Program

OUTPUT:

Location Value

4000 04

4002 04

5000 10

5002 00

QUESTION 2:

Write an ALP IN 8086 to divide a 16-bit number with an 8-bit number and store the result.

Page 8: Av222 lab 1 st part

ALGORITHM:

Initialize the Data Segment.

Store the 16-bit number in location 4000 and the 8-bit number in location 4002.

Read the first number in AX register and the second number in BL register.

Divide the 2 values using ‘DIV’ instruction.

Store the quotient in location 5000 and remainder in 5002.

PROGRAM:

Addr. Opcode Operand Mnemonic Comment

2000 BE 00 40 MOV SI, 4000 Loading location 4000 to Source Index

2003 BF 00 50 MOV DI, 5000 Loading location 5000 to Destination Index

2006 B8 00 00 MOV AX, 0000 Loading the value 0000 to AX register

2009 8E D8 MOV DS, AX Initializing Data Segment using AX register

200B 8B 04 MOV AX, [SI] Getting the 1st value to AX register

200D 46 INC SI Incrementing the Source Index

200E 46 INC SI Incrementing the Source Index

200F 8A 1C MOV BL, [SI] Getting the 2nd value to BL register

2011 F6 E3 DIV BL Dividing AX by BL and Quot. in AL register

2013 88 05 MOV [DI], AL Storing the Quotient in location 5000

2015 47 INC DI Incrementing the Destination Index

2016 88 25 MOV [DI], AH Storing the Remainder in location 5001

2018 CC INT 03 End of the Program

OUTPUT:

Location Value

4000 10

4002 07

5000 02

5002 02

QUESTION 3:

Write an ALP IN 8086 to find Factorial of a number.

ALGORITHM:

Initialize the Data Segment.

Store the number (8-bit) for which factorial is required in location 4000.

Read the number in AL register.

Recursively decrease and multiply to get the Factorial.

Store the result in location 5000.

Page 9: Av222 lab 1 st part

PROGRAM:

Addr. Opcode Operand Mnemonic Comment

2000 BE 00 40 MOV SI, 4000 Loading location 4000 to Source Index

2003 BF 00 50 MOV DI, 5000 Loading location 5000 to Destination Index

2006 B8 00 00 MOV AX, 0000 Loading the value 0000 to AX register

2009 8E D8 MOV DS, AX Initializing Data Segment using AX register

200B 8A 04 MOV AL, [SI] Getting the 1st value to AL register

200D 46 INC SI Incrementing the Source Index

200E 8A 1C MOV BL, [SI] Getting the 2nd value to BL register 2010 F6 E3 MUL BL Multiply value of AL with BL register.

2012 FE CB DEC BL Decrementing the value stored in BL register

2014 74 01 JZ 2018 Jump if Zero to 2018

2016 EB F8 JMP 2010 Unconditional JMP to 2010 2018 88 05 MOV [DI], AL Moving the Factorial to 5000

201A CC INT 03 End of the Program

OUTPUT:

Location Value

4000 01

4001 05

5000 78

QUESTION 4:

Write an ALP IN 8086 to find LCM of 2 Numbers.

ALGORITHM:

Initialize the Data Segment.

Store 16-bit numbers in locations 4000 - 4003.

Read the first number in AX register and the second number in BX register.

Use Euclid’s lemma concept of finding HCF.

Then use the property of HCF and LCM, viz, HCF x LCM = Product of the numbers.

Store the LCM in location 5000.

Page 10: Av222 lab 1 st part

PROGRAM:

Addr. Opcode Operand Mnemonic Comment

2000 BE 00 40 MOV SI, 4000 Loading location 4000 to Source Index

2003 BF 00 50 MOV DI, 5000 Loading location 5000 to Destination Index

2006 B8 00 00 MOV AX, 0000 Loading the value 0000 to AX register

2009 8E D8 MOV DS, AX Initializing Data Segment using AX register

200B 8A 04 MOV AL, [SI] Getting the 1st value to AL register

200D 8A E8 MOV CH, AL Moving the value of AL register to CH

200F 46 INC SI Incrementing the Source Index

2010 8A 1C MOV BL, [SI] Getting the 2nd value to AL register

2012 8A D3 MOV DL, BL Moving the value of BL register to DL 2014 B4 00 MOV AH, 00 Loading 00 to AH register

2016 F6 F3 DIV BL Dividing value of AX register by BL

2018 8A C3 MOV AL, BL Moving the value of BL register to AL

201A 8A DC MOV BL, AH Moving the value of AH register to BL

201C 80 FC 00 CMP AH, 00 Comparing the value of AH register with 00

201F 75 F3 JNE 2014 Jump if not zero to LABEL 1

2021 8A C8 MOV CL, AL Moving the value of AL register to CL

2023 8A C5 MOV AL, CH Moving the value of CH register to AL

2025 F6 E2 MUL DL Multiplying Value of DL with AL register

2027 F6 F1 DIV CL Multiplying Value of CL with AL register

2029 88 05 MOV [DI], AL Moving the value of AL to 5000

202B CC INT 03 End of the Program

OUTPUT:

Location Value

4000 03

4001 05

5000 0F

QUESTION 5:

Write an ALP IN 8086 to multiply 3 numbers and store the result.

ALGORITHM:

Initialize the Data Segment.

Store 16-bit numbers in locations 4000 - 4005.

Read the first number in AX register and the second number in BX register.

Multiply them.

Store Product and Carry in BX and CX registers respectively.

Multiply and Add the respective positions with the third number

Store the Product in location 5000.

Page 11: Av222 lab 1 st part

PROGRAM:

Addr. Opcode Operand Mnemonic Comment

2000 BE 00 40 MOV SI, 4000 Loading location 4000 to Source Index

2003 BF 00 50 MOV DI, 5000 Loading location 5000 to Destination Index

2006 B8 00 00 MOV AX, 0000 Loading the value 0000 to AX register

2009 8E D8 MOV DS, AX Initializing Data Segment using AX register

200B 8B 04 MOV AX, [SI] Getting the 1st value to AX register

200D 46 INC SI Incrementing the Source Index

200E 46 INC SI Incrementing the Source Index

200F 8B 1C MOV BX, [SI] Getting the 2nd value to BX register

2011 46 INC SI Incrementing the Source Index

2012 46 INC SI Incrementing the Source Index

2013 8B 0C MOV CX, [SI] Getting the 3rd value to CX register

2015 F7 E3 MUL BX Multiplying value of AX register with BX

2017 8B DA MOV BX, DX Moving the value of DX register to BX

2019 F7 E1 MUL CX Multiplying value of AX register with CX

201B 89 05 MOV [DI], AX Moving value of AX register to location 5000

201D 8B C1 MOV AX, CX Moving the value of CX register to AX

201F 8B CA MOV CX, DX Moving the value of DX register to CX

2021 F7 E3 MUL BX Multiplying value of AX register with BX

2023 03 C1 ADD AX, CX Adding values of AX, CX registers

2025 47 INC DI Incrementing the Destination Index

2026 47 INC DI Incrementing the Destination Index

2027 89 05 MOV [DI], AX Moving value of AX register to location 5002

2029 47 INC DI Incrementing the Destination Index

202A 47 INC DI Incrementing the Destination Index

202B 89 15 MOV [DI], DX Moving value of DX register to location 5004

202D CC INT 03 End of the Program

OUTPUT:

Location Value

4000 02

4002 03

4003 04

5000 06

5002 18

5004 00

Page 12: Av222 lab 1 st part

LAB 3: ARRAYS

QUESTION 1:

Write an ALP IN 8086 to find the largest number from a given array.

ALGORITHM:

Initialize the Data Segment.

Store 8-bit numbers in locations beginning from 4000.

Read the first number in AL register and the second number in BL register.

Perform swapping of the two numbers in such a way that the larger number comes

1st.

Repeat the above procedure equal to the number of numbers which are compared.

PROGRAM:

Addr Opcode Operand Mnemonic Comment

2000 BE 00 40 MOV SI, 4000 Loading location 4000 to Source Index

2003 BF 00 50 MOV DI, 5000 Loading location 5000 to Destination Index

2006 B8 00 00 MOV AX, 0000 Loading the value 0000 to AX register

2009 8E D8 MOV DS, AX Initializing Data Segment using AX register

200B 8A 0C MOV CL, [SI] Getting the no. of numbers to CL register

200D 46 INC SI Incrementing the Source Index

200E 8A 04 MOV AL, [SI] Getting the 1st value to AL register 2010 46 INC SI Incrementing the Source Index

2011 8A 1C MOV BL, [SI] Getting the 2nd value to BL register

2013 FE C9 DEC CL Decrementing the value of CL register

2015 74 04 JE 201B Jump if Zero to 201B

2017 3A C3 CMP AL, BL Comparing values of AL, BL registers

2019 72 03 JB 201E Jump if Borrow is generated to 201E 201B 88 05 MOV [DI], AL Moving the value of AL register to 5000

201D CC INT 03 End of the Program 201E 8A C3 MOV AL, BL Moving the value of BL register to AL

2020 EB EE JMP 2010 Unconditional to 2010

OUTPUT:

Location Value

4000 04

4001 60

4002 55

4003 17

4004 89

4005 36

4006 76

Page 13: Av222 lab 1 st part

4007 45

4008 04

4009 10

5000 89

QUESTION 2:

Write an ALP IN 8086 to find the 2nd largest number from a given array.

ALGORITHM:

Initialize the Data Segment.

Store 8-bit numbers in locations beginning from 4000.

Read the first number in AL register and the second number in BL register.

Perform swapping of the two numbers in such a way that the larger number comes

1st.

Repeat the above procedure equal to the number of numbers which are compared.

PROGRAM:

Addr. Opcode Operand Mnemonic Comment

2000 BE 00 40 MOV SI, 4000 Loading location 4000 to Source Index

2003 BF 00 50 MOV DI, 5000 Loading location 5000 to Destination Index

2006 B8 00 00 MOV AX, 0000 Loading the value 0000 to AX register

2009 8E D8 MOV DS, AX Initializing Data Segment using AX register

200B 8B 0C MOV CX, [SI] Getting the no. of numbers to CX register

200D 46 INC SI Incrementing the Source Index

200E 46 INC SI Incrementing the Source Index

200F 8A 04 MOV AL, [SI] Getting the 1st value to AL register 2011 46 INC SI Incrementing the Source Index

2012 8A 1C MOV BL, [SI] Getting the 2nd value to BL register

2014 3A C3 CMP AL, BL Comparing values of AL, BL registers

2016 76 02 JBE 201A Jump if below or equal to 201A

2018 8A C3 MOV AL, BL Moving the value of BL register to AL 201A E2 F5 LOOP 2011 Looping the statement from 2011

201C BE 00 40 MOV SI, 4000 Loading location 4000 to Source Index 201F 8B 0C MOV CX, [SI] Getting the no. of numbers to CX register 2021 46 INC SI Incrementing the Source Index 2022 46 INC SI Incrementing the Source Index 2023 8A 24 MOV AH, [SI] Moving the value at 4002 to AH register 2025 46 INC SI Incrementing the Source Index 2026 8A 1C MOV BL, [SI] Moving the value at 4003 to AH register 2028 3A DC CMP BL, AH Comparing values of BL, AH registers 202A 75 F3 JBE 201F Jump if below or equal to 201F 202C 3A D8 CMP BL, AL Comparing values of BL, AL registers

Page 14: Av222 lab 1 st part

202E 73 02 JNB 2032 Jump if no Borrow is generated to 2032

2030 8A E3 MOV AH, BL Moving the value of BL register to AH 2032 E2 F1 LOOP 2025 Looping the statements from 2025

2034 88 25 MOV [DI], AH Moving the value of AH register to 5000

2036 CC INT 03 End of the Program

OUTPUT:

Location Value

4000 04

4001 60

4002 55

4003 17

4004 89

4005 36

4006 76

4007 45

4008 04

4009 10

5000 76

QUESTION 3:

Write an ALP IN 8086 for arranging a given array of elements in ascending order.

ALGORITHM:

Initialize the Data Segment.

Store 8-bit numbers in locations beginning from 4000.

Read the first number in AL register and the second number in BL register.

Perform swapping of the two numbers in such a way that the larger number comes

1st.

Repeat the above procedure equal to the number of numbers which are compared.

Page 15: Av222 lab 1 st part

PROGRAM:

Addr. Opcode Operand Mnemonic Comment

2000 B8 00 00 MOV AX, 0000 Loading the value 0000 to AX register

2003 8E D8 MOV DS, AX Initializing Data Segment using AX register

2005 B2 05 MOV DL, 05 Loading DL register with the value 05

2007 BE 00 40 MOV SI, 4000 Loading location 4000 to Source Index

200A 8A F0 MOV DH, AL Moving the value of AL register to DH 200C 8A 04 MOV AL, [SI] Getting the 1st value to AL register

200E 46 INC SI Incrementing the Source Index

200F 8A 24 MOV AH, [SI] Getting the 2nd value to AH register

2011 3A E0 CMP AH, AL Comparing values of AH, AL registers

2013 77 0B JNBE 201B Jump if not below or equal to 201B

2015 88 04 MOV [SI], AL Moving the value of AL register to SI

2017 4E DEC SI Decrementing the Source Index

2018 88 34 MOV [SI], AH Moving the value of BH register to SI

201A 46 INC SI Incrementing the Source Index 201B FE CF DEC BH Decrementing the value of BH

201D 80 FF 00 CMP BH, 00 Comparing value of BH with 00 2020 77 EA JNBE 200C Jump if not below or equal to 200C 2022 FE CA DEC DL Decrementing the value of DL 2024 80 FA 00 CMP DL, 00 Comparing value of DL with 00 2027 77 DE JNBE 2007 Jump if not below or equal to 2007 2029 CC INT 03 End of the Program

OUTPUT:

Location Value (before) Location Value (after)

4000 04 4000 04

4001 60 4001 04

4002 55 4002 10

4003 17 4003 17

4004 89 4004 36

4005 36 4005 45

4006 76 4006 55

4007 45 4007 60

4008 04 4008 76

4009 10 4009 89

Page 16: Av222 lab 1 st part

QUESTION 4:

Write an ALP IN 8086 for sorting a given array of strings in Alphabetical order.

[Hint: Write a subroutine to compare and exchange two strings. Use strings of fixed

length]

Instructions useful for the lab session:

Instruction Operands Description

CMPSB

No Operands

Compare bytes: ES: [DI] from DS: [SI]. Algorithm:

DS: [SI] - ES: [DI] Set flags according to the result: OF, SF, ZF, AF, PF, CF If DF = 0 then

SI = SI + 1

DI = DI +1 Else

SI = SI – 1

DI = DI – 1

REPE Chain instruction

Repeat following CMPSB, CMPSW, SCASB, SCASW instructions while ZF = 2 (result is Equal), maximum CX times. Algorithm: Check_CX: If CX </> 0 then do the following chain instructions

CX = CX – 1

If ZF = 1 then: o Go back to Check_CX

Else o Exit from REPE cycle

Else

Exit from REPE cycle Also study CMPSW, REPNE, REPNZ, REPZ, SCASW and SCASB.

ALGORITHM:

Initialize the Data Segment.

Initialize CX register with number of elements in string.

Compare the strings, stored in the Source Index and Destination Index, byte by byte.

Store 00 if both are Equal, otherwise 01 if both are not equal.

Page 17: Av222 lab 1 st part

PROGRAM:

Addr. Opcode Operand Mnemonic Comment

2000 B2 03 MOV DL, 02 Loading the value 02 to DL register

2002 BE 00 40 MOV SI, 4000 Loading location 4000 to Source Index

2005 BF 04 40 MOV DI, 4004 Loading location 4004 to Destination Index

2008 E8 F5 0F CALL 3000 Calling the statements from location 3000

200B 72 01 JB 200E Jump if borrow is generated to 200E

200D BE 00 40 MOV SI, 4000 Loading location 4000 to Source Index

2010 BF 04 40 MOV DI, 4004 Loading location 4004 to Destination Index

2013 E8 EA 10 CALL 3100 Calling the statements from location 3100

2016 BE 04 40 MOV SI, 4004 Loading location 4004 to Source Index

2019 BF 08 40 MOV DI, 4008 Loading location 4008 to Destination Index

201C E8 E1 0F CALL 3000 Calling the statements from location 3000

201F 72 09 JB 202A Jump if borrow is generated to 202A

2021 BE 04 40 MOV SI, 4004 Loading location 4004 to Source Index

2024 BF 08 40 MOV DI, 4008 Loading location 4008 to Destination Index

2027 E8 D6 10 CALL 3100 Calling the statements from location 3100

202A 80 FA 00 CMP DL, 00 Comparing the value of DL reg. with 00

202D FE CA DEC DL Decrementing the value of DL register

202F 74 02 JE 2033 Jump if zero to 2033

2031 EB CF JMP 2002 Unconditional Jump to 2002

2033 CC INT 03 End of Program

3000 B9 04 00 MOV CX, 0004 Loading the value 0004 to CX register

3003 A6 CMP SB Comparing String Bytes

3004 75 FA JNE 3000 Jump if not zero to 3000

3006 E2 FB LOOP 3003 Looping the statements from 3003

3008 C3 RET Returning back to the main program

3100 B9 04 00 MOV CX, 0004 Loading the value 0004 to CX register

3103 8A 04 MOV AL, [SI] Moving the value of Source Index to AL reg.

3105 8A 1D MOV BL, [DI] Moving the value of Dest. Index to BL reg.

3107 86 C3 XCHG AL, BL Exchanging the values of AL, BL registers

3109 88 04 MOV [SI], AL Moving the value of AL reg. to Source Index

310B 88 1D MOV [DI], BL Moving the value of BL reg. to Dest. Index

310D 46 INC SI Incrementing Source Index

310E 47 INC DI Incrementing Destination Index

310F E2 F2 LOOP 3103 Looping statements from 3103

3111 C3 RET Returning back to the main program

OUTPUT:

Location Val. (before) αβs Location Val. (after) αβs

4000 44 D 4000 44 D

4001 4F O 4001 45 E

Page 18: Av222 lab 1 st part

4002 4E N 4002 56 V

4003 44 D 4003 41 A

4004 47 G 4004 44 D

4005 49 I 4005 4F O

4006 4C L 4006 4E N

4007 4D M 4007 44 D

4008 44 D 4008 47 G

4009 45 E 4009 49 I

400A 56 V 400A 4C L

400B 41 A 400B 4D M

LAB 4: OPERATIONS IN 8086

QUESTION 1:

Write an ALP IN 8086 for verifying all the Logic Operations.

ALGORITHM:

Initialize the Data Segment.

Store two 16-bit numbers in location 4000 – 4003.

Read the first number in AX register and the second number in BX register.

Knowing the basic gates to be NOT, AND, OR and XOR, we can simply NOT the

previous 2 to get NAND, NOR and XNOR gates.

PROGRAM:

Addr. Opcode Operand Mnemonic Comment

2000 BE 00 40 MOV SI, 4000 Loading location 4000 to Source Index

2003 BF 00 50 MOV DI, 5000 Loading location 5000 to Destination Index

2006 B8 00 00 MOV AX, 0000 Loading the value 0000 to AX register

2009 8E D8 MOV DS, AX Initializing Data Segment using AX register

200B B9 03 00 MOV CX, 0003 Loading the value 0003 to CX register

200E 8B 04 MOV AX, [SI] Getting the 1st value to AX register

2010 46 INC SI Incrementing the Source Index

2011 46 INC SI Incrementing the Source Index

Page 19: Av222 lab 1 st part

2012 8B 1C MOV BX, [SI] Getting the 2nd value to BX register 2014 50 PUSH AX Pushing the value of AX register

2015 E2 FD LOOP 2014 Looping the statements from 2014

2017 23 C3 AND AX, BX AND operation on AX, BX registers

2019 89 05 MOV [DI], AX Moving the value of AX to Destination Index

201B 47 INC DI Incrementing the Destination Index

201C 47 INC DI Incrementing the Destination Index

201D F7 D0 NOT AX NOT operation on AX register 201F 89 05 MOV [DI], AX Moving the value of AX to Destination Index 2021 47 INC DI Incrementing the Destination Index 2022 47 INC DI Incrementing the Destination Index 2023 58 POP AX Popping the value of AX register 2024 0B C3 OR AX, BX OR operation on AX, BX registers 2026 89 05 MOV [DI], AX Moving the value of AX to Destination Index 2028 47 INC DI Incrementing the Destination Index 2029 47 INC DI Incrementing the Destination Index 202A F7 D0 NOT AX NOT operation on AX register 202C 89 05 MOV [DI], AX Moving the value of AX to Destination Index 202E 47 INC DI Incrementing the Destination Index 202F 47 INC DI Incrementing the Destination Index 2030 58 POP AX Popping the value of AX register 2031 33 C3 XOR AX, BX XOR operation on AX, BX registers 2033 89 05 MOV [DI], AX Moving the value of AX to Destination Index 2035 47 INC DI Incrementing the Destination Index 2036 47 INC DI Incrementing the Destination Index 2037 F7 D0 NOT AX NOT operation on AX register 2039 89 05 MOV [DI], AX Moving the value of AX to Destination Index 203B 47 INC DI Incrementing the Destination Index 203C 47 INC DI Incrementing the Destination Index 203D 58 POP AX Popping the value of AX register 203E F7 D0 NOT AX NOT operation on AX register 2040 89 05 MOV [DI], AX Moving the value of AX to Destination Index 2042 CC INT 03 End of the Program

OUTPUT:

Location Value

4000 F00F

4002 0FF0

5000 0000

5002 FFFF

5004 FFFF

5006 0000

5008 FFFF

500A 0000

Page 20: Av222 lab 1 st part

500C 0FF0

QUESTION 2:

Write an ALP IN 8086 for verifying all the Shift and Rotate operations.

ALGORITHM:

Initialize the Data Segment.

Store the 16-bit number in location 4000.

Read the number in AX register.

PROGRAM:

Addr. Opcode Operand Mnemonic Comment

2000 BE 00 40 MOV SI, 4000 Loading location 4000 to Source Index

2003 BF 00 50 MOV DI, 5000 Loading location 5000 to Destination Index

2006 B8 00 00 MOV AX, 0000 Loading the value 0000 to AX register

2009 8E D8 MOV DS, AX Initializing Data Segment using AX register

200B 8B 04 MOV AX, [SI] Getting the 1st value to AX register

200D B9 06 00 MOV CX, 0006 Loading the value 0006 to CX register

2010 50 PUSH AX Pushing the value of AX register

2011 E2 FD LOOP 2010 Loop statements from 2010

2013 D1 C8 ROR AX, 1 Rotate AX right by 1 2015 89 05 MOV [DI], AX Moving the value of AX to Destination Index

2017 47 INC DI Incrementing the Destination Index

2018 47 INC DI Incrementing the Destination Index

2019 58 POP AX Popping the value of AX register

201A D1 C0 ROL AX, 1 Rotate AX left by 1

201C 89 05 MOV [DI], AX Moving the value of AX to Destination Index

201E 47 INC DI Incrementing the Destination Index 201F 47 INC DI Incrementing the Destination Index 2020 58 POP AX Popping the value of AX register 2021 D1 D8 RCR AX, 1 Rotate (CF before MSB) AX right by 1 2023 89 05 MOV [DI], AX Moving the value of AX to Destination Index 2025 47 INC DI Incrementing the Destination Index 2026 47 INC DI Incrementing the Destination Index 2027 58 POP AX Popping the value of AX register 2028 D1 D0 RCL AX, 1 Rotate (CF after LSB) AX left by 1 202A 89 05 MOV [DI], AX Moving the value of AX to Destination Index 202C 47 INC DI Incrementing the Destination Index 202D 47 INC DI Incrementing the Destination Index 202E 58 POP AX Popping the value of AX register 202F D1 F8 SAR AX, 1 Shift AX right by 1 (with new MSB = old MSB) 2031 89 05 MOV [DI], AX Moving the value of AX to Destination Index 2033 47 INC DI Incrementing the Destination Index 2034 47 INC DI Incrementing the Destination Index

Page 21: Av222 lab 1 st part

2035 58 POP AX Popping the value of AX register 2036 D1 E0 SHL AX, 1 Shift AX left by 1 (with new LSB = 0) 2038 89 05 MOV [DI], AX Moving the value of AX to Destination Index 203A 47 INC DI Incrementing the Destination Index 203B 47 INC DI Incrementing the Destination Index 203C 58 POP AX Popping the value of AX register 203D D1 E8 SHR AX, 1 Shift AX right by 1 (with new MSB = 0) 203F 89 05 MOV [DI], AX Moving the value of AX to Destination Index 2041 CC INT 03 End of the Program

OUTPUT:

Location Value

4000 0007

5000 8003

5002 000E

5004 8003

5006 000E

5008 0007

500A 000E

500C 8003

QUESTION 3:

Write an ALP IN 8086 for generating the Fibonacci sequence.

ALGORITHM:

Initialize the Data Segment.

Store the values of 1st 2 terms of the Fibonacci sequence in locations 4000 & 4002

respectively.

Perform a recursive exchange and add operation to obtain the successive terms of

the Fibonacci sequence.

As the subsequent terms are obtained store them.

PROGRAM:

Addr. Opcode Operand Mnemonic Comment

2000 BE 00 40 MOV SI, 4000 Loading location 4000 to Source Index

2003 BF 00 50 MOV DI, 5000 Loading location 5000 to Destination Index

2006 B8 00 00 MOV AX, 0000 Loading the value 0000 to AX registers

2009 8E D8 MOV DS, AX Initializing Data Segment using AX register

200B B9 00 00 MOV AL,00 Getting the 1st value to AX register

200E 8A 0C MOV CL, [SI] Loading the no. of terms to CL register

2010 88 05 MOV [DI], AL Moving the value of AL to Destination Index

2012 47 INC DI Incrementing the Destination Index

2013 B4 01 MOV AH, 01 Loading the value 01 to AH register

Page 22: Av222 lab 1 st part

2015 02 E0 ADD AH, AL Adding the values of AH, AL registers

2017 88 25 MOV [DI], AH Moving the value of AH to Destination Index

2019 47 INC DI Incrementing the Destination Index

201A 86 C4 XCHG AL, AH Exchanging the values of AL, AH registers

201C E2 F7 LOOP 2015 Loop statements from 2015

201E CC INT 03 End of the Program

OUTPUT:

Location Value

4000 05

5000 00

5001 01

5002 01

5003 02

5004 03

5005 05

5006 08

QUESTION 4:

Write an ALP IN 8086 for computing nPr and nCr.

ALGORITHM:

Initialize the Data Segment.

Store the values of ‘n’ and ‘r in locations 4000 & 4002’respectively.

Do the following calculations in chronological order:

o n!

o (n – r)

o (n – r)!

o [n!/(n – r)!] = nPr

o r!

o [nPr/ r!] = nCr

As nPr and nCr are obtained store them.

PROGRAM:

Page 23: Av222 lab 1 st part

Addr. Opcode Operand Mnemonic Comment

2000 BE 00 40 MOV SI, 4000 Loading location 4000 to Source Index

2003 BF 00 50 MOV DI, 5000 Loading location 5000 to Destination Index

2006 B8 00 00 MOV AX, 0000 Loading the value 0000 to AX register

2009 8E D8 MOV DS, AX Initializing Data Segment using AX register

200B 8B 04 MOV AX, [SI] Getting the value of ‘n’ to AX register

200D 46 INC SI Incrementing the Source Index

200E 46 INC SI Incrementing the Source Index

200F 8B 1C MOV BX, [SI] Getting the value of ‘r’ to BX register

2011 50 PUSH AX Pushing the value of AX to stack memory

2012 8B C8 MOV CX, AX Moving the value of AX register to CX

2014 B8 01 00 MOV AX, 0001 Loading the value 0001 to AX register

2017 F7 E1 MUL CX Multiply CX register with AX

2019 E2 FC LOOP 2017 Loop statements from 2017

201B 8B D0 MOV DX, AX Moving the value of AX register to DX

201D 58 POP AX Popping the value of AX from stack memory

201E 92 XCHG AX, DX Exchanging the values of AX, DX registers

201F 2B D3 SUB DX, BX Subtracting the value of BX from DX register

2021 8B CA MOV CX, DX Moving the value of DX register to CX

2023 8B D0 MOV DX, AX Moving the value of AX register to DX

2025 B8 01 00 MOV AX, 0001 Loading the value 0001 to AX register

2028 52 PUSH DX Pushing the value of DX to stack memory 2029 F7 E1 MUL CX Multiply CX register with AX

202B E2 FC LOOP 2029 Loop statements from 2029

202D 5A POP DX Popping the value of DX from stack memory

202E 92 XCHG AX, DX Exchanging the values of AX, DX registers

202F F6 F2 DIV DL Divide value of AX register by DL

2031 89 05 MOV [DI], AX Moving the value of AX to Destination Index

2033 91 XCHG AX, CX Exchanging the values of AX, CX registers

2034 87 D9 XCHG BX, CX Exchanging the values of BX, CX registers

2036 B8 01 00 MOV AX, 0001 Loading the value 0001 to AX register 2039 F7 E1 MUL CX Multiply CX register with AX

203B E2 FC LOOP 2039 Loop statements from 2039

203D 93 XCHG AX, BX Exchanging the values of AX, BX registers

203E F6 F3 DIV BL Divide value of AX register by BL

2040 47 INC DI Incrementing the Destination Index

2041 47 INC DI Incrementing the Destination Index

2042 89 05 MOV [DI], AX Moving the value of AX to Destination Index

2044 CC INT 03 End of the Program

OUTPUT:

Location Value

4000 0005

4002 0002

Page 24: Av222 lab 1 st part

5000 0014

5002 000A