Programming Techniques

Programming Techniques

1. Write an ALP (assembly language programming) for addition of two 8-bit data BB H and 11 H.

Ans. 0200 MOV AL, BB H : 8-bit data BB H into AL 0202 MOV CL, 11 H : 8-bit data 11 H into CL

0204 ADD AL, CL : Contents of AL and CL added

0206 HLT : Stop.

Comment : Result in AL = CC H.

2. Write an ALP for addition of two 16-bit data BB11 H and 1122 H. Ans. 0200 MOV AX, BB11 H : 16-bit data BB11 H into AX

0203 MOV CX, 1122 H : 16-bit data 1122 H into CX

0206 ADD AX, CX : Contents of AX and CX added

0208 HLT : Stop

Comment : Result in AX = CC33 H.

3. Write an ALP for addition of two 8-bit data BB H and 11 H. The first data has an offset address of 0304 H and displacement.

Ans. 0200 MOV BX, 0304 H : Offset address put in BX 0203 MOV AL, 11 H : 8-bit data 11H into AL

0205 ADD AL, [BX + 07] : 8-bit data from offset + displacement added with AL 0207 HLT : Stop.

Comment : Result in AL = CC H.

4. Write an ALP that subtracts 1234 H existing in DX from the word beginning at memory location MEMWDS.

Ans. 0200 MOV DX, 1234 H : 16-bit data 1234 H put into DX

0203 SUB MEMWDS, DX : Subtract data word 1234 H existing in DX from the data

word pointed to by MEMWDS.

0208 HLT : Stop.

Comment : If MEMWDS points to 3000 H then,

[3001 H : 3000 H] ← [3001 H : 3000 H] – 1234 H

5. Write an ALP which multiplies two 8-bit data 21 H and 17 H. Ans. 0200 MOV AL, 21 H : 8-bit multiplicand 21 H put into AL

0202 MOV CL, 17 H : 8-bit multiplier 17 H put into CL

0204 MUL CL : Contents of CL and AL are multiplied and the result

stored in AX

0206 HLT : Stop.

Comment : Result in AX = 02F7 H.

6. Write an ALP for dividing 1234 H by 34 H.

Ans. 0200 MOV AX, 1234 H : 16-bit dividend in 1234 H 0203 MOV CL, 34 H : 8-bit divisor in 34 H

0205 DIV CL : Content of AX divided by content of CL

0207 HLT : Stop.

Comment: Result in AX with

Quotient in AL = 59 H and

Remainder in AH = 20 H.

7. Write ALP that saves the contents of 8086’s flags in memory location having an offset 1212 H and then to reload the flags from the contents of the memory location having an offset 2121 H.

Ans. 0200 LAHF : Load AH from flags

0201 MOV [1212], A H : Move the contents of AH to memory locations pointed to

by offset 1212 H

0205 MOV AH, [2121] : Move the contents of memory locations pointed to offset

2121 H to AH

0209 SAHF : Store AH into flags

020A HLT : Stop.

8. Write an ALP that transfers a block of 100 bytes of data. The source and destination memory blocks start at 3000 H and 4000 H memory locations respectively. The data segment register value is DSADDR.

Ans. 2000 MOV AX, DSADDR : Move initial address of DS register into AX.

2003 MOV DS, AX : DS loaded with AX

2005 MOV SI, 3000 H : Source address put into SI.

2008 MOV DI, 4000 H : Destination address put into DI.

200B MOV CX, 64 H : Count value for number of bytes put into CX register

200D MOV AH, [SI] : Source byte moved into AH

200F MOV [DI], AH : AH byte moved into destination address

2011 INC SI : Increment source address

2012 INC DI : Increment destination address

2013 DEC CX : Decrement CX count

2014 JNZ 200D : Jump to 200D H until CX = 0

2017 HLT : Stop.

9. Write an ALP for ASCII addition of two numbers 2 H and 5 H.

Ans. 2000 MOV AL, 32 H : ASCII code 32 H for number 2 H is moved into AL 2002 MOV BL, 35 H : ASCII code 35 H for number 5 H is moved into BL 2004 AAA : ASCII adjust for addition

2005 HLT : Stop.

Result : (AL) = 07 H.

10. Write an ALP to find the average of two numbers. Ans. 2000 MOV AL, 72 H : Get 1st number 72 H in AL

2002 ADD AL, 78 H : Add 2nd number 78 H with 72 H (in AL)

2004 ADC AH, 00 H : Put the carry in AH

242 Understanding 8085 Microprocessor and Peripheral ICs through Problems and Solutions

2006 SAR AX, 1 : Divide Sum by 2

