Difference between revisions of "Absolute"

From Free Pascal wiki
(note little endian)
m (alignment)
Line 11: Line 11:
 
     anInt      : Integer;
 
     anInt      : Integer;
 
     anotherInt : Integer absolute anInt;
 
     anotherInt : Integer absolute anInt;
     firstByte  : Byte absolute anInt;
+
     firstByte  : Byte   absolute anInt;
 
   
 
   
 
begin
 
begin
Line 17: Line 17:
 
     // in the other
 
     // in the other
 
     anInt := 20;
 
     anInt := 20;
 +
 
     WriteLn(IntToStr(anInt) + '  ' + IntToStr(anotherInt)); // Outputs: 20  20
 
     WriteLn(IntToStr(anInt) + '  ' + IntToStr(anotherInt)); // Outputs: 20  20
  
 
     // a value of 20 fits in the first byte:
 
     // a value of 20 fits in the first byte:
     WriteLn('firstByte: ' + IntToStr(firstByte)); // Outputs: firstByte: 20
+
 
 +
     WriteLn('firstByte: ' + IntToStr(firstByte));           // Outputs: firstByte: 20
 
    
 
    
 
     anotherInt := 333;
 
     anotherInt := 333;
 +
 
     WriteLn(IntToStr(anInt) + '  ' + IntToStr(anotherInt)); // Outputs: 333 333
 
     WriteLn(IntToStr(anInt) + '  ' + IntToStr(anotherInt)); // Outputs: 333 333
  
 
     // 333 is too large a value to fit in one byte
 
     // 333 is too large a value to fit in one byte
 
     // little-endian x64 - least significant byte is first in memory:
 
     // little-endian x64 - least significant byte is first in memory:
     // 333 = 101001101 =  01001101 00000001 = 0x4D 0x01 = decimal: 77 1
+
     // 333 = 101001101 =  01001101 00000001 in memory = 0x4D 0x01 = decimal: 77 1
     WriteLn('firstByte: ' + IntToStr(firstByte)); // Outputs: firstByte: 77
+
 
 +
     WriteLn('firstByte: ' + IntToStr(firstByte));           // Outputs: firstByte: 77
 
end.
 
end.
 
</syntaxhighlight>
 
</syntaxhighlight>

Revision as of 04:31, 19 September 2017

Deutsch (de) English (en) español (es) suomi (fi) français (fr) русский (ru)

The absolute modifier causes a variable to be stored at the same memory location as another variable.


// Example on little endian x64 processor
Uses SysUtils;

Var
    anInt      : Integer;
    anotherInt : Integer absolute anInt;
    firstByte  : Byte    absolute anInt;
 
begin
    // with both Integer variables at the same memory location, a change to one is reflected
    // in the other
    anInt := 20;

    WriteLn(IntToStr(anInt) + '  ' + IntToStr(anotherInt)); // Outputs: 20  20

    // a value of 20 fits in the first byte:

    WriteLn('firstByte: ' + IntToStr(firstByte));           // Outputs: firstByte: 20
   
    anotherInt := 333;

    WriteLn(IntToStr(anInt) + '  ' + IntToStr(anotherInt)); // Outputs: 333 333

    // 333 is too large a value to fit in one byte
    // little-endian x64 - least significant byte is first in memory:
    // 333 = 101001101 =  01001101 00000001 in memory = 0x4D 0x01 = decimal: 77 1

    WriteLn('firstByte: ' + IntToStr(firstByte));           // Outputs: firstByte: 77
end.