Add all the A tasks
This commit is contained in:
parent
2dd7375f96
commit
051504d65b
1608 changed files with 18584 additions and 0 deletions
1
Task/Address-of-a-variable/0DESCRIPTION
Normal file
1
Task/Address-of-a-variable/0DESCRIPTION
Normal file
|
|
@ -0,0 +1 @@
|
|||
{{basic data operation}}Demonstrate how to get the address of a variable and how to set the address of a variable.
|
||||
2
Task/Address-of-a-variable/1META.yaml
Normal file
2
Task/Address-of-a-variable/1META.yaml
Normal file
|
|
@ -0,0 +1,2 @@
|
|||
---
|
||||
note: Basic Data Operations
|
||||
|
|
@ -0,0 +1,4 @@
|
|||
[4]INT test := (222,444,666,888);
|
||||
REF INT reference := test[3];
|
||||
REF INT(reference) := reference + 111;
|
||||
print(("test value is now: ",test))
|
||||
|
|
@ -0,0 +1 @@
|
|||
PROC establish = (REF FILE file, STRING idf, CHANNEL chan, INT p, l, c) INT: ~
|
||||
|
|
@ -0,0 +1,3 @@
|
|||
The_Address : System.Address;
|
||||
I : Integer;
|
||||
The_Address := I'Address;
|
||||
|
|
@ -0,0 +1,2 @@
|
|||
I : Integer;
|
||||
for I'Address use 16#A100#;
|
||||
|
|
@ -0,0 +1,3 @@
|
|||
I : Integer;
|
||||
J : Integer;
|
||||
for I'Address use J'Address;
|
||||
|
|
@ -0,0 +1,6 @@
|
|||
use std, array (: array.arg also defines pointer operators :)
|
||||
let var = 42
|
||||
let ptr = &var (: value of ptr is address of var :)
|
||||
print var (: prints 42 :)
|
||||
(*ptr)++ (: increments value pointed by ptr :)
|
||||
print var (: prints 43 :)
|
||||
|
|
@ -0,0 +1,3 @@
|
|||
use std, array
|
||||
=:mac:= -> int& { * (0x400000 as int*) }
|
||||
printf "%x\n" mac (: may crash depending on operating system :)
|
||||
|
|
@ -0,0 +1 @@
|
|||
msgbox % &var
|
||||
|
|
@ -0,0 +1,8 @@
|
|||
'get a variable's address:
|
||||
DIM x AS INTEGER, y AS LONG
|
||||
y = VARPTR(x)
|
||||
|
||||
'can't set the address, but can access a given memory location... 1 byte at a time
|
||||
DIM z AS INTEGER
|
||||
z = PEEK(y)
|
||||
z = z + (PEEK(y) * 256)
|
||||
10
Task/Address-of-a-variable/Fortran/address-of-a-variable.f
Normal file
10
Task/Address-of-a-variable/Fortran/address-of-a-variable.f
Normal file
|
|
@ -0,0 +1,10 @@
|
|||
program test_loc
|
||||
|
||||
implicit none
|
||||
integer :: i
|
||||
real :: r
|
||||
|
||||
i = loc (r)
|
||||
write (*, '(i0)') i
|
||||
|
||||
end program test_loc
|
||||
16
Task/Address-of-a-variable/Go/address-of-a-variable-1.go
Normal file
16
Task/Address-of-a-variable/Go/address-of-a-variable-1.go
Normal file
|
|
@ -0,0 +1,16 @@
|
|||
package main
|
||||
|
||||
import (
|
||||
"fmt"
|
||||
"unsafe"
|
||||
)
|
||||
|
||||
func main() {
|
||||
myVar := 3.14
|
||||
myPointer := &myVar
|
||||
fmt.Println(myPointer)
|
||||
fmt.Printf("%p\n", myPointer)
|
||||
|
||||
addr := int64(uintptr(unsafe.Pointer(myPointer)))
|
||||
fmt.Printf("0x%x\n", addr)
|
||||
}
|
||||
29
Task/Address-of-a-variable/Go/address-of-a-variable-2.go
Normal file
29
Task/Address-of-a-variable/Go/address-of-a-variable-2.go
Normal file
|
|
@ -0,0 +1,29 @@
|
|||
== Get ==
|
||||
|
||||
There are at least three ways to get the address of a variable in IWBASIC. The first is to use the address of operator:
|
||||
|
||||
DEF X:INT
|
||||
PRINT &X
|
||||
'This will print in the console window (after OPENCONSOLE is issued.)
|
||||
'To Print in an open window the appropriate Window variable is specified, e.g., PRINT Win,&X.
|
||||
|
||||
The second is to use a pointer:
|
||||
|
||||
DEF X:INT
|
||||
DEF pPointer:POINTER
|
||||
pPointer=X
|
||||
|
||||
The third is to use the Windows API function Lstrcpy. That is done in the same way as the Creative Basic example;
|
||||
however, the function would be declared as follows: DECLARE IMPORT,Lstrcpy(P1:POINTER,P2:POINTER),INT.
|
||||
|
||||
== Set ==
|
||||
|
||||
It appears to the author that the closest one can come to being able to assign an address to a variable is to set
|
||||
which bytes will be used to store a variable in a block of reserved memory:
|
||||
|
||||
DEF pMem as POINTER
|
||||
pMem = NEW(CHAR,1000) : 'Get 1000 bytes to play with
|
||||
#<STRING>pMem = "Copy a string into memory"
|
||||
pMem += 100
|
||||
#<UINT>pMem = 34234: 'Use bytes 100-103 to store a UINT
|
||||
DELETE pMem
|
||||
3
Task/Address-of-a-variable/Go/address-of-a-variable-3.go
Normal file
3
Task/Address-of-a-variable/Go/address-of-a-variable-3.go
Normal file
|
|
@ -0,0 +1,3 @@
|
|||
var =: 52 NB. Any variable (including data, functions, operators etc)
|
||||
var_addr =: 15!:6<'var' NB. Get address
|
||||
new_var =: 15!:7 var_addr NB. Set address
|
||||
|
|
@ -0,0 +1,2 @@
|
|||
use Scalar::Util qw(refaddr);
|
||||
print refaddr(\my $v), "\n"; # 135691508
|
||||
|
|
@ -0,0 +1,4 @@
|
|||
my $a = 12;
|
||||
my $b = \$a; # get reference
|
||||
$$b = $$b + 30; # access referenced value
|
||||
print $a; # prints 42
|
||||
|
|
@ -0,0 +1,6 @@
|
|||
my $a = 12;
|
||||
our $b; # you can overlay only global variables (this line is only for strictness)
|
||||
*b = \$a;
|
||||
print $b; # prints 12
|
||||
$b++;
|
||||
print $a; # prints 13
|
||||
14
Task/Address-of-a-variable/PicoLisp/address-of-a-variable.l
Normal file
14
Task/Address-of-a-variable/PicoLisp/address-of-a-variable.l
Normal file
|
|
@ -0,0 +1,14 @@
|
|||
: (setq X 7)
|
||||
-> 7
|
||||
|
||||
: (adr 'X)
|
||||
-> -2985527269106
|
||||
|
||||
: (val (adr -2985527269106))
|
||||
-> 7
|
||||
|
||||
: (set (adr -2985527269106) '(a b c))
|
||||
-> (a b c)
|
||||
|
||||
: X
|
||||
-> (a b c)
|
||||
|
|
@ -0,0 +1,2 @@
|
|||
foo = object() # Create (instantiate) an empty object
|
||||
address = id(foo)
|
||||
|
|
@ -0,0 +1 @@
|
|||
zzz=storage(xxx)
|
||||
|
|
@ -0,0 +1,6 @@
|
|||
#lang racket
|
||||
|
||||
(require ffi/unsafe)
|
||||
|
||||
(define (madness v) ; i'm so sorry
|
||||
(cast v _racket _gcpointer))
|
||||
|
|
@ -0,0 +1,6 @@
|
|||
(ptr-ref (madness +) _short)
|
||||
(ptr-ref (madness (/ 4 3)) _short)
|
||||
(ptr-ref (madness 3.2) _short)
|
||||
(ptr-ref (madness (sqrt -2)) _short)
|
||||
(ptr-ref (madness #\a) _short)
|
||||
(ptr-ref (madness 'foo) _short)
|
||||
3
Task/Address-of-a-variable/Ruby/address-of-a-variable.rb
Normal file
3
Task/Address-of-a-variable/Ruby/address-of-a-variable.rb
Normal file
|
|
@ -0,0 +1,3 @@
|
|||
>foo = Object.new # => #<Object:0x10ae32000>
|
||||
>id = foo.object_id # => 2238812160
|
||||
>"%x" % (id << 1) # => "10ae32000"
|
||||
|
|
@ -0,0 +1,5 @@
|
|||
|p|
|
||||
p := Point x:10 y:20.
|
||||
ObjectMemory addressOf:p.
|
||||
ObjectMemory collectGarbage.
|
||||
ObjectMemory addressOf:p
|
||||
|
|
@ -0,0 +1,4 @@
|
|||
|ptr|
|
||||
ptr := ExternalBytes new:10.
|
||||
ptr address.
|
||||
ptr byteAt:1 put: 16rFF.
|
||||
21
Task/Address-of-a-variable/Tcl/address-of-a-variable-1.tcl
Normal file
21
Task/Address-of-a-variable/Tcl/address-of-a-variable-1.tcl
Normal file
|
|
@ -0,0 +1,21 @@
|
|||
package require critcl
|
||||
# This code assumes an ILP32 architecture, like classic x86 or VAX.
|
||||
critcl::cproc peek {int addr} int {
|
||||
union {
|
||||
int i;
|
||||
int *a;
|
||||
} u;
|
||||
|
||||
u.i = addr;
|
||||
return *u.a;
|
||||
}
|
||||
critcl::cproc poke {int addr int value} void {
|
||||
union {
|
||||
int i;
|
||||
int *a;
|
||||
} u;
|
||||
|
||||
u.i = addr;
|
||||
*u.a = value;
|
||||
}
|
||||
package provide poker 1.0
|
||||
|
|
@ -0,0 +1,7 @@
|
|||
package require poker
|
||||
|
||||
# Increment a memory location; this will probably crash if you try for real.
|
||||
# We don't define how to get a good address, but it's not usually a problem
|
||||
# for embedded programming...
|
||||
set where 0x12340
|
||||
poke $where [expr {[peek $where] + 1}]
|
||||
Loading…
Add table
Add a link
Reference in a new issue