flat assembler
Message board for the users of flat assembler.

Index > Main > What is the preferred way to set a value in a virtual ?

Author
Thread Post new topic Reply to topic
Ton



Joined: 06 Jan 2005
Posts: 22
Ton
Consider the set up of two local variables
func:
push ebp
mov ebp,esp
sub esp,8
virtual at esp
fd dd ?
filled dd ?
end virtual
mov [filled],200h
; blabla
mov esp,ebp
pop ebp
ret

This works. But what is the preferred way to preset 'filled'. I expected
filled dd 200h
but then 'filled' is not set to 200h.

Best Regards,
Ton
Post 15 Mar 2005, 13:37
View user's profile Send private message Reply with quote
Tomasz Grysztar
Assembly Artist


Joined: 16 Jun 2003
Posts: 7715
Location: Kraków, Poland
Tomasz Grysztar
The data defined as "virtual" is not placed in the output file, therefore any initialization done there has no effect (except for the use with "load" directive, but that's a different topic) - it only defines labels for you. To initialize data addressed by those labels correctly at run time it's the programmer's responsibility, and generally it should be done just like you did: "mov [filled],200h" etc. Since you have allocated the space for those variables manually (with "sub esp,8"), it's obvious that you have fill them with the right values manually, too.

It is possible (with clever use of "virtual" and "load" directives) to make some macros to automatically allocate and initialize local variables with the defined values, but that's a different story.
Post 15 Mar 2005, 13:48
View user's profile Send private message Visit poster's website Reply with quote
Ton



Joined: 06 Jan 2005
Posts: 22
Ton
Thanks a lot for your fast reply.
load loads from the compiled source or file. Thus somehow I need to set a value. Something like :

virtual at esp
xor eax,eax
add eax, 200h
load dword filled from eax
end virtual

How is this clever use really done?

--
Ton
Post 15 Mar 2005, 14:33
View user's profile Send private message Reply with quote
Tomasz Grysztar
Assembly Artist


Joined: 16 Jun 2003
Posts: 7715
Location: Kraków, Poland
Tomasz Grysztar
I mean something like:
Code:
virtual at esp
 filled dd 200h
 size = $-esp
 load value dword from 0+esp
end virtual

push ebp
mov ebp,esp
sub esp,size
mov dword [esp],value    

But to make this automated, some very complicated macro solutions would have to be applied, not sure if it's worth it.
Post 15 Mar 2005, 15:42
View user's profile Send private message Visit poster's website Reply with quote
Ton



Joined: 06 Jan 2005
Posts: 22
Ton
Clever !

I stick to the initial approach Wink
Post 15 Mar 2005, 17:16
View user's profile Send private message Reply with quote
r22



Joined: 27 Dec 2004
Posts: 805
r22
If your code section is writeable/readable why not just have your value as part of the function rather than the stack

Code:
Function:
push ebp
mov ebp,esp
...
mov esp,ebp
pop ebp
retn 0
Over_Here: dd 200h    
Post 16 Mar 2005, 06:33
View user's profile Send private message AIM Address Yahoo Messenger Reply with quote
Ton



Joined: 06 Jan 2005
Posts: 22
Ton
Yes, that works as well, but then the executable is a bit longer.
Post 16 Mar 2005, 07:50
View user's profile Send private message Reply with quote
r22



Joined: 27 Dec 2004
Posts: 805
r22
that's not true.
SUB ESP,8 takes up more bytes than a arbitrary dword value 200h after a function return.
Post 17 Mar 2005, 06:09
View user's profile Send private message AIM Address Yahoo Messenger Reply with quote
MazeGen



Joined: 06 Oct 2003
Posts: 975
Location: Czechoslovakia
MazeGen
Wink

SUB takes 3 bytes, dword value 4 bytes:

83EC 08 sub esp,8
Post 18 Mar 2005, 17:58
View user's profile Send private message Visit poster's website Reply with quote
Tomasz Grysztar
Assembly Artist


Joined: 16 Jun 2003
Posts: 7715
Location: Kraków, Poland
Tomasz Grysztar
But you still have to initialize that dword with at least one more instruction, don't you? Wink
Post 18 Mar 2005, 18:12
View user's profile Send private message Visit poster's website Reply with quote
r22



Joined: 27 Dec 2004
Posts: 805
r22
Its actually
SUB ESP,8 <==This way used more often
MOV ESP,200h <==Because people are funny
Versus
Label: dd 200h <==This way shorter

MazeGen, I forgot the MOV because the space you make on the stack has to be initialized.

So using the Label: DD 200h after the return is less bytes
Post 18 Mar 2005, 21:56
View user's profile Send private message AIM Address Yahoo Messenger Reply with quote
Ton



Joined: 06 Jan 2005
Posts: 22
Ton
$ more try.asm
format elf executable
entry main

main:
mov eax, 1
mov dword ebx,[a]
int 80h
a: dd 5
$ more try2.asm

format elf executable
entry main

main:
mov eax, 1
mov dword ebx,5h
int 80h
$ ll try try2
-rwxrwxr-x 1 tonl tonl 101 Mar 21 08:53 try
-rwxr-xr-x 1 tonl tonl 96 Mar 21 08:55 try2


For now I think mov dword esp,200h is shorter.

Best Regards,
Ton
Post 21 Mar 2005, 07:57
View user's profile Send private message Reply with quote
Frank



Joined: 17 Jun 2003
Posts: 100
Frank
For your consideration:

Code:
        push    200h
        push    0

        virtual at esp
                fd     dd ?
                filled dd ?
        end virtual
    

Seven bytes to allocate space for, and initialize both variables. If you don't care about initializing "fd", then use "push eax" instead of "push 0", and it's down to six bytes. For small initialization values (-128 to +127) it's even less -- "push 127 / push eax" takes three bytes.
Post 23 Mar 2005, 17:40
View user's profile Send private message Reply with quote
Display posts from previous:
Post new topic Reply to topic

Jump to:  


< Last Thread | Next Thread >
Forum Rules:
You cannot post new topics in this forum
You cannot reply to topics in this forum
You cannot edit your posts in this forum
You cannot delete your posts in this forum
You cannot vote in polls in this forum
You cannot attach files in this forum
You can download files in this forum


Copyright © 1999-2020, Tomasz Grysztar.

Powered by rwasa.