The MASM Forum

General => The Campus => Topic started by: UoSunsetter on August 22, 2012, 11:36:26 PM

Title: Confusion about strings
Post by: UoSunsetter on August 22, 2012, 11:36:26 PM
Hi! I was playing with the codes, than I noticed that I am confused about strings(see my previous posts on old forum. I keep making mistakes on strings  :biggrin:).

Code: [Select]
source db "uosunsetter",0  ; null terminated string
When I use "source", what I think is I'm using the address of first char in the string. With other words, "source" represents the beginning of the buffer as a reference, byte pointer. For example:

Code: [Select]
mov esi, source
Now esi has the address of letter "u" right? To access the next letter I just increase esi and move [esi] to ah. However here I get confused:

Code: [Select]
push MB_OKCANCEL
push offset source
push offset source
push 0
call MessageBox

What is this??? What am I pushing? Pointer of pointer? Why not only "source"? This example was the first example I saw, thus, I was using strings in this way which is not true I guess:

Code: [Select]
mov esi, OFFSET source
I don't even know what it does now. :(
Title: Re: Confusion about strings
Post by: ToutEnMasm on August 23, 2012, 12:14:58 AM
mov esi,source ; load a value in esi (4 bytes ,the size of esi) from the adress of source
lea esi, source  ; load the adresse of source in esi:The linker could change this one
mov esi,offset source
;same as lea except that it is the compiler who give the adress replacing offset source by a constant (the linker couldn't change this)

with functions:
invoke MessageBox,NULL,ADDR source,addr Titre,MB_OK
Title: Re: Confusion about strings
Post by: dedndave on August 23, 2012, 12:49:47 AM
Code: [Select]
source db "uosunsetter",0

mov esi, source

ESI will hold 'usou', the first 4 bytes in little-endian order

what you want is...
Code: [Select]
mov esi, offset sourcenow, ESI holds the address of source
Title: Re: Confusion about strings
Post by: tuohai on August 23, 2012, 02:13:32 AM
soure is not a byte pointer. there isn't  byte pointer in masm32. all the pointers is dword.
Title: Re: Confusion about strings
Post by: jj2007 on August 23, 2012, 02:46:09 AM
soure is not a byte pointer. there isn't  byte pointer in masm32. all the pointers is dword.

include \masm32\include\masm32rt.inc

.code
source db "uosunsetter",0  ; null terminated string
start:
   mov esi, offset byte ptr source
   MsgBox 0, esi, 0, MB_OK
   exit
end start

 ;)

... and welcome to the forum :icon14:
Title: Re: Confusion about strings
Post by: Vortex on August 23, 2012, 03:35:23 AM
Hi tuohai,

There are also qword pointers.
Title: Re: Confusion about strings
Post by: jj2007 on August 23, 2012, 03:47:47 AM
Erol, the assembler zoo is full of strange animals :P
Title: Re: Confusion about strings
Post by: qWord on August 23, 2012, 04:00:30 AM
While we're on it:
Code: [Select]
mov esi,sourcethis won't assemble, because source is of the type BYTE whereas ESI is DWORD  :badgrin:
It should be mention that MASM automatically dereference data labels (unlike other assmblers)– to get the Pointer requires the OFFSET/ADDR operator or a instruction like LEA.
Title: Re: Confusion about strings
Post by: UoSunsetter on August 26, 2012, 12:07:43 AM
Thanks for making it clear. I really need to learn proper debugging to avoid that kind of noob questions :)
Title: Re: Confusion about strings
Post by: hutch-- on August 26, 2012, 11:03:32 AM
Just to clarify something in a previous post.

> soure is not a byte pointer. there isn't  byte pointer in masm32. all the pointers is dword.

There IS in fact such a thing as a BYTE PTR in 32 bit MASM, it is a DWORD that points to a BYTE location, usually the start of a string sequence of bytes. In 16 bit DOS it was a BYTE PTR that was a WORD and if we ever get a fully long 64 bit mode, a BYTE PTR will be a 64 bit unsigned integer.

To the original question, its a distinction between WHERE data is located (its address) and WHAT the data is (its content).