> 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 by asking a question.

Perform an HTTP GET request on the following URL with the `ask` and `goal` query parameters:

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

`ask` is the immediate question: it should be specific, self-contained, and written in natural language.
`goal` is what the user is ultimately trying to achieve, the reason they need the answer. Sharing it helps GitBook give you a better, more relevant answer. A goal is most helpful when it describes the outcome the user wants rather than restating the question. For example, with `ask=how do I create an API token`, a goal like `automate deployments from our CI pipeline` lets GitBook tailor the answer to that use case.

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.
