News:

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

local strings

Started by white scorpion, February 28, 2005, 10:24:02 AM

Previous topic - Next topic

white scorpion

Hi all,

i'm looking for a way to declare a local string with a value

so it would be something like:

LOCAL string[]:BYTE="my string",0

or

LOCAL string[]="my string",0 :BYTE


what is the correct syntaxis for this?

hutch--

What you are after is impossible as there is no method to assign a string to a LOCAL at assembly time but what you can do that most compilers do is assign a string POINTER at assembly time to a normal string in the .DATA section. A string IS initialised data so it can only be done that way.


mov eax, OFFSET MyString
mov pstr, eax


There is a macro that does this for you in MASM32, it is used like this,


LOCAL pstr    :DWORD
.....
sas pstr, "Hi I am a string"


Note that it does not have to be placed at the beginning of the proc like a LOCAL.
Download site for MASM32      New MASM Forum
https://masm32.com          https://masm32.com/board/index.php

roticv

Why would you want to make your string local? ::)

white scorpion

since i am planning on using a procedure in multiple programs, and since i would like to just copy and past it, this would be the best solution.

but i think i have found a way:

;***********************************************************
;The Procedure to add debug privileges to a program
;***********************************************************

AddDebugPrivileges PROC
;**********************


LOCAL tP:TOKEN_PRIVILEGES
LOCAL hToken: HANDLE
LOCAL CurProc:DWORD
.data
debugpriv   db "SeDebugPrivilege",0
.code


invoke GetCurrentProcess
mov CurProc,eax
lea eax,hToken

invoke OpenProcessToken,CurProc,TOKEN_ADJUST_PRIVILEGES,eax
.if eax==0
mov eax,1
ret
.endif

lea eax,tP.Privileges[0].Luid
invoke LookupPrivilegeValue,NULL,addr debugpriv,eax
.if eax==0
invoke CloseHandle,[hToken]
mov eax,1
ret
.endif

mov [tP.PrivilegeCount],1
mov tP.Privileges[0].Attributes,SE_PRIVILEGE_ENABLED
lea eax,tP

invoke AdjustTokenPrivileges,hToken,FALSE,eax,0,NULL,0
.if eax==0
invoke CloseHandle,[hToken]
mov eax,1
ret
.endif

invoke CloseHandle,[hToken]
mov eax,0
ret
AddDebugPrivileges ENDP
;***********************************************************


donkey

It is not possible to declare an initialized string as a local value, you have declared it as global here. You can however include the global declaration as part of the procedure though it involves a small price to pay in terms of execution speed because of cache problems with data in the code section...

MyProc FRAME SomeParam
LOCAL StringVar[256] :BYTE

; Code here

RET

; declarations after RET
SomeString: DB "Hello There",0

ENDF


Quotesince i am planning on using a procedure in multiple programs,

Once again this is what libs are made for and they would be the solution, a much better one than copy and paste. Both MASM and GoAsm will import the global strings with the proc from the lib file.
"Ahhh, what an awful dream. Ones and zeroes everywhere...[shudder] and I thought I saw a two." -- Bender
"It was just a dream, Bender. There's no such thing as two". -- Fry
-- Futurama

Donkey's Stable

white scorpion

i already was under the impression that it was global, but my biggest need is to put it locally on file.

i've tried using sas like Hutch said, but if i do exactly as he told i have an error with wrong syntaxis for sas.
i've been looking for an explanation for sas, but i can not find it anywhere.


testp PROC
LOCAL pString :DWORD

sas pString, "Hello World"

mov eax,pString
add eax,12         ;strlen from "Hello World" +1
mov byte ptr [eax],0

invoke MessageBoxA,NULL,pString,NULL,MB_OK
invoke ExitProcess,0

testp ENDP


this is suppose to work (discard second 'NULL' from MessageBox), but unfortunately it doesn't: syntax error : sas

any ideas?

Mark Jones

Hmm, looking in macros.asm, sas appears to append the terminating 00h automatically, so you are trying to overwrite an existing 00h with a 00h. Hmm try this:


.data
szCaption db "Debug",0

.code     ; call testp from your wndproc

testp PROC
LOCAL pString:DWORD

sas pString, "Hello World"

mov eax,pString
add eax,13         ;strlen from "Hello World" +1
mov byte ptr [eax],"!"
inc eax
mov byte ptr [eax],00h

