Author Topic: Confusion about strings  (Read 3966 times)

UoSunsetter

  • Guest
Confusion about strings
« 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. :(

ToutEnMasm

  • Member
  • *****
  • Posts: 1189
    • EditMasm
Re: Confusion about strings
« Reply #1 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
Fa is a musical note to play with CL

dedndave

  • Member
  • *****
  • Posts: 8823
  • Still using Abacus 2.0
    • DednDave
Re: Confusion about strings
« Reply #2 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

tuohai

  • Guest
Re: Confusion about strings
« Reply #3 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.

jj2007

  • Member
  • *****
  • Posts: 9644
  • Assembler is fun ;-)
    • MasmBasic
Re: Confusion about strings
« Reply #4 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:

Vortex

  • Member
  • *****
  • Posts: 1989
Re: Confusion about strings
« Reply #5 on: August 23, 2012, 03:35:23 AM »
Hi tuohai,

There are also qword pointers.

jj2007

  • Member
  • *****
  • Posts: 9644
  • Assembler is fun ;-)
    • MasmBasic
Re: Confusion about strings
« Reply #6 on: August 23, 2012, 03:47:47 AM »
Erol, the assembler zoo is full of strange animals :P

qWord

  • Member
  • *****
  • Posts: 1473
  • The base type of a type is the type itself
    • SmplMath macros
Re: Confusion about strings
« Reply #7 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.
MREAL macros - when you need floating point arithmetic while assembling!

UoSunsetter

  • Guest
Re: Confusion about strings
« Reply #8 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 :)

hutch--

  • Administrator
  • Member
  • ******
  • Posts: 6589
  • Mnemonic Driven API Grinder
    • The MASM32 SDK
Re: Confusion about strings
« Reply #9 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).
hutch at movsd dot com
http://www.masm32.com    :biggrin:  :skrewy: