Author Topic: concatenate string....  (Read 15033 times)

Offline ravi0807

  • Jr. Member
  • *
  • Posts: 9
concatenate string....
« on: February 16, 2014, 07:30:37 AM »
;this program is to concatenate 2 user entered string
;it is only accepting 2 strings .....but concatenation is not done
;plzzz help me to find error

Code: [Select]
section .data
msg1 db 10,"enter 1st string",10
msg1_len equ $-msg1
msg2 db 'enter 2nd string',10
msg2_len equ $-msg2
result db 'result =',10
result_len equ $-result

section .bss
input1 resb 20
input2 resb 5
answer resb 2

%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,1
mov ecx,%1
mov edx,%2
int 80h
%endmacro


section .text
global _start     
    _start:
display msg1,msg1_len
accept input2,20
display msg2,msg2_len
accept input2,10

;****************
mov edx,10    ;check termination of string
mov ecx,0
l1:cmp [input1+ecx],edx
je join
inc ecx
jmp l1

mov eax,0
mov edx,0
join:cmp al,10
je exit
mov eax,[input2+edx]
mov [input1+ecx],eax
inc edx
inc ecx
jmp join




exit:
display result,result_len
display input1,10
mov eax,1
mov ebx,1
int 80h
« Last Edit: February 16, 2014, 12:29:10 PM by Frank Kotler »

Offline Frank Kotler

  • NASM Developer
  • Hero Member
  • *****
  • Posts: 2667
  • Country: us
Re: concatenate string....
« Reply #1 on: February 16, 2014, 02:21:19 PM »
I just edited your post to put "code tags" around your code - makes it easier to cut-and-paste...

The first problem I see is:
Code: [Select]
display msg1,msg1_len
accept input2,20
display msg2,msg2_len
accept input2,10

You're reading both strings into the same buffer - "input2". Did you intend to do that, or just a typo? In any case, the second input is going to overwrite the first. Not going to work...

Then, you're doing a 32-bit "cmp" trying to find a single byte.
Code: [Select]
l1:cmp [input1+ecx],edx

This will "probably" work, since there are "probably" some zeros after the string (depending on what the pesky user enters) but it isn't what you want. You could use just dl, or you don't have to use a register at all.  If Nasm doesn't have a register to determine the size, we need to tell it.
Code: [Select]
l1:
cmp byte [input1 + ecx], 10  ; or "LF" or "LINEFEED" to define what "10" means

Also, sys_read returns the length read in eax (including the linefeed!) so we "know" the length, if we choose to remember it. Think about whether you want to include the linefeeds in your "result" string.

Code: [Select]
mov eax,[input2+edx]
mov [input1+ecx],eax

Again, you're using a 32-bit register to move a byte (and again it will "probably" work). I'd just use al here. For longer strings, it is worthwhile to move four bytes at a time, but you have to move one byte at a time until you find a favorable alignment, then move four (or more) bytes at a time (add ecx, 4) until you get to within four bytes of the end, then move one byte at a time until the end. Moving four bytes whether they're all part of the string or not really isn't "right". I'd go with al.

Code: [Select]
display result,result_len
display input1,10
mov eax,1
mov ebx,1
int 80h

How do you know you want to display 10 characters? Why are you returning 1?

I haven't tried this - perhaps I will - but that's what I see at first glance.

Best,
Frank


Offline ravi0807

  • Jr. Member
  • *
  • Posts: 9
Re: concatenate string....
« Reply #2 on: February 16, 2014, 03:15:24 PM »
thank you
i made all changes which were suggested by u, but still getting error

in last 4 line , my objective is to print concatenated string and terminate program
when i run program,it is accepting strings but not displaying concatenated
i am getting...''segmentation fault(core dump)" error
plz suggest
« Last Edit: February 16, 2014, 03:17:21 PM by ravi0807 »

Offline Frank Kotler

  • NASM Developer
  • Hero Member
  • *****
  • Posts: 2667
  • Country: us
Re: concatenate string....
« Reply #3 on: February 16, 2014, 03:59:16 PM »
Code: [Select]
l1:cmp [input1+ecx], dl
je join
inc ecx
jmp l1

mov eax,0
mov edx,0 ; <- this is never executed!
join:cmp al,10
je exit
mov al,[input2+edx]
mov [input1+ecx], al
inc edx
inc ecx
jmp join

Could that be your problem?

Best,
Frank


Offline ravi0807

  • Jr. Member
  • *
  • Posts: 9
Re: concatenate string....
« Reply #4 on: February 17, 2014, 12:40:59 PM »
thanks.....its worked..... :)