C++: C++ to machine code via TDM-GCC

Discuss other programming languages besides AutoHotkey
User avatar
jeeswg
Posts: 4990
Joined: 19 Dec 2016, 01:58
Location: UK

C++: C++ to machine code via TDM-GCC

24 May 2018, 16:07

Based on the code and tutorials here (thanks joedf and nnnik):
MCode4GCC -- C/C++ to MCode Generator - AutoHotkey Community
https://autohotkey.com/boards/viewtopic.php?f=6&t=4642
MCode Tutorial (Compiled Code in AHK) - AutoHotkey Community
https://autohotkey.com/boards/viewtopic.php?f=7&t=32
MCode tutorial - AutoHotkey Community
https://autohotkey.com/boards/viewtopic.php?f=11&t=116

- I have created a stand-alone script to convert C++ code to machine code.

- I have assumed that you can strip trailing 0x90 bytes from the end of machine code. Is this correct?
- Does it make a difference speed-wise whether you define the C++ function as Cdecl or Stdcall?
- I based the command-line text off of joedf's script, can anyone explain the role of vPathTempX and 2>, or indicate any good sources re. the command-line text. (The GCC documentation is quite hard to follow.)
- Any suggestions for different command-line parameters to use would be welcome.
- Any comments are welcome. Thanks.

Code: [Select all] [Expand] [Download] GeSHi © Codebox Plus

User avatar
jeeswg
Posts: 4990
Joined: 19 Dec 2016, 01:58
Location: UK

Re: C++: C++ to machine code via TDM-GCC

24 May 2018, 22:28

- A further point:
- Why do WStr and Ptr not work here as expected?

Code: [Select all] [Expand] [Download] GeSHi © Codebox Plus

- Ultimately I'm looking to create a script that starts at a pointer, loops through the specified number of bytes, replacing null bytes with a specified byte. And then the same thing again but with shorts/ints.
Last edited by jeeswg on 25 May 2018, 08:43, edited 1 time in total.
Helgef
Posts: 3163
Joined: 17 Jul 2016, 01:02
Contact:

Re: C++: C++ to machine code via TDM-GCC

25 May 2018, 03:01

On unicode build, "astr", myUnicodeString, will pass an address to a temporary ansi string, which has been converted from the unicode string (on unicode builds your strings are always unicode strings). How do you expect chr(0x101) to be converted? The way it is converted is not documented as far as I know. Suitably, on my default ansi code page, it seems to convert it to "???..." (chr(0x3f)), but this really doesn't matter (I get the same result from strputget). When "astr", myUnicodeString returns, the content of myUnicodeString is updated to the content of the temporary string passed to the dllcall, which in your case* contains one byte characters, as your result shows, converting a one byte character to a two byte character can only happen in one reasonable way, that is, (on my default ansi code page) 0x3f -> 0x003f, which has one byte before the first zero byte, which is what your function finds.

If you omit the first dllcall, you'll get your expected result.

See dllcall types and Script Compatibility for reference.

* see this.

Cheers. (and thanks for sharing your script :wave: )

Edit:
- Ultimately I'm looking to create a script that starts at a pointer, loops through the specified number of bytes, replacing null bytes with a specified byte. And then the same thing again but with shorts/ints.
Short of ints, this and this can do that.
User avatar
jeeswg
Posts: 4990
Joined: 19 Dec 2016, 01:58
Location: UK

Re: C++: C++ to machine code via TDM-GCC

25 May 2018, 08:55

- @Helgef: Thanks. Sorry but I'm still unclear.
- Ignoring AStr/WStr for the moment. My thoughts are these: if I use Ptr and &var, the function should receive a pointer to the start of the string, and loop through each byte. If it's a 26-char Unicode string with no null bytes inside of it, that's 52 non null-bytes, hence the function would return 52, a length of 52 bytes.
- I had been looking through your functions, although if I can't fix this problem, it's hard to make sense of what's going on. So I'd be 'guessing'.
- Perhaps you could try writing C++ code for what I'm trying to do.

- These functions do seem to work with Ptr and &var:

Code: [Select all] [Expand] [Download] GeSHi © Codebox Plus

[EDIT:] replaced str[i]=0 with str[i]==0.
Last edited by jeeswg on 25 May 2018, 12:36, edited 5 times in total.
Helgef
Posts: 3163
Joined: 17 Jul 2016, 01:02
Contact:

Re: C++: C++ to machine code via TDM-GCC

25 May 2018, 09:02

I recommend you take a course In c /c ++ before proceeding.
str[i]=0, this is assignment.

