8086 program to reverse a string - GeeksforGeeks

17 Dec, 2021
Problem: Given a string we have to reverse the string and print the reversed string. Examples:  
Input: String : "This is a sample string"
Output: gnirts elpmas a si sihT

Input: String : "Geeks for Geeks"
Output: skeeG rof skeeG 
Explanation:  
Create a stringTraverse through the stringPush the characters in the stackCount the number of charactersLoad the starting address of the stringPOP the top character of the stack until count is not equal to zeroPut the character and reduce the count and increase the addressContinue until the count is greater than zeroLoad the effective address of the string in dx using LEA commandPrint the sting by calling the interrupt with 9H in AHThe string must be terminated by ‘$’ sign
Create a string
Traverse through the string
Push the characters in the stack
Count the number of characters
Load the starting 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 sting by calling the interrupt with 9H in AH
The string must be terminated by ‘$’ sign
Program: 
CPP
.MODEL SMALL.STACK 100H.DATA ; The string to be printedSTRING DB 'This is a sample string', '$' .CODEMAIN PROC FARMOV AX,@DATAMOV DS,AX ; call reverse functionCALL REVERSE ; load address of the stringLEA DX,STRING ; output the string; loaded in dxMOV AH, 09HINT 21H ; interrupt to exitMOV AH, 4CHINT 21H MAIN ENDPREVERSE 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         ; 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 ENDPEND MAIN
Output:  
gnirts elpmas a si sihT
Note: The program cannot be run on an online editor, please use MASM to run the program and use dos box to run MASM, you might use any 8086 emulator to run the program. 
andrew1234
akshaysingh98088
rs1686740
Computer Organization and Architecture
microprocessor
system-programming
Computer Organization & Architecture
microprocessor
Writing code in comment?
Please use ide.geeksforgeeks.org,
generate link and share the link here.
Comments
Old Comments
Architecture of 8086
Logical and Physical Address in Operating System
Computer Organization and Architecture | Pipelining | Set 1 (Execution, Stages and Throughput)
Addressing modes in 8085 microprocessor
Computer Organization | RISC and CISC
Architecture of 8085 microprocessor
Interrupts
Memory Hierarchy Design and its Characteristics
Computer Organization | Von Neumann architecture
Difference between Von Neumann and Harvard Architecture