2008 MOV [3000 H], AL : Copy AL content in memory location 3000 H 200B HLT : Stop.

11. Write an ALP for moving a block consisting of 10 bytes from memory locations starting from 5000 H to memory locations starting from 6000 H. Use LOOP instruction.

Ans. 2000 CLD : Clear direction flag 2001 MOV SI, 4000 H : Source address put in SI

2004 MOV DI, 5000 H : Destination address put in DI

2007 MOV CX, 000A H : Put number of bytes to be transferred in CX.

200A MOV SB : 1 byte copied from memory addressed by SI to addressed

by DI.

200B LOOPNZ 200A H : Loop till CX = 0

200D HLT : Stop.

12. Write an ALP to find 2’s complement of a string of 100 bytes. Ans. 2000 CLD : Clear direction flag

2001 MOV SI, 4000 H : Source address put in SI

2004 MOV DI, 5000 H : Destination address put in DI

2007 MOV CX, 0064 H : Put the number of bytes to be 2’s complemented in CX 200A LODSB : Data byte to AL and INC SI

200B NEGAL : 2’s Complement of AL

200D STOSB : Current AL value into DI and INC DI 200E LOOPNZ 200A H : Loop till CX = 0.

2010 HLT : Stop.

13. Write an ALP for block move of 100 bytes using Repeat instruction. Ans. 2000 CLD : Clear direction flag

2001 MOV SI, 4000 H : Source address put in SI

2004 MOV DI, 5000 H : Destination address put in DI

2007 MOV CX, 0064 H : Put number of bytes to be block moved into CX 200A REPNZ : Repeat till CX = 0

200B MOVSB : Move data byte addressed by SI to DI.

2009 HLT : Stop.

14. Write an ALP to evaluate X (Y + Z), where X = 10 H, Y = 20 H and Z = 30 H.

Ans. 2000 MOV AL, 20 H : 20 H put in AL 2002 MOV CL, 30 H : 30 H put in CL

2004 ADD AL, CL : AL and CL are added up and result in AL 2006 MOV CL, AL : AL transferred in CL

2008 MOV AL, 10 H : 10 H put in AL

200A MUL CL : AL and CL are multiplied and result in AL 200C MOV SI, 4000 H : Source address in SI

200F MOV SI, AL : AL put in SI

2011 HLT : Stop.

15. Write an ALP that reverses the contents of the bytes TABLE through TABLE + N – 1.

Ans. 2000 MOV CL, N : Number N put in CL 2002 MOV CH, 00 H : 00 H put in CH

2004 MOV SI, TABLE : Starting address of TABLE put in SI 2007 MOV DI, SI : DI loaded with SI value

2009 SUB DI, 01 : 01 subtracted from DI

200B ADD DI, CX : CX value added to present DI value 200D SHR CX, 01 : Divide CX count value by 2

200F MOV AL, [SI] : Move data pointed to by SI into AL 2011 XCHG AL, [DI] : Exchange AL with data pointed to by DI 2013 MOV [SI], AL : Save the exchanged data in SI

2014 INC SI : Increment SI

2015 DEC DI : Decrement DI

2016 LOOP 200F H : Continue till CX = 0

2019 HLT : Stop.

16. Write an ALP to find the maximum value of a byte from a string of bytes. Ans. 2000 MOV SI, 3000 H : Source address put in SI

2003 MOV CX, 0100 H : Count value of bytes put in CX

2006 MOV AH, 00 H : AH initialised with 00H

2008 CMP AH, [SI] : AH compared with data pointed to by SI 200A JAE 200E H : Jump if AH is ≥ (SI) to 200E H

200C MOV AH, [SI] : Otherwise, move (SI) to AH 200E INC SI : Increment SI

200F LOOPNZ 2008 : Loop unless CX ≠ 0

2011 MOV [SI], AH : (AH) transferred to the memory location pointed to by SI 2013 HLT : Stop.

17. Write an ALP to find the minimum value of a byte from a string of bytes. Ans. 2000 MOV SI, 3000 H : Source address put in SI

2003 MOV CX, 0100 H : Count value of bytes put in CX

2006 MOV AH, 00 H : AH initialised with 00H

2008 CMP AH, [SI] : AH compared with data pointed to by SI 200A JB 200E H : Jump if (AH) < (SI) to 200E H

200C MOV AH, [SI] : Otherwise move (SI) to AH

200E INC SI : Increment SI

200F LOOPNZ 2008 : Loop unless CX ≠ 0

2011 MOV [SI], AH : (AH) transferred to the memory location pointed to by SI 2013 HLT : Stop.

Leave a comment

Your email address will not be published. Required fields are marked *