invoke MessageBox,0,addr szCaption,addr pString,MB_OK

testp ENDP


Perhaps local DWORDs "point" to only 4 bytes of (reserved) memory space? (And thus, trying to cram a string larger than four bytes into that memory location causes a problem?) Sorry, more questions than answers. :)
"To deny our impulses... foolish; to revel in them, chaos." MCJ 2003.08

white scorpion

QuotePerhaps local DWORDs "point" to only 4 bytes of (reserved) memory space? (And thus, trying to cram a string larger than four bytes into that memory location causes a problem?) Sorry, more questions than answers. :)
perhaps it is indeed.

btw, i have also tried sas without adding the extra 0, but that doesn't matter, still same error......
i've also tried:

pString :LPSTR

sas pString,"hello world, this is a test"


but this also doesn't work ;(

MichaelW

The most likely cause of  "error A2008: syntax error : sas" is that you did not include macros.asm in your source. This works just as it should:

; ««««««««««««««««««««««««««««««««««««««««««««««««««««««««««««««
    .486                       ; create 32 bit code
    .model flat, stdcall       ; 32 bit memory model
    option casemap :none       ; case sensitive

    include \masm32\include\windows.inc
    include \masm32\include\masm32.inc
    include \masm32\include\user32.inc
    include \masm32\include\kernel32.inc
    includelib \masm32\lib\masm32.lib
    includelib \masm32\lib\user32.lib
    includelib \masm32\lib\kernel32.lib
    include \masm32\macros\macros.asm     ; <===============
; ««««««««««««««««««««««««««««««««««««««««««««««««««««««««««««««
    .data
    .code
; ««««««««««««««««««««««««««««««««««««««««««««««««««««««««««««««
start:
; ««««««««««««««««««««««««««««««««««««««««««««««««««««««««««««««
    call  testp
    mov   eax,input(13,10,"Press enter to exit...")
    exit

testp proc
    LOCAL pString:DWORD

    sas   pString, "my other brother darryl"
    invoke MessageBox,NULL,pString,NULL,MB_OK

    ret
testp endp
; ««««««««««««««««««««««««««««««««««««««««««««««««««««««««««««««
end start

eschew obfuscation

white scorpion

your kidding me??!

sas is a macro? i was under the impression it was just another opcode i didn't knew yet.
that explains why i couldn't find any documentary about it!


Farabi

If you want to add a string on your procedure use,



jmp @f
Those who had universe knowledges can control the world by a micro processor.
http://www.wix.com/farabio/firstpage

"Etos siperi elegi"

donkey

The jmp @F method is not a recommended way to do things, first there is a penalty for data in the code section and also the penalty of the branch. You are better off just to put it in the data section where it is meant to be or if you must use data in the code section then put it after the return or at some point where it will not be executed. You can also do this, which is the best way to include it with the proc...

MyProc FRAME

DATA SECTION
SomeString DB "Hello there",0
CODE SECTION

; code goes here
ENDF


or in MASM

MyProc PROC

.DATA
SomeString DB "Hello there",0
.CODE

; code goes here
MyProc ENDP
"Ahhh, what an awful dream. Ones and zeroes everywhere...[shudder] and I thought I saw a two." -- Bender
"It was just a dream, Bender. There's no such thing as two". -- Fry
-- Futurama

Donkey's Stable

rags

If I understand you right Donkey ,this code
Quote from: donkey on March 01, 2005, 01:32:24 PM
MyProc FRAME

DATA SECTION
SomeString DB "Hello there",0
CODE SECTION

; code goes here
ENDF


or in MASM

MyProc PROC

.DATA
SomeString DB "Hello there",0
.CODE

; code goes here
MyProc ENDP

will put the SomeString into the .DATA of the program at compile time, even if the function MyProc is part of SomeLib.lib included with the Include directive?
Am I right ?
regards,
    Mike
God made Man, but the monkey applied the glue -DEVO

donkey

Hi Rags,

That's right. But with libs, even if you do things normally, that is have the .DATA section outside the proc, if you reference the data then the LINK.EXE program will import it along with any other dependancies so it is not necessary when using libs. This is mainly for snippets where you want to keep the code and data together in an easy to cut&paste package.
"Ahhh, what an awful dream. Ones and zeroes everywhere...[shudder] and I thought I saw a two." -- Bender
"It was just a dream, Bender. There's no such thing as two". -- Fry
-- Futurama

Donkey's Stable