Data commit

This commit is contained in:
Ingy döt Net 2023-07-01 11:58:00 -04:00
parent 7387c8f97b
commit cb5bb5e222
199093 changed files with 3378972 additions and 0 deletions

View file

@ -0,0 +1,3 @@
---
from: http://rosettacode.org/wiki/Pointers_and_references
note: Basic Data Operations

View file

@ -0,0 +1 @@
{{basic data operation}}In this task, the goal is to demonstrate common operations on pointers and references. These examples show pointer operations on the stack, which can be dangerous and is rarely done. Pointers and references are commonly used along with [[Memory allocation]] on the [[heap]].

View file

@ -0,0 +1,2 @@
LDA 8 ;load the byte stored at memory address 0x0008 into the accumulator.
LDA #8 ;load the number 8 into the accumulator.

View file

@ -0,0 +1,2 @@
LDX $2000 ;load the byte at memory address $2000 into X.
LDY $75 ;load the byte at memory address $0075 into Y. This instruction executes faster than the one above it.

View file

@ -0,0 +1,2 @@
TSX
LDA $0101,X ;load the byte most recently pushed on the stack into A without pulling it off the stack.

View file

@ -0,0 +1,3 @@
inline unsigned char foo (unsigned char a, unsigned char b, unsigned char c){
return a+b+c;
}

View file

@ -0,0 +1,18 @@
LDA #arg_C ;load whatever this value is into accumulator
PHA
LDA #arg_B
PHA
LDA #arg_A
PHA
foo:
PHX
TSX
;stack looks like this: __, X,LOW(PC),HIGH(PC),arg_A,arg_B,arg_C
;stack pointer points to __, as does $100,X after a TSX
LDA $0106,X ;LDA #arg_C
CLC
ADC $0105,X
CLC
ADC $0104,X ;ADC #arg_A
;we have the desired value in A, now return.
PLX

View file

@ -0,0 +1,4 @@
LDX #$80
LDA $2000,X ;load the byte stored at $2080
LDY #$FF
LDA $2080,Y ;load the byte stored at $217F

View file

@ -0,0 +1,2 @@
LDX #$C0
LDA $80,X ;load from $0040

View file

@ -0,0 +1,3 @@
myVar equ $100000 ; a section of user ram given a label
myData: ; a data table given a label
dc.b $80,$81,$82,$83

View file

@ -0,0 +1,15 @@
;implements:
;uint64_t foo (uint16_t a,uint16_t b,uint16_t c){return a+b+c;}
MOVE.L #arg2,-(SP)
MOVE.L #arg1,-(SP)
MOVE.L #arg0,-(SP)
JSR foo
LEA (12,SP),SP ;discard the three values pushed prior to the call.
RTS
foo:
;outputs to D0
MOVE.L (4,SP),D0
ADD.L (8,SP),D0
ADD.L (12,SP),D0
RTS

View file

@ -0,0 +1 @@
MOVE.B #$10,myVar

View file

@ -0,0 +1 @@
MOVE.L #$FFEEDDCC,myVar

View file

@ -0,0 +1 @@
LEA myData,A0 ;address of myData is stored in A0

View file

@ -0,0 +1,3 @@
MOVE.B (A0),D0 ;D0 = 0x00000080
MOVE.W (A0),D0 ;D0 = 0x00008081
MOVE.L (A0),D0 ;D0 = 0x80818283

View file

@ -0,0 +1,4 @@
LEA myData,A0 ;Assume for this example that data registers all equal 0.
MOVE.B (A0)+,D0 ;D0 = 00000080
MOVE.B (A0)+,D1 ;D1 = 00000081
MOVE.B (A0)+,D2 ;D2 = 00000082

View file

@ -0,0 +1,4 @@
LEA myData+4,A0 ;Assume for this example that data registers all equal 0.
MOVE.B -(A0),D0 ;D0 = 00000083
MOVE.B -(A0),D1 ;D1 = 00000082
MOVE.B -(A0),D2 ;D2 = 00000081

View file

