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

Talk about things C/C++, some related to AutoHotkey
User avatar
jeeswg
Posts: 6902
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

;to download TDM-GCC 64-bit version:
;TDM-GCC : Download
;http://tdm-gcc.tdragon.net/download

;based on:
;MCode4GCC -- C/C++ to MCode Generator - AutoHotkey Community
;https://autohotkey.com/boards/viewtopic.php?f=6&t=4642

;links:
;Using the GNU Compiler Collection (GCC): Invoking GCC
;https://gcc.gnu.org/onlinedocs/gcc/Invoking-GCC.html
;InBuf function currently 32-bit only (machine code binary buffer searching) - AutoHotkey Community
;https://autohotkey.com/boards/viewtopic.php?f=5&t=28393&p=219717#p219717

;==================================================

;q:: ;C++ to machine code via TDM-GCC
vPathTempPart := A_Desktop "\z gcc temp"
vPathCp := "C:\TDM-GCC-64\bin\gcc.exe"

if !FileExist(vPathCp)
{
	MsgBox, % "error: file not found:`r`n" vPathCp
	return
}

;vCode := JEE_GetSelectedText()

vCode := Clipboard

if (vCode = "")
{
	MsgBox, % "error: no C++ code"
	return
}

if 0
vCode = ;continuation section
(Join`r`n
int TheAnswerToEverything() {
	return 42;
}
)

vFlags:= "-O3 "
vList := "32,64"
Loop, Parse, vList, % ","
{
	vBits := A_LoopField
	vMCode := "vMCode" vBits
	vAsm := TdmGccCppToAsm(vCode, vPathCp, vFlags " -m" vBits, vPathTempPart, vLog)
	;MsgBox, % vAsm
	%vMCode% := TdmGccAsmToMCode(vAsm)
	while RegExMatch(%vMCode%, "90$")
		%vMCode% := SubStr(%vMCode%, 1, -2)
	%vMCode% := Hex2Base64(%vMCode%)
	%vMCode% := Trim(%vMCode%, "`r`n")
	;MsgBox, % vLog
}
;vOutput := vMCode32 "`r`n" vMCode64
vOutput := "2,x86:" vMCode32 ",x64:" vMCode64

Clipboard := vOutput
MsgBox, % vOutput
return

;==================================================

;e.g.
/*
// set first ANSI character
int stringwrite(char *str)
{
  str[0] = 123;
  return 1;
}
*/

;x32 and x64 hex
;8B442404C6007BB801000000C3909090
;B801000000C6017BC390909090909090

;x32 and x64 hex (trailing 90s stripped)
;8B442404C6007BB801000000C3
;B801000000C6017BC3

;x32 and x64 base64 (trailing 90s stripped)
;2,x86:i0QkBMYAe7gBAAAAww==,x64:uAEAAADGAXvD

;==================================================

Hex2Base64(hex) {
	sz:=StringToBinary(b,hex)
	Base64enc(out,b,sz)
	VarSetCapacity(out,-1) ; Strip everything after first null byte
	;return SubStr(out,1,sz) ; Strip garbage at the end
	return out
}
;http://www.autohotkey.com/board/topic/85709-base64enc-base64dec-base64-encoder-decoder/
Base64enc( ByRef OutData, ByRef InData, InDataLen ) { ; by SKAN
	DllCall("Crypt32.dll\CryptBinaryToString" (A_IsUnicode?"W":"A")
		,UInt,&InData,UInt,InDataLen,UInt,1,UInt,0,UIntP,TChars,"CDECL Int")
	VarSetCapacity(OutData,Req:=TChars*(A_IsUnicode?2:1))
	DllCall("Crypt32.dll\CryptBinaryToString" (A_IsUnicode?"W":"A")
		,UInt,&InData,UInt,InDataLen,UInt,1,Str,OutData,UIntP,Req,"CDECL Int")
	Return TChars
}
; BinaryToString() / StringToBinary() from laszlo, updated by joedf
; http://ahkscript.org/forum/viewtopic.php?p=304556#304556
; fmt = 1:base64, 4:hex-table, 5:hex+ASCII, 10:offs+hex, 11:offs+hex+ASCII, 12:raw-hex
StringToBinary(ByRef bin, hex, fmt=12) {    ; return length, result in bin
	DllCall("Crypt32.dll\CryptStringToBinary","Str",hex,"UInt",StrLen(hex),"UInt",fmt,"UInt",0,"UInt*",cp,"UInt",0,"UInt",0,"CDECL UInt") ; get size
	VarSetCapacity(bin,cp)
	DllCall("Crypt32.dll\CryptStringToBinary","Str",hex,"UInt",StrLen(hex),"UInt",fmt,"UInt",&bin,"UInt*",cp,"UInt",0,"UInt",0,"CDECL UInt")
	Return cp
}

