lab 4 procedures, stacks and arrays in assembly language

17
1 Independent University, Bangladesh (IUB) School of Engineering and Computer Science Department of Electrical and Electronic Engineering Spring 2016 COURSE CODE : ECR 209L COURSE NAME : Microprocessors and Interfacing Laboratory CREDIT : 1 COURSE TEACHER : Dr. Mustafa Habib Chowdhury LAB 4 Intel 8086 Microprocessor: Procedures, Stacks and Arrays in Assembly Language

Upload: others

Post on 06-Dec-2021

23 views

Category:

Documents


0 download

TRANSCRIPT

Page 1: LAB 4 Procedures, Stacks and Arrays in Assembly Language

1

Independent University, Bangladesh (IUB)

School of Engineering and Computer Science

Department of Electrical and Electronic Engineering

Spring 2016

COURSE CODE : ECR 209L

COURSE NAME : Microprocessors and Interfacing Laboratory

CREDIT : 1

COURSE TEACHER : Dr. Mustafa Habib Chowdhury

LAB 4 – Intel 8086 Microprocessor:

Procedures, Stacks and Arrays in Assembly

Language

Page 2: LAB 4 Procedures, Stacks and Arrays in Assembly Language

2

Page 3: LAB 4 Procedures, Stacks and Arrays in Assembly Language

3

Objectives:

• To be familiar with stack operations.

• Calling and executing functions within assembly language programs.

• Arrays in assembly language programming.

Introduction to stack:

Stack is a segment of memory where some register values can be stored so that

it is not lost. In assembly language programming we have only four registers with

which we can perform operations. These are AX, BX, CX and DX. But in many

problems we may need a lot of registers. So we need to reuse these registers

again and again. This can be done, for example, by storing the present value of

AX in the stack segment. Now we can perform other tasks with AX. This will

certainly change the content of AX. After this operation has been performed, it is

possible to get back the original values of AX from the stack and thus restore its

previous value. The first task (storing value of AX in stack) is done by PUSH

command. The second task (restoring the value of AX from stack) is done by

POP command in assembly language. A simple program can clarify this.

MOV AX, 3256H ;Stores 3256H in AX. This is AX’s original value

PUSH AX ;Puts AX’s value in stack

MOV AX, 1254H ;AX is assigned to an intermediate value 1254H

DEC AX ;New value of AX is 1253H. Certainly AX’s value is changed.

POP AX ;This restores AX’s previous value and now AX is 3256H

Now to discuss how the stack segment is organized. Stack segment is a

different segment other than default segment 0000 (in which we use to

work). It is an array of memory that can store values. Obviously, every

memory content has an address. Each time a PUSH instruction is implemented

or called, the values stored in a corresponding register is stored in the stack

Page 4: LAB 4 Procedures, Stacks and Arrays in Assembly Language

4

memory. If another PUSH is called, then that corresponding registers value is

stored in a memory l o c a t i o n i n t h e s t a c k s e g m e n t that has

address 2 bytes lower than previous one. All these can be shown as:

1. PUSH AX

2. PUSH BX

3. PUSH CX

4. …

5. …

6. …

7. POP CX

8. POP BX

9. POP AX

Here the numbers are simply program line numbers. In line 1 AX is PUSHed.

Now suppose stack starts with address value 100H. This is called Stack Pointer

(SP this indicates where to put a value if something is PUSHed into the stack).

Now AX’s value is stored in 100H address. Stack pointer is changed to 0FEH.

This means it is decremented by 2. If BX is PUSHed into the stack, then the

contents of BX is stored in locat ion 0FEH in the s ta ck and the new

value of the SP is 0FCH. Finally if CX is PUSHed into the stack, then the

contents of CX is stored in in 0FCH. All these are shown graphically below.

Page 5: LAB 4 Procedures, Stacks and Arrays in Assembly Language

5

00F2H

00F4H

00F6H

00F8H

00FAH

00FCH

00FEH

0100H

00F2H

00F4H

00F6H

00F8H

00FAH

00FCH

00FEH

0100H AX

00F2H

00F4H

00F6H

00F8H

00FAH

00FCH

00FEH BX

0100H AX

At the beginning.

SP

After the call, PUSH AX

SP

After the call, PUSH BX

SP

Page 6: LAB 4 Procedures, Stacks and Arrays in Assembly Language

6

00F2H

00F4H

00F6H

00F8H

00FAH

00FCH CX

00FEH BX

0100H AX

00F2H

00F4H

00F6H

00F8H

00FAH

00FCH

00FEH BX

0100H AX

00F2H

00F4H

00F6H

00F8H

00FAH

00FCH

00FEH

0100H AX

After the call, PUSH CX

SP

When POP command is executed the value stored last in stack, is freed first.