@ -0,0 +1,3 @@
LEA myData,A0
MOVE.W #$0C,D0
LEA (4,A0,D0),A3 ;A3 = address of myData + 4 + $0C

View file

@ -0,0 +1,3 @@
myPointers: dc.l myData, myData2
myData: dc.b $80,$81,$82,$83
myData2: dc.b $84,$85,$86,$87

View file

@ -0,0 +1 @@
LEA myData2,A1

View file

@ -0,0 +1,2 @@
LEA myPointers,A1
LEA (4,A1),A1

View file

@ -0,0 +1,14 @@
.model small
.stack 1024
.data ; data segment begins here
UserRam byte 256 dup (0) ; the next 256 bytes have a value of zero. The address of the 0th of these bytes can be referenced as "UserRam"
tempByte equ UserRam ;this variable's address is the same as that of the 0th byte of UserRam
tempWord equ UserRam+2 ;this variable's address is the address of UserRam + 2
.code
start:
mov ax, @data
mov ds, ax
mov ax, @code
mov es, ax

View file

@ -0,0 +1,9 @@
mov ax,0FFFFh ;see note 1
mov word ptr [ds:tempWord],ax ;store hexadecimal value FFFF into tempWord
mov bl, 80h
mov byte ptr [ds:tempByte],bl ;the register size needs to match the data type
;Note 1:
;UASM doesn't like leading hex digits A-F so a 0 is placed in front.
;It doesn't change the storage type of the operand. (i.e. this is still a 16 bit value even though there are 5 digits)

View file

@ -0,0 +1,4 @@
mov ax, byte ptr [ds:tempByte] ;assembler will generate an error - operands don't match
mov al, word ptr [ds:tempWord] ;assembler will generate an error - operands don't match
mov ax, word ptr [ds:tempByte] ;assembler will allow this even though the intended data size is wrong
mov al, byte ptr [ds:tempWord] ;assembler will allow this even though the intended data size is wrong

View file

@ -0,0 +1 @@
LDS bx,UserRamPtr ;loads [ds:bx] with UserRamPtr

View file

@ -0,0 +1,6 @@
.data
myString db "Hello World!",0 ;the zero is the null terminator
.code
mov bx, seg myString ;load into bx the segment where myString is stored.
mov ds, bx ;load this segment into the data segment register. On the 8086, segment registers can't be loaded directly.
mov bx, offset MyString ;the memory address of the beginning of myString. The "H" is stored here.

View file

@ -0,0 +1,2 @@
add bx, 2 ;add 2 to bx. bx contains the memory address of the first "l" in "Hello"
mov al,[ds:bx] ;dereference the pointer and store the value it points to into al.

View file

@ -0,0 +1,2 @@
INT var := 3;
REF INT pointer := var;

View file

@ -0,0 +1,3 @@
[9,9]INT sudoku;
REF [,]INT middle;
middle := sudoku[4:6,4:6];

View file

@ -0,0 +1,4 @@
[30]CHAR hay stack := "straw straw needle straw straw";
REF[]CHAR needle = hay stack[13:18];
needle[2:3] := "oo";
print((hay stack))

View file

@ -0,0 +1,2 @@
INT v = pointer; # sets v to the value of var (i.e. 3) #
REF INT(pointer) := 42; # sets var to 42 #

View file

@ -0,0 +1,2 @@
INT othervar;
pointer := othervar;

View file

@ -0,0 +1 @@
pointer := NIL; # 0 cannot be cast to NIL #

View file

@ -0,0 +1,2 @@
[9]INT array;
pointer := array[LWB array];

View file

@ -0,0 +1 @@
REF INT alias = var;

View file

@ -0,0 +1,2 @@
INT v2 = alias; # sets v2 to the value of var, that is, 3 #
alias := 42; # sets var to 42 #

View file

@ -0,0 +1 @@
printf(($"alias "b("IS","ISNT")" var!"l$, alias IS var));

View file

@ -0,0 +1,2 @@
[9]INT array2;
REF INT ref3 = array2[LWB array2];

View file

@ -0,0 +1,4 @@
;this example uses VASM syntax, your assembler may not use the # before constants or the semicolon for comments
MOV R1,#0x04000000
MOV R0,#0x403
STR R0,[R1] ;store 0x403 into memory address 0x04000000.

View file

@ -0,0 +1,2 @@
type Int_Access is access Integer;
Int_Acc : Int_Access := new Integer'(5);

View file

@ -0,0 +1 @@
type Safe_Int_Access is not null access Integer;

View file

@ -0,0 +1,7 @@
declare
type Int_Ptr is access all Integer;
Ref : Int_Ptr;
Var : aliased Integer := 3;
Val : Integer := Var;
begin
Ref := Var'Access; -- "Ref := Val'Access;" would be a syntax error

View file

@ -0,0 +1 @@
type Safe_Int_Ptr is not null access all Integer;

View file

@ -0,0 +1,2 @@
Var : Integer;
Var_Address : Address := Var'Address;

View file

@ -0,0 +1,5 @@
-- Demonstrate the overlay of one object on another
A : Integer;
B : Integer;
for B'Address use A'Address;
-- A and B start at the same address

View file

@ -0,0 +1,8 @@
type Container is array (Positive range <>) of Element;
for I in Container'Range loop
declare
Item : Element renames Container (I);
begin
Do_Something(Item); -- Here Item is a reference to Container (I)
end;
end loop;

View file

@ -0,0 +1,4 @@
type Container is array (Positive range <>) of Element;
for Item of Container loop
Do_Something(Item);
end loop;

View file

@ -0,0 +1,5 @@
VarSetCapacity(var, 100) ; allocate memory
NumPut(87, var, 0, "Char") ; store 87 at offset 0
MsgBox % NumGet(var, 0, "Char") ; get character at offset 0 (87)
MsgBox % &var ; address of contents pointed to by var structure
MsgBox % *&var ; integer at address of var contents (87)

View file

@ -0,0 +1,26 @@
REM Pointer to integer variable:
pointer_to_varA = ^varA%
!pointer_to_varA = 123456
PRINT !pointer_to_varA
REM Pointer to variant variable:
pointer_to_varB = ^varB
|pointer_to_varB = PI
PRINT |pointer_to_varB
REM Pointer to procedure:
PROCmyproc : REM conventional call to initialise
pointer_to_myproc = ^PROCmyproc
PROC(pointer_to_myproc)
REM Pointer to function:
pointer_to_myfunc = ^FNmyfunc
PRINT FN(pointer_to_myfunc)
END
DEF PROCmyproc
PRINT "Executing myproc"
ENDPROC
DEF FNmyfunc
= "Returned from myfunc"

View file

@ -0,0 +1 @@
int* pointer2(&var);

View file

@ -0,0 +1,4 @@
int var = 3;
int& ref = var;
// or alternatively:
int& ref2(var);

View file

@ -0,0 +1,2 @@
int v = ref; // sets v to the value of var, that is, 3
ref = 42; // sets var to 42

View file

@ -0,0 +1,2 @@
int array[10];
int& ref3 = array[0];

View file

@ -0,0 +1 @@
v = (&ref)[3]; // read value of array[3]; however doing this is bad style

View file

@ -0,0 +1,25 @@
static void Main(string[] args)
{
int p;
p = 1;
Console.WriteLine("Ref Before: " + p);
Value(ref p);
Console.WriteLine("Ref After : " + p);
p = 1;
Console.WriteLine("Val Before: " + p);
Value(p);
Console.WriteLine("Val After : " + p);
Console.ReadLine();
}
private static void Value(ref int Value)
{
Value += 1;
}
private static void Value(int Value)
{
Value += 1;
}

View file

@ -0,0 +1,2 @@
int var = 3;
int *pointer = &var;

View file

@ -0,0 +1,37 @@
#include <stdio.h>
#include <stdlib.h>
typedef struct {
int val;
} num;
int addNodes(num **array, int elems);
int main(void) {
int numElems, i;
num *arr = NULL;
numElems = addNodes(&arr, 10);
for (i = 0; i < numElems; i++) {
printf("%d) %d\n", i+1, arr[i].val);
}
free(arr);
return 0;
}
int addNodes(num **array, int elems) {
num *temp = NULL;
int i;
for (i = 0; i < elems; i++) {
temp = realloc(*array, (i+1) * sizeof **array);
if (temp == NULL) {
free(*array); return -1;
} else {
*array = temp;
}
(*array)[i].val = i;
// Alternatives:
// ((*array)+i)->val = i; // or
// (*((*array)+i)).val = i;
}
return i;
}

View file

@ -0,0 +1,2 @@
int v = *pointer; /* sets v to the value of var (i.e. 3) */
*pointer = 42; /* sets var to 42 */

View file

@ -0,0 +1,2 @@
int othervar;
pointer = &othervar;

View file

@ -0,0 +1 @@
pointer = NULL; /* needs having stddef.h included */

View file

@ -0,0 +1 @@
pointer = 0; /* actually any constant integer expression evaluating to 0 could be used, e.g. (1-1) will work as well */

View file

@ -0,0 +1 @@
pointer = (void*)0; /* C only, not allowed in C++ */

View file

@ -0,0 +1,4 @@
int array[10];
pointer = array;
/* or alternatively: */
pointer = &array[0];

View file

@ -0,0 +1,2 @@
pointer += 3; /* pointer now points to array[3] */
pointer -= 2; /* pointer now points to array[1] */

View file

@ -0,0 +1,5 @@
v = pointer[3]; /* accesses third-next object, i.e. array[4] */
v = pointer[-1]; /* accesses previous object, i.e. array[0] */
/* or alternatively */
v = *(pointer + 3); /* array[4] */
v = *(pointer - 1); /* array[0] */

View file

@ -0,0 +1,2 @@
01 ptr USAGE POINTER TO Some-Type.
01 prog-ptr USAGE PROGRAM-POINTER "some-program". *> TO is optional

View file

@ -0,0 +1,3 @@
ALLOCATE heap-item RETURNING ptr
...
FREE ptr

View file

@ -0,0 +1 @@
SET prog-ptr TO ENTRY "some-program"

View file

@ -0,0 +1,2 @@
SET ptr1 UP BY 10
SET ptr2 DOWN BY LENGTH OF foo

View file

@ -0,0 +1,2 @@
SET ptr1 TO ptr2 *> ptr1 points to where ptr2 points
SET ptr2 TO ADDRESS OF ptr3 *> ptr2 points to ptr3

View file

@ -0,0 +1,2 @@
SET ADDRESS OF foo TO ptr
MOVE "bar" TO foo

View file

@ -0,0 +1 @@
01 obj USAGE OBJECT-REFERENCE "some-object".

View file

@ -0,0 +1,2 @@
INVOKE SomeClass "new" RETURNING obj-ref
SET another-obj-ref TO obj-ref

View file

@ -0,0 +1,28 @@
void main() {
// Take the address of 'var' and placing it in a pointer:
int var;
int* ptr = &var;
// Take the pointer to the first item of an array:
int[10] data;
auto p2 = data.ptr;
// Depending on variable type, D will automatically pass either
// by value or reference.
// By value: structs, statically sized arrays, and other
// primitives (int, char, etc...);
// By reference: classes;
// By kind of reference: dynamically sized arrays, array slices.
struct S {}
class C {}
void foo1(S s) {} // By value.
void foo2(C c) {} // By reference.
void foo3(int i) {} // By value.
void foo4(int[4] i) {} // By value (unlike C).
void foo6(int[] i) {} // Just length-pointer struct by value.
void foo5(T)(ref T t) {} // By reference regardless of what type
// T really is.
}

View file

@ -0,0 +1 @@
pMyPointer : Pointer ;

View file

@ -0,0 +1 @@
pIntPointer : ^Integer ;

View file

@ -0,0 +1,4 @@
MyRecord = Record
FName : string[20];
LName : string[20];
end;

