2019-02-03 - In IL: Array Instructions
In IL
- Part 1 - Introduction
- Part 2 - Variables and Types
- Part 3 - Variables in Visual Basic .NET
- Part 4 - Instructions and the Stack
- Part 5 - Volume of a Cylinder (Operations)
- Part 6 - Branching Instructions
- Part 7 - Largest of Two Numbers (if-else)
- Part 8 - Largest of Three Numbers (If-ElseIf-Else)
- Part 9 - Switch instruction
- Part 10 - Grade Analyser (switch)
- Part 11 - Prize Calculator (switch-2)
- Part 12 - VB Grade Analyser (Select)
- Part 13 - Loop Instructions
- Part 14 - Print the Alphabet (while)
- Part 15 - Print the Alphabet (do while, for)
- Part 16 - Print the Alphabet (Do Until)
- Part 17 - Print the Alphabet (break, continue)
- Part 18 - Array Instructions
- Part 19 - Summing Arrays
- Part 20 - Other Instructions
- Part 21 - Assemblies
- Part 22 - Class Definitions
- Part 23 - C# Classes and Structs
- Part 24 - VB Classes, Modules and Structures
- Part 25 - Field Definitions
- Part 26 - Field Declarations
An array is a series of elements laid out continuously in memory with each element being accessible using its index value. The two key properties of an array are its bounds and its dimensions. Bounds indicate the lowest and highest possible index values while dimensions indicate how many index values are required. For example a two-dimensional array could be used as a table with one index representing the rows and the other index representing the columns. The bounds would indicate how many rows and columns the table has. IL allows arrays with multiple dimensions and various bounds but treats one-dimensional arrays with a 0 for the lower bound as special. These arrays are referred to as vectors and there are special IL instructions for working with these types of arrays.
newarr (NEW ARRay)
Pops a integer off of the stack and creates a new array able to contain that many elements.
Instruction | Description | Binary Format |
---|---|---|
newarr <type> | Create new array | 0x8D <T> |
ldlen (LoaD LENgth)
Pops an array off of the stack and pushes the length of the array onto the stack.
Instruction | Description | Binary Format |
---|---|---|
ldlen | Length of array | 0x8E |
ldelema (LoaD ELEMent Address)
Pops an index value and an array off of the stack and pushes the address of the element of the array at the specified index onto the stack.
Instruction | Description | Binary Format |
---|---|---|
ldelema <type> | load element address of specified type | 0x8F <T> |
ldelem (LoaD ELEMent)
Pops an index value and an array off of the stack and pushes the element of the array at the specified index onto the stack.
Instruction | Description | Binary Format |
---|---|---|
ldelem.i1 | load 8 bit integer element | 0x90 |
ldelem.u1 | load 8 bit unsigned integer element | 0x91 |
ldelem.i2 | load 16 bit integer element | 0x92 |
ldelem.u2 | load 16 bit unsigned integer element | 0x93 |
ldelem.i4 | load 32 bit integer element | 0x94 |
ldelem.u4 | load 32 bit unsigned integer element | 0x95 |
ldelem.i8 | load 64 bit integer element | 0x96 |
ldelem.u8 | load 64 bit unsigned integer element | 0x96 |
ldelem.i | load native integer element | 0x97 |
ldelem.r4 | load 32 bit floating point element | 0x98 |
ldelem.r8 | load 64 bit floating point element | 0x99 |
ldelem.ref | load object element | 0x9A |
ldelem <type> | load element of specified type | 0xA3 <T> |
stelem (SeT ELEMent)
Pops a value, an index value, and an array off of the stack and sets the element of the array at the specified index to the value.
Instruction | Description | Binary Format |
---|---|---|
stelem.i | set native integer element | 0x9B |
stelem.i1 | set 8 bit integer element | 0x9C |
stelem.i2 | set 16 bit integer element | 0x9D |
stelem.i4 | set 32 bit integer element | 0x9E |
stelem.i8 | set 64 bit integer element | 0x9F |
stelem.r4 | set 32 bit floating point element | 0xA0 |
stelem.r8 | set 64 bit floating point element | 0xA1 |
stelem.ref | set object element | 0xA2 |
stelem<type> | set element of specified type | 0xA4<T> |
Next time we'll look at some programs which use arrays.
Comments: