flat assembler
Message board for the users of flat assembler.

Index > Windows > Help removing new line from strings.

Author
Thread Post new topic Reply to topic
superj_2004



Joined: 28 Dec 2006
Posts: 4
superj_2004
Hello, I want to remove the new line of a string. I've tried many ways but noone of them worked.

After the search string matches, it moves one character to the right (this is where the id string starts), after that I use repne scasb to search for the new line (10), and replace 10 for 0. but when I print it, its like nothing was done. Here is a sample code of what Im doing.

Code:
format PE console

entry start  

include 'win32ax.inc'

.data
        search db 'mystring'
        strLength = $-search
        string db 'mystring id2', 10   ;This is the string I'll work with after it matches.

.code
start:

        mov esi, search
        mov edi, string
        mov ecx, strLength
        
        repe cmpsb                      ; Verify if search matches string.
        jnz @f
                inc edi                 ; move one char forward. (to skip the space between the 'mystring' and 'id2').
                mov eax, edi    ; backup edi
                
                mov dl, 10              
                repne scasb             ; Search for 10 (new line).
                mov [edi - 1], byte 0   ; Replace 10 for 0.
                
                cinvoke printf, eax             ; print 'id2' to the screen
        @@:

        invoke ExitProcess,0

section '.idata' import data readable writeable

        library kernel32,'KERNEL32.DLL',\
                        msvcrt,'MSVCRT.DLL'

        import kernel32, ExitProcess, 'ExitProcess'
        import msvcrt, printf, 'printf'
    



Thanks a lot for your help!
Post 31 Dec 2006, 20:02
View user's profile Send private message Reply with quote
kohlrak



Joined: 21 Jul 2006
Posts: 1421
Location: Uncle Sam's Pad
kohlrak
XD

i'll give you my own untested code to see if you can figure it out from that, since i'm not exactly sure what everything in your program does (due to lack of experience).

Code:
macro erase10 __erase10string  {
        mov ecx, 0
        mov ebx, __erase10string
        ;So far it should be obvious...
loopy:
        mov al, [ecx+ebx]
        and al, 10 ;check if 10
        jnz endofloop ;exit loop
        inc ecx ;increment to try again
        jmp loopy ;Go back to start of loop
endofloop: ;exit of loop
        mov [ecx+ebx], byte 0 ;Terminates, ecx should not have been incremented by one so it should still be on 10.
}    


Quote:
scas subtracts the destination string element from AL, AX, or EAX (depending on the size of string element) and updates the flags AF, SF, ZF, PF, CF and OF. If the values are equal, ZF is set, otherwise it is cleared. The operand should be the destination string element addressed by DI or EDI.


