News:

MASM32 SDK Description, downloads and other helpful links
MASM32.com New Forum Link
masmforum WebSite

Reading past Terminated String

Started by ic2, May 19, 2007, 11:47:27 PM

Previous topic - Next topic

ic2

This should be doable but i don't know how...
Would anyone know how would i get all of the these numbers from Terminated strings.

The idea is to copy strings 1-5 into a separate buffer in One Chunk if possible ....

while at the same time or afterward replacing the zero byte with a dash, than terminate the new string.  I tried a lot of things but could not make it work.  I know i could use CatString but i want to learn how to do it as i described above in a difference way if possible.  Not to go pass the letter A


x_1    db  "1",0
x_2    db  "2",0
x_3    db  "3",0
x_4    db  "4",0
x_5    db  "5",0
c_A    db  "A",0
c_B    db  "B",0
c_C    db  "C",0
c_D    db  "D",0
c_E    db  "E",0

Output needed:    1-2-3-4-5


Thanks in advance. 

Ehtyar

Something like:

lea eax, X_1 ; load eax with address of your first buffer
cmp BYTE PTR eax, 'A' ; just in case first letter is A
jz end ; go past the loop if it is
@@:
add eax, 1 ; move to first null termination
mov BYTE PTR eax, '-' ; make it a hyphen
add eax, 1 ; move to next letter
cmp BYTE PTR eax, 'A' ; check it for a
jnz @b ; if it's not, move on to next buffer
end: ; do the rest of your stuff with your new buffer here

My syntax is quite possibly wrong, but this should give you the general idea, provided i understand what you're trying to do. You can use lea eax, X_1 again to get the start of your new buffer.

Ehtyar.

ic2

Thanks Ehtyar,

It works, but not the exactly the way i was think of.  But it gave me even move food for thought :)...