;==================================================

TdmGccCppToAsm(vCode, vPathCP, vFlags:="", vPathTempPart:="", ByRef vLog:="")
{
	if (vPathTempPart = "")
		vPathTempPart := A_Desktop "\z gcc temp"
	SplitPath, vPathTempPart,, vPathTempDir
	if !FileExist(vPathTempDir)
		FileCreateDir, % vPathTempDir
	if !FileExist(vPathTempDir)
	{
		MsgBox, % "error: dir not found:`r`n" vPathTempDir
		Exit
	}
	vPathTempSource := vPathTempPart " source.c"
	vPathTempAsm := vPathTempPart " asm"
	vPathTempX := vPathTempPart " x"
	vPathTempLog := vPathTempPart " log"
	FileAppend, % vCode "`r`n", % "*" vPathTempSource
	if !FileExist(vPathCP)
		return 0
	EnvGet, vEnvPath, Path
	SplitPath, vPathCP,, vDirCP
	EnvSet, Path, % vDirCP
	;MsgBox, %ComSpec% /c %vPathCP% %vFlags% -Wa`,-aln="%vPathTempAsm%" "%vPathTempSource%" -o "%vPathTempX%" 2> "%vPathTempLog%",, UseErrorLevel Hide
	RunWait, %ComSpec% /c %vPathCP% %vFlags% -Wa`,-aln="%vPathTempAsm%" "%vPathTempSource%" -o "%vPathTempX%" 2> "%vPathTempLog%",, UseErrorLevel Hide
	vErrorCP := ErrorLevel
	EnvSet, Path, % vEnvPath
	if (vErrorCP = "ERROR")
		return 0
	FileRead, vAsm, % vPathTempAsm
	FileRead, vLog, % vPathTempLog
	;MsgBox, % vAsm
	;MsgBox, % vLog
	FileDelete, % vPathTempSource
	FileDelete, % vPathTempAsm
	FileDelete, % vPathTempX
	FileDelete, % vPathTempLog
	return vAsm
}

;==================================================

