Poking AMOS string constants

Started by Mequa, October 23, 2010, 12:58:39 AM

Previous topic - Next topic

0 Members and 1 Guest are viewing this topic.

Mequa

AMOS did something amusing if you used =Varptr() to grab the address of string constants (such as A$="Hello") and tried to modify their contents using the Poke command. This also works under UAE.

I tried this on my friend Douglas back in the day, let's just say this little program didn't do quite as he expected:

A$="Douglas is a duck!"
Poke Varptr(A$)+13,Peek(Varptr(A$)+13)+2

(Caution: the above results may be offensive!)

BooBoo

Ok I didnt even need to try this to figure it out ;D

But intresting that you can add to the value

Is there an easy way I can make A$ = A
E.g A$="22"
I wont A to equal the same as A$

I want to be able to HEX edit values of my compiled code thats why im using A$...

Mequa

Like this?

A$="22"
A=Val("$"+A$)
Print Hex$(A)

Mequa

If you want to dynamically modify a hex string in the AMOS source from within the program itself, you can try something like this:

' Hex string to be hacked (3 characters):     
A$="ABC"

Print "A$="+Chr$(34)+A$+Chr$(34)
I=Val("$"+A$) : If I>=0 and I<4096 Then Colour 1,I Else Colour 1,$A40

' Hex value to A replace A$ (prompted for demonstration):
A=$DEF : Print "Enter a new HEX value without $ (ABC): "
Input "";HEXINPUT$ : A=Val("$"+HEXINPUT$) : If HEXINPUT$="" Then End

' Grab the hex string version of A (without $):
B$=Hex$(A) : B$=Right$(B$,Len(B$)-1)
' Test if the string A$ is large enough:
If Len(A$)<Len(B$) Then Print "Error: String A$ too small!" : Bell : End

' Poke the contents into the string A$ and append spaces:
For X=0 To Len(A$)-1
   If X<Len(B$) Then P=Peek(Varptr(B$)+X) Else P=32
   Poke Varptr(A$)+X,P
Next X

' Finally get the new value as an integer and display in hex: 
A=Val("$"+A$) : Print "New value: "+Right$(Hex$(A),Len(Hex$(A))-1)
Print "A$="+Chr$(34)+A$+Chr$(34)
If A>=0 and A<4096 Then Colour 1,A Else Colour 1,$A40


SamuraiCrow


BooBoo

Thanks Dude :)

A$="22"
A=Val(A$)
Seems to work fine - Well ill simply create a seperate program to edit the values of the compiled code

Mequa

#6
A=Val("22") will return 22 in decimal, I believe AMOS requires A=Val("$22") for hexadecimal from string.

To convert back to string hex format use H$=Hex$(A). This will create a string beginning with $ followed by the hex values.

As for my original 2 line AMOS program above, at first it doesn't appear to do anything.
Then you return to the editor... :D

Mequa


Lonewolf10


That is cool, but bad :(
You only want the string to be manipulated during the program and return to it's original state when you return to the editor. Thankfully I rarely use the Varptr function in my programs, but for those that did it would make developing programs interesting.
If you ran the program again, would the first line change to:

A$="Douglas is a huck!"

...???


Regards,
Lonewolf10


Mequa

Yes, and then "juck" and "luck" etc.

This could be fixed as follows:

A$="Bob is a duck!"
Poke Vartpr(A$)+9,102

Lonewolf10


Here's one for you (just found it today). Type the following into Direct Mode:

Print "S";pen$(4)

Then try using direct mode after that!!!

(You'll find all text is invisible after the "S" has been printed! The fix is to go back to the Editor and then return to Direct Mode)


Regards,
Lonewolf10


Mequa

A$="AMOS is cool"
P=Varptr(A$)
A$=A$+" indeed!"
Loke P+8,1936222580
Print A$


OK, that's a bit rude :)
But it does demonstrate AMOS's difference between string constants and string variables.

Hungry Horace

Quote from: Lonewolf10 on November 12, 2010, 05:42:15 PM

That is cool, but bad :(
You only want the string to be manipulated during the program and return to it's original state when you return to the editor. Thankfully I rarely use the Varptr function in my programs, but for those that did it would make developing programs interesting.
If you ran the program again, would the first line change to:

A$="Douglas is a huck!"


I've just realised, surely you could use this as a clever way of doing a program "version" string... I am forever forgetting to change mine manually, so i am tempted to write a re-usable/generic routine for this and stick it in my programs!  (even if it is incrementing on every run, it'd still be handy)
Quote from: KillerGorillabecause winuae is made of code and your amiga is made of stuff


Lonewolf10

Quote from: Hungry Horace on April 03, 2011, 02:43:37 PM
I've just realised, surely you could use this as a clever way of doing a program "version" string... I am forever forgetting to change mine manually, so i am tempted to write a re-usable/generic routine for this and stick it in my programs!  (even if it is incrementing on every run, it'd still be handy)


Perhaps. I don't usually change the version string until after a major update or introduction of something bit (e.g my file selector routine).


Regards,
Lonewolf10

Hungry Horace

Quote from: Lonewolf10 on April 06, 2011, 06:54:32 PM
Perhaps. I don't usually change the version string until after a major update or introduction of something bit (e.g my file selector routine).

"build number" then ;)

My problem is i *never* remember to update it manually!
Quote from: KillerGorillabecause winuae is made of code and your amiga is made of stuff