ALP to Concatenate Two Strings

Assembly Language Program (ALP) to concatenate two strings.


👇👇👇👇

section .data

    str1 db "Hello, ", 0

    str2 db "world!", 0

    result db 30   ; Reserve space for the concatenated string


section .text

    global _start


_start:

    ; Load the addresses of the strings into registers

    mov esi, str1

    mov edi, result


    ; Copy the first string to the result buffer

    call string_copy


    ; Move the destination index to the end of the copied string

    mov ecx, eax


    ; Copy the second string to the result buffer, starting at the end of the first string

    mov esi, str2

    mov edi, ecx

    call string_copy


    ; Print the concatenated string

    mov eax, 4  ; System call number for write

    mov ebx, 1  ; File descriptor for stdout

    mov edx, eax  ; Length of the concatenated string

    int 0x80  ; Call the kernel


    ; Exit the program

    mov eax, 1  ; System call number for exit

    xor ebx, ebx  ; Exit code 0

    int 0x80  ; Call the kernel


string_copy:

    ; Copy bytes from the source to the destination until a null byte is encountered

    ; ESI = source address

    ; EDI = destination address

    ; Returns the number of bytes copied in EAX


    xor eax, eax  ; Clear the counter


    copy_loop:

        mov dl, byte [esi]  ; Load the byte from the source

        mov byte [edi], dl  ; Store the byte to the destination

        inc esi  ; Increment source index

        inc edi  ; Increment destination index

        inc eax  ; Increment counter

        cmp dl, 0  ; Check for null byte

        jne copy_loop  ; If not null, continue copying


    ret


ALP to Concatenate Two Strings ALP to Concatenate Two Strings Reviewed by Future Engineers on June 01, 2023 Rating: 5

No comments:

8085 MICROPROCESSORS

 The 8085 microprocessor is an 8-bit microprocessor developed by Intel in the mid-1970s. It is a predecessor to the 8086 microprocessor and ...

Powered by Blogger.