TdmGccAsmToMCode(vAsm, vDoClean:=1)
{
	if vDoClean
	{
		vAsm2 := vAsm, vAsm := ""
		Loop, Parse, vAsm2, `n, `r
		{
			if InStr(A_LoopField, ".ident	" """" "GCC: (GNU)")
				break
			vAsm .= A_LoopField "`n"
		}
	}
	vPos := 1, vMatch := "", vOutput := ""

	;old-style (to variables)
	;while vPos := RegExMatch(vAsm, "`ami)^\s*\d+(\s[\dA-F]{4}\s|\s{6})([\dA-F]+)", vMatch, vPos+StrLen(vMatch))
	;	MsgBox, % vMatch2
	;while vPos := RegExMatch(vAsm, "`ami)^\s*\d+(\s[\dA-F]{4}\s|\s{6})([\dA-F]+)", vMatch, vPos+StrLen(vMatch))
	;	vOutput .= vMatch2

	;new-style (to RegExMatch object)
	;while vPos := RegExMatch(vAsm, "O`ami)^\s*\d+(\s[\dA-F]{4}\s|\s{6})([\dA-F]+)", oMatch, vPos+StrLen(oMatch.0))
	;	MsgBox, % oMatch.2
	while vPos := RegExMatch(vAsm, "O`ami)^\s*\d+(\s[\dA-F]{4}\s|\s{6})([\dA-F]+)", oMatch, vPos+StrLen(oMatch.0))
		vOutput .= oMatch.2
	return vOutput
}

;==================================================
homepage | tutorials | wish list | fun threads | donate
WARNING: copy your posts/messages before hitting Submit as you may lose them due to CAPTCHA
User avatar
jeeswg
Posts: 6902
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

;stringlen := MCode("2,x86:i0wkBDPAOAF0B0CAPAgAdfnD,x64:M8A4AXQKSP/B/8CAOQB19vPD") ;equivalent to line below
stringlen := MCode("2,x86:i1QkBDHAgDoAdBCQjXQmAIPAAYA8AgB19/PD88M=,x64:gDkAdBpIg8EBMcAPH0QAAEiDwQGDwAGAef8AdfPzwzHAww==")
stringlen2 := MCode("2,x86:i1QkBDHAZoM6AHQQjXQmAIPAAWaDPEIAdfbzw/PD,x64:ZoM5AHQaSIPBAjHADx9AAEiDwQKDwAFmg3n+AHXy88MxwMM=")
;vText := "abcdefghijklmnopqrstuvwxyz"
;I use Chr(257) aka Chr(0x101), because it gives a stream of 1-bytes, with no null bytes
vText := ""
Loop, 26
	vText .= Chr(0x101)
MsgBox, % DllCall(stringlen, "AStr",vText, "Cdecl") ;26
MsgBox, % DllCall(stringlen, "WStr",vText, "Cdecl") ;1 ;expected 52
MsgBox, % DllCall(stringlen, "Str",vText, "Cdecl") ;1 ;expected 52
MsgBox, % DllCall(stringlen, "Ptr",&vText, "Cdecl") ;1 ;expected 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
return

;==================================================

;GitHub - joedf/MCode4GCC: MCode4GCC is an MCode generator using the GCC Compiler.
;https://github.com/joedf/MCode4GCC

MCode(mcode) {
	static e := {1:4, 2:1}, c := (A_PtrSize=8) ? "x64" : "x86"
	if (!regexmatch(mcode, "^([0-9]+),(" c ":|.*?," c ":)([^,]+)", m))
		return
	if (!DllCall("crypt32\CryptStringToBinary", "str", m3, "uint", 0, "uint", e[m1], "ptr", 0, "uint*", s, "ptr", 0, "ptr", 0))
		return
	p := DllCall("GlobalAlloc", "uint", 0, "ptr", s, "ptr")
	if (c="x64")
		DllCall("VirtualProtect", "ptr", p, "ptr", s, "uint", 0x40, "uint*", op)
	if (DllCall("crypt32\CryptStringToBinary", "str", m3, "uint", 0, "uint", e[m1], "ptr", p, "uint*", s, "ptr", 0, "ptr", 0))
		return p
	DllCall("GlobalFree", "ptr", p)
}

;==================================================

/*
source code
[stringlen:]
MCode Tutorial (Compiled Code in AHK) - AutoHotkey Community
https://autohotkey.com/boards/viewtopic.php?f=7&t=32
[stringlen2: a slight variant using 'short' instead of 'char', not present at the link]

int stringlen(char *str)
{
  int i=0;
  for (; str[i]!=0; i++);
  return i;
}

int stringlen2(short *str)
{
  int i=0;
  for (; str[i]!=0; i++);
  return i;
}

note: for 'stringlen' the tutorial states the machine code as:
2,x86:i0wkBDPAOAF0B0CAPAgAdfnD,x64:M8A4AXQKSP/B/8CAOQB19vPD
my script gave instead:
2,x86:i1QkBDHAgDoAdBCQjXQmAIPAAYA8AgB19/PD88M=,x64:gDkAdBpIg8EBMcAPH0QAAEiDwQGDwAGAef8AdfPzwzHAww==
both appear to function identically
*/
- 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.
homepage | tutorials | wish list | fun threads | donate
WARNING: copy your posts/messages before hitting Submit as you may lose them due to CAPTCHA
Helgef
Posts: 4709
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: 6902
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

replace null bytes with spaces

C++:
int repnullsspace(char *str, int size)
{
  int i=0;
  for (; i!=size; i++)
    if (str[i]==0)
      str[i]=32;
  return 1;
}

AHK:
repnullsspace := MCode("2,x86:i0wkCItEJASFyY0UCHQQkIA4AHUDxgAgg8ABOdB18bgBAAAAww==,x64:hdJ0HY1C/0iNRAEBDx9AAIA5AHUDxgEgSIPBAUg5wXXvuAEAAADD")
DllCall(repnullsspace, "Ptr",&vData, "Int",vSize, "Cdecl")

==================================================

replace null bytes with a specified byte

C++:
int repnulls(char *str, int size, int charnew)
{
  int i=0;
  for (; i!=size; i++)
    if (str[i]==0)
      str[i]=charnew;
  return 1;
}

AHK:
repnulls := MCode("2,x86:U4tcJAyLRCQIi0wkEIXbjRQYdA6AOAB1AogIg8ABOdB18rgBAAAAW8M=,x64:hdJ0HY1C/0iNRAEBDx9AAIA5AHUDRIgBSIPBAUg5wXXvuAEAAADD")

vPath := A_AhkPath
oFile := FileOpen(vPath, "r")
oFile.Pos := 0
oFile.RawRead(vData, vSize := oFile.Length)
oFile.Close()

DllCall(repnulls, "Ptr",&vData, "Int",vSize, "Int",Ord("@"), "Cdecl")

Clipboard := StrGet(&vData, vSize, "CP0")
MsgBox, % "done"
return
[EDIT:] replaced str=0 with str==0.
Last edited by jeeswg on 25 May 2018, 12:36, edited 5 times in total.
homepage | tutorials | wish list | fun threads | donate
WARNING: copy your posts/messages before hitting Submit as you may lose them due to CAPTCHA
Helgef
Posts: 4709
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=0, this is assignment.

Cheers.
User avatar
jeeswg
Posts: 6902
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.
homepage | tutorials | wish list | fun threads | donate
WARNING: copy your posts/messages before hitting Submit as you may lose them due to CAPTCHA
User avatar
jeeswg
Posts: 6902
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

C++

int stringedit(char *str)
{
  int i=0;
  for (; i<20; i++)
    str[i]=3;
  return i;
}

int stringedit(char *str)
{
  int i=0;
  for (; i<20; i++)
    str[0]=i;
  return i;
}

int stringedit(char *str)
{
  int i=0;
  for (; i<20; i++)
    str[0]=i+10;
  return i;
}

// didn't work
int stringedit(char *str)
{
  int i=0;
  for (; i<20; i++)
    str[i]=i;
  return i;
}
homepage | tutorials | wish list | fun threads | donate
WARNING: copy your posts/messages before hitting Submit as you may lose them due to CAPTCHA
User avatar
nnnik
Posts: 4500
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: 4709
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: 6902
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

;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

q:: ;test byte write
vSize := 255
VarSetCapacity(vData, vSize, 0)
vFunc := MCode("2,x86:i0QkBGYPbwUAAAAAxkAQEMZAEREPEQDGQBISxkATE7gUAAAAw5CQkJCQkJCQkJCQAAECAwQFBgcICQoLDA0ODw==,x64:Zg9vBQAAAAC4FAAAAMZBEBDGQRERDxEBxkESEsZBExPDkJCQkJCQkJCQkJCQkJCQAAECAwQFBgcICQoLDA0ODw==")
MsgBox, % DllCall(vFunc, "Ptr",&vData, "Cdecl")

vOutput := ""
Loop, % vSize
	vOutput .= (A_Index=1?"":",") NumGet(&vData, A_Index-1, "UChar")
MsgBox, % vOutput
return

/*
int stringedit(char *str)
{
  int i=0;
  for (; i<20; i++)
    str[i]=i;
  return i;
}
*/
homepage | tutorials | wish list | fun threads | donate
WARNING: copy your posts/messages before hitting Submit as you may lose them due to CAPTCHA
Helgef
Posts: 4709
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: 6902
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.
homepage | tutorials | wish list | fun threads | donate
WARNING: copy your posts/messages before hitting Submit as you may lose them due to CAPTCHA
Helgef
Posts: 4709
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: 6902
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.
homepage | tutorials | wish list | fun threads | donate
WARNING: copy your posts/messages before hitting Submit as you may lose them due to CAPTCHA
User avatar
nnnik
Posts: 4500
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: 6902
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.
homepage | tutorials | wish list | fun threads | donate
WARNING: copy your posts/messages before hitting Submit as you may lose them due to CAPTCHA
User avatar
nnnik
Posts: 4500
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: 6902
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 ... athematik)
Thanks, Google Translate didn't suggest it. And searches for mathematics and Fakultät just gave me faculties (institutions).
homepage | tutorials | wish list | fun threads | donate
WARNING: copy your posts/messages before hitting Submit as you may lose them due to CAPTCHA
Helgef
Posts: 4709
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: 6902
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/
homepage | tutorials | wish list | fun threads | donate
WARNING: copy your posts/messages before hitting Submit as you may lose them due to CAPTCHA

Return to “C/C++”

Who is online

Users browsing this forum: No registered users and 7 guests