Here POP CX has to be called before BX or AX can be POPed. POP command

automatically increases SP value by 2. So total operation of POP is opposite to

PUSH.

After the call, POP CX

SP

After the call, POP BX

SP

Page 7: LAB 4 Procedures, Stacks and Arrays in Assembly Language

7

00F2H

00F4H

00F6H

00F8H

00FAH

00FCH

00FEH

0100H

After the call, POP AX

SP

Exercise Part 1:

(a) Program 1: This program stores two values in AX and BX. It finds

remainder of division operation between AX and BX, and stores it to BX. AX’s

value need not to be altered. Clearly observe all the steps and write what

happens.

CODE SEGMENT

ASSUME CS:CODE,DS:DATA

MOV AX, 3256H

MOV BX, 125AH

PUSH AX

LEV: SUB AX, BX

CMP AX, BX

JG LEV

MOV BX, AX

POP AX

Page 8: LAB 4 Procedures, Stacks and Arrays in Assembly Language

8

HLT

CODE ENDS

END

Introduction:

Shift and Rotate command:

Shift and Rotate commands are used to convert a number to another form where

some bits are shifted or rotated. Basic difference between “shift” and “rotate” is

shift command makes “fall of” bits at the end of register whereas rotate

command makes “Wrap around” at the end of the register. There are both

arithmetic (SAL and SAR) and logical (SHL and SHR) Shift instructions.

Graphical operations for these commands are shown below.

SAL and SHL are two mnemonics for the same instruction. This instruction shifts

each bit in the specified destination some number of bit positions to the left. As a

bit is shifted out of the LSB operation, a 0 is put in the LSB position. The MSB will

be shifted into CF. In the case of multi-bit shift, CF will contain the bit most

recently shifted out from the MSB. Bits shifted into CF previously will be lost.

The SAR instruction shifts each bit in the specified destination some number of

bit positions to the right. As a bit is shifted out of the MSB position, a copy

of the old MSB is put in the MSB position. In other words, the sign bit is

copied into the MSB. The LSB will be shifted into CF. In the case of multiple-

Page 9: LAB 4 Procedures, Stacks and Arrays in Assembly Language

9

bit shift, CF will contain the bit most recently shifted out from the LSB. Bits

shifted into CF previously will be lost.

The SHR instruction shifts each bit in the specified destination some number of

bit positions to the right. As a bit is shifted out of the MSB position, a 0 is put in

its place. The bit shifted out of the LSB position goes to CF. In the case of multi-

bit shifts, CF will contain the bit most recently shifted out from the LSB. Bits

shifted into CF previously will be lost.

The following figure gives an overview:

There are 4 types (ROL, ROR, RCL, RCR) of Rotate instructions.

Page 10: LAB 4 Procedures, Stacks and Arrays in Assembly Language

10

ROL = Rotate LEFT

This instruction rotates all the bits in a specified word or byte to the left some

number of bit positions. The data bit rotated out of MSB is circled back into the LSB. It is also copied into CF. In the case of multiple-bit rotate, CF will contain a

copy of the bit most recently moved out of the MSB.

ROR = Rotate RIGHT This instruction rotates all the bits in a specified word or byte some number of bit positions to right. The operation is desired as a rotate rather than shift, because the bit moved out of the LSB is rotated around into the MSB. The data bit moved out of the LSB is also copied into CF. In the case of multiple bit rotates, CF will contain a copy of the bit most recently moved out of the LSB.

RCL – Rotate with Carry LEFT; RCL Destination, Count

This instruction rotates all the bits in a specified word or byte some number of bit

positions to the left. The operation is circular because the MSB of the operand is rotated into the carry flag and the bit in the carry flag is rotated around into LSB

of the operand. For multi-bit rotates, CF will contain the bit most recently rotated

out of the MSB.

RCR – Rotate with Carry RIGHT; RCR Destination, Count

Page 11: LAB 4 Procedures, Stacks and Arrays in Assembly Language

11

This instruction rotates all the bits in a specified word or byte some number of bit positions to the right. The operation is circular because the LSB of the operand is

rotated into the carry flag and the bit in the carry flag is rotate around into MSB of the operand. For multi-bit rotate, CF will contain the bit most recently rotated

out of the LSB.

Some simple codes can be given to clarify the idea.

MOV CL,03H ; MOV AX,02F3H; In binary 0000 0010 1111 0011 SHR AX,CL ; In binary 0000 0000 0101 1110

In this procedure, SHR commands inserts 0’s from right side. Each time a 0 is

inserted left most bit is vanished from register content.

MOV CL,03H ; MOV AX,82F3H ; In binary 1000 0010 1111 0011 SAR AX,CL ; In binary 1111 0000 0101 1110

In this procedure, SHR commands inserts MSB content from right side. Each time it is inserted, left most bit is vanished from register content.

