News:

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

How can i get the last 5 chars from a buffer

Started by gavin, July 24, 2005, 09:15:48 PM

Previous topic - Next topic

gavin

Hi guys.

I ran into a slight problem in my code ,i'm making a udp socket program.

My problem is i have a string in a buffer and i need to get the last 5 chars of that string.

Any ideas would be great.

Thanks.
Gavin.

PBrennick

gavin,

.data
SomeString  db  255 dup (0)

.code

...
        mov  esi, offset MRUDump    ; Point to the string
        add    esi, 250
...

Paul

The GeneSys Project is available from:
The Repository or My crappy website

hutch--

Gavin,

With MASM32, you can either use the macro,


mov lpString, right$(lpString,slen)


or do a direct call to the masm32 library procedure szRight,


szRight proc source:DWORD, dest:DWORD, lnth:DWORD


It is a good idea once you undersand the technique to have a good look at the code to see how it works.

Download site for MASM32      New MASM Forum
https://masm32.com          https://masm32.com/board/index.php

raymond

"Assuming" you want the last 5 characters of a null-terminated string in a buffer of sufficient size declared as a global variable,

mov edi,offset buffer
mov ecx,SIZEOF buffer
xor eax,eax
repnz scasb
sub edi,6

EDI would then be pointing to the start of the last 5 characters regardless of the size of the string or the size of the buffer.

If your "buffer" is allocated memory, you would have to modify the first two instructions.

Raymond
When you assume something, you risk being wrong half the time
http://www.ray.masmcode.com

gavin

Hi guys.
I'm learning as i go along so i went with Raymonds way.
It's working fine now but i had to make a few changes and in the end i didn't have to d it ,but i learned from that code.
Thanks all of you guys.


AeroASM

Quote from: raymond on July 25, 2005, 03:26:14 AM
mov edi,offset buffer
mov ecx,SIZEOF buffer
xor eax,eax
repnz scasb
sub edi,6

What is wrong with:


mov edi,offset buffer + sizeof buffer - 6


or with an allocated buffer:


mov edi,pMem
add edi,ccbMem

Jeff

if there was a possibility of the string not taking up the entire buffer, it would take care of it.  ;)

PBrennick

Jeff,
That may be true but it is not what he asked for.  That is why my answer is so similar to Aeros.  He is asking about a string that fills the buffer so testing the length is not necessary and I doubt he used it.  He said he modified Raymonds response and I think that is why.

Getting too fancy is never a good idea when you are trying to teach someone.  It just confuses the person.  and repnz scasb is an advanced instruction.

Paul
The GeneSys Project is available from:
The Repository or My crappy website

raymond

Quotei have a string in a buffer

I've reread gavin's original post over and over again but I couldn't find anything indicating that his string was taking up the entire buffer. One assumption should be as good as the other. My assumption was to cover ALL possibilities.

Buffers are normally declared with a size sufficient to handle many possible strings of varying size (such as file names).

Raymond
When you assume something, you risk being wrong half the time
http://www.ray.masmcode.com