Concatination String

You might also like

Download as pdf or txt
Download as pdf or txt
You are on page 1of 2

section .

data
string1 db 'Hello', 0
string2 db ' World', 0
result_buffer db 50

section .text
global _start

_start:
mov esi, string1 ; Source address (string1)
mov edi, result_buffer ; Destination address
call copy_string ; Call subroutine to copy string

mov esi, string2 ; Source address (string2)


call copy_string ; Call subroutine to copy string

; Print the concatenated string


mov eax, 4 ; syscall number for sys_write
mov ebx, 1 ; file descriptor 1 (stdout)
mov ecx, result_buffer ; address of the string to print
call print_string ; Call subroutine to print string

; Exit the program


mov eax, 1 ; syscall number for sys_exit
xor ebx, ebx ; return 0 status
int 0x80 ; call kernel

copy_string: ; Copy string from esi to edi


copy_loop:
lodsb ; Load byte from source (esi) into al and increment esi
stosb ; Store byte from al into destination (edi) and increment edi
test al, al ; Test if it's the null terminator
jnz copy_loop ; If not null terminator, continue copying
ret

print_string:
; Print the null-terminated string at the address in ecx
mov edx, ecx ; length of the string
call print_string_length
ret

print_string_length:
; Print the null-terminated string at the address in edx
mov eax, 4 ; syscall number for sys_write
mov ebx, 1 ; file descriptor 1 (stdout)
int 0x80 ; call kernel
ret

You might also like