flat assembler
Message board for the users of flat assembler.

Index > Main > "virtual at" and "struct" problem

Author
Thread Post new topic Reply to topic
felsario



Joined: 30 Dec 2013
Posts: 4
felsario
Hi all!

There is a code:
Code:
format PE Console

section '.code' import readable writeable executable

include 'win32ax.inc'

library msvcrt, 'msvcrt.dll'

import  msvcrt,\
        printf, 'printf',\
        malloc, 'malloc',\
        getchar, 'getchar',\
        strcpy, 'strcpy'


struct MY
    a rb 10
    b rd 1
ends


entry $

    cinvoke malloc, sizeof.MY

    virtual at eax
        m MY
    end virtual

    cinvoke strcpy, m.a, text       ; <- problem :/

    cinvoke getchar
    xor eax, eax
    ret


    text db 'hello', 0
    frmt db '%s %d', 13, 10, 0
    


What am i doing wrong?
Post 10 Dec 2014, 08:38
View user's profile Send private message Reply with quote
revolution
When all else fails, read the source


Joined: 24 Aug 2004
Posts: 17279
Location: In your JS exploiting you and your system
revolution
You can try with this:
Code:
    cinvoke strcpy, addr m.a, text       ; <- works now    
The problem is that you can't directly push a register with an offset:
Code:
push eax+m.a ;no such instruction exists    
So we change it to this:
Code:
lea edx,[eax+m.a]
push edx    
The cinvoke macro does this when you put "addr" in front of an address.
Post 10 Dec 2014, 08:47
View user's profile Send private message Visit poster's website Reply with quote
felsario



Joined: 30 Dec 2013
Posts: 4
felsario
Ah yes.. thank you very much!
Post 10 Dec 2014, 08:49
View user's profile Send private message Reply with quote
JohnFound



Joined: 16 Jun 2003
Posts: 3500
Location: Bulgaria
JohnFound
IMO, hidden usage of EDX register is very bad practice. I would suggest explicit use:
Code:
    lea     edx, [m.a]
    cinvoke ctrcpy, edx, text    


It will make the code much more readable and will prevent obscure bugs. One step further is to not use virtual:
Code:
    cinvoke malloc, sizeof.MY
    lea     edx, [eax+MY.a]
    cinvoke strcpy, edx, text     


P.S. The above examples are important when the offset inside the structure is no zero. But in the discussed code MY.a is zero, so the most simple code is without using additional register at all:
Code:
    cinvoke malloc, sizeof.MY
    cinvoke strcpy, eax, text     
Post 10 Dec 2014, 13:15
View user's profile Send private message Visit poster's website ICQ Number Reply with quote
revolution
When all else fails, read the source


Joined: 24 Aug 2004
Posts: 17279
Location: In your JS exploiting you and your system
revolution
JohnFound wrote:
P.S. The above examples are important when the offset inside the structure is no zero. But in the discussed code MY.a is zero, so the most simple code is without using additional register at all:
Code:
    cinvoke malloc, sizeof.MY
    cinvoke strcpy, eax, text     
I think this last one is one of those dangerous premature optimisation things. At the very least you should add an assert to make sure the structure is still what you expect of it.
Code:
assert MY.a = 0    
But perhaps an alternative solution that doesn't require editing the file again if the structure is changing is this:
Code:
if MY.a = 0
    cinvoke strcpy, eax, text
else
    cinvoke strcpy, addr eax+MY.a, text
end if    
Although I doubt such an optimisation will be really necessary (or noticeable) in anything that is using cinvoke anyway.
Post 10 Dec 2014, 13:49
View user's profile Send private message Visit poster's website Reply with quote
JohnFound



Joined: 16 Jun 2003
Posts: 3500
Location: Bulgaria
JohnFound
You are right, generally, but premature optimizations are not so bad in assembly language as in HLLs. There are very rare problems with them and as a rule easy to find and solve. Hidden use of register is much, much more dangerous IMHO.
Post 10 Dec 2014, 17:29
View user's profile Send private message Visit poster's website ICQ Number 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. Also on YouTube, Twitter.

Website powered by rwasa.