You are on page 1of 12

Reversing a String in 8086

Assembly language

Reversing a String in 8086 Assembly language

OBJECTIVE:

Reversing a string using 8086 assembly language in a 8086 stimulator

ABSTRACT:

To find the reverse, we just copy the string from one memory location to another in
reverse order and display it. We first copy the first two bytes of the string array as it is
in the new string, since they remain same for the reversed string. Then we position the
SI pointer to the end of the given string and copy character by character in the new
string in reverse order. Finally the new string is displayed, which is the reverse of the
original string.

STC/SPRT/CSE/2022-2023 Page 1
Reversing a String in 8086
Assembly language

INTRODUCTION:
Intel 8086 microprocessor is the enhanced version of Intel 8085 microprocessor. It was
designed by Intel in 1976. o The 8086 microprocessor is a16-bit, N-channel, HMOS
microprocessor.
Where the HMOS is used for "High-speed Metal Oxide Semiconductor". o Intel
8086 is built on a single semiconductor chip and packaged in a 40-pin IC package.
The type of package is DIP (Dual Inline Package). o Intel 8086 uses 20 address lines
and 16 data- lines. It can directly address up to 220 = 1 Mbyte of memory. o It
consists of a powerful instruction set, which provides operation like division and
multiplication very quickly.
8086 is designed to operate in two modes, i.e., Minimum and Maximum mode.

STC/SPRT/CSE/2022-2023 Page 2
Reversing a String in 8086
Assembly language

Using 8086 micro-processor, we will create a program that reverses a string using all
the above process.

To find the reverse, we just copy the string from one memory location to another in
reverse order and display it.
We first copy the first two bytes of the string array as it is in the new string, since they
remain same for the reversed string. Then we position the SI pointer to the end of the
given string and copy character by character in the new string in reverse order.
Finally the new string is displayed, which is the reverse of the original string.

SOFTWARE REQUIREMENTS:

8086 Microprocessor Emulator.

STC/SPRT/CSE/2022-2023 Page 3
Reversing a String in 8086
Assembly language

METHODOLOGY:

• Launch 8086 Emulator.


• Open a new file and write the assembly code for reversing a string and save it as
.asm file format.
• Create a string.

• Traverse through the string.


• Push the characters in the stack.
• Count the number of characters.
• Load the staring address of the string.
• POP the top character of the stack until count is not equal to zero.
• Put the character and reduce the count and increase the address.
• Continue until the count is greater than zero.
• Load the effective address of the string in dx using LEA command.
• Print the string by calling the interrupt with 9H in AH.
• The string must be terminated by ‘$’ sign.

STC/SPRT/CSE/2022-2023 Page 4
Reversing a String in 8086
Assembly language

ADVANTAGES OF USING PROCEDURES:

• Reusability of code:
The procedures provide us an ease in our code by making the set of instructions
reusable. So, we need not write the same set of instructions again and again when
required.
• Less usage of memory:
The procedure is a subprogram which is stored in the memory only one. But it
can used as many times as required. So, this occupies less memory space.
• Development becomes easier:
By using procedures in a code, the modular programming can be well
implemented and so, each part of the code is written in a different module which
can be developed by a separate developer. So, this reduces the burden on one
developer and the programming becomes simple for each of them.
• Reduced development time:
As separate developers can work on different modules of programs separately,
multiple developers can work on the project simultaneously which reduces the
total development time of the project.
• Debugging and error fixing becomes easier:
It becomes easier to detect and fix errors if the program is present in different
modules rather than the entire program being present in a single code fragment.

STC/SPRT/CSE/2022-2023 Page 5
Reversing a String in 8086
Assembly language

FLOWCHART:

STC/SPRT/CSE/2022-2023 Page 6
Reversing a String in 8086
Assembly language

DISADVANTAGES OF USING PROCEDURES:

• Extra code is required to integrate the procedures:


Every time a procedure is to be implemented in the program, we require
CALL and RET instructions to integrate the procedures with the calling
program.
• Extra time required to link the procedures:
Whenever a call to procedure is made, the control of the processor goes to the
procedure code, and then returns to the calling program code. This takes a lot of
extra time.
• Not efficient way to use for a small set of instructions:
When the number of instructions is much less, then the calling and returning
from the procedures itself takes a lot more time than executing the instructions
itself. So, it is better to use procedures in cases where the set of instructions to
be included in the procedure is large.
• Extra load on the processor:
The processor needs to do extra work to save the status of the current procedure
and load status of the called procedure. Also, the instruction queue must be
emptied so that the instructions of the procedure can be filled in the queue.

STC/SPRT/CSE/2022-2023 Page 7
Reversing a String in 8086
Assembly language

PROGRAM:

.MODEL SMALL
.STACK 100H
.DATA

; The string to be printed


STRING DB 'This is a sample string', '$'

.CODE
MAIN PROC FAR
MOV AX,@DATA
MOV DS,AX

; call reverse function


CALL REVERSE

; load address of the string


LEA DX,STRING

; output the string


; loaded in dx
MOV AH, 09H
INT 21H

; interrupt to exit
MOV AH, 4CH
INT 21H

MAIN ENDP
REVERSE PROC
; load the offset of
; the string
MOV SI, OFFSET STRING

; count of characters of the;


;string
MOV CX, 0H

LOOP1:
; compare if this is;
;the last character
MOV AX, [SI]
CMP AL, '$'
JE LABEL1

; else push it in the;


;stack
PUSH [SI]

; increment the pointer;


;and count
INC SI
STC/SPRT/CSE/2022-2023 Page 8
Reversing a String in 8086
Assembly language

INC CX

JMP LOOP1

LABEL1:
; again load the starting;
;address of the string
MOV SI, OFFSET STRING

LOOP2:
;if count not equal to zero
CMP CX,0
JE EXIT

; pop the top of stack


POP DX

; make dh, 0
XOR DH, DH

; put the character of the;


;reversed string
MOV [SI], DX

; increment si and;
;decrement count
INC SI
DEC CX

JMP LOOP2

EXIT:
; add $ to the end of string
MOV [SI],'$ '
RET

REVERSE ENDP
END MAIN

. MODEL SMALL
.STACK 100H
.DATA

; The string to be printed


STRING DB 'This is a sample string', '$'

.CODE
MAIN PROC FAR
MOV AX,@DATA
MOV DS,AX

STC/SPRT/CSE/2022-2023 Page 9
Reversing a String in 8086
Assembly language

; call reverse function


CALL REVERSE

; load address of the string


LEA DX,STRING

; output the string


; loaded in dx
MOV AH, 09H
INT 21H

; interrupt to exit
MOV AH, 4CH
INT 21H

MAIN ENDP
REVERSE PROC
; load the offset of
; the string
MOV SI, OFFSET STRING

; count of characters of the;


;string
MOV CX, 0H

LOOP1:
; compare if this is;
;the last character
MOV AX, [SI]
CMP AL, '$'
JE LABEL1

; else push it in the;


;stack
PUSH [SI]

; increment the pointer;


;and count
INC SI
INC CX

JMP LOOP1

LABEL1:
; again load the starting;
;address of the string
MOV SI, OFFSET STRING

LOOP2:
;if count not equal to zero
CMP CX,0
JE EXIT

STC/SPRT/CSE/2022-2023 Page 10
Reversing a String in 8086
Assembly language
; pop the top of stack
POP DX

; make dh, 0
XOR DH, DH

; put the character of the;


;reversed string
MOV [SI], DX

; increment si and;
;decrement count
INC SI
DEC CX

JMP LOOP2

EXIT:
; add $ to the end of string
MOV [SI],'$ '
RET

REVERSE ENDP
END MAIN

STC/SPRT/CSE/2022-2023 Page 11
Reversing a String in 8086
Assembly language

Output:

STC/SPRT/CSE/2022-2023 Page 12

You might also like