I think that means that scas (scasb in your case) goes BACKWORDS, but i'm not sure. You should always know the max size of a variable, so if it does go backwords, you can always to the scas from the max size. But i could be wrong on that. That and the fact that you're storing EDI into eax to back it up (i'd use push edi) and print edi out. Incase it dosn't go backwords, try directly placing string's address into edi and printinting the string from EDI using pop edi if you use push edi to back it up, because it appaers that your code is using eax when you're trying to use eax for something else.
Post 01 Jan 2007, 19:10
View user's profile Send private message Visit poster's website AIM Address Yahoo Messenger MSN Messenger Reply with quote
Goplat



Joined: 15 Sep 2006
Posts: 181
Goplat
superj_2004: SCASx compares with AL, not DL. Also, you have to set ECX to the maximum length to search for first.

kohlrak: SCASx can go backwards if the direction flag is set, but that's no different from all the other string instructions.

Quote:

and al, 10 ;check if 10
jnz endofloop ;exit loop

That should be CMP/JZ, not AND/JNZ.
Post 01 Jan 2007, 19:41
View user's profile Send private message Reply with quote
kohlrak



Joined: 21 Jul 2006
Posts: 1421
Location: Uncle Sam's Pad
kohlrak
Oops.

EDIT: now that i read the deffiniton again with new understanding, it appears to imply that it only checks one value then exits it's little scan, rather than searching the whole string.
Post 01 Jan 2007, 19:44
View user's profile Send private message Visit poster's website AIM Address Yahoo Messenger MSN Messenger Reply with quote
superj_2004



Joined: 28 Dec 2006
Posts: 4
superj_2004
Thanks a lot!
Post 02 Jan 2007, 02:07
View user's profile Send private message Reply with quote
hutch--



Joined: 19 Jun 2003
Posts: 10
hutch--
I enjoy playing with FASM from time to time. here is a simple algo put into a library module that will remove any conbination of ascii 13 and 10.

Code:
; ¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤

format MS COFF

section '.text' code readable executable

public _remlf as "remlf@4"
 
; ¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤

_remlf:

  ; -- -----------------------------------------------------------
  ; replaces any combination of ascii 13 & 10 with a single space.
  ; -------------------------------------------------------------
    mov ecx, [esp+4]
    mov edx, [esp+4]
    sub ecx, 1

  @@:
    add ecx, 1
  backin:
    movzx eax, BYTE [ecx]
    test eax, eax                   ; test for terminator
    jz zero
    cmp eax, 13                     ; test for CR
    je crlfeed
    cmp eax, 10                     ; test for LF
    je crlfeed
    mov [edx], al                   ; write byte to destination
    add edx, 1
    jmp @B

  crlfeed:
    add ecx, 1
    movzx eax, BYTE [ecx]
    cmp eax, 13                     ; test for CR
    je crlfeed
    cmp eax, 10                     ; test for LF
    je crlfeed
    test eax, eax                   ; test for terminator
    jz zero

    mov BYTE [edx], 32              ; write a space to replace
    add edx, 1                      ; the CRLF combination
    jmp backin

  zero:
    mov BYTE [edx], 0
    mov eax, [esp+4]

    ret 4

; ¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤
    


Regards,

hutch at movsd dot com
Post 02 Jan 2007, 14:04
View user's profile Send private message Reply with quote
hamoz



Joined: 14 Dec 2006
Posts: 29
hamoz
Hello hutch--,

can you tell me how can I execute your MSCOFF example coz I have no
idea.

thanks alot Crying or Very sad
Post 09 Feb 2007, 02:58
View user's profile Send private message Reply with quote
hutch--



Joined: 19 Jun 2003
Posts: 10
hutch--
hamoz,

The procedure only takes one argument, IE "[esp+4]" and it writes the results back over the same source.
Code:
push [address_of_string]
call remlf
    


Result is written to the original string.

Regards,

hutch at movsd dot com
Post 09 Feb 2007, 19:19
View user's profile Send private message Reply with quote
okasvi



Joined: 18 Aug 2005
Posts: 382
Location: Finland
okasvi
hamoz wrote:
Hello hutch--,

can you tell me how can I execute your MSCOFF example coz I have no
idea.

thanks alot Crying or Very sad


It would require linking, I guess hutch-- is used to that Wink

Anyway, you can easily make it to work in your own srcs which doesnt require linking, just copy&paste everything inbetween last two lines of ; ¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤
and place it into your code where you keep procs, and ofc add ;remlf by hutch-- somewhere in your sources Smile

Two things I would do if I would use it in my one-file-source is to prefix with dot(.) all labels after _remlf, and change ret to retn as it's not using proc macro anyway.

_________________
When We Ride On Our Enemies
support reverse smileys |:
Post 10 Feb 2007, 10:04
View user's profile Send private message MSN Messenger Reply with quote
hamoz



Joined: 14 Dec 2006
Posts: 29
hamoz
Thanks guys.....but Can anyone do it here for me, the last chance Wink
Post 10 Feb 2007, 18:35
View user's profile Send private message Reply with quote
Display posts from previous:
Post new topic Reply to topic

Jump to:  


< Last Thread | Next Thread >
Forum Rules:
You cannot post new topics in this forum
You cannot reply to topics in this forum
You cannot edit your posts in this forum
You cannot delete your posts in this forum
You cannot vote in polls in this forum
You cannot attach files in this forum
You can download files in this forum


Copyright © 1999-2020, Tomasz Grysztar. Also on GitHub, YouTube, Twitter.

Website powered by rwasa.