Create an object at a given address: Difference between revisions

From Rosetta Code
Content added Content deleted
(-Java, no memory access like this)
(Refine the task slightly, crosslink)
Line 1: Line 1:
{{task}}{{omit from|Java}}
{{task|Basic Data Operations}}
{{basic data operation}}
In systems programing it is sometimes required to place language objects at specific memory locations, like I/O registers, hardware interrupt vectors etc.
In systems programing it is sometimes required to place language objects at specific memory locations, like I/O registers, hardware interrupt vectors etc.


Line 5: Line 6:
Show how language objects can be allocated at a specific machine addresses.
Show how language objects can be allocated at a specific machine addresses.


Since most [[OS]]es prohibit access to the physical memory if it is not mapped by the application, as an example, rather than a physical address, take the address of some existing object. E.g. create an integer object. Print the machine address of the object. Take the address of the object and create another integer object at this address. Print the value of this object to verify that it is same as one of the origin. Change the value of the origin and verify it again.
Since most [[OS]]es prohibit access to the physical memory if it is not mapped by the application, as an example, rather than a physical address, take the address of some existing object (using suitable [[Address Operations|address operations]] if necessary). For example, create an integer object. Print the machine address of the object. Take the address of the object and create another integer object at this address. Print the value of this object to verify that it is same as one of the origin. Change the value of the origin and verify it again.


=={{Header|Ada}}==
=={{Header|Ada}}==
Line 36: Line 37:
456
456
</pre>
</pre>

{{omit from|Java}}

Revision as of 20:50, 8 June 2009

Task
Create an object at a given address
You are encouraged to solve this task according to the task description, using any language you may know.

Basic Data Operation
This is a basic data operation. It represents a fundamental action on a basic data type.

You may see other such operations in the Basic Data Operations category, or:

Integer Operations
Arithmetic | Comparison

Boolean Operations
Bitwise | Logical

String Operations
Concatenation | Interpolation | Comparison | Matching

Memory Operations
Pointers & references | Addresses

In systems programing it is sometimes required to place language objects at specific memory locations, like I/O registers, hardware interrupt vectors etc.

Task

Show how language objects can be allocated at a specific machine addresses.

Since most OSes prohibit access to the physical memory if it is not mapped by the application, as an example, rather than a physical address, take the address of some existing object (using suitable address operations if necessary). For example, create an integer object. Print the machine address of the object. Take the address of the object and create another integer object at this address. Print the value of this object to verify that it is same as one of the origin. Change the value of the origin and verify it again.

Ada

In Ada object address can be specified using the address representation clause RM 13.3: <lang ada> type IO_Port is mod 2**8; -- One byte Device_Port : type IO_Port; for Device_Port'Address use 16#FFFF_F000#; </lang> In the example above the address is specified constant. It is also possible to specify address dynamically as the following solution of the task does: <lang ada> with Ada.Text_IO; use Ada.Text_IO; with System.Storage_Elements; use System.Storage_Elements;

procedure Test_Address is

  X : Integer := 123;
  Y : Integer;
  for Y'Address use X'Address;

begin

  Put_Line ("At address:" & Integer_Address'Image (To_Integer (Y'Address)));
  Put_Line (Integer'Image (Y));
  X := 456;
  Put_Line (Integer'Image (Y));

end Test_Address; </lang> Sample output:

At address: 38207236
 123
 456