Data commit
This commit is contained in:
parent
7387c8f97b
commit
cb5bb5e222
199093 changed files with 3378972 additions and 0 deletions
3
Task/Address-of-a-variable/00-META.yaml
Normal file
3
Task/Address-of-a-variable/00-META.yaml
Normal file
|
|
@ -0,0 +1,3 @@
|
|||
---
|
||||
from: http://rosettacode.org/wiki/Address_of_a_variable
|
||||
note: Basic Data Operations
|
||||
6
Task/Address-of-a-variable/00-TASK.txt
Normal file
6
Task/Address-of-a-variable/00-TASK.txt
Normal file
|
|
@ -0,0 +1,6 @@
|
|||
{{basic data operation}}
|
||||
|
||||
;Task:
|
||||
Demonstrate how to get the address of a variable and how to set the address of a variable.
|
||||
<br><br>
|
||||
|
||||
|
|
@ -0,0 +1,3 @@
|
|||
LA R3,I load address of I
|
||||
...
|
||||
I DS F
|
||||
|
|
@ -0,0 +1,7 @@
|
|||
USING MYDSECT,R12
|
||||
LA R12,I set @J=@I
|
||||
L R2,J now J is at the same location as I
|
||||
...
|
||||
I DS F
|
||||
MYDSECT DSECT
|
||||
J DS F
|
||||
|
|
@ -0,0 +1,9 @@
|
|||
;;;;;;; zero page RAM memory addresses
|
||||
CursorX equ $00
|
||||
CursorY equ $01
|
||||
temp equ $02
|
||||
;;;;;;; memory-mapped hardware registers
|
||||
BorderColor equ $D020
|
||||
|
||||
Joystick1 equ $DC01
|
||||
Joystick2 equ $DC00
|
||||
|
|
@ -0,0 +1,8 @@
|
|||
LDA #$20 ; load a constant into the accumulator
|
||||
|
||||
CLC
|
||||
ADC #$50
|
||||
ASL
|
||||
SBC #$30 ; do some math
|
||||
|
||||
STA temp ; store the result in a temp variable (really a zero-page memory address).
|
||||
|
|
@ -0,0 +1,2 @@
|
|||
LDA #$30 ;load into the accumulator the constant value 0x30
|
||||
LDA $30 ;load into the accumulator the value stored at memory address 0x0030.
|
||||
|
|
@ -0,0 +1 @@
|
|||
byte $30,$20,$10,$00 ;these are constant numeric values, not memory addresses.
|
||||
|
|
@ -0,0 +1,9 @@
|
|||
UserRam equ $100000
|
||||
Cursor_X equ UserRam ;$100000, byte length
|
||||
Cursor_Y equ UserRam+1 ;$100001, byte length
|
||||
SixteenBitData equ UserRam+2 ;$100002, word length (VASM doesn't allow labels to begin with numbers.)
|
||||
ThirtyTwoBitData equ UserRam+4 ;$100004, long length
|
||||
|
||||
;GET THE ADDRESS
|
||||
|
||||
LEA ThirtyTwoBitData,A0 ;load $100004 into A0.
|
||||
|
|
@ -0,0 +1,7 @@
|
|||
MOVE.L #$11223344,D0
|
||||
MOVE.L D0,(A0) ; store D0 into ThirtyTwoBitData ($100004)
|
||||
; HEXDUMP OF $100004:
|
||||
; $100004: $11
|
||||
; $100005: $22
|
||||
; $100006: $33
|
||||
; $100007: $44
|
||||
|
|
@ -0,0 +1,12 @@
|
|||
.model small
|
||||
.stack 1024
|
||||
.data
|
||||
UserRam 256 DUP (0) ;define the next 256 bytes as user RAM, initialize them all to zero.
|
||||
|
||||
.code
|
||||
|
||||
mov ax, seg UserRam ;load into AX the segment where UserRam is stored.
|
||||
mov es, ax ;load that value into the Extra Segment register
|
||||
|
||||
mov ax, offset UserRam ;load into AX the address of UserRam
|
||||
mov di, ax ;load that value into the destination index register
|
||||
|
|
@ -0,0 +1,2 @@
|
|||
mov ax, 0FFFFh
|
||||
mov [es:di],ax ;store 0xFFFF into the base address of UserRam.
|
||||
|
|
@ -0,0 +1,13 @@
|
|||
.model small
|
||||
.stack 1024
|
||||
.data
|
||||
UserRam 256 DUP (0) ;define the next 256 bytes as user RAM, initialize them all to zero.
|
||||
|
||||
.code
|
||||
mov ax, @data ; load the data segment into ax
|
||||
mov ds,ax ; point the data segment register to the data segment.
|
||||
; (The 8086 can only load segment registers from ax, bx, cx, dx, or the pop command)
|
||||
|
||||
|
||||
mov ax, 0FFFFh ; load the number 65535 (or -1 if you prefer) into ax.
|
||||
mov word ptr [ds:UserRam] ; store this quantity in user ram.
|
||||
|
|
@ -0,0 +1,73 @@
|
|||
/* ARM assembly AArch64 Raspberry PI 3B */
|
||||
/* program adrvar.s */
|
||||
/*************************************/
|
||||
/* Constantes */
|
||||
/*************************************/
|
||||
.equ STDOUT, 1
|
||||
.equ WRITE, 64
|
||||
.equ EXIT, 93
|
||||
/*************************************/
|
||||
/* Initialized data */
|
||||
/*************************************/
|
||||
.data
|
||||
szMessage: .asciz "Hello. \n" // message
|
||||
szRetourLigne: .asciz "\n"
|
||||
qValDeb: .quad 5 // value 5 in array of 8 bytes
|
||||
/*************************************/
|
||||
/* No Initialized data */
|
||||
/*************************************/
|
||||
.bss
|
||||
qValeur: .skip 8 // reserve 8 bytes in memory
|
||||
/*************************************/
|
||||
/* Program code */
|
||||
/*************************************/
|
||||
.text
|
||||
.global main
|
||||
main:
|
||||
ldr x0,=szMessage // adresse of message short program
|
||||
bl affichageMess // call function
|
||||
// or
|
||||
ldr x0,qAdrszMessage // adresse of message big program (size code > 4K)
|
||||
bl affichageMess // call function
|
||||
|
||||
ldr x1,=qValDeb // adresse of variable -> x1 short program
|
||||
ldr x0,[x1] // value of iValdeb -> x0
|
||||
ldr x1,qAdriValDeb // adresse of variable -> x1 big program
|
||||
ldr x0,[x1] // value of iValdeb -> x0
|
||||
|
||||
/* set variables */
|
||||
ldr x1,=qValeur // adresse of variable -> x1 short program
|
||||
str x0,[x1] // value of x0 -> iValeur
|
||||
ldr x1,qAdriValeur // adresse of variable -> x1 big program
|
||||
str x0,[x1] // value of x0 -> iValeur
|
||||
|
||||
|
||||
/* end of program */
|
||||
mov x0,0 // return code
|
||||
mov x8,EXIT // request to exit program
|
||||
svc 0 // perform the system call
|
||||
qAdriValDeb: .quad qValDeb
|
||||
qAdriValeur: .quad qValeur
|
||||
qAdrszMessage: .quad szMessage
|
||||
qAdrszRetourLigne: .quad szRetourLigne
|
||||
/******************************************************************/
|
||||
/* String display with size compute */
|
||||
/******************************************************************/
|
||||
/* x0 contains string address (string ended with zero binary) */
|
||||
affichageMess:
|
||||
stp x0,x1,[sp,-16]! // save registers
|
||||
stp x2,x8,[sp,-16]! // save registers
|
||||
mov x2,0 // size counter
|
||||
1: // loop start
|
||||
ldrb w1,[x0,x2] // load a byte
|
||||
cbz w1,2f // if zero -> end string
|
||||
add x2,x2,#1 // else increment counter
|
||||
b 1b // and loop
|
||||
2: // x2 = string size
|
||||
mov x1,x0 // string address
|
||||
mov x0,STDOUT // output Linux standard
|
||||
mov x8,WRITE // code call system "write"
|
||||
svc 0 // call systeme Linux
|
||||
ldp x2,x8,[sp],16 // restaur 2 registres
|
||||
ldp x0,x1,[sp],16 // restaur 2 registres
|
||||
ret // retour adresse lr x30
|
||||
|
|
@ -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,69 @@
|
|||
/* ARM assembly Raspberry PI */
|
||||
/* program adrvar.s */
|
||||
|
||||
/* Constantes */
|
||||
.equ STDOUT, 1
|
||||
.equ WRITE, 4
|
||||
.equ EXIT, 1
|
||||
/* Initialized data */
|
||||
.data
|
||||
szMessage: .asciz "Hello. \n " @ message
|
||||
szRetourLigne: .asciz "\n"
|
||||
iValDeb: .int 5 @ value 5 in array of 4 bytes
|
||||
/* No Initialized data */
|
||||
.bss
|
||||
iValeur: .skip 4 @ reserve 4 bytes in memory
|
||||
|
||||
.text
|
||||
.global main
|
||||
main:
|
||||
ldr r0,=szMessage @ adresse of message short program
|
||||
bl affichageMess @ call function
|
||||
@ or
|
||||
ldr r0,iAdrszMessage @ adresse of message big program (size code > 4K)
|
||||
bl affichageMess @ call function
|
||||
|
||||
ldr r1,=iValDeb @ adresse of variable -> r1 short program
|
||||
ldr r0,[r1] @ value of iValdeb -> r0
|
||||
ldr r1,iAdriValDeb @ adresse of variable -> r1 big program
|
||||
ldr r0,[r1] @ value of iValdeb -> r0
|
||||
|
||||
|
||||
/* set variables */
|
||||
ldr r1,=iValeur @ adresse of variable -> r1 short program
|
||||
str r0,[r1] @ value of r0 -> iValeur
|
||||
ldr r1,iAdriValeur @ adresse of variable -> r1 big program
|
||||
str r0,[r1] @ value of r0 -> iValeur
|
||||
|
||||
|
||||
/* end of program */
|
||||
mov r0, #0 @ return code
|
||||
mov r7, #EXIT @ request to exit program
|
||||
swi 0 @ perform the system call
|
||||
iAdriValDeb: .int iValDeb
|
||||
iAdriValeur: .int iValeur
|
||||
iAdrszMessage: .int szMessage
|
||||
iAdrszRetourLigne: .int szRetourLigne
|
||||
/******************************************************************/
|
||||
/* affichage des messages avec calcul longueur */
|
||||
/******************************************************************/
|
||||
/* r0 contient l adresse du message */
|
||||
affichageMess:
|
||||
push {fp,lr} /* save des 2 registres */
|
||||
push {r0,r1,r2,r7} /* save des autres registres */
|
||||
mov r2,#0 /* compteur longueur */
|
||||
1: /*calcul de la longueur */
|
||||
ldrb r1,[r0,r2] /* recup octet position debut + indice */
|
||||
cmp r1,#0 /* si 0 c est fini */
|
||||
beq 1f
|
||||
add r2,r2,#1 /* sinon on ajoute 1 */
|
||||
b 1b
|
||||
1: /* donc ici r2 contient la longueur du message */
|
||||
mov r1,r0 /* adresse du message en r1 */
|
||||
mov r0,#STDOUT /* code pour écrire sur la sortie standard Linux */
|
||||
mov r7, #WRITE /* code de l appel systeme 'write' */
|
||||
swi #0 /* appel systeme */
|
||||
pop {r0,r1,r2,r7} /* restaur des autres registres */
|
||||
pop {fp,lr} /* restaur des 2 registres */
|
||||
bx lr /* retour procedure */
|
||||
|
||||
|
|
@ -0,0 +1,6 @@
|
|||
PROC Main()
|
||||
BYTE v=[123]
|
||||
|
||||
PrintF("Value of variable: %B%E",v)
|
||||
PrintF("Address of variable: %H%E",@v)
|
||||
RETURN
|
||||
|
|
@ -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 @@
|
|||
N = N : PRINT PEEK (131) + PEEK (132) * 256
|
||||
|
|
@ -0,0 +1,3 @@
|
|||
0 DEF FN F(X) = 0
|
||||
1 FOR A = PEEK(105) + PEEK(106) * 256 TO PEEK(107) + PEEK(108) * 256 STEP 7 : IF PEEK(A) <> ASC("F") + 128 OR PEEK(A + 1) <> 0 THEN NEXT A : A = 0 : PRINT "FN F NOT FOUND"
|
||||
2 IF A THEN PRINT A
|
||||
|
|
@ -0,0 +1,2 @@
|
|||
LOMEM: 4096
|
||||
I% = I% : PRINT PEEK (131) + PEEK (132) * 256
|
||||
|
|
@ -0,0 +1,4 @@
|
|||
S$ = "HELLO" : POKE 768, PEEK (131) : POKE 769, PEEK (132) : A = PEEK(768) + PEEK(769) * 256
|
||||
PRINT S$ : PRINT A "- " PEEK(A) " " PEEK(A + 1) + PEEK(A + 2) * 256
|
||||
POKE 768, ASC("H") : POKE 769, ASC("I") : POKE A, 2: POKE A + 1, 0 : POKE A + 2, 3
|
||||
PRINT S$ : PRINT A "- " PEEK(A) " " PEEK(A + 1) + PEEK(A + 2) * 256
|
||||
|
|
@ -0,0 +1,10 @@
|
|||
0 DEF FN F(X) = 1
|
||||
1 DEF FN B(X) = 2
|
||||
2 N$ = "F" : GOSUB 8 : FA = A
|
||||
3 N$ = "B" : GOSUB 8 : BA = A
|
||||
4 PRINT FN F(0)
|
||||
5 POKE FA, PEEK(BA) : POKE FA + 1, PEEK(BA + 1)
|
||||
6 PRINT FN F(0)
|
||||
7 END
|
||||
8 FOR A = PEEK(105) + PEEK(106) * 256 TO PEEK(107) + PEEK(108) * 256 STEP 7 : IF PEEK(A) = ASC(LEFT$(N$,1)) + 128 AND PEEK(A + 1) = ASC(MID$(N$ + CHR$(0), 2, 1)) THEN A = A + 2 : RETURN
|
||||
9 NEXT A : PRINT "FN " N$ " NOT FOUND"
|
||||
|
|
@ -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,7 @@
|
|||
x: 2
|
||||
xInfo: info.get 'x
|
||||
|
||||
print [
|
||||
"address of x:" xInfo\address
|
||||
"->" from.hex xInfo\address
|
||||
]
|
||||
|
|
@ -0,0 +1,7 @@
|
|||
var num = 12
|
||||
var pointer = ptr(num) # get pointer
|
||||
|
||||
print pointer # print address
|
||||
|
||||
@unsafe # bad idea!
|
||||
pointer.addr = 0xFFFE # set the address
|
||||
|
|
@ -0,0 +1 @@
|
|||
msgbox % &var
|
||||
|
|
@ -0,0 +1,2 @@
|
|||
°A→B
|
||||
.B now contains the address of A
|
||||
|
|
@ -0,0 +1,2 @@
|
|||
1234→A
|
||||
1→{A}
|
||||
|
|
@ -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)
|
||||
|
|
@ -0,0 +1,5 @@
|
|||
REM get a variable's address:
|
||||
y% = ^x%
|
||||
|
||||
REM can't set a variable's address, but can access a given memory location (4 bytes):
|
||||
x% = !y%
|
||||
|
|
@ -0,0 +1,3 @@
|
|||
'---get a variable's address
|
||||
LOCAL x TYPE long
|
||||
PRINT ADDRESS(x)
|
||||
|
|
@ -0,0 +1,2 @@
|
|||
int i = 5;
|
||||
int* p = &i;
|
||||
|
|
@ -0,0 +1 @@
|
|||
int* p = (int*)((void*)0xDEADBEEF);
|
||||
|
|
@ -0,0 +1,5 @@
|
|||
unsafe
|
||||
{
|
||||
int i = 5;
|
||||
void* address_of_i = &i;
|
||||
}
|
||||
2
Task/Address-of-a-variable/C/address-of-a-variable-1.c
Normal file
2
Task/Address-of-a-variable/C/address-of-a-variable-1.c
Normal file
|
|
@ -0,0 +1,2 @@
|
|||
int i;
|
||||
void* address_of_i = &i;
|
||||
3
Task/Address-of-a-variable/C/address-of-a-variable-2.c
Normal file
3
Task/Address-of-a-variable/C/address-of-a-variable-2.c
Normal file
|
|
@ -0,0 +1,3 @@
|
|||
#include <memory>
|
||||
int i;
|
||||
auto address_of_i = std::addressof(i);
|
||||
1
Task/Address-of-a-variable/C/address-of-a-variable-3.c
Normal file
1
Task/Address-of-a-variable/C/address-of-a-variable-3.c
Normal file
|
|
@ -0,0 +1 @@
|
|||
int& i = *(int*)0xA100;
|
||||
3
Task/Address-of-a-variable/C/address-of-a-variable-4.c
Normal file
3
Task/Address-of-a-variable/C/address-of-a-variable-4.c
Normal file
|
|
@ -0,0 +1,3 @@
|
|||
#include <new>
|
||||
struct S { int i = 0; S() {} };
|
||||
auto& s = *new (reinterpret_cast<void*>(0xa100)) S;
|
||||
5
Task/Address-of-a-variable/C/address-of-a-variable-5.c
Normal file
5
Task/Address-of-a-variable/C/address-of-a-variable-5.c
Normal file
|
|
@ -0,0 +1,5 @@
|
|||
static union
|
||||
{
|
||||
int i;
|
||||
int j;
|
||||
};
|
||||
2
Task/Address-of-a-variable/C/address-of-a-variable-6.c
Normal file
2
Task/Address-of-a-variable/C/address-of-a-variable-6.c
Normal file
|
|
@ -0,0 +1,2 @@
|
|||
int i;
|
||||
int& j = i;
|
||||
4
Task/Address-of-a-variable/C/address-of-a-variable-7.c
Normal file
4
Task/Address-of-a-variable/C/address-of-a-variable-7.c
Normal file
|
|
@ -0,0 +1,4 @@
|
|||
#include <cstring>
|
||||
inline float read_as_float(int const& i) { float f; memcpy(&f, &i, sizeof(f)); return f; }
|
||||
int i = 0x0a112233;
|
||||
float f = read_as_float(i);
|
||||
|
|
@ -0,0 +1,7 @@
|
|||
data division.
|
||||
working-storage section.
|
||||
01 ptr usage pointer.
|
||||
01 var pic x(64).
|
||||
|
||||
procedure division.
|
||||
set ptr to address of var.
|
||||
|
|
@ -0,0 +1,12 @@
|
|||
OCOBOL*> Rosetta Code set address example
|
||||
*> tectonics: cobc -x setaddr.cob && ./setaddr
|
||||
program-id. setaddr.
|
||||
data division.
|
||||
working-storage section.
|
||||
01 prealloc pic x(8) value 'somedata'.
|
||||
01 var pic x(8) based.
|
||||
procedure division.
|
||||
set address of var to address of prealloc
|
||||
display var end-display
|
||||
goback.
|
||||
end program setaddr.
|
||||
|
|
@ -0,0 +1,12 @@
|
|||
100 REM ALLOCATE ALL VARS FIRST SO THEY DON'T MOVE
|
||||
110 X=123:Y=456:PX=0:PY=0:I=0:T=0
|
||||
120 PRINT "BEFORE:X="XCHR$(20)",Y="Y
|
||||
130 PX=POINTER(X):PY=POINTER(Y)
|
||||
140 REM VARS ARE STORED IN RAM BANK 1
|
||||
150 BANK 1
|
||||
160 FOR I=0 TO 6
|
||||
170 : T = PEEK(PY+I)
|
||||
180 : POKE PY+I,PEEK(PX+I)
|
||||
190 : POKE PX+I,T
|
||||
200 NEXT I
|
||||
210 PRINT "AFTER: X="XCHR$(20)",Y="Y
|
||||
|
|
@ -0,0 +1,9 @@
|
|||
100 X=PEEK(71)+256*PEEK(72):PX=X:X=123
|
||||
110 Y=PEEK(71)+256*PEEK(72):PY=Y:Y=456
|
||||
120 PRINT "BEFORE:X ="XCHR$(20)", Y ="Y
|
||||
130 FOR I=0 TO 6
|
||||
140 : T = PEEK(PY+I)
|
||||
150 : POKE PY+I,PEEK(PX+I)
|
||||
160 : POKE PX+I,T
|
||||
170 NEXT I
|
||||
180 PRINT "AFTER: X ="XCHR$(20)", Y ="Y
|
||||
|
|
@ -0,0 +1,16 @@
|
|||
;;; Demonstration of references by swapping two variables using a function rather than a macro
|
||||
;;; Needs http://paste.lisp.org/display/71952
|
||||
(defun swap (ref-left ref-right)
|
||||
;; without with-refs we would have to write this:
|
||||
;; (psetf (deref ref-left) (deref ref-right)
|
||||
;; (deref ref-right) (deref ref-left))
|
||||
(with-refs ((l ref-left) (r ref-right))
|
||||
(psetf l r r l)))
|
||||
|
||||
(defvar *x* 42)
|
||||
(defvar *y* 0)
|
||||
|
||||
(swap (ref *x*) (ref *y*))
|
||||
|
||||
;; *y* -> 42
|
||||
;; *x* -> 0
|
||||
|
|
@ -0,0 +1,27 @@
|
|||
(use-package :ffi)
|
||||
|
||||
(defmacro def-libc-call-out (name &rest args)
|
||||
`(def-call-out ,name
|
||||
(:language :stdc)
|
||||
#-cygwin(:library "libc.so.6")
|
||||
#+cygwin (:library "cygwin1.dll")
|
||||
,@args))
|
||||
|
||||
(progn
|
||||
(def-libc-call-out errno-location
|
||||
#-cygwin (:name "__errno_location")
|
||||
#+cygwin (:name "__errno")
|
||||
(:arguments)
|
||||
(:return-type (c-pointer int)))
|
||||
|
||||
(defun get-errno ()
|
||||
(let ((loc (errno-location)))
|
||||
(foreign-value loc)))
|
||||
|
||||
(defun set-errno (value)
|
||||
(let ((loc (errno-location)))
|
||||
(setf (foreign-value loc) value)))
|
||||
|
||||
(defsetf get-errno set-errno)
|
||||
|
||||
(define-symbol-macro errno (get-errno)))
|
||||
|
|
@ -0,0 +1,14 @@
|
|||
MODULE AddressVar;
|
||||
IMPORT SYSTEM,StdLog;
|
||||
|
||||
VAR
|
||||
x: INTEGER;
|
||||
|
||||
PROCEDURE Do*;
|
||||
BEGIN
|
||||
StdLog.String("ADR(x):> ");StdLog.IntForm(SYSTEM.ADR(x),StdLog.hexadecimal,8,'0',TRUE);StdLog.Ln
|
||||
END Do;
|
||||
|
||||
BEGIN
|
||||
x := 10;
|
||||
END AddressVar.
|
||||
|
|
@ -0,0 +1,72 @@
|
|||
== Get ==
|
||||
|
||||
To get the address of a variable without using the Windows API:
|
||||
|
||||
DEF X:INT
|
||||
DEF pPointer:POINTER
|
||||
pPointer=X
|
||||
|
||||
----
|
||||
|
||||
To get the address of a variable using the Windows API Lstrcpy function called in Creative Basic:
|
||||
(This may give users of another language without a native way to get the address of a variable to work around that problem.)
|
||||
|
||||
DEF Win:WINDOW
|
||||
DEF Close:CHAR
|
||||
DEF ScreenSizeX,ScreenSizeY,Col:INT
|
||||
|
||||
'***Map Function***
|
||||
DECLARE "Kernel32",Lstrcpy(P1:POINTER,P2:POINTER),INT
|
||||
'The pointers replace the VB3 variable type of Any.
|
||||
|
||||
'Note: This is translated from VB3 or earlier code, and "Ptr" is *not* a Creative Basic pointer.
|
||||
DEF Ptr:INT
|
||||
DEF X1:INT
|
||||
DEF X2:STRING
|
||||
|
||||
X1=123
|
||||
|
||||
'***Call function***
|
||||
Ptr=Lstrcpy(X1,X1)
|
||||
|
||||
GETSCREENSIZE(ScreenSizeX,ScreenSizeY)
|
||||
|
||||
WINDOW Win,0,0,ScreenSizeX,ScreenSizeY,@MINBOX|@MAXBOX|@SIZE|@MAXIMIZED,0,"Skel Win",MainHandler
|
||||
|
||||
'***Display address***
|
||||
PRINT Win, "The address of x1 is: " + Hex$(Ptr)
|
||||
X2="X2"
|
||||
|
||||
WAITUNTIL Close=1
|
||||
|
||||
CLOSEWINDOW Win
|
||||
|
||||
END
|
||||
|
||||
SUB MainHandler
|
||||
|
||||
SELECT @CLASS
|
||||
|
||||
CASE @IDCLOSEWINDOW
|
||||
|
||||
Close=1
|
||||
|
||||
ENDSELECT
|
||||
|
||||
RETURN
|
||||
|
||||
Note: The Windows Dev Center (http://msdn.microsoft.com/en-us/library/windows/desktop/ms647490%28v=vs.85%29.aspx) says
|
||||
improper use of the Lstrcpy function may compromise security. A person is advised to see the Windows Dev site before using
|
||||
the Lstrcopy function.
|
||||
|
||||
== Set ==
|
||||
|
||||
It appears to the author the closest one can come to setting the address of a variable is to set which bytes will be
|
||||
used to store a variable in a reserved block of 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
|
||||
2
Task/Address-of-a-variable/D/address-of-a-variable-1.d
Normal file
2
Task/Address-of-a-variable/D/address-of-a-variable-1.d
Normal file
|
|
@ -0,0 +1,2 @@
|
|||
int i;
|
||||
int* ip = &i;
|
||||
1
Task/Address-of-a-variable/D/address-of-a-variable-2.d
Normal file
1
Task/Address-of-a-variable/D/address-of-a-variable-2.d
Normal file
|
|
@ -0,0 +1 @@
|
|||
int* ip = cast(int*)0xdeadf00d;
|
||||
8
Task/Address-of-a-variable/D/address-of-a-variable-3.d
Normal file
8
Task/Address-of-a-variable/D/address-of-a-variable-3.d
Normal file
|
|
@ -0,0 +1,8 @@
|
|||
void test(ref int i) {
|
||||
import std.stdio;
|
||||
writeln(&i);
|
||||
}
|
||||
|
||||
void main() {
|
||||
test(* (cast(int*)0xdeadf00d) );
|
||||
}
|
||||
|
|
@ -0,0 +1,7 @@
|
|||
var
|
||||
i: integer;
|
||||
p: ^integer;
|
||||
begin
|
||||
p := @i;
|
||||
writeLn(p^);
|
||||
end;
|
||||
|
|
@ -0,0 +1,4 @@
|
|||
var
|
||||
crtMode: integer absolute $0040;
|
||||
str: string[100];
|
||||
strLen: byte absolute str;
|
||||
28
Task/Address-of-a-variable/Draco/address-of-a-variable.draco
Normal file
28
Task/Address-of-a-variable/Draco/address-of-a-variable.draco
Normal file
|
|
@ -0,0 +1,28 @@
|
|||
/* This code uses a CP/M-specific address to demonstrate fixed locations,
|
||||
* so it will very likely only work under CP/M */
|
||||
proc nonrec main() void:
|
||||
/* When declaring a variable, you can let the compiler choose an address */
|
||||
word var;
|
||||
|
||||
/* Or you can set the address manually using @, to a fixed address */
|
||||
word memtop @ 0x6; /* CP/M stores the top of memory at 0x0006 */
|
||||
|
||||
/* or to the address of another variable, by naming that variable */
|
||||
word var2 @ var; /* var2 will overlap var */
|
||||
|
||||
/* This works with both automatically and manually placed variables. */
|
||||
word memtop2 @ memtop; /* same as "memtop2 @ 0x6" */
|
||||
|
||||
/* Once a variable is declared, you can't change its address at runtime. */
|
||||
|
||||
var := 1234; /* assign a value to var _and_ var2 */
|
||||
|
||||
/* The address of a variable can be retrieved using the & operator.
|
||||
* However, this returns a pointer type, which is distinct from an
|
||||
* integer type. To use it as a number, we have to coerce it to an integer
|
||||
* first. */
|
||||
writeln("var address=", pretend(&var,word):5, " value=", var:5);
|
||||
writeln("memtop address=", pretend(&memtop,word):5, " value=", memtop:5);
|
||||
writeln("var2 address=", pretend(&var2,word):5, " value=", var2:5);
|
||||
writeln("memtop2 address=", pretend(&memtop2,word):5, " value=", memtop2:5)
|
||||
corp
|
||||
|
|
@ -0,0 +1,4 @@
|
|||
........
|
||||
A%=100
|
||||
ADDR=VARPTR(A%)
|
||||
.......
|
||||
|
|
@ -0,0 +1,8 @@
|
|||
PROGRAM POINTER
|
||||
BEGIN
|
||||
A%=100
|
||||
ADDR=VARPTR(A%)
|
||||
PRINT(A%) ! prints 100
|
||||
POKE(ADDR,200)
|
||||
PRINT(A%) ! prints 200
|
||||
END PROGRAM
|
||||
|
|
@ -0,0 +1,9 @@
|
|||
program test_loc
|
||||
implicit none
|
||||
|
||||
integer :: i
|
||||
real :: r
|
||||
|
||||
i = loc(r)
|
||||
print *, i
|
||||
end program
|
||||
|
|
@ -0,0 +1,4 @@
|
|||
' FB 1.05.0 Win64
|
||||
Dim a As Integer = 3
|
||||
Dim p As Integer Ptr = @a
|
||||
Print a, p
|
||||
|
|
@ -0,0 +1,3 @@
|
|||
Var p = Cast(Integer Ptr, 1375832)
|
||||
*p = 42
|
||||
Print p, *p
|
||||
|
|
@ -0,0 +1,11 @@
|
|||
window 1
|
||||
|
||||
short i = 575
|
||||
ptr j
|
||||
|
||||
j = @i
|
||||
|
||||
printf @"Address of i = %ld",j
|
||||
print @"Value of i = ";peek word(j)
|
||||
|
||||
HandleEvents
|
||||
27
Task/Address-of-a-variable/GDScript/address-of-a-variable.gd
Normal file
27
Task/Address-of-a-variable/GDScript/address-of-a-variable.gd
Normal file
|
|
@ -0,0 +1,27 @@
|
|||
extends MainLoop
|
||||
|
||||
|
||||
func _process(_delta: float) -> bool:
|
||||
var a := Node.new()
|
||||
var b := Node.new()
|
||||
|
||||
# a and b are different objects with different RIDs
|
||||
print(a.get_instance_id())
|
||||
print(b.get_instance_id())
|
||||
|
||||
assert(a != b)
|
||||
assert(a.get_instance_id() != b.get_instance_id())
|
||||
|
||||
# Set b to refer to the same object as a
|
||||
b.free()
|
||||
b = a
|
||||
|
||||
# a and b are now the same object and have the same RID
|
||||
print(a.get_instance_id())
|
||||
print(b.get_instance_id())
|
||||
|
||||
assert(a == b)
|
||||
assert(a.get_instance_id() == b.get_instance_id())
|
||||
|
||||
a.free()
|
||||
return true # Exit
|
||||
32
Task/Address-of-a-variable/Go/address-of-a-variable.go
Normal file
32
Task/Address-of-a-variable/Go/address-of-a-variable.go
Normal file
|
|
@ -0,0 +1,32 @@
|
|||
package main
|
||||
|
||||
import (
|
||||
"fmt"
|
||||
"unsafe"
|
||||
)
|
||||
|
||||
func main() {
|
||||
myVar := 3.14
|
||||
myPointer := &myVar
|
||||
fmt.Println("Address:", myPointer, &myVar)
|
||||
fmt.Printf("Address: %p %p\n", myPointer, &myVar)
|
||||
|
||||
var addr64 int64
|
||||
var addr32 int32
|
||||
ptr := unsafe.Pointer(myPointer)
|
||||
if unsafe.Sizeof(ptr) <= unsafe.Sizeof(addr64) {
|
||||
addr64 = int64(uintptr(ptr))
|
||||
fmt.Printf("Pointer stored in int64: %#016x\n", addr64)
|
||||
}
|
||||
if unsafe.Sizeof(ptr) <= unsafe.Sizeof(addr32) {
|
||||
// Only runs on architectures where a pointer is <= 32 bits
|
||||
addr32 = int32(uintptr(ptr))
|
||||
fmt.Printf("Pointer stored in int32: %#08x\n", addr32)
|
||||
}
|
||||
addr := uintptr(ptr)
|
||||
fmt.Printf("Pointer stored in uintptr: %#08x\n", addr)
|
||||
|
||||
fmt.Println("value as float:", myVar)
|
||||
i := (*int32)(unsafe.Pointer(&myVar))
|
||||
fmt.Printf("value as int32: %#08x\n", *i)
|
||||
}
|
||||
|
|
@ -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/J/address-of-a-variable.j
Normal file
3
Task/Address-of-a-variable/J/address-of-a-variable.j
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,8 @@
|
|||
julia> x = [1, 2, 3]
|
||||
julia> ptr = pointer_from_objref(x)
|
||||
Ptr{Void} @0x000000010282e4a0
|
||||
julia> unsafe_pointer_to_objref(ptr)
|
||||
3-element Array{Int64,1}:
|
||||
1
|
||||
2
|
||||
3
|
||||
|
|
@ -0,0 +1,24 @@
|
|||
julia> A = [1, 2.3, 4]
|
||||
3-element Array{Float64,1}:
|
||||
1.0
|
||||
2.3
|
||||
4.0
|
||||
|
||||
julia> p = pointer(A)
|
||||
Ptr{Float64} @0x0000000113f70d60
|
||||
|
||||
julia> unsafe_load(p, 3)
|
||||
4.0
|
||||
|
||||
julia> unsafe_store!(p, 3.14159, 3)
|
||||
julia> A
|
||||
3-element Array{Float64,1}:
|
||||
1.0
|
||||
2.3
|
||||
3.14149
|
||||
|
||||
julia> pointer_to_array(p, (3,))
|
||||
3-element Array{Float64,1}:
|
||||
1.0
|
||||
2.3
|
||||
3.14149
|
||||
|
|
@ -0,0 +1,8 @@
|
|||
julia>
|
||||
julia> q = convert(Ptr{Float64}, 0x0000000113f70d68)
|
||||
Ptr{Float64} @0x0000000113f70d68
|
||||
|
||||
julia> B = pointer_to_array(q, (2,))
|
||||
2-element Array{Float64,1}:
|
||||
2.3
|
||||
3.14149
|
||||
|
|
@ -0,0 +1,10 @@
|
|||
// Kotlin Native v0.5
|
||||
|
||||
import kotlinx.cinterop.*
|
||||
|
||||
fun main(args: Array<String>) {
|
||||
val intVar = nativeHeap.alloc<IntVar>()
|
||||
intVar.value = 42
|
||||
with(intVar) { println("Value is $value, address is $rawPtr") }
|
||||
nativeHeap.free(intVar)
|
||||
}
|
||||
|
|
@ -0,0 +1,23 @@
|
|||
1) lambdas
|
||||
|
||||
{lambda {:x} {* :x :x}}
|
||||
-> _LAMB_123
|
||||
|
||||
2) arrays
|
||||
|
||||
'{A.new hello world} // defining an array
|
||||
-> _ARRA_123 // as replaced and used before post-processing
|
||||
-> [hello,world] // if unused after post-processing
|
||||
|
||||
3) pairs
|
||||
|
||||
{pre
|
||||
'{P.new hello world} // defining a pair
|
||||
-> _PAIR_123 // as replaced and used before post-processing
|
||||
-> (hello world) // if unused after post-processing
|
||||
|
||||
4) quotes
|
||||
|
||||
'{+ 1 2} // protecting an expression
|
||||
-> _QUOT_124 // as replaced and protected before post-processing
|
||||
-> {+ 1 2} // as displayed after post-processing
|
||||
10
Task/Address-of-a-variable/Lua/address-of-a-variable.lua
Normal file
10
Task/Address-of-a-variable/Lua/address-of-a-variable.lua
Normal file
|
|
@ -0,0 +1,10 @@
|
|||
t = {}
|
||||
print(t)
|
||||
f = function() end
|
||||
print(f)
|
||||
c = coroutine.create(function() end)
|
||||
print(c)
|
||||
u = io.open("/dev/null","w")
|
||||
print(u)
|
||||
print(_G, _ENV) -- global/local environments (are same here)
|
||||
print(string.format("%p %p %p", print, string, string.format)) -- themselves formatted as pointers
|
||||
|
|
@ -0,0 +1,34 @@
|
|||
module checkVarptr {
|
||||
declare GetMem8 lib "msvbvm60.GetMem8" {Long addr, Long retValue}
|
||||
long long x=1234567812345678&&, z
|
||||
dim k(2,2,2) as long long
|
||||
call GetMem8(VarPtr(x), VarPtr(z))
|
||||
call GetMem8(VarPtr(x), VarPtr(k(1,0,1)))
|
||||
print z=x
|
||||
print k(1,0,1)=x
|
||||
link z to m
|
||||
print VarPtr(z)=VarPtr(m)
|
||||
checkref(&z)
|
||||
sub checkref(&p)
|
||||
print VarPtr(z)=VarPtr(p)
|
||||
end sub
|
||||
}
|
||||
checkVarptr
|
||||
module checkVarptr {
|
||||
// Using byref at GemMem8 signature
|
||||
declare GetMem8 lib "msvbvm60.GetMem8" {long &addr, long &retValue}
|
||||
declare PutMem8 lib "msvbvm60.PutMem8" {long &addr, retValue as long long}
|
||||
long long x=1234567812345678&&, z
|
||||
dim k(2,2,2) as long long
|
||||
call GetMem8(&x, &z)
|
||||
call GetMem8(&x, &k(1,0,1))
|
||||
print z=x
|
||||
print k(1,0,1)=x
|
||||
checkref(&z)
|
||||
print z=987654321987654321&&
|
||||
sub checkref(&p)
|
||||
print VarPtr(z)=VarPtr(p)
|
||||
call PutMem8(&p, 987654321987654321&&)
|
||||
end sub
|
||||
}
|
||||
checkVarptr
|
||||
|
|
@ -0,0 +1,2 @@
|
|||
> addressof( x );
|
||||
18446884674469911422
|
||||
|
|
@ -0,0 +1,2 @@
|
|||
> pointto( 18446884674469911422 );
|
||||
x
|
||||
|
|
@ -0,0 +1,6 @@
|
|||
> addressof( sin( x )^2 + cos( x )^2 );
|
||||
18446884674469972158
|
||||
|
||||
> pointto( 18446884674469972158 );
|
||||
2 2
|
||||
sin(x) + cos(x)
|
||||
|
|
@ -0,0 +1,12 @@
|
|||
MODULE GetAddress;
|
||||
|
||||
FROM SYSTEM IMPORT ADR;
|
||||
FROM InOut IMPORT WriteInt, WriteLn;
|
||||
|
||||
VAR var : INTEGER;
|
||||
adr : LONGINT;
|
||||
BEGIN
|
||||
adr := ADR(var); (*get the address*)
|
||||
WriteInt(adr, 0);
|
||||
WriteLn;
|
||||
END GetAddress.
|
||||
|
|
@ -0,0 +1,9 @@
|
|||
MODULE SetAddress;
|
||||
|
||||
CONST adress = 134664460;
|
||||
|
||||
VAR var [adress] : INTEGER;
|
||||
|
||||
BEGIN
|
||||
(*do nothing*)
|
||||
END SetAddress.
|
||||
|
|
@ -0,0 +1,5 @@
|
|||
import native
|
||||
|
||||
a = 5
|
||||
|
||||
println format("0x%08x", native.address(a))
|
||||
|
|
@ -0,0 +1,10 @@
|
|||
import native
|
||||
|
||||
a = 123
|
||||
b = 456
|
||||
|
||||
ptr = native.address(a)
|
||||
println native.object(ptr)
|
||||
|
||||
ptr = native.address(b)
|
||||
println native.object(ptr)
|
||||
|
|
@ -0,0 +1,2 @@
|
|||
(set 'a '(1 2 3))
|
||||
(address a)
|
||||
4
Task/Address-of-a-variable/Nim/address-of-a-variable.nim
Normal file
4
Task/Address-of-a-variable/Nim/address-of-a-variable.nim
Normal file
|
|
@ -0,0 +1,4 @@
|
|||
var x = 12
|
||||
var xptr = addr(x) # Get address of variable
|
||||
echo cast[int](xptr) # and print it
|
||||
xptr = cast[ptr int](0xFFFE) # Set the address
|
||||
13
Task/Address-of-a-variable/OCaml/address-of-a-variable.ocaml
Normal file
13
Task/Address-of-a-variable/OCaml/address-of-a-variable.ocaml
Normal file
|
|
@ -0,0 +1,13 @@
|
|||
let address_of (x:'a) : nativeint =
|
||||
if Obj.is_block (Obj.repr x) then
|
||||
Nativeint.shift_left (Nativeint.of_int (Obj.magic x)) 1 (* magic *)
|
||||
else
|
||||
invalid_arg "Can only find address of boxed values.";;
|
||||
|
||||
let () =
|
||||
let a = 3.14 in
|
||||
Printf.printf "%nx\n" (address_of a);;
|
||||
let b = ref 42 in
|
||||
Printf.printf "%nx\n" (address_of b);;
|
||||
let c = 17 in
|
||||
Printf.printf "%nx\n" (address_of c);; (* error, because int is unboxed *)
|
||||
|
|
@ -0,0 +1,5 @@
|
|||
VAR a: LONGINT;
|
||||
VAR b: INTEGER;
|
||||
|
||||
b := 10;
|
||||
a := SYSTEM.ADR(b); (* Sets variable a to the address of variable b *)
|
||||
|
|
@ -0,0 +1 @@
|
|||
SYSTEM.PUT(a, b); (* Sets the address of b to the address of a *)
|
||||
|
|
@ -0,0 +1,9 @@
|
|||
package main
|
||||
|
||||
main :: proc() {
|
||||
i : int = 42
|
||||
ip : ^int = &i // Get address of variable
|
||||
|
||||
address := uintptr(0xdeadf00d)
|
||||
ip2 := cast(^int)address // Set the address
|
||||
}
|
||||
14
Task/Address-of-a-variable/Oforth/address-of-a-variable.fth
Normal file
14
Task/Address-of-a-variable/Oforth/address-of-a-variable.fth
Normal file
|
|
@ -0,0 +1,14 @@
|
|||
tvar: A
|
||||
10 to A
|
||||
|
||||
tvar: B
|
||||
#A to B
|
||||
B .s
|
||||
[1] (Variable) #A
|
||||
>ok
|
||||
|
||||
12 B put
|
||||
A .s
|
||||
[1] (Integer) 12
|
||||
[2] (Variable) #A
|
||||
>ok
|
||||
|
|
@ -0,0 +1 @@
|
|||
issquare(n, &m)
|
||||
33
Task/Address-of-a-variable/PL-I/address-of-a-variable.pli
Normal file
33
Task/Address-of-a-variable/PL-I/address-of-a-variable.pli
Normal file
|
|
@ -0,0 +1,33 @@
|
|||
declare addr builtin; /* retrieve address of a variable */
|
||||
declare ptradd builtin; /* pointer addition */
|
||||
declare cstg builtin; /* retrieve length of the storage of a variable */
|
||||
declare hbound builtin; /* retrieve the number of elements in an array */
|
||||
|
||||
declare p pointer;
|
||||
declare i bin fixed(31) init(42);
|
||||
p = addr(i); /* Obtain address of variable, stored in integer variable k */
|
||||
|
||||
/* how to read a string bit by bit - example for pointerAdd */
|
||||
/* we built a pointer (movingPointer), which will move through the */
|
||||
/* storage of a variable (exampleTxt). attached to the pointer is */
|
||||
/* an array of bits (movingBit) - this means wherever the pointer */
|
||||
/* is pointing to, this will also be the position of the array. */
|
||||
/* only whole bytes can be addressed. to get down to the single bits, */
|
||||
/* an array of 8 bits is used. */
|
||||
|
||||
declare exampleTxt char(16) init('Hello MainFrame!);
|
||||
declare movingPointer pointer;
|
||||
declare movingBit(8) bit(01) based(movingPointer);
|
||||
|
||||
declare walkOffset bin fixed(31);
|
||||
declare walkBit bin fixed(31);
|
||||
|
||||
do walkOffset = 0 to cstg(exampleTxt)-1;
|
||||
movingPointer = ptradd(addr(exampleTxt, walkOffset);
|
||||
|
||||
do walkBit = 1 to hbound(movingBit);
|
||||
put skip list( 'bit at Byte ' !!walkOffset
|
||||
!!' and position '!!walkBit
|
||||
!!' is ' !!movingBit(walkBit));
|
||||
end;
|
||||
end;
|
||||
79
Task/Address-of-a-variable/PL-M/address-of-a-variable.plm
Normal file
79
Task/Address-of-a-variable/PL-M/address-of-a-variable.plm
Normal file
|
|
@ -0,0 +1,79 @@
|
|||
100H:
|
||||
/* THERE IS NO STANDARD LIBRARY
|
||||
THIS DEFINES SOME BASIC I/O USING CP/M */
|
||||
BDOS: PROCEDURE (F,A); DECLARE F BYTE, A ADDRESS; GO TO 5; END BDOS;
|
||||
EXIT: PROCEDURE; CALL BDOS(0,0); END EXIT;
|
||||
PUT$CHAR: PROCEDURE (C); DECLARE C BYTE; CALL BDOS(2,C); END PUT$CHAR;
|
||||
PRINT: PROCEDURE (S); DECLARE S ADDRESS; CALL BDOS(9,S); END PRINT;
|
||||
NEWLINE: PROCEDURE; CALL PRINT(.(13,10,'$')); END NEWLINE;
|
||||
|
||||
/* PRINT 16-BIT HEX VALUE (POINTER) */
|
||||
PRINT$HEX: PROCEDURE (N);
|
||||
DECLARE N ADDRESS, (I, C) BYTE;
|
||||
DO I=0 TO 3;
|
||||
IF I <> 3 THEN C = SHR(N,12-4*I) AND 0FH;
|
||||
ELSE C = N AND 0FH;
|
||||
IF C >= 10 THEN C = C - 10 + 'A';
|
||||
ELSE C = C + '0';
|
||||
CALL PUT$CHAR(C);
|
||||
END;
|
||||
END PRINT$HEX;
|
||||
|
||||
/* OF COURSE WE WILL NEED SOME VALUES TO POINT AT TOO */
|
||||
DECLARE FOO ADDRESS INITIAL (0F00H);
|
||||
DECLARE BAR ADDRESS INITIAL (0BA1H);
|
||||
|
||||
/* OK, NOW ON TO THE DEMONSTRATION */
|
||||
|
||||
/* THE ADDRESS OF A VARIABLE CAN BE RETRIEVED BY PREPENDING IT WITH A
|
||||
DOT, E.G.: */
|
||||
CALL PRINT(.'FOO IS $'); CALL PRINT$HEX(FOO);
|
||||
CALL PRINT(.' AND ITS ADDRESS IS $'); CALL PRINT$HEX(.FOO);
|
||||
CALL NEWLINE;
|
||||
|
||||
CALL PRINT(.'BAR IS $'); CALL PRINT$HEX(BAR);
|
||||
CALL PRINT(.' AND ITS ADDRESS IS $'); CALL PRINT$HEX(.BAR);
|
||||
CALL NEWLINE;
|
||||
|
||||
/* PL/M DOES NOT HAVE C-STYLE POINTERS. INSTEAD IT HAS 'BASED' VARIABLES.
|
||||
|
||||
WHEN A VARIABLE IS DECLARED 'BASED', ITS ADDRESS IS STORED IN ANOTHER
|
||||
VARIABLE.
|
||||
|
||||
NOTE HOWEVER THAT THE 'ADDRESS' DATATYPE IS SIMPLY A 16-BIT INTEGER,
|
||||
AND DOES NOT INTRINSICALLY POINT TO ANYTHING.
|
||||
|
||||
THE FOLLOWING DECLARES A VARIABLE 'POINTER', WHICH WILL HOLD AN ADDRESS,
|
||||
AND A VARIABLE 'VALUE' WHICH WILL BE LOCATED AT WHATEVER THE VALUE IN
|
||||
'POINTER' IS. */
|
||||
|
||||
DECLARE POINTER ADDRESS;
|
||||
DECLARE VALUE BASED POINTER ADDRESS;
|
||||
|
||||
/* WE CAN NOW ACCESS 'FOO' AND 'BAR' THROUGH 'VALUE' BY ASSIGNING THEIR
|
||||
ADDRESSES TO 'POINTER'. */
|
||||
POINTER = .FOO;
|
||||
CALL PRINT(.'POINTER IS $'); CALL PRINT$HEX(POINTER);
|
||||
CALL PRINT(.' AND VALUE IS $'); CALL PRINT$HEX(VALUE); CALL NEWLINE;
|
||||
|
||||
POINTER = .BAR;
|
||||
CALL PRINT(.'POINTER IS $'); CALL PRINT$HEX(POINTER);
|
||||
CALL PRINT(.' AND VALUE IS $'); CALL PRINT$HEX(VALUE); CALL NEWLINE;
|
||||
|
||||
/* MUTATION IS ALSO POSSIBLE OF COURSE */
|
||||
VALUE = 0BA3H;
|
||||
CALL PRINT(.'VALUE IS NOW $'); CALL PRINT$HEX(VALUE);
|
||||
CALL PRINT(.' AND BAR IS NOW $'); CALL PRINT$HEX(BAR); CALL NEWLINE;
|
||||
|
||||
/* LASTLY, PL/M SUPPORTS ANONYMOUS CONSTANTS.
|
||||
YOU CAN TAKE THE ADDRESS OF AN IMMEDIATE CONSTANT, AND PL/M
|
||||
WILL STORE THE CONSTANT IN THE CONSTANT POOL AND GIVE YOU ITS
|
||||
ADDRESS. THE CONSTANT MUST HOWEVER BE A BYTE OR A LIST OF BYTES. */
|
||||
POINTER = .(0FEH,0CAH); /* NOTE THE DOT, AND THE LOW-ENDIAN 16-BIT VALUE */
|
||||
CALL PRINT(.'POINTER IS $'); CALL PRINT$HEX(POINTER);
|
||||
CALL PRINT(.' AND VALUE IS $'); CALL PRINT$HEX(VALUE); CALL NEWLINE;
|
||||
|
||||
/* NOTE THAT THIS IS ALSO HOW STRINGS WORK - SEE ALL THE DOTS IN FRONT
|
||||
OF THE STRINGS IN THE 'PRINT' CALLS */
|
||||
CALL EXIT;
|
||||
EOF
|
||||
|
|
@ -0,0 +1,43 @@
|
|||
== Get ==
|
||||
|
||||
adr(variable)
|
||||
|
||||
Example:
|
||||
|
||||
dim a
|
||||
print adr(a)
|
||||
|
||||
== Set ==
|
||||
|
||||
Whether Panoramic is able to set the value of a variable may depend on what is meant by that. Panoramic implements the
|
||||
poke command to set a byte from a value of 0 to 255 (inclusive). Panoramic also implements the peek command to get
|
||||
the value of a byte, so it is possible to the following:
|
||||
|
||||
(A)
|
||||
dim a
|
||||
rem a variable with no post-fix is a real.
|
||||
poke adr(a),57
|
||||
rem the value of a variable being set by setting an address, the address of a in this instance.
|
||||
|
||||
(B)
|
||||
dim a%,b%
|
||||
rem % means integer.
|
||||
b%=57
|
||||
poke adr(a%),b%
|
||||
rem b% being assigned to the address of a%, in this instance.
|
||||
rem it is even possible to free b%
|
||||
free b%
|
||||
print a%
|
||||
|
||||
(C)
|
||||
dim a,b
|
||||
b=57
|
||||
poke adr(a),b
|
||||
b=peek(adr(a))
|
||||
print b
|
||||
rem the address of b being, in effect, set to the address of a, the address of a, in this instance.
|
||||
|
||||
rem Observations and further insight welcome.
|
||||
|
||||
''Note:'' An attempt to poke a real or an integer (Panoramic's only numeric types) value of less than 0 or of more than
|
||||
255 will cause an error.
|
||||
|
|
@ -0,0 +1,2 @@
|
|||
use Scalar::Util qw(refaddr);
|
||||
print refaddr(\my $v), "\n"; # 140502490125712
|
||||
|
|
@ -0,0 +1 @@
|
|||
printf "%p", $v; # 7fc949039590
|
||||
|
|
@ -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
|
||||
26
Task/Address-of-a-variable/Phix/address-of-a-variable.phix
Normal file
26
Task/Address-of-a-variable/Phix/address-of-a-variable.phix
Normal file
|
|
@ -0,0 +1,26 @@
|
|||
-->
|
||||
<span style="color: #008080;">procedure</span> <span style="color: #000000;">address</span><span style="color: #0000FF;">()</span>
|
||||
<span style="color: #004080;">object</span> <span style="color: #000000;">V</span>
|
||||
<span style="color: #004080;">integer</span> <span style="color: #000000;">addr4</span> <span style="color: #000080;font-style:italic;">-- stored /4 (assuming dword aligned, which it will be)</span>
|
||||
#ilASM{
|
||||
[32]
|
||||
lea eax,[V]
|
||||
shr eax,2
|
||||
mov [addr4],eax
|
||||
[64]
|
||||
lea rax,[V]
|
||||
shr rax,2
|
||||
mov [addr4],rax
|
||||
[]
|
||||
}
|
||||
<span style="color: #008080;">if</span> <span style="color: #7060A8;">machine_bits</span><span style="color: #0000FF;">()=</span><span style="color: #000000;">32</span> <span style="color: #008080;">then</span>
|
||||
<span style="color: #000000;">poke4</span><span style="color: #0000FF;">(</span><span style="color: #000000;">addr4</span><span style="color: #0000FF;">*</span><span style="color: #000000;">4</span><span style="color: #0000FF;">,</span><span style="color: #000000;">123</span><span style="color: #0000FF;">)</span>
|
||||
<span style="color: #008080;">elsif</span> <span style="color: #7060A8;">machine_bits</span><span style="color: #0000FF;">()=</span><span style="color: #000000;">64</span> <span style="color: #008080;">then</span>
|
||||
<span style="color: #000000;">poke8</span><span style="color: #0000FF;">(</span><span style="color: #000000;">addr4</span><span style="color: #0000FF;">*</span><span style="color: #000000;">4</span><span style="color: #0000FF;">,</span><span style="color: #000000;">123</span><span style="color: #0000FF;">)</span>
|
||||
<span style="color: #008080;">end</span> <span style="color: #008080;">if</span>
|
||||
<span style="color: #0000FF;">?</span><span style="color: #000000;">V</span>
|
||||
<span style="color: #008080;">if</span> <span style="color: #7060A8;">getc</span><span style="color: #0000FF;">(</span><span style="color: #000000;">0</span><span style="color: #0000FF;">)</span> <span style="color: #008080;">then</span> <span style="color: #008080;">end</span> <span style="color: #008080;">if</span>
|
||||
<span style="color: #008080;">end</span> <span style="color: #008080;">procedure</span>
|
||||
|
||||
<span style="color: #000000;">address</span><span style="color: #0000FF;">()</span>
|
||||
<!--
|
||||
Some files were not shown because too many files have changed in this diff Show more
Loading…
Add table
Add a link
Reference in a new issue