MEMORY and some examples
Mar 31, 2013 0:52:54 GMT 1
Post by bigbass on Mar 31, 2013 0:52:54 GMT 1
I tried to do this in just BaCon using MEMORY
seems like a good way to do some things
and I wanted to post an example
we can read an address but we cant rewrite to it
the same way C code does with pointers
The address of i = 134555888
The value of i = 5
which would print 5, since ip points to i, and i is (at the moment) 5.
But the address of ip and the address of i are different ip =134555928
So ip doesn't really point to i it just has the same value stored
===DO it in MEMORY ======
get the address of ip and store it in memory
show the value of what is in memory
5
get the addres of ip and store it in mem
the address of ip
134555928
we can change whats in memory but
we can't change a value using a pointer
7
maybe we could rewrite some things
by doing it in memory
seems like a good way to do some things
and I wanted to post an example
we can read an address but we cant rewrite to it
the same way C code does with pointers
'==================
'the C code ported
' int i = 5;
'==================
i = 5
'============================================
' ip = &i the C code ported . FIRST STEP
' &i generates a pointer to i in two steps
'============================================
ip = ADDRESS(i)
PRINT "The address of i = " ,ip
'============================================
' the variable ip. points to i .SECOND STEP
'============================================
ip = i
PRINT "The value of i = ", ip
PRINT
PRINT "which would print 5, since ip points to i, and i is (at the moment) 5."
PRINT
PRINT "But the address of ip and the address of i are different ip =" , ADDRESS(ip)
PRINT " So ip doesn't really point to i it just has the same value stored"
PRINT
PRINT
' cant use pointers the same way so lets do it in memory
' do this in memory because using a "*" gives problems
OPTION MEMTYPE long
mem = MEMORY(500)
PRINT "===DO it in MEMORY ======"
PRINT "get the address of ip and store it in memory"
'---get the stored value from whats in the address of ip and store it in mem
POKE mem, PEEK(ADDRESS(ip))
PRINT "show the value of what is in memory "
'--- show the value of mem
PRINT PEEK(mem)
PRINT
PRINT "get the address of ip and store it in mem"
POKE mem,ADDRESS(ip)
'--- print the adress of ip
PRINT "the address of ip"
PRINT PEEK(mem)
PRINT
'====================================
' now set whatever ip points to 7
' *ip = 7; the C code ported
' a bit tricky but lets try
' mem is a dynamic variable but not a pointer
' that lets you change the value in an address
'====================================
POKE mem,7
PRINT "we can change whats in memory but "
PRINT "we can't change a value using a pointer"
PRINT PEEK(mem)
'clean up so we don't have memory leaks
FREE mem
PRINT "maybe we could rewrite some things "
PRINT "by doing it in memory"
The address of i = 134555888
The value of i = 5
which would print 5, since ip points to i, and i is (at the moment) 5.
But the address of ip and the address of i are different ip =134555928
So ip doesn't really point to i it just has the same value stored
===DO it in MEMORY ======
get the address of ip and store it in memory
show the value of what is in memory
5
get the addres of ip and store it in mem
the address of ip
134555928
we can change whats in memory but
we can't change a value using a pointer
7
maybe we could rewrite some things
by doing it in memory