Arrays |
|
Arrays are declared like normal variables. The only difference are the dimension numbers: int a[10];This array can store "10" short integers. To write into an array we use a special move opcode: move_i_a L0, a, L1;The first argument is the value to write, the second is the array and the third is the array index. To read from an array: move_a_i a, L1, L0;
array.na
1| int a[10];
2|
3| push_i 0, L0;
4| push_i 9, L1;
5|
6| lab init_a;
7| move_i_a L0, a, L0;
8| inc_l L0;
9| lseq_l L0, L1, L2;
10| jmp_l L2, init_a;
11|
12| push_i 0, L0;
13| push_i 1, L2;
14|
15| lab print_a;
16| move_a_i a, L0, L3;
17| print_l L3;
18| print_n L2;
19| inc_l L0;
20| lseq_l L0, L1, L4;
21| jmp_l L4, print_a;
22|
23| push_i 0, L0;
24| exit L0;
Opcodes
L = long register, D = double register
BV = byte variable, IV = int variable, LV = long int variable
DV = double variable
LI = array index
register to array
move_i_a L, IV, LI;
move_l_a L, LV, LI;
move_d_a D, DV, LI;
move_b_a L, BV, LI;
array to register
move_a_i IV, LI, L;
move_a_l LV, LI, L;
move_a_d DV, LI, D;
move_a_b BV, LI, L;
Free an arrayTo free the allocated memory of an array, you can use "dealloc".If a program tries to read or write to a freed array, you get a "overflow" error message.
dealloc a; deallocate array "a"
Nano deallocates all variables on programm end. But you can use this to resize an array. Resize an arrayTo resize an array, we have to deallocate it first. Then we declare it with a new size.This looks like this:
int size;
push_i 10, L0;
pull_i L0, size;
int a[size]; space for 10 int variables
...
dealloc a; deallocate array
push_i 20, L0;
pull_i L0, size;
int a[size]; space for 20 int variables
Prev: Jumps | Next: Strings
|