Note: Only the CL register can be used to contain the value (or count) by

which the Operand will shift/rotate.

MOV CL,03H ;

MOV AX,82F3H ; In binary 1000 0010 1111 0011

ROR AX,CL ; In binary 0111 0000 0101 1110

The whole procedure can be visualized as follows.

Page 12: LAB 4 Procedures, Stacks and Arrays in Assembly Language

12

Here rotate by 3 operation is shown. It is clearly seen that every bit is assigned

to a new position that is 3 places away from previous one. Unlike the shift

command no right bit is destroyed. It is placed in the leftmost position.

Exercise part 1:

(a) Program 1:

MOV CL,02H

MOV AX,105AH

SHL AX,CL

RET

Obtain AX register value and write the previous value and present value of the AX

register in binary form. What type of operation is this?

(b) Program 2:

MOV CL,04H

MOV AX,564AH

SAL AX,CL

RET

Page 13: LAB 4 Procedures, Stacks and Arrays in Assembly Language

13

Obtain the AX register value and write the previous value and present value of

the AX register in binary form. What type of operation is this ?

(c) Perform for similar values of AX and CL with ROL, ROR, RCL, RCR command.

LOOP in assembly language: Loop commands are used to perform the same operation again and again. This is

like ‘for’ or ‘while’ type loop instructions in ‘C’ or ‘MATLAB’. A common example can be shown as,

MOV CX, 0100D

MOV AX, 564AH Lev: DEC AX

Loop LEV RET

Here CX acts as a count register. Loop Lev instruction leads instruction to go back

to Lev level until CX is zero. Each time Lev level is executed CX is decreased by 1. Loop command can be used for waiting purposes. Such as,

MOV CX, 0100D

Wt: NOP Loop Wt

RET

Here the loop is executed until CX is zero. If 1 loop takes 1ms, the program will wait for 100ms.

Exercise part 2: (a) Program 1:

MOV AX,1025H MOV BX,475AH MOV CX,50H

Page 14: LAB 4 Procedures, Stacks and Arrays in Assembly Language

14

Lev: INC AX DEC BX LOOP Lev RET

Observe and describe the operation of this code.

(b) Program 2: This code is to find the Greatest Common Divisor (GCD) of two numbers. The flow chart shown below explains the algorithm to find the GCD of

two arbitrary numbers:

Page 15: LAB 4 Procedures, Stacks and Arrays in Assembly Language

15

The GCD of two numbers is performed by dividing the greater number by the smaller number till the remainder is zero. If it is zero, the divisor is the GCD. If

the remainder is not zero, the remainder and the divisor of the previous division are the new set of two numbers. The process is repeated by dividing the greater

of the two numbers by the smaller number till the remainder is zero and the GCD is found.

The following program computes the GCD of any two numbers (e.g., 5 and 3) and stores the result in the BX register and the ‘GCD’ variable. .model small DATA SEGMENT NUM1 DW 0005H NUM2 DW 0003H GCD DW ? DATA ENDS CODE SEGMENT ASSUME CS:CODE,DS:DATA START: MOV AX, DATA ;Load the Data to AX. MOV DS, AX ;Move the Data AX to DS. MOV AX, NUM1 ;Move the first number to AX. MOV BX, NUM2 ;Move the second number to BX. UP: CMP AX, BX ;Compare the two numbers. JE EXIT ;If equal, go to EXIT label. JB EXCG ;If first number is below than second, go to EXCG label. UP1: MOV DX, 0H ;Initialize the DX. DIV BX ;Divide the first number in AX by the second number in BX, the ; result is stored in AX and the remainder is stored in DX CMP DX, 0 ;Compare remainder to check if it is zero or not. JE EXIT ;If remainder = zero, jump to EXIT label. MOV AX, DX ;If remainder is non-zero, move remainder to AX. JMP UP ;Jump to UP label. EXCG: XCHG AX, BX ;Exchange the remainder and quotient. JMP UP1 ;Jump to UP1. EXIT: MOV GCD,BX ;Store the result in GCD. MOV AH,4CH INT 21H CODE ENDS

Page 16: LAB 4 Procedures, Stacks and Arrays in Assembly Language

16

In the above program, the GCD of 5 and 3 are found. You can change the values

of AX and BX and obtain the result for any other values. Find GCD of 08D4H and 235H ?

Homework Questions:

1. Suppose x = 20 and y = 28. Add y with x for 30 times and store the result in

DX.

2. Multiply 12 by 6 and store the result in DX.

3. Find Least Common Multiplier of 12d and 9d. First draw a flowchart showing

your logic and then write an assembly code based on the flow chart.

Page 17: LAB 4 Procedures, Stacks and Arrays in Assembly Language

17