Thanks Charles ,
@sym should be no problem -- newLISP has a "pack" and "unpack" function (to do things "ByRef")
the manual gives following possibilities :
c a signed 8-bit number
b an unsigned 8-bit number
d a signed 16-bit short number
u an unsigned 16-bit short number
ld a signed 32-bit long number
lu an unsigned 32-bit long number
Ld a signed 64-bit long number
Lu an unsigned 64-bit long number
f a float in 32-bit representation
lf a double float in 64-bit representation
sn a string of n null padded ASCII characters
nn n null characters
> switch to big endian byte order
< switch to little endian byte order
There may be a more direct way (?) i.o. using a DLL also from the manual
=======================================================================================================
Copies int-bytes of memory from int-from-address to int-to-address. This function can be used for direct memory writing/reading or for hacking newLISP internals (e.g., type bits in newLISP cells, or building functions with binary executable code on the fly).
Note that this function should only be used when familiar with newLISP internals. cpymem can crash the system or make it unstable if used incorrectly.
(set 's "0123456789")
(cpymem "xxx" (+ (address s) 5) 3)
s → "01234xxx89")
The example copies a string directly into a string variable.
The following example creates a new function from scratch, runs a piece of binary code, and adds up two numbers. This assembly language snippet shows the x86 (Intel CPU) code to add up two numbers and return the result:
55 push ebp
8B EC mov ebp, esp
8B 45 08 mov eax, [ebp+08]
03 45 0C add eax, [ebp+0c]
5D pop ebp
C3 ret
; for Win32/stdcall change last line
C2 08 00 ret
The binary representation is attached to a new function created in newLISP:
; set up 32-bit version of machine code
(set 'foo-code (append
(pack "bbbbbbbbbb" 0x55 0x8B 0xEC 0x8B 0x45 0x08 0x03 0x45 0x0C 0x5D)
(if (= ostype "Win32") (pack "bbb" 0xC2 0x08 0x00) (pack "b" 0xC3))))
; put a function cell template into foo, protect symbol from deletion
(constant 'foo print)
; put the correct type, either 'stdcall' or 'cdecl'
(cpymem (pack "ld" (if (= ostype "Win32") 8456 4360)) (first (dump foo)) 4)
; put the address of foo-code into the new function cell
(cpymem (pack "ld" (address foo-code)) (+ (first (dump foo)) 12) 4)
; take the name address from the foo symbol, copy into function cell
(set 'sym-name (first (unpack "lu" (+ (address 'foo)
)))
(cpymem (pack "ld" sym-name) (+ (first (dump foo))
4)
; test the new function
(println "3 * 4 -> " (foo 3 4))
The last example will not work on all hardware platforms and OSs.
Use the dump function to retrieve binary addresses and the contents from newLISP cells.
==========================================what'd you think ??
best Rob (oops , not intended occasional smilies )