The MASM Forum Archive 2004 to 2012

General Forums => The Campus => Topic started by: gavin on July 24, 2005, 09:15:48 PM

Title: How can i get the last 5 chars from a buffer
Post by: gavin on July 24, 2005, 09:15:48 PM
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.
Title: Re: How can i get the last 5 chars from a buffer
Post by: PBrennick on July 24, 2005, 10:33:57 PM
gavin,

.data
SomeString  db  255 dup (0)

.code

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

Paul

Title: Re: How can i get the last 5 chars from a buffer
Post by: hutch-- on July 24, 2005, 10:48:36 PM
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.

Title: Re: How can i get the last 5 chars from a buffer
Post by: raymond on July 25, 2005, 03:26:14 AM
"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
Title: Re: How can i get the last 5 chars from a buffer
Post by: gavin on July 26, 2005, 10:49:10 AM
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.

Title: Re: How can i get the last 5 chars from a buffer
Post by: AeroASM on July 27, 2005, 04:44:44 PM
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
Title: Re: How can i get the last 5 chars from a buffer
Post by: Jeff on July 27, 2005, 04:53:02 PM
if there was a possibility of the string not taking up the entire buffer, it would take care of it.  ;)
Title: Re: How can i get the last 5 chars from a buffer
Post by: PBrennick on July 27, 2005, 07:25:10 PM
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
Title: Re: How can i get the last 5 chars from a buffer
Post by: raymond on July 28, 2005, 02:36:17 AM
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