String Copy INCREMENT_LENGTH:
ORG 0H ; Origin INC R0 ; Increment length
MOV DPTR, #SRC ; Load the address of the source string into INC DPTR ; Move to the next character
DPTR SJMP LENGTH_LOOP ; Continue loop
MOV R0, #DST ; Load the address of the destination string into
R0 END_LENGTH:
COPY_LOOP: ; Start of copy loop ; Length of the string is now in R0
MOV A, @DPTR ; Move byte from source string to accumulator SJMP $ ; Infinite loop to halt the program
MOV @R0, A ; Move byte from accumulator to destination STRING: DB 'HELLO$', 0
string END
INC DPTR ; Increment source address
INC R0 ; Increment destination address String Reversal
CJNE A, #0, COPY_LOOP ; Continue until NULL character (0) ORG 0H ; Origin
is encountered MOV DPTR, #STRING ; Load the address of the string into DPTR
SJMP $ ; Infinite loop to halt the program ; Find the end of the string
SRC: DB 'HELLO$', 0 MOV R0, DPTR
DST: DB 20 DUP('$') ; Reserve 20 bytes for the destination string FIND_END:
END MOV A, @R0
CJNE A, #0, INCREMENT_END
String Comparison SJMP END_FOUND
ORG 0H ; Origin INCREMENT_END:
MOV DPTR, #STR1 ; Load the address of the first string into INC R0
DPTR SJMP FIND_END
MOV R0, #STR2 ; Load the address of the second string into R0 END_FOUND:
COMPARE_LOOP: DEC R0 ; Point to the last character of the string (excluding
MOV A, @DPTR ; Load byte from first string into accumulator NULL)
CJNE A, @R0, NOT_EQUAL ; If not equal, jump to MOV R1, DPTR ; Save the start of the string
NOT_EQUAL MOV R2, R0 ; Save the end of the string
CJNE A, #0, COMPARE_LOOP ; Continue loop until end of REVERSE_LOOP:
string (NULL character) MOV A, @R1 ; Load character from start
MOV B, @R2 ; Load character from end
; If we reach here, strings are equal MOV @R1, B ; Swap characters
; Add code for equal strings MOV @R2, A
SJMP $ ; Infinite loop to halt the program INC R1 ; Move start pointer forward
NOT_EQUAL: DEC R2 ; Move end pointer backward
; Add code for not equal strings CJNE R1, R2, REVERSE_LOOP ; Continue loop until pointers
SJMP $ ; Infinite loop to halt the program meet or cross
STR1: DB 'HELLO$', 0 MOV @R1, #0 ; Null-terminate the reversed string
STR2: DB 'HELLO$', 0 SJMP $ ; Infinite loop to halt the program
END STRING: DB 'HELLO$', 0
END
String Length
ORG 0H ; Origin
MOV DPTR, #STRING ; Load the address of the string into DPTR
MOV R0, #0 ; Initialize length to 0
LENGTH_LOOP:
MOV A, @DPTR ; Load byte from string into accumulator
CJNE A, #0, INCREMENT_LENGTH ; If not NULL, increment
length
SJMP END_LENGTH ; End of string, jump to end