PageRenderTime 51ms CodeModel.GetById 20ms RepoModel.GetById 0ms app.codeStats 0ms

/QuickAlchemy/Hack/AS3/AlchemyAD_hack.as

http://flaswf.googlecode.com/
ActionScript | 158 lines | 63 code | 11 blank | 84 comment | 0 complexity | 125d8ae8cb8aa531026c88e3e515a2ca MD5 | raw file
  1. /*
  2. [Adobe Alchemy Hacks]
  3. AlchemyAD_hack.as
  4. {Simple example for using inline asm and
  5. accessing memory in AS3}
  6. By Bruce Jawn (January/23/2011)
  7. [http://bruce-lab.blogspot.com]
  8. To compile this source file with Alchemy:
  9. cd /cygdrive/f/alchemy/
  10. java -Xms16M -Xmx196M -jar F:/alchemy/bin/asc.jar -AS3 -strict -import F:/alchemy/flashlibs/global.abc -import F:/alchemy/flashlibs/playerglobal.abc -config Alchemy::Debugger=false -config Alchemy::NoDebugger=true -config Alchemy::Shell=false -config Alchemy::NoShell=true -config Alchemy::LogLevel=0 -config Alchemy::Vector=true -config Alchemy::NoVector=false -config Alchemy::SetjmpAbuse=false -swf AlchemyAD_hack,800,600,60 AlchemyAD_hack.as
  11. */
  12. package
  13. {
  14. import flash.display.Sprite;
  15. import flash.text.TextField;
  16. import flash.utils.ByteArray;
  17. import flash.utils.Endian;
  18. import flash.system.ApplicationDomain;
  19. public class AlchemyAD_hack extends Sprite{
  20. public function AlchemyAD_hack ()
  21. {
  22. /*Create the print shell*/
  23. var MyShell:TextField=new TextField();
  24. MyShell.height=600;
  25. addChild(MyShell);
  26. function print(output:*):void
  27. {
  28. MyShell.appendText(String(output));
  29. MyShell.appendText("\n");
  30. }
  31. /*Test Memory Write*/
  32. //ByteArray for the test
  33. var testData:ByteArray = new ByteArray();
  34. testData.endian = Endian.LITTLE_ENDIAN;
  35. testData.length=0xffff*4;
  36. //select testdata in memory
  37. ApplicationDomain.currentDomain.domainMemory=testData;
  38. var AdrInt:int=0;
  39. //the test value we will write into testData via memory
  40. var testValue:int=123;
  41. //write the testValue into testData
  42. ApplicationDomain.currentDomain.domainMemory[0] = testValue;
  43. //Check if testValue has been written into testData
  44. print(testData[0]);//should print 123
  45. /*Test Memory Read*/
  46. var readedValue:int=ApplicationDomain.currentDomain.domainMemory[0];
  47. print(readedValue);//should print 123
  48. /*Test Inline ASM*/
  49. //label and jump
  50. __asm(jump, target('myLable'));
  51. print("not jumped!");//this line will be skipped
  52. __asm(label, lbl('myLable'));
  53. print("jumped!");
  54. //switch jump
  55. var myState:int=1;
  56. __asm(push(myState), switchjump('state0','state1','state2'));
  57. __asm(lbl('state0'));
  58. print("This is state0.");
  59. __asm(lbl('state1'));
  60. print("This is state1.");
  61. __asm(lbl('state2'));
  62. print("This is state2.");
  63. //iftrue jump
  64. var temp:int=1;
  65. __asm(push(temp!=0), iftrue, target('turejump'));
  66. print("iftrue not jumped!");//this line will be skipped
  67. __asm(label, lbl('turejump'));
  68. print("iftrue jumped!");
  69. /*Test Alchemy Memory Instructions*/
  70. //All memory opcodes listed here:
  71. /*
  72. Get a 32 bit value at the location addr and return as an int:
  73. _mr32(addr:int):int{ return __xasm<int>(push(addr), op(0x37)); }
  74. Get a 16 bit unsigned value at the location addr and return as an int:.
  75. _mru16(addr:int):int { return __xasm<int>(push(addr), op(0x36)); }
  76. Get a 16 bit signed value at the location addr and return as an int:
  77. _mrs16(addr:int):int { return __xasm<int>(push(addr), op(0x36)); } // li16
  78. Get a 8 bit value at the location addr and return as an int:
  79. _mru8(addr:int):int { return __xasm<int>(push(addr), op(0x35)); }
  80. Get a 8 bit value at the location addr and return as an int:
  81. _mrs8(addr:int):int { return __xasm<int>(push(addr), op(0x35)); }
  82. Get a float value at the location addr and return as an Number:
  83. _mrf(addr:int):Number { return __xasm<int>(push(addr), op(0x38)); }
  84. Get a double value at the location addr and return as an Number:
  85. _mrd(addr:int):Number { return __xasm<int>(push(addr), op(0x39)); }
  86. Write an int as a 32 bit value at the location addr:
  87. _mw32(addr:int, val:int):void { __asm(push(val), push(addr), op(0x3c)); }
  88. Write an int as a 16 bit value at the location addr:
  89. _mw16(addr:int, val:int):void { __asm(push(val), push(addr), op(0x3b)); }
  90. Write an int as a 8 bit value at the location addr:
  91. _mw8(addr:int, val:int):void { __asm(push(val), push(addr), op(0x3a)); }
  92. Write a Number as a float at the location addr:
  93. _mwf(addr:int, val:Number):void { __asm(push(val), push(addr), op(0x3d)); }
  94. Write a Number as a double at the location addr:
  95. _mwd(addr:int, val:Number):void { __asm(push(val), push(addr), op(0x3e)); }
  96. */
  97. //Write an int 654321 as a 32 bit value at the location 1000
  98. __asm(push(654321),push(1000),op(0x3c));
  99. //Trace the memory
  100. ApplicationDomain.currentDomain.domainMemory.position=1000;
  101. print(ApplicationDomain.currentDomain.domainMemory.readInt());//should print 654321
  102. //Get a 32 bit value at the location 1000 and return as an int
  103. var temp:int=__xasm<int>(push(1000), op(0x37));
  104. print(temp);//should print 654321
  105. /*Test some AVM2 Instructions*/
  106. //More AVM2 Instructions can be found at:
  107. //http://www.anotherbigidea.com/javaswf/avm2/AVM2Instructions.html
  108. //test add: 0xA0
  109. var var1:int=123;
  110. var var2:int=321;
  111. //write (var1+var2)=444 to testData[0] via memory
  112. //ApplicationDomain.currentDomain.domainMemory.position=AdrInt;
  113. //ApplicationDomain.currentDomain.domainMemory.writeInt(var1+var2);
  114. __asm(push(var1), push(var2), op(0xA0), push(AdrInt), op(0x3c));
  115. testData.position=0;
  116. print(testData.readInt());//should print 444
  117. //test subtract: 0xA1
  118. var result:int=__xasm(push(var1), push(var2), op(0xA1));//var result=var1-var2;
  119. print(result);//should print -198
  120. //write (var1-var2)=-198 to testData[1] via memory in a different way
  121. __asm(push(result),push(AdrInt+4),op(0x3c));
  122. testData.position=4;
  123. print(testData.readInt());//should print -198
  124. }//end of function AlchemyAD_hack
  125. }//end of class
  126. }//end of pacakge
  127. /*
  128. References:
  129. http://labs.adobe.com/wiki/index.php/Alchemy:Documentation:Developing_with_Alchemy:AS3_API
  130. http://unitzeroone.com/blog/2009/05/22/another-scream-on-flash-alchemy-memory-and-compilers/
  131. http://blog.frankula.com/?p=211
  132. http://forums.adobe.com/message/2616985
  133. http://forums.adobe.com/message/3001861
  134. Special thanks to Bernd Paradies (http://forums.adobe.com/people/Bernd%20Paradies)
  135. */