Author Topic: concatenating strings, repeated output.  (Read 10054 times)

Offline Slash5331

  • Jr. Member
  • *
  • Posts: 9
concatenating strings, repeated output.
« on: October 12, 2013, 02:44:54 AM »
Sorry all, I have been posting a lot lately but this should be my last in a while :)

I have put together a program which will take user input in the form
firstname.lastname
and convert and output it as
firstname.lastname@yahoo.com.

it does this by concatenating the input string and the msg string i initialized in my .data

Code: [Select]
section .data
msg: db "@yahoo.com", 10

section .bss
input resb 500

section .text
global _start
_start:

mov eax, 3
mov ebx, 0
mov ecx, input ;get user input
mov edx, 500
int 0x80

mov ecx, 0
mov edx, 10 ;10 is ascii for linefeed
loopS:
cmp ecx, 40
je endloopS
cmp [input+ecx], edx ;this section of code looks for linefeed character
je equal ;if linefeed found, jump to equal
inc ecx ;otherwise, move along 1 char and retry
jmp loopS


equal:
mov edx, 0
nloop: ;exit the loops if we have reached our maximum size
cmp ecx, 500
je endloopS

mov eax, [msg+edx]
mov [input+ecx], eax ;append the two strings together
inc edx
inc ecx
jmp nloop





endloopS:

mov eax, 4
mov ebx, 1
mov ecx, input ;print and exit
mov edx, 500
int 0x80

mov eax, 1
mov ebx, 0
int 0x80

it seems to be working to a certain degree, however, when I enter a name the output is repeated;
when i entered kyle.butler:

Code: [Select]
kyle.butler@yahoo.com
kyle.butler@yahoo.com
kyle.butler@yahoo.com
kyle.butler@yahoo.com
kyle.butler@yahoo.com
kyle.butler@yahoo.com
kyle.butler@yahoo.com
kyle.butler@yahoo.com
kyle.butler@yahoo.com
kyle.butler@yahoo.com
kyle.butler@yahoo.com
kyle.butler@yahoo.com
kyle.butler@yahoo.com
kyle.butler@yahoo.com
kyle.butler@yahoo.com
kyle.butler@yahoo.com
kyle.butler@yahoo.com
kyle.butler@yahoo.com
kyle.butler@yahoo.com
kyle.butler@yahoo.com
kyle.butler@yahoo.com
kyle.butler@yahoo

i thought this might be due to my input size having 500 bytes reserved for it but I am not sure how I would fix this.

Cheers

Offline Frank Kotler

  • NASM Developer
  • Hero Member
  • *****
  • Posts: 2667
  • Country: us
Re: concatenating strings, repeated output.
« Reply #1 on: October 12, 2013, 06:10:41 AM »
This puzzled me for a while. I know that sys_write will print 500 bytes, if we ask it to, regardless of whether there's anything "interesting" there. But where do all the copies of the string come from? Well, I think they come from right here:
Code: [Select]
nloop: ;exit the loops if we have reached our maximum size
cmp ecx, 500
je endloopS

mov eax, [msg+edx]
mov [input+ecx], eax ;append the two strings together
inc edx
inc ecx
jmp nloop
You don't look for the linefeed that ends "msg", so after concatenating "msg" to the user input you proceed into the input buffer and keep concatenating itself to itself until you've moved a full 500 characters. I think that's why your program repeats itself.

Code: [Select]
cmp al, 10 ; linefeed? end of "msg"? Note: al, not eax!
je endloopS
Something like that should fix it. You'll still be printing 500 mostly useless bytes, but at least they'll be zeros! :)

You could take a slightly different approach to this. When sys_read returns, the number of bytes actually read is in eax. This includes the linefeed caused by hitting "enter" to end input. So we know where the linefeed is, without having to walk down the string looking for it. We want to back up one, and overwrite the linefeed with the '@' from "msg". The length of "msg" is known, so we know how many bytes to move without looking for the end of that, either. When it comes to printing out the results, the length of user input (not counting the linefeed) plus the length of "msg" ought to be the number we want in edx for the sys_write to print just the number of characters we need. I think you might get a "better" program out of it, but the way you're doing it is okay too.

Don't apologize for posting a lot!  I hope we hear more from you. Compared to the guys who want to know "do u haz the kode?", you're a pleasure to try to help!

Best,
Frank


Offline Slash5331

  • Jr. Member
  • *
  • Posts: 9
Re: concatenating strings, repeated output.
« Reply #2 on: October 12, 2013, 06:30:36 AM »
Thanks, it worked :)