I been wanting to know how to do this forever... I tried everything but asking :( ... Now I'm 9 steps closer  :)...


invoke MessageBox,0,offset x_1,0,0

Results is:
1-2-3-4-5-A

The only real problem is that it over-write the Strings in memory, making them unuseable for other stuff because the zero terminator has been replaced.

That's why i wanted to copy all 5 strings to another buffer than operate on it from there.

Your code is is perfect for doing the job but better with-in another buffer.

I always know the Characters count so my final question about this is... How do i copy the first 5 terminated strings to another buffer.  Strlen stops at 0.

I'm going to try to change parts of strlen to only stop at the Characters (A).

I just want to know how to do that REALLY... because i already see that all i got to do is reverse your code to just replace the dash back with the 00h zero terminator... And that may even be as fast and efficient as placing it all into another buffer.  But i just want to know how to do it...

Thanks again Ehtyar


.386
.model flat,stdcall
option casemap:none

include \masm32\include\windows.inc
include \masm32\include\user32.inc
include \masm32\include\kernel32.inc
includelib \masm32\lib\user32.lib
includelib \masm32\lib\kernel32.lib

.data
x_1    db  "1",0
x_2    db  "2",0
x_3    db  "3",0
x_4    db  "4",0
x_5    db  "5",0
c_A    db  "A",0
c_B    db  "B",0
c_C    db  "C",0
c_D    db  "D",0
c_E    db  "E",0

;Output needed:    1-2-3-4-5

.data?
; r_BUFFER db 20 dup(?)

r_BUFFER   dd ?

numA  dd ?
numB  dd ?

.code
start:
;.........................................

lea eax, x_1                            ; load your first buffer
cmp BYTE PTR [eax], 'A'                 ; just in case letter A
jz end_it                               ; go past if it is
@@:
add eax, 1                              ; move to first null
mov BYTE PTR [eax], '-'                 ; make it a hyphen


add eax, 1                              ; move to next letter

cmp BYTE PTR [eax], 'A'                 ; check it for (A) again
jnz @b                                  ; if not,go to next buffer
                                        ; do the rest of your
                                        ; stuff with your new
                                        ; buffer here

invoke MessageBox,0,offset x_1,0,0

end_it:

invoke ExitProcess,0


sinsi

The shorter way

  mov eax,offset x_1
  mov ebx,offset buff
  mov ch,'-'
next:
  mov cl,[eax]
  mov [ebx],cl
  cmp cl,'5'
  jz done
  mov [ebx+1],ch
  add eax,2
  add ebx,2
  jmp next
done:
  mov byte ptr [ebx+1],0

or the longer way

  mov ebx,OFFSET buff
  mov esi,OFFSET dash

  invoke lstrcat,ebx,OFFSET x_1
  invoke lstrcat,ebx,esi
  invoke lstrcat,ebx,OFFSET x_2
  invoke lstrcat,ebx,esi
  invoke lstrcat,ebx,OFFSET x_3
  invoke lstrcat,ebx,esi
  invoke lstrcat,ebx,OFFSET x_4
  invoke lstrcat,ebx,esi
  invoke lstrcat,ebx,OFFSET x_5

The longer way will work with any length string.
Light travels faster than sound, that's why some people seem bright until you hear them.

Ehtyar

Ooops :S Just a little oversight there.....sorry about that. I've never been one for algorithms, probably should have tested it myself before posting. Anyway, glad i could help even in a limited capacity ;)

Ehtyar.

ic2

QuoteOoops :S Just a little oversight there.....

That was'nt an oversight to me.  It help me to see things a lot more clearly and still figuring out a lot of new things i did not know about behind it.

I would not have thought to over-write than repair with the same algorithms.  I have over-written string buffers in the pass
(by accident or trying something new) than had no clue how to get it back until now.  I bet I'm not the only one.  Now i know how to deal with limited capacity  :)

My goal was to cut-down on memory usage.  I been writing in a way to get multi ways of re-using a single or a group of string buffers.  And now i see it all was not in vain.

What's happening sinsi,

Thanks for the example,   I got Ehtyar code near down pack and running like crazy.  I'm be checking your out very shortly.  It looks wonderful.  Can't wait to get to it...

I'll be re-building with all of this for quite a while.  Replacing all the silly work-around junk in my code.

be back soon with a cool procedure build from all of this.


Thanks Guys

ic2

For the most part i got this code doing near what i am after.

..............................................
This is what i'm after:

DT00  :0100  :0200  :0300  :0400  :0500
..............................................
But this is what i get:

:0100  :0200  :0300  :0400  :0500  :A00 
..............................................
It suppose to return the data like so:
base on the original code design:
I can live with this until i get sinsi code to read:
more than one char including repairing this one.
i like them both:

:DT00  :0100  :0200  :0300  :0400  :0500  :A00 
..............................................



I stuck a call to szCopy and two calls to szCatStr which use szLen.  All three have been modified to totally free-up EAX so basically it allow this extra coding to jump in-between Ehtyar code to do some farther modification of strings... But for some reason i loss the start-up string.  DT disappear but everything else display correctly.

My main concern is the start-up string and not the ending of the string. That part can be fixed with strRev or inString that way it won't  compucate things since im still asking for help.  Do anyone know where i went wrong with this code.  See Attachment:




[attachment deleted by admin]

drizz

wsprintf anyone?
.data
x_1 db "1",0
x_2 db "2",0
x_3 db "3",0
x_4 db "4",0
x_5 db "5",0
fmtstring db 'DT00 :%02s00 :%02s00 :%02s00 :%02s00 :%02s00',0
fmtstring2 db 'DT00 :%02d00 :%02d00 :%02d00 :%02d00 :%02d00',0
.data?
outbuffer db 100h dup(?)
.code
invoke wsprintf,addr outbuffer,addr fmtstring,addr x_1,addr x_2,addr x_3,addr x_4,addr x_5
invoke MessageBox,0,addr outbuffer,0,0
invoke wsprintf,addr outbuffer,addr fmtstring2,1,2,3,4,5
invoke MessageBox,0,addr outbuffer,0,0
The truth cannot be learned ... it can only be recognized.

dsouza123

Taking advantage of uniform string sizes and repetitive code.


.386
.model flat,stdcall
option casemap:none

include \masm32\include\windows.inc
include \masm32\include\user32.inc
include \masm32\include\kernel32.inc
includelib \masm32\lib\user32.lib
includelib \masm32\lib\kernel32.lib

.data
x_xx    db "xx",0

DATA_MARK    db "DT",0
x_01    db "01",0
x_02    db "02",0
x_03    db "03",0
x_04    db "04",0
x_05    db "05",0
c_A     db  "A",0
c_B     db  "B",0
c_C     db  "C",0
c_D     db  "D",0
c_E     db  "E",0


  ADD_THIS  db  "00  ",0
  szCaption db  "Caption",0

.data?
  TEMP_32   db  32 dup(?)
  TEMP_256  db 256 dup(?)


.code
start:
  mov esi, offset DATA_MARK
  mov edi, offset TEMP_256
  mov edx, dword ptr ADD_THIS

  mov ecx, 6
@@: 
  mov eax, [esi]
  add esi, 3
  mov [edi], eax
  add edi, 2
  mov [edi], edx
  add edi, 4
  mov byte ptr [edi], ":"
  add edi, 1
  dec ecx
  jnz @B

  sub edi, 3
  mov byte ptr [edi], 0

  invoke MessageBox,0,addr TEMP_256,addr szCaption, MB_OK

  invoke ExitProcess,0
end start

[attachment deleted by admin]

ic2

Thanks drizz, dsouza123 and everybody

I'm 100 step closer to writing smaller and smarter ASM.

ic2

Hello Everybody

I'm hooked on this PTR stuff and have got all of these examples working within my programs.   So now i can go back and review them as an refresher course as i continue to learn.

I been working with this code attached below and i can't get it to insert text properly.

This one is based on Ehtyar example, but I'm now using a counter.  I been trying since i last posted and is not getting anywhere fast.  Could someone help me out with this once again.  I'll be into this for quite a while.

Thanks in advance

ic2

[attachment deleted by admin]

dsouza123

This works, hope it gives you some ideas.

.386
.model flat,stdcall
option casemap:none

include \masm32\include\windows.inc
include \masm32\include\user32.inc
include \masm32\include\kernel32.inc
includelib \masm32\lib\user32.lib
includelib \masm32\lib\kernel32.lib

.data
rString    db "1x0",13,10,0

x_0     db "0",0
x_1     db "1",0
x_2     db "2",0
x_3     db "3",0
x_4     db "4",0
x_5     db "5",0
x_6     db "6",0
x_7     db "7",0
x_8     db "8",0
x_9     db "9",0

Counter  dd  ?
TEMP_32  db  32 dup(?)
TEMP_256 db 256 dup(?)

szTitle db "100 to 190",0

.code
start:
  mov   esi, offset x_0
  mov   edi, offset TEMP_256
  mov   eax, dword ptr rString
  movzx edx,  word ptr rString+4

  mov Counter, 10
@@:
  mov ah, [esi]
  add esi, 2
  mov [edi+0], eax
  mov [edi+4],  dx
  add edi, 5
  dec Counter
  jnz @B

  invoke MessageBox, 0, offset TEMP_256, offset szTitle, 0
  invoke ExitProcess,0
end start

[attachment deleted by admin]

ic2

Thanks dsouza123,

You guys have provided nearly all the information of How-to-Insert.  This is not easily founded, anywhere, at any ASM Forum.  During my days of searching, I type keyword "Insert Text" and found ONLY one single direct THREAD about it out of a hundreds ...  My eye ball start throbbing a lot.   I searched that hard.

The thing is, all of the above is going to help me and many others to cut our programs down to size.  I see it all mentally and have replaced a little junk code already in my spare moments when not searching.  But i can't move any farther until i know what can be done.

This is too important to me so. Everything here is much more than i was hoping for, but I'm not going to pass up the chance of a life time to get to the bottom of it all when the solution is only a line or two away. 

dsouza123, your code is perfect for DWORD size strings.  I can ONLY see why it don't work for longer strings and not much farther. 

If it can be change to do the SAME THING for the beginning and the end of longer strings, for me that will be the bomb.

Operating on Both Ends or Not... is not important as long as it can do either end separately.  Getting to the middle should'nt be a big deal but that don't even matter anyway, i use instring.

I was trying CLD and STD but i have not even got to first based because the code is to deep to understand and change right off.

per group of strings will always be the same length.
per group of strings will always received the same size insert.


rString    db "1x0-ABCDEFGHIJKLMN",13,10,0

rString    db "ABCDEFGHIJKLMN-1x0",13,10,0


Can we get around this?

dsouza123


.386
.model flat,stdcall
option casemap:none

include \masm32\include\windows.inc
include \masm32\include\user32.inc
include \masm32\include\kernel32.inc
includelib \masm32\lib\user32.lib
includelib \masm32\lib\kernel32.lib

.data
rString    db "1x0-ABCDEFGHIJKLMN",13,10,0

x_0     db "0",0
x_1     db "1",0
x_2     db "2",0
x_3     db "3",0
x_4     db "4",0
x_5     db "5",0
x_6     db "6",0
x_7     db "7",0
x_8     db "8",0
x_9     db "9",0

szTitle db "100 to 190",0

strln    dd  sizeof rString   ; 21 bytes
Counter  dd  szTitle - x_0
TEMP_32  db  32 dup(?)
TEMP_256 db 256 dup(99)


.code
start:
  mov   esi, offset x_0
  mov   edi, offset TEMP_256

  shr Counter, 1     ; byte count list of strings / bytes per string

L0:
  mov ecx, 0         ; copy rString to destination starting at edi
L1:
  mov al, [rString+ecx]
  mov [edi+ecx], al
  inc ecx
  cmp ecx, strln     ; 0..20, 21 bytes
  jb  L1

  mov  al, [esi]
  add esi, 2
  mov [edi+1], al    ; replace x char using current char from list of strings
  add edi, strln
  dec edi            ; edi increased by strln - 1 (== 20)
  dec Counter
  jnz L0

  invoke MessageBox, 0, offset TEMP_256, offset szTitle, 0
  invoke ExitProcess,0
end start

ic2