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
No comments: