> For the complete documentation index, see [llms.txt](https://digitalgarden.batamladen.com/llms.txt). Markdown versions of documentation pages are available by appending `.md` to page URLs; this page is available as [Markdown](https://digitalgarden.batamladen.com/notes/programming/assembly/assembly-8086/examples-projects/reverse-a-string.md).

# Reverse a String

{% tabs %}
{% tab title="code.asm" %}

```nasm
DATA SEGMENT
	InputString db "Hello00!", 0, '$'  ; Null-terminated string to be reversed
	
DATA ENDS

CODE SEGMENT
	ASSUME CS:CODE, DS:DATA

	START:

	MOV AX, DATA        		    ; Load data segment address
	MOV DS, AX

	LEA SI, InputString  		    ; Load address of the input string (SI points to the beginning)


	; Calculate the length
	LEA SI, InputString
	MOV CX, -1
DO:
    	LODSB
    	INC CX
    	CMP AL, 0
    	JNE DO


    	MOV DI, OFFSET InputString
    	ADD DI, CX
	DEC DI

	; Reverse the string
	MOV SI, OFFSET InputString	    ; Reset SI to the beginning

reverseLoop:
	CMP SI, DI             		    ; Compare SI and DI to check if we've reached the middle
	JAE done               		    ; If SI >= DI, the string is reversed

	MOV AL, [SI]           		    ; Load character from the beginning
	MOV AH, [DI]           		    ; Load character from the end
	MOV [SI], AH           		    ; Swap characters
	MOV [DI], AL
	INC SI                 		    ; Move SI forward
	DEC DI                 		    ; Move DI backward
	JMP reverseLoop

done:
	; Print the reversed string
	MOV AH, 09h            		    ; Function code for string output
	MOV DX, OFFSET InputString  	    ; DS:DX points to the reversed string; The string must ends with '$'
	INT 21h                		    ; Display the reversed string


    	MOV AX, 4C00h           	    ; Function code for program termination
    	INT 21h                 	    ; Trigger software interrupt 21h (DOS)

CODE ENDS
END START	
```

{% endtab %}
{% endtabs %}


---

# Agent Instructions
This documentation is published with GitBook. GitBook is the documentation platform designed so that both humans and AI agents can read, navigate, and reason over technical content effectively. Learn more at gitbook.com.

## Querying This Documentation
If you need additional information that is not directly available in this page, you can query the documentation dynamically by asking a question.

Perform an HTTP GET request on the current page URL with the `ask` query parameter, and the optional `goal` query parameter:

```
GET https://digitalgarden.batamladen.com/notes/programming/assembly/assembly-8086/examples-projects/reverse-a-string.md?ask=<question>&goal=<endgoal>
```

`ask` is the immediate question: it should be specific, self-contained, and written in natural language.
`goal` is optional and describes the broader end goal you are ultimately trying to accomplish on behalf of the user. GitBook uses it to tailor the answer towards what is most useful for that goal.

The response will contain a direct answer to the question and relevant excerpts and sources from the documentation.

Use this mechanism when the answer is not explicitly present in the current page, you need clarification or additional context, or you want to retrieve related documentation sections.
