Author Topic: Search for and replace characters in a string  (Read 13328 times)

Offline Slash5331

  • Jr. Member
  • *
  • Posts: 9
Search for and replace characters in a string
« on: October 11, 2013, 11:16:29 PM »
Ok, I have written a basic program which has the purpose of altering a string.  Input should be in the form:

"Firstname Lastname" and it converts the input to "Firstname.Lastname" and writes to the screen
The following code is my program, and I get errors when assembling (invalid effective address) on lines 20 and 26.  I imagine it's something to do with my syntax being completely out, but here it is anyway.

Code: [Select]
section .data
section .bss
input resb 20 ;will store input
count resb 4 ;will hold our current position in the string

section .text

mov eax, 3
mov ebx, 0 ;take user input
mov ecx, input ;should be in form:  Firstname Lastname
mov edx, 20 ;eg Kyle Butler
int 0x80

mov ecx, 0
mov edx, 32 ;ecx holds counter, edx hold ascii for space
loopS:
mov [count], ecx ;store whatever is in ecx in our count variable
cmp ecx, 20 ;if ecx is 20, we are done
je endloopS
mov ebx, dword[input+count] ;otherwise, convert current char in the string to ascii number
cmp ebx, edx ;compare that ascii to ascii for space, held in edx
je equal ;if they are equal, replace space with a period
inc ecx ;increment ecx and restart the loop
jmp loopS ;to check the next character in the string
equal:
mov [input+count], 46
inc ecx ;replace space with period and restart loop to look for
jmp loopS ;next space character

endloopS:
mov eax, 4
mov ebx, 1
mov ecx, input ;output the new string to the screen
mov edx, 20
int 0x80

mov eax, 1
mov ebx, 0 ;standard exit procedure
int 0x80

Just as a side question, it would be great if someone could link me to a nasm tutorial which is actually designed for beginners as I find the manuals and things confusing.  Cheers

Offline Rob Neff

  • Forum Moderator
  • Full Member
  • *****
  • Posts: 430
  • Country: us
Re: Search for and replace characters in a string
« Reply #1 on: October 12, 2013, 12:25:10 AM »
The issue is that you're attempting to add two addresses together and retrieve the data from gods knows where.

Change this:
Code: [Select]
mov ebx, dword[input+count]

To something like this:
Code: [Select]
mov ebx, dword[input+ecx]

Offline Slash5331

  • Jr. Member
  • *
  • Posts: 9
Re: Search for and replace characters in a string
« Reply #2 on: October 12, 2013, 12:45:09 AM »
Ok, this is my new code.
It compiles and runs, but does not change the string.  The output is the same as the input.
what am i doing wrong?

Code: [Select]
section .data
section .bss
input resb 20 ;will store input
count resb 4 ;will hold our current position in the string

section .text
global _start
_start:

mov eax, 3
mov ebx, 0 ;take user input
mov ecx, input ;should be in form:  Firstname Lastname
mov edx, 20 ;eg Kyle Butler
int 0x80

mov ecx, 0
mov edx, 32 ;ecx holds counter, edx hold ascii for space
loopS:
;mov [count], ecx ;store whatever is in ecx in our count variable
cmp ecx, 20 ;if ecx is 20, we are done
je endloopS
mov ebx, dword[input+ecx] ;otherwise, convert current char in the string to ascii number
cmp ebx, edx ;compare that ascii to ascii for space, held in edx
je equal ;if they are equal, replace space with a period
inc ecx ;increment ecx and restart the loop
jmp loopS ;to check the next character in the string
equal:
mov [input+ecx], dword 46
inc ecx ;replace space with period and restart loop to look for
jmp loopS ;next space character

endloopS:
mov eax, 4
mov ebx, 1
mov ecx, input ;output the new string to the screen
mov edx, 20
int 0x80

mov eax, 1
mov ebx, 0 ;standard exit procedure
int 0x80

Online Frank Kotler

  • NASM Developer
  • Hero Member
  • *****
  • Posts: 2628
  • Country: us
Re: Search for and replace characters in a string
« Reply #3 on: October 12, 2013, 12:58:30 AM »
Well that's "interesting". You get one of those errors with "beroset" in it. The number is just the line number where the error pops up (changed slightly now). Ed Beroset (one of the many people who made Nasm what it is) put that in so we could tell which of several effective address errors caused it. Perhaps more could be done with that to make a more informative error message... someday...

In any case, the line that's causing the problem is:
Code: [Select]
mov ebx, [input + count]
That isn't what you want to do, but I'm not sure why it isn't a valid effective address. You're adding together the address of "input" plus the address of "count". There's no valid memory at such a large number - it would segfault if Nasm let us do it - but I'm not sure why Nasm won't let us do it. (because it is not a "scalar" - it's a "relocatable value", I guess?)

What you would "like" to do is something like...
Code: [Select]
mov ebx, [input + [count]] ; input plus [contents of count]
Unfortunately, there's no such instruction. However, by luck or good planning, ecx happens to hold the contents of "[count]". We can do:
Code: [Select]
mov ebx, [input + ecx]
This assembles without error... but it doesn't quite "work". We're getting four bytes from that address, and comparing it to edx which has a space in the low byte (dl) and the upper bytes clear, so they never quite match. I dealt with this by...
Code: [Select]
movzx ebx, byte [input + ecx]
It might be easier just to use dl and bl for the bytes you want to compare. Lessee... once we fix the "invalid effective address", Nasm complains that it needs to know the size of the period...
Code: [Select]
mov byte [input + ecx], 46
... and I think that makes it work! ld complains about not being able to find the entrypoint, but guesses right. ("global _start" and a "_start:" label fix that - could be "commence" if you tell ld "-e commence", but it's easier to use the default "_start". Your entrypoint doesn't need to be the first thing in "section .text" - you can put subroutines first and "_start" later if you want.)

Good job!

For tutorials using Nasm, I like http://www.drpaulcarter.com.pcasm It uses the C library to allow it to be used on any OS.

Another tutorial was written by one of our regular contributors here, Alfonso VĂ­ctor Caballero Hurtado. http://abreojosensamblador.net/IndexEn.html  It was written in Spanish, but is mostly translated to English. It covers other assemblers besides Nasm, but there's plenty of Nasm in it!

Another place you might look is in the tutorials section at http://www.asm.sourceforge.net It leans mostly towards Linux/Unix stuff. Some of it is for (G)as, but there's plenty of Nasm there, too.

Best,
Frank



Offline Slash5331

  • Jr. Member
  • *
  • Posts: 9
Re: Search for and replace characters in a string
« Reply #4 on: October 12, 2013, 01:11:21 AM »
Thank you so much :)
I feel like I understand everything you told me, it's just that i'm not so sure i'd be able to figure that out myself, but it'll come with experience I guess.

And thanks for the links too :)

EDIT: and i did actually try the
Code: [Select]
mov ebx, [input + [count]] but of course it didn't work haha