View file

@ -0,0 +1 @@
pMyRecord : ^MyRecord ;

View file

@ -0,0 +1,5 @@
type
pFoo = ^tFoo; { allowed despite tFoo not yet being defined }
tFoo = record
value1, value2: integer;
end;

View file

@ -0,0 +1 @@
IntVar := pIntPointer^ ;

View file

@ -0,0 +1 @@
IntVar := integer(MyPointer^);

View file

@ -0,0 +1,3 @@
Inc(PtrVar); //increment by one element
Inc(PtrVar, 4); //incremement by four elements
Dec(PtrVar); //decrement by one element

View file

@ -0,0 +1,3 @@
{$POINTERMATH ON}
PrevIntVar := MyIntPtr[-1];
Rec4 := MyRecPtr[4];

View file

@ -0,0 +1,13 @@
(define B (box 42))
→ B ;; box reference
(unbox B)
→ 42 ;; box contents
;; sets new value for box contents
(define ( change-by-ref abox avalue)
(set-box! abox avalue) )
(change-by-ref B 666)
→ #[box 666]
(unbox B)
→ 666

View file

@ -0,0 +1 @@
real, pointer :: pointertoreal

View file

@ -0,0 +1,6 @@
type intpointer
integer, pointer :: p
end type intpointer
!...
type(intpointer), dimension(100) :: parray

View file

@ -0,0 +1 @@
nullify(pointertoreal)

View file

@ -0,0 +1 @@
real, pointer :: apointer => NULL()

View file

@ -0,0 +1,2 @@
real, target :: areal
pointertoreal => areal

View file

@ -0,0 +1 @@
if ( associated(pointertoreal) ) !...

View file

@ -0,0 +1,2 @@
integer, dimension(:), pointer :: array
allocate(array(100))

View file

@ -0,0 +1,4 @@
integer, target :: i
integer, pointer :: pi
!... ...
if ( associated(pi, target=i) ) !...

View file

@ -0,0 +1,8 @@
real, dimension(20), target :: a
real, dimension(20,20), target :: b
real, dimension(:), pointer :: p
p => a(5:20)
! p(1) == a(5), p(2) == a(6) ...
p => b(10,1:20)
! p(1) == b(10,1), p(2) == b(10,2) ...

View file

@ -0,0 +1,4 @@
real, dimension(20) :: a
real, dimension(16) :: p
p = a(5:20)

View file

@ -0,0 +1,47 @@
' FB 1.05.0 Win64
Type Cat
name As String
age As Integer
End Type
Type CatInfoType As Sub (As Cat Ptr)
Sub printCatInfo(c As Cat Ptr)
Print "Name "; c->name, "Age"; c-> age
Print
End Sub
' create Cat object on heap and store a pointer to it
Dim c As Cat Ptr = New Cat
' set fields using the pointer and the "crow's foot" operator
c->name = "Fluffy"
c->age = 9
' print them out through a procedure pointer
Dim cit As CatInfoType = ProcPtr(printCatInfo)
cit(c)
Delete c
c = 0
Dim i As Integer = 3
' create an integer pointer variable and set it to the address of 'i'
Dim pi As Integer Ptr = @i
'change the variable through the pointer
*pi = 4
'print out the result
print "i ="; *pi
'create a reference to the variable i
Dim ByRef As Integer j = i
' set j (and hence i) to a new value
j = 5
' print them out
Print "i ="; i, "j ="; j
Sleep

View file

@ -0,0 +1,2 @@
var p *int // declare p to be a pointer to an int
i = &p // assign i to be the int value pointed to by p

View file

@ -0,0 +1 @@
var m map[string]int

View file

@ -0,0 +1 @@
m := make(map[string]int)

View file

@ -0,0 +1,4 @@
b := []byte(hello world)
c := b
c[0] = 'H'
fmt.Println(string(b))

View file

@ -0,0 +1,4 @@
func three() *int {
i := 3
return &i // valid. no worry, no crash.
}

Some files were not shown because too many files have changed in this diff Show more