String Manipulation – concatenation & substring using extern (Assembly x86)
April 23, 2016
Categorised in: Assembly Codes, MIT Codes
;****str1.asm**** extern conc_proc,sub_proc global str1,str1_size,str2,str2_size ;global should be used instead of extern %include “macro.asm” section .data msg db 10,13,”Enter the first string : ” msgl equ $-msg msg1 db 10,13,”Enter the second string : ” msgl1 equ $-msg1 msg2 db 10,10,”######## WELCOME TO MENU DRIVEN PROGRAMME #########” db 10,10,”1)Contactenate the string”,10,”2)Substring”,10,”3)Exit” db 10,10,”Enter your choice ” msgl2 equ $-msg2 section .bss choice resb 2 str1 resb 50 str2 resb 50 str1_size resb 4 str2_size resb 4 section .text global _start _start: display msg,msgl accept str1,50 dec eax mov [str1_size],eax display msg1,msgl1 accept str2,50 dec eax mov [str2_size],eax display msg2,msgl2 accept choice,2 cmp byte [choice],31h je l1 cmp byte [choice],32h je l2 exit l1: call conc_proc jmp _start l2: call sub_proc jmp _start ;****str2.asm**** global conc_proc,sub_proc extern str1,str1_size,str2,str2_size %include “macro.asm” section .data msg db 10,”Contactenated string is ” msgl equ $-msg msg1 db 10,”Substring is present” msgl1 equ $-msg1 msg2 db 10,”Substring is not present” msgl2 equ $-msg2 msg3 db 10,”Number of substring is ” msgl3 equ $-msg3 section .bss str3 resb 100 str3_size resb 2 subs resb 50 end_add resb 4 curr_add resb 4 ;we have to use curr_add sscount resb 2 dispbuff resb 4 section .text global _main _main: conc_proc: mov esi,str1 mov edi,str3 mov ecx,0 ;this step should be include otherwise it well give error mov ecx,[str1_size] rep movsb mov esi,str2 mov ecx,0 mov ecx,[str2_size] rep movsb mov ecx,0 mov ecx,[str1_size] add ecx,[str2_size] mov [str3_size],ecx display msg,msgl display str3,[str3_size] ret sub_proc: mov esi,str1 mov [curr_add],esi mov ecx,[str1_size] add esi,ecx dec esi mov [end_add],esi mov eax,00h mov [sscount],eax mov esi,str1 begin: mov edi,str2 mov ecx,00h mov ecx,[str2_size] repe cmpsb jne conti inc byte [sscount] conti: inc byte [curr_add] mov esi,[curr_add] cmp esi,[end_add] jbe begin cmp byte [sscount],00h je no display msg3,msgl3 display msg1,msgl1 mov bx,[sscount] mov ecx,4 mov esi,dispbuff l1: rol bx,4 mov al,bl and al,0fh cmp al,9h jbe l2 add al,7h l2: add al,30h mov [esi],al inc esi loop l1 ;add eax,30h ;mov [sscount],eax display dispbuff,4 jmp end no:display msg2,msgl2 end: ret ;****macro.asm**** %macro display 2 mov eax,4 mov ebx,1 mov ecx,%1 mov edx,%2 int 80h %endmacro %macro accept 2 mov eax,3 mov ebx,0 mov ecx,%1 mov edx,%2 int 80h %endmacro %macro exit 0 mov eax,1 mov ebx,0 int 80h %endmacro
Pratik Kataria is currently learning Springboot and Hibernate.
Technologies known and worked on: C/C++, Java, Python, JavaScript, HTML, CSS, WordPress, Angular, Ionic, MongoDB, SQL and Android.
Softwares known and worked on: Adobe Photoshop, Adobe Illustrator and Adobe After Effects.