[Include] pointers.inc - Pointers for PAWN!
#33

Here's an interesting little experiment with custom operators allowing you to do this:
pawn Код:
new variable = 200;
new Pointer:ptr = PointerToVar(variable);

ptr = 123;

printf("%d", variable);
// Output: 123
Note, though, this only works for assignments. Other things like ++, --, /, * doesn't work. Though they could be added just as easy.

Source:
pawn Код:
// Operator called when an untagged variable is assigned to a value tagged with Pointer
stock Pointer:operator=(value) {
    static addr;
   
    // Get return address
    #emit LOAD.S.alt 4
   
    // Add COD
    #emit LCTRL     0
    #emit ADD
    #emit MOVE.alt
   
    // Subtract DAT
    #emit LCTRL     1
    #emit SUB.alt
   
    // Read the first opcode's parameter
    #emit ADD.C     4
    #emit STOR.pri  addr
    #emit LREF.pri  addr
    #emit STOR.pri  addr
   
    // Is this address relative to the previous frame?
    if (addr <= 0) {
        // Add the previous frame to make it absolute
        #emit LOAD.S.pri  0
        #emit LOAD.alt    addr
        #emit ADD
       
        // Load it and store it
        #emit LOAD.I
        #emit STOR.pri    addr
    } else {
        // Load it and store it
        #emit LOAD.pri  addr
        #emit LOAD.I
        #emit STOR.pri  addr
    }
   
    // Store the new value there
    if (addr) {
        #emit LOAD.S.pri  value
        #emit LOAD.alt    addr
        #emit STOR.I
    }
   
    // Add 8 to the return address (to skip the assignment)
    #emit LOAD.S.pri 4
    #emit ADD.C      8
    #emit STOR.S.pri 4
   
    // Return the same value the pointer was
    return Pointer:0;
}


stock Pointer:PointerToVar(...) {
    #emit LOAD.S.pri  12
    #emit RETN
   
    return Pointer:0;
}
Reply


Messages In This Thread
pointers.inc - Pointers for PAWN! - by Slice - 18.01.2012, 08:26
Re: pointers.inc - Pointers for PAWN! - by Ronaldo_raul™ - 18.01.2012, 08:33
Re: pointers.inc - Pointers for PAWN! - by SoUrAv - 18.01.2012, 08:39
Respuesta: pointers.inc - Pointers for PAWN! - by [Nikk] - 18.01.2012, 08:43
Re: pointers.inc - Pointers for PAWN! - by Lorenc_ - 18.01.2012, 08:50
Re: pointers.inc - Pointers for PAWN! - by T0pAz - 18.01.2012, 09:16
Re: pointers.inc - Pointers for PAWN! - by CaHbKo - 18.01.2012, 09:48
Re: pointers.inc - Pointers for PAWN! - by [S]trong - 18.01.2012, 10:20
Re: pointers.inc - Pointers for PAWN! - by steki. - 18.01.2012, 10:23
Re: pointers.inc - Pointers for PAWN! - by Niko_boy - 18.01.2012, 11:07
Re: pointers.inc - Pointers for PAWN! - by System64 - 18.01.2012, 11:14
Re: pointers.inc - Pointers for PAWN! - by CaHbKo - 18.01.2012, 11:30
Re: pointers.inc - Pointers for PAWN! - by Slice - 18.01.2012, 12:10
Re: pointers.inc - Pointers for PAWN! - by Ricop522 - 18.01.2012, 13:47
Re: pointers.inc - Pointers for PAWN! - by steki. - 18.01.2012, 15:26
Re: pointers.inc - Pointers for PAWN! - by Brian_G - 18.01.2012, 16:11
Re: pointers.inc - Pointers for PAWN! - by KingHual - 18.01.2012, 16:39
Re: pointers.inc - Pointers for PAWN! - by RyDeR` - 18.01.2012, 16:42
Re: pointers.inc - Pointers for PAWN! - by Zh3r0 - 18.01.2012, 17:11
Re: pointers.inc - Pointers for PAWN! - by steki. - 18.01.2012, 17:13
Re: pointers.inc - Pointers for PAWN! - by Zh3r0 - 18.01.2012, 17:17
Re: pointers.inc - Pointers for PAWN! - by steki. - 18.01.2012, 17:21
Re: pointers.inc - Pointers for PAWN! - by CaHbKo - 18.01.2012, 17:43
Re: pointers.inc - Pointers for PAWN! - by Slice - 18.01.2012, 17:50
Respuesta: pointers.inc - Pointers for PAWN! - by Adoniiz - 19.01.2012, 02:37
Re: pointers.inc - Pointers for PAWN! - by Slice - 19.01.2012, 04:58
Re: pointers.inc - Pointers for PAWN! - by The King's Bastard - 01.02.2012, 20:51
Re: pointers.inc - Pointers for PAWN! - by Slice - 02.02.2012, 06:42
Re: pointers.inc - Pointers for PAWN! - by The King's Bastard - 02.02.2012, 07:45
Re: pointers.inc - Pointers for PAWN! - by (LifeStealeR) - 02.02.2012, 08:32
Re: pointers.inc - Pointers for PAWN! - by Slice - 13.06.2012, 13:57
Re: pointers.inc - Pointers for PAWN! - by Hiddos - 13.06.2012, 14:38
Re: pointers.inc - Pointers for PAWN! - by Slice - 25.06.2012, 07:06
Re: pointers.inc - Pointers for PAWN! - by Яlackon - 28.07.2012, 22:04
Re: pointers.inc - Pointers for PAWN! - by BMB1 - 28.07.2012, 22:07
Re: pointers.inc - Pointers for PAWN! - by Ballu Miaa - 11.11.2012, 22:40
Re: pointers.inc - Pointers for PAWN! - by Dayvison_ - 27.03.2016, 23:31
Re: pointers.inc - Pointers for PAWN! - by Slice - 27.03.2016, 23:38
Re: pointers.inc - Pointers for PAWN! - by Dayvison_ - 28.03.2016, 00:01
Re: pointers.inc - Pointers for PAWN! - by Kar - 28.03.2016, 00:30
Re: pointers.inc - Pointers for PAWN! - by Slice - 28.03.2016, 08:20
Re: pointers.inc - Pointers for PAWN! - by Dayvison_ - 28.03.2016, 14:26
Re: pointers.inc - Pointers for PAWN! - by Slice - 28.03.2016, 16:18
Re: pointers.inc - Pointers for PAWN! - by Spmn - 25.05.2016, 20:56
Re: pointers.inc - Pointers for PAWN! - by Yashas - 26.05.2016, 04:29
Re: pointers.inc - Pointers for PAWN! - by Ivan_Ino - 26.05.2016, 11:24
Re: pointers.inc - Pointers for PAWN! - by vannesenn - 26.05.2016, 15:43
Re: pointers.inc - Pointers for PAWN! - by Yashas - 01.03.2017, 10:22
Re: pointers.inc - Pointers for PAWN! - by Nero_3D - 02.03.2017, 23:15
Re: pointers.inc - Pointers for PAWN! - by nG Inverse - 31.07.2017, 00:24
Re: pointers.inc - Pointers for PAWN! - by Damian - 08.03.2019, 14:46

Forum Jump:


Users browsing this thread: 3 Guest(s)