Cheers.
User avatar
jeeswg
Posts: 4990
Joined: 19 Dec 2016, 01:58
Location: UK

Re: C++: C++ to machine code via TDM-GCC

25 May 2018, 09:04

- Thanks, I've fixed it. My repnulls function is now working.
- [I hadn't double-checked that function yet because the stringlen function still wasn't(/isn't) working.]
- However, I still haven't got the stringlen function working.
- I've looked through what you've said carefully, multiple times. Apologies but I still don't see an explanation for the problem.
- If you could create a version of the stringlen function that works with Ptr, that might help to comprehend the situation better. Thanks.
User avatar
jeeswg
Posts: 4990
Joined: 19 Dec 2016, 01:58
Location: UK

Re: C++: C++ to machine code via TDM-GCC

25 May 2018, 19:46

- There are some basic examples here, but the 4th one isn't working.
- All 4 compile, but the last one doesn't seem to have an effect, or return a value.

Code: [Select all] [Expand] [Download] GeSHi © Codebox Plus

User avatar
nnnik
Posts: 3225
Joined: 30 Sep 2013, 01:01
Location: Germany

Re: C++: C++ to machine code via TDM-GCC

25 May 2018, 23:28

You should probably repeat the chapter about strings and string encodings
Recommends AHK Studio
Helgef
Posts: 3163
Joined: 17 Jul 2016, 01:02
Contact:

Re: C++: C++ to machine code via TDM-GCC

26 May 2018, 03:30

// didn't work

It works as I expect, it returns 20 and initialises the char array as: 0, 1 ... 19
Apologies but I still don't see an explanation for the problem.

No need for apologies, I didn't see this comment I will try to see if I can better my explaination.

Edit:
jeeswg wrote:- If you could create a version of the stringlen function that works with Ptr, that might help to comprehend the situation better. Thanks.

Your stringlen code works, "astr", str modifies the string, if you omit that dllcall, you will get your expected result from "ptr", &str.

Cheers.
User avatar
jeeswg
Posts: 4990
Joined: 19 Dec 2016, 01:58
Location: UK

Re: C++: C++ to machine code via TDM-GCC

26 May 2018, 04:59

YES. OK Helgef, here we are.
As you imply, if I comment out the AStr line, I get the 52 for the other lines that I was expecting.
AStr is *modifying* the string.
Hmm, it's a surprise that AStr is modifying the string, however!
I had thought that AHK would create a temporary variable based on vText, and pass that, leaving vText untouched.
(So perhaps you don't need to change your explanation. I'll give it a reread.)

Code: [Select all] [Download] GeSHi © Codebox Plus

;MsgBox, % DllCall(stringlen, "AStr",vText, "Cdecl") ;26 ;if this line is called, the next 3 lines return 1
MsgBox, % DllCall(stringlen, "WStr",vText, "Cdecl") ;52
MsgBox, % DllCall(stringlen, "Str",vText, "Cdecl") ;52
MsgBox, % DllCall(stringlen, "Ptr",&vText, "Cdecl") ;52

MsgBox, % DllCall(stringlen2, "AStr",vText, "Cdecl") ;13
MsgBox, % DllCall(stringlen2, "WStr",vText, "Cdecl") ;26
MsgBox, % DllCall(stringlen2, "Str",vText, "Cdecl") ;26
MsgBox, % DllCall(stringlen2, "Ptr",&vText, "Cdecl") ;26

It's nice to know that my 4th stringedit function works for you, but I can't think why it isn't working for me. Could you post the machine code that you're getting? And/or check the code below. Thanks.

Code: [Select all] [Expand] [Download] GeSHi © Codebox Plus

Helgef
Posts: 3163
Joined: 17 Jul 2016, 01:02
Contact:

Re: C++: C++ to machine code via TDM-GCC

26 May 2018, 06:23

Hello jeeswg :)
Hmm, it's a surprise that AStr is modifying the string, however!
I had thought that AHK would create a temporary variable based on vText, and pass that, leaving vText untouched.

It does create a temporary string based on vText and passes its address, but then, it updates vText to match this temporary string, this is sort of the purpose of using "xstr" vs "ptr", you avoid the need to do strput/get and varsetcapacity str,-1 when passing/retrieving strings. I suppose the documentation could explicitly state that both "astr" and "wstr" will update the variable as described for "str".
dllcall -> str wrote:A string such as "Blue" or MyVar. If the called function modifies the string and the argument is a naked variable, its contents will be updated. For example, the following call would convert the contents of MyVar to uppercase: DllCall("CharUpper", "Str", MyVar).

I think your main problem is that you had some false expectations on how chr(0x101) would be converted to an ansi character, passing "astr", chr(0x101) to dllcall is basically a lie and you got punished for lying.


I can't think why it isn't working for me

You will never figure it out unless you take my advice and take a course in c / c++, you need some basic understanding of optimising compilers. I'll give you a hint which you can ponder until your hair turns grey (or falls off, if it is already grey), if you change i<20 to i<15 your script will produce runnable code :morebeard:. Also, I recommend you use v2, or at least use try / check errorlevel for your your dllcalls. Doing this in v1 is painful :facepalm: .

Cheers.
User avatar
jeeswg
Posts: 4990
Joined: 19 Dec 2016, 01:58
Location: UK

Re: C++: C++ to machine code via TDM-GCC

26 May 2018, 06:50

Helgef wrote:It works as I expect, it returns 20 and initialises the char array as: 0, 1 ... 19
So it worked for you didn't it?

The error 0xc0000005 is access violation, suggesting a problem reading/writing data, however, the 1st of 4 stringedit examples successfully wrote to the first 20 bytes.
Last edited by jeeswg on 26 May 2018, 07:07, edited 1 time in total.
Helgef
Posts: 3163
Joined: 17 Jul 2016, 01:02
Contact:

Re: C++: C++ to machine code via TDM-GCC

26 May 2018, 07:05

So it worked for you didn't it?
I used my script :angel: .
User avatar
jeeswg
Posts: 4990
Joined: 19 Dec 2016, 01:58
Location: UK

Re: C++: C++ to machine code via TDM-GCC

26 May 2018, 07:20

- If 15 works but 16 doesn't, that suggests that *something* can only handle a nibble (4 bits of information). Although some of my other functions suggested that there is no limit of 16.
- I looked through a tonne of links: AutoHotkey mcode "access violation", I didn't find any solution.
User avatar
nnnik
Posts: 3225
Joined: 30 Sep 2013, 01:01
Location: Germany

Re: C++: C++ to machine code via TDM-GCC

26 May 2018, 07:25

Did you really read both of my tutorials?
Recommends AHK Studio
User avatar
jeeswg
Posts: 4990
Joined: 19 Dec 2016, 01:58
Location: UK

Re: C++: C++ to machine code via TDM-GCC

26 May 2018, 07:29

- @nnnik: Yes. Where did you come across this function: MyFunction(a-1,b*a), you call it the 'faculty' function, do you have a reference for it in German/English? Thanks.
- [EDIT:] Looking at it again the function pointer stuff looks like it could be relevant, but from reading the tutorial I'm not clear on what the nature of the problem and solution are.
Last edited by jeeswg on 26 May 2018, 07:32, edited 1 time in total.
User avatar
nnnik
Posts: 3225
Joined: 30 Sep 2013, 01:01
Location: Germany

Re: C++: C++ to machine code via TDM-GCC

26 May 2018, 07:31

Well I guess I never mentioned that you need to use VarSetCapacity to initialize data in them then.
the name might be wrong but on your calculator it normally looks like : n!
Recommends AHK Studio
User avatar
jeeswg
Posts: 4990
Joined: 19 Dec 2016, 01:58
Location: UK

Re: C++: C++ to machine code via TDM-GCC

26 May 2018, 07:35

Factorial - Wikipedia
https://en.wikipedia.org/wiki/Factorial
Fakultät (Mathematik) – Wikipedia
https://de.wikipedia.org/wiki/Fakult%C3%A4t_(Mathematik)
Thanks, Google Translate didn't suggest it. And searches for mathematics and Fakultät just gave me faculties (institutions).
Helgef
Posts: 3163
Joined: 17 Jul 2016, 01:02
Contact:

Re: C++: C++ to machine code via TDM-GCC

26 May 2018, 07:35

It is called factorial in English ( n! )
User avatar
jeeswg
Posts: 4990
Joined: 19 Dec 2016, 01:58
Location: UK

Re: C++: C++ to machine code via TDM-GCC

26 May 2018, 11:16

- Add VarSetCapacity to the script? Add a VarSetCapacity C++ equivalent to the C++ code?
- Any ideas re. what's causing the access violation? It seems that a lot of users have had that error when using machine code, but never got a response on the forum. Thanks.
- Or what, is 'char *str', a pointer, but not a 4-byte/8-byte number (depending on the OS), but 0.5 bytes!? So it needs to converted to be a ptr, or replaced with *something*?
Why C++ Member Function Pointers Are 16 Bytes Wide - Vlad Lazarenko
http://lazarenko.me/wide-pointers/

Return to “Other Programming Languages”

Who is online

Users browsing this forum: No registered users and 1 guest