Variable size/Set: Difference between revisions

Line 34:
* (24 bits => 16 MB of storage)
</lang>
 
=={{header|8086 Assembly}}==
Syntax will vary depending on the assembler you're using, but the following should apply to most assemblers.
 
<lang asm>
.data ;data segment
 
TestValue_00 byte 0 ;an 8-bit variable
TestValue_01 word 0 ;a 16-bit variable
TestValue_02 dword 0 ;a 32-bit variable
 
.code
 
start:
 
mov dh, byte ptr [ds:TestValue_00] ;load the value stored at the address "TestValue_00"
mov ax, word ptr [ds:TestValue_01] ;load the value stored at the address "TestValue_01"</lang>
 
There is some enforcement of variable sizes, but it's not as strict as most other languages. The two commands above would not have worked had the wrong register sizes been used. However, using the wrong labels is perfectly legal in the eyes of most assemblers.
 
<lang asm>mov al, byte ptr [ds:TestValue_02]
;even though this was listed as a dword in the data segment, the assembler lets us do this!</lang>
 
In fact, data sizes don't matter to the CPU all that much, as the following definitions are all equivalent:
<lang asm>foo byte 0,0,0,0
bar word 0,0
baz dword 0</lang>
 
The data types are more for the programmer's convenience, so that it's clear how the data should be interpreted.
 
Data can be organized in a table for better readability; however it has no effect on how the data is structured apart from visually. (The data will be organized in a linear fashion regardless, so it makes no difference to the CPU). The following two structures are the same:
<lang asm>Array1 byte 00,01,02,03
 
Array2 byte 00,01
byte 02,03</lang>
 
If you have a variable of a pre-determined size that isn't 1, 2, or 4 bytes you can use the following to define it:
<lang asm>BigNumber byte 256 dup (0) ;reserve 256 bytes, each equals zero</lang>
 
 
=={{header|Ada}}==
1,489

edits