You are on page 1of 9

Experiment # 11

Shift, Rotate and LOOP Instructions


Objective
1. To reinforce concepts of 8086 shift and rotate instructions in assembly language.
2. To reinforce concepts of 8086 loop instructions in assembly language.

Theory

Shift Instructions
Shift and rotate instructions move bit strings (or operand treated as a bit string).
Shift instructions move a bit string (or operand treated as a bit string) to the right or left, with
excess bits discarded (although one or more bits might be preserved in flags). In arithmetic shift
left or logical shift left zeros are shifted into the low-order bit. In arithmetic shift right the sign
bit (most significant bit) is shifted into the high-order bit. In logical shift right zeros are shifted
into the high-order bit.

There are various shift instructions as shown in the table.

Following instructions would clear the concept;

SHL BX, 1 ; Shift left logical by one position, the word in BX

SAL AL, CL ; Shift left arithmetic, AL by count specified in CL


SHL DATA2, CL ; Shift logical left, the content of memory DATA2 by the count specified in
CL

SHR DX, 1 ; Shift right by one position, the word in DX

SHR CH, CL ; shift right the content of CH, by count in CL

Following is the example to convert a packed byte to two unpacked bytes, which explains the
shift instructions operations.

MOV AL, 95H

MOV BL, AL

AND AL, OFH

AND BL,OFOH

MOV CL,04

SHR BL,CL

Rotate Instructions

Rotate instructions are similar to shift instructions, except that rotate instructions are circular,
with the bits shifted out one end returning on the other end. Rotates can be to the left or right.
Rotates can also employ an extend bit for multi-precision rotates. Following table shows the
rotate instructions.
Following instructions explains the rotate operations;

ROL SI,CL ;rotate left the word in SI, by the count in CL

ROR AL,1 ; rotate right the word in DX the count in CL

Execute the following code and analyse its result:

MOV BX, 5634H

MOV CL,8

ROL BX,CL

MOV AL, 0C6H

MOV CL,4

ROR AL,CL

LOOP Instruction

There is a very useful and frequently used instruction called loop. This combines jump with a
counter. The register CX is assigned to decrement every time loop executes. When CL=0, the
looping is exited.
Following program explains the use of LOOP instruction.

MOV CX, 6

MOV BX, 0

REP:

MOV AL, N1[BX]

ADD AL, N2 [BX]

MOV N3[BX],AL

INC BX

LOOP REP

N1 DB 12H, 13H, 14H, 15H, 16H, 17H

N2 DB 09, 09, 12H, 13H, 08, 02

N3 DB 6 DUP(0)

Execute the above code step by step and analyse its output.

Tasks

Find the biggest number in an array of bytes. Store that number in memory location
[0101].
Write a program that counts the numbers of 1s in a binary number.
Write a program that calculates the factorial of a variable N.
Conclusion

__________________________________________________________________
__________________________________________________________________
__________________________________________________________________
__________________________________________________________________
__________________________________________________________________
Experiment 12
Procedures

Objective

1. To reinforce concepts of 8086 procedures in assembly language.

Theory

Procedure is a part of code that can be called from your program in order to make some specific
task. Procedures make program more structural and easier to understand. Generally procedure
returns to the same point from where it was called.
The syntax for procedure declaration:
name PROC
; here goes the code
; of the procedure ...
RET
name ENDP
Name - is the procedure name, the same name should be in the top and the bottom, this is used to
check correct closing of procedures.
Probably, you already know that RET instruction is used to return to operating system. The same
instruction is used to return from procedure (actually operating system sees your program as a
special procedure).
PROC and ENDP are compiler directives, so they are not assembled into any real machine code.
Compiler just remembers the address of procedure.
CALL instruction is used to call a procedure.
Here is an example:

The above example calls procedure m1, does MOV BX, 5, and returns to the next instruction
after CALL: MOV AX, 2.
There are several ways to pass parameters to procedure, the easiest way to pass parameters is by
using registers, here is another example of a procedure that receives two parameters in AL and
BL registers, multiplies these parameters and returns the result in AX register:
In the above example value of AL register is update every time the procedure is called, BL
register stays unchanged, so this algorithm calculates 2 in power of 4, so final result in AX
register is 16 (or 10h).
Here goes another example that uses a procedure to print a Hello World! Message:
"b." - prefix before [SI] means that we need to compare bytes, not words. When you need to
compare words add "w." prefix instead. When one of the compared operands is a register it's not
required because compiler knows the size of each register.

Tasks
Write a program that calculates the sum of first 20 integers by calling procedure sum_int.
Write a program that that square of a number, using a procedure named SQUARE.
Write a program which contains the procedure for calculating the factorial variable N.

Conclusion
__________________________________________________________________
__________________________________________________________________
__________________________________________________________________
__________________________________________________________________
_______________________________________________________________

You might also like