; ============================================================
; Assignment 4 (a) : Concatenation of two strings
; ============================================================
%macro rw 4
mov rax,%1
mov rdi,%2
mov rsi,%3
mov rdx,%4
syscall
%endmacro
section .data
msg1 db "Enter first string : "
len1 equ $-msg1
msg2 db "Enter second string : "
len2 equ $-msg2
msg3 db "Concatenated string : "
len3 equ $-msg3
section .bss
str1 resb 100
str2 resb 100
result resb 200
n1 resb 1
n2 resb 1
ntot resb 1
ch resb 1 ; holds one character at a time
section .text
global _start
_start:
; ---- read first string, one char at a time ----
rw 1,1,msg1,len1
mov rdi,str1
mov bl,0 ; bl = length counter (safe, rw never touches rbx)
read1:
push rdi ; save pointer, rw is about to clobber rdi
rw 0,0,ch,1 ; read exactly one character
pop rdi
mov al,[ch]
cmp al,10 ; newline?
je done1
mov [rdi],al
inc rdi
inc bl
cmp bl,99 ; don't overflow the 100-byte buffer
jae done1
jmp read1
done1:
mov [n1],bl
; ---- read second string, one char at a time ----
rw 1,1,msg2,len2
mov rdi,str2
mov bl,0
read2:
push rdi
rw 0,0,ch,1
pop rdi
mov al,[ch]
cmp al,10
je done2
mov [rdi],al
inc rdi
inc bl
cmp bl,99
jae done2
jmp read2
done2:
mov [n2],bl
; ---- concatenate ----
mov rsi,str1
mov rdi,result
mov cl,[n1]
copy1:
cmp cl,0
je skip1
mov al,[rsi]
mov [rdi],al
inc rsi
inc rdi
dec cl
jmp copy1
skip1:
mov rsi,str2
mov cl,[n2]
copy2:
cmp cl,0
je skip2
mov al,[rsi]
mov [rdi],al
inc rsi
inc rdi
dec cl
jmp copy2
skip2:
mov byte [rdi],10
mov al,[n1]
add al,[n2]
inc al
mov [ntot],al
rw 1,1,msg3,len3
movzx rdx,byte [ntot]
rw 1,1,result,rdx
rw 60,0,0,0
To embed this project on your website, copy the following code and paste it into your website's HTML: