ORANGE pico の配列のデータがメモリ上でどのように格納されているかを観察してみた。
これまでにわかっていること
以前の記事などで、以下のことがわかっている。
- 変数名の20バイト前に、変数のデータが4バイトリトルエンディアンで格納されている。
- 配列では同じ変数名が4個使われ、データのアドレスや要素数の情報が格納されている。
- 配列の1要素には16ビット符号付き整数しか格納できない。
- 3次元配列までは使えるが、4次元配列を作ろうとすると Syntax error になる。
プログラム
まず、観察用に1次元、2次元、3次元の配列をそれぞれ用意する。
要素数をそれぞれ変え、どの要素かがわかりやすいデータを格納する。
次に、もう1個配列を用意し、負の添字を使用して適当な量のダンプを行う。
今回は配列の観察のみが目的であるため、RAMの最初からダンプしなくてよい。
さらに、最初から CyberChef の To Hexdump の形式でUARTに出力するようにした。(改行コードはLF)
10 dim onedarray(7)
20 dim twodarray(6,5)
30 dim threedarray(4,3,2)
40 for i=0 to 7
50 onedarray(i) = &H1000 + i
60 next
70 for i=0 to 6
80 for j=0 to 5
90 twodarray(i,j) = &H2000 + &H10 * i + j
100 next
110 next
120 for i=0 to 4
130 for j=0 to 3
140 for k=0 to 2
150 threedarray(i,j,k) = &H3000 + &H100 * i + &H10 * j + k
160 next
170 next
180 next
190 size=2048:offset=0:dim checker(0)
200 for i=0 to size-1 step 16
210 uartput 1,format$("%08x ", i)
220 for j=0 to 7
230 value=checker(-size/2+i/2+j-offset/2)
240 uartput 1,format$("%02x %02x ",value & &HFF, (value>>8) & &HFF)
250 next
260 uartput 1," |"
270 for j=0 to 15
280 value=(checker(-size/2+i/2+j/2-offset/2)>>((j%2)*8)) & &HFF
290 if &H20<=value && value<&H7F then uartput 1,chr$(value) else uartput 1,"."
300 next
310 uartput 1,"|"+chr$(&H0A)
320 next
実行結果
以下のダンプ結果が得られた。
ダンプ結果全体
00000000 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 |................|
00000010 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 |................|
00000020 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 |................|
00000030 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 |................|
00000040 00 00 00 00 00 00 00 00 18 d6 00 a0 01 00 00 00 |................|
00000050 50 00 00 00 06 00 00 00 20 d6 00 a0 00 00 00 00 |P....... .......|
00000060 60 d7 00 a0 01 00 00 00 00 00 00 00 00 00 00 00 |`...............|
00000070 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 |................|
00000080 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 |................|
00000090 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 |................|
000000a0 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 |................|
000000b0 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 |................|
000000c0 00 00 00 00 00 00 00 00 00 00 00 00 d0 dc 00 a0 |................|
000000d0 08 dd 00 a0 40 dd 00 a0 90 dd 00 a0 00 00 00 00 |....@...........|
000000e0 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 |................|
000000f0 00 00 00 00 00 00 00 00 00 00 00 00 68 dd 00 a0 |............h...|
00000100 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 |................|
00000110 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 |................|
00000120 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 |................|
00000130 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 |................|
00000140 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 |................|
00000150 00 00 00 00 00 00 00 00 00 00 00 00 88 de 00 a0 |................|
00000160 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 |................|
00000170 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 |................|
00000180 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 |................|
00000190 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 |................|
000001a0 00 00 00 00 00 00 00 00 78 d7 00 a0 01 00 00 00 |........x.......|
000001b0 14 00 00 00 00 00 00 00 80 d7 00 a0 00 00 00 00 |................|
000001c0 d0 d7 00 a0 01 00 00 00 00 00 00 00 00 00 00 00 |................|
000001d0 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 |................|
000001e0 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 |................|
000001f0 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 |................|
00000200 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 |................|
00000210 00 00 00 00 00 00 00 00 e8 d7 00 a0 01 00 00 00 |................|
00000220 14 00 00 00 04 00 00 00 f0 d7 00 a0 00 00 00 00 |................|
00000230 40 d8 00 a0 01 00 00 00 00 00 00 00 d0 da 00 a0 |@...............|
00000240 c8 dd 00 a0 00 00 00 00 00 00 00 00 00 00 00 00 |................|
00000250 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 |................|
00000260 00 00 00 00 00 00 00 00 00 00 00 00 b0 d9 00 a0 |................|
00000270 00 00 00 00 00 00 00 00 00 00 00 00 10 dc 00 a0 |................|
00000280 00 00 00 00 00 00 00 00 58 d8 00 a0 01 00 00 00 |........X.......|
00000290 14 00 00 00 04 00 00 00 60 d8 00 a0 00 00 00 00 |........`.......|
000002a0 b0 d8 00 a0 01 00 00 00 00 00 00 00 00 db 00 a0 |................|
000002b0 f0 dd 00 a0 00 00 00 00 00 00 00 00 00 00 00 00 |................|
000002c0 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 |................|
000002d0 00 00 00 00 00 00 00 00 00 00 00 00 e0 d9 00 a0 |................|
000002e0 00 00 00 00 00 00 00 00 00 00 00 00 40 dc 00 a0 |............@...|
000002f0 00 00 00 00 00 00 00 00 c8 d8 00 a0 01 00 00 00 |................|
00000300 14 00 00 00 04 00 00 00 d0 d8 00 a0 00 00 00 00 |................|
00000310 20 d9 00 a0 01 00 00 00 00 00 00 00 30 db 00 a0 | ...........0...|
00000320 18 de 00 a0 00 00 00 00 00 00 00 00 00 00 00 00 |................|
00000330 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 |................|
00000340 00 00 00 00 00 00 00 00 00 00 00 00 10 da 00 a0 |................|
00000350 00 00 00 00 00 00 00 00 00 00 00 00 70 dc 00 a0 |............p...|
00000360 00 00 00 00 00 00 00 00 38 d9 00 a0 01 00 00 00 |........8.......|
00000370 14 00 00 00 04 00 00 00 40 d9 00 a0 00 00 00 00 |........@.......|
00000380 90 d9 00 a0 01 00 00 00 00 00 00 00 60 db 00 a0 |............`...|
00000390 40 de 00 a0 00 00 00 00 00 00 00 00 00 00 00 00 |@...............|
000003a0 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 |................|
000003b0 00 00 00 00 00 00 00 00 00 00 00 00 40 da 00 a0 |............@...|
000003c0 00 00 00 00 00 00 00 00 00 00 00 00 a0 dc 00 a0 |................|
000003d0 00 00 00 00 00 00 00 00 a8 d9 00 a0 01 00 00 00 |................|
000003e0 00 10 01 10 02 10 03 10 04 10 05 10 06 10 07 10 |................|
000003f0 c0 d9 00 a0 01 00 00 00 c8 d9 00 a0 98 d9 00 a0 |................|
00000400 00 00 00 00 00 00 00 00 d8 d9 00 a0 01 00 00 00 |................|
00000410 6f 6e 65 64 61 72 72 61 79 00 00 a0 00 00 00 00 |onedarray.......|
00000420 f0 d9 00 a0 01 00 00 00 f8 d9 00 a0 08 00 00 00 |................|
00000430 00 00 00 00 00 00 00 00 08 da 00 a0 01 00 00 00 |................|
00000440 6f 6e 65 64 61 72 72 61 79 00 00 00 00 00 00 00 |onedarray.......|
00000450 20 da 00 a0 01 00 00 00 28 da 00 a0 00 00 00 00 | .......(.......|
00000460 00 00 00 00 00 00 00 00 38 da 00 a0 01 00 00 00 |........8.......|
00000470 6f 6e 65 64 61 72 72 61 79 00 00 00 00 00 00 00 |onedarray.......|
00000480 50 da 00 a0 01 00 00 00 58 da 00 a0 00 00 00 00 |P.......X.......|
00000490 00 00 00 00 00 00 00 00 68 da 00 a0 01 00 00 00 |........h.......|
000004a0 6f 6e 65 64 61 72 72 61 79 00 00 00 00 00 00 00 |onedarray.......|
000004b0 c8 da 00 a0 01 00 00 00 00 20 10 20 20 20 30 20 |......... . 0 |
000004c0 40 20 50 20 60 20 01 20 11 20 21 20 31 20 41 20 |@ P ` . . ! 1 A |
000004d0 51 20 61 20 02 20 12 20 22 20 32 20 42 20 52 20 |Q a . . " 2 B R |
000004e0 62 20 03 20 13 20 23 20 33 20 43 20 53 20 63 20 |b . . # 3 C S c |
000004f0 04 20 14 20 24 20 34 20 44 20 54 20 64 20 05 20 |. . $ 4 D T d . |
00000500 15 20 25 20 35 20 45 20 55 20 65 20 00 00 00 00 |. % 5 E U e ....|
00000510 e0 da 00 a0 01 00 00 00 e8 da 00 a0 70 da 00 a0 |............p...|
00000520 00 00 00 00 00 00 00 00 f8 da 00 a0 01 00 00 00 |................|
00000530 74 77 6f 64 61 72 72 61 79 00 00 00 00 00 00 00 |twodarray.......|
00000540 10 db 00 a0 01 00 00 00 18 db 00 a0 07 00 00 00 |................|
00000550 00 00 00 00 00 00 00 00 28 db 00 a0 01 00 00 00 |........(.......|
00000560 74 77 6f 64 61 72 72 61 79 00 00 00 00 00 00 00 |twodarray.......|
00000570 40 db 00 a0 01 00 00 00 48 db 00 a0 06 00 00 00 |@.......H.......|
00000580 00 00 00 00 00 00 00 00 58 db 00 a0 01 00 00 00 |........X.......|
00000590 74 77 6f 64 61 72 72 61 79 00 00 a0 00 00 00 00 |twodarray.......|
000005a0 70 db 00 a0 01 00 00 00 78 db 00 a0 00 00 00 00 |p.......x.......|
000005b0 00 00 00 00 00 00 00 00 88 db 00 a0 01 00 00 00 |................|
000005c0 74 77 6f 64 61 72 72 61 79 00 00 00 00 00 00 00 |twodarray.......|
000005d0 08 dc 00 a0 01 00 00 00 00 30 00 31 00 32 00 33 |.........0.1.2.3|
000005e0 00 34 10 30 10 31 10 32 10 33 10 34 20 30 20 31 |.4.0.1.2.3.4 0 1|
000005f0 20 32 20 33 20 34 30 30 30 31 30 32 30 33 30 34 | 2 3 40001020304|
00000600 01 30 01 31 01 32 01 33 01 34 11 30 11 31 11 32 |.0.1.2.3.4.0.1.2|
00000610 11 33 11 34 21 30 21 31 21 32 21 33 21 34 31 30 |.3.4!0!1!2!3!410|
00000620 31 31 31 32 31 33 31 34 02 30 02 31 02 32 02 33 |11121314.0.1.2.3|
00000630 02 34 12 30 12 31 12 32 12 33 12 34 22 30 22 31 |.4.0.1.2.3.4"0"1|
00000640 22 32 22 33 22 34 32 30 32 31 32 32 32 33 32 34 |"2"3"42021222324|
00000650 20 dc 00 a0 01 00 00 00 28 dc 00 a0 90 db 00 a0 | .......(.......|
00000660 00 00 00 00 00 00 00 00 38 dc 00 a0 01 00 00 00 |........8.......|
00000670 74 68 72 65 65 64 61 72 72 61 79 00 00 00 00 00 |threedarray.....|
00000680 50 dc 00 a0 01 00 00 00 58 dc 00 a0 05 00 00 00 |P.......X.......|
00000690 00 00 00 00 00 00 00 00 68 dc 00 a0 01 00 00 00 |........h.......|
000006a0 74 68 72 65 65 64 61 72 72 61 79 00 00 00 00 00 |threedarray.....|
000006b0 80 dc 00 a0 01 00 00 00 88 dc 00 a0 04 00 00 00 |................|
000006c0 00 00 00 00 00 00 00 00 98 dc 00 a0 01 00 00 00 |................|
000006d0 74 68 72 65 65 64 61 72 72 61 79 00 00 00 00 00 |threedarray.....|
000006e0 b0 dc 00 a0 01 00 00 00 b8 dc 00 a0 03 00 00 00 |................|
000006f0 00 00 00 00 00 00 00 00 c8 dc 00 a0 01 00 00 00 |................|
00000700 74 68 72 65 65 64 61 72 72 61 79 00 00 00 00 00 |threedarray.....|
00000710 e0 dc 00 a0 01 00 00 00 e8 dc 00 a0 10 07 00 00 |................|
00000720 00 00 00 00 00 00 00 00 f0 dc 00 a0 01 00 00 00 |................|
00000730 69 00 00 00 00 00 00 00 00 dd 00 a0 00 00 00 00 |i...............|
00000740 69 00 00 00 00 00 00 00 18 dd 00 a0 01 00 00 00 |i...............|
00000750 20 dd 00 a0 02 00 00 00 00 00 00 00 00 00 00 00 | ...............|
00000760 28 dd 00 a0 01 00 00 00 6a 00 00 00 00 00 00 00 |(.......j.......|
00000770 38 dd 00 a0 00 00 00 00 6b 00 00 00 00 00 00 00 |8.......k.......|
00000780 50 dd 00 a0 01 00 00 00 58 dd 00 a0 03 00 00 00 |P.......X.......|
00000790 00 00 00 00 00 00 00 00 60 dd 00 a0 01 00 00 00 |........`.......|
000007a0 6b 00 00 00 00 00 00 00 78 dd 00 a0 01 00 00 00 |k.......x.......|
000007b0 80 dd 00 a0 00 08 00 00 00 00 00 00 00 00 00 00 |................|
000007c0 88 dd 00 a0 01 00 00 00 73 69 7a 65 00 00 00 00 |........size....|
000007d0 a0 dd 00 a0 01 00 00 00 a8 dd 00 a0 00 00 00 00 |................|
000007e0 00 00 00 00 00 00 00 00 b0 dd 00 a0 01 dd 00 a0 |................|
000007f0 6f 66 66 73 65 74 00 00 c0 dd 00 a0 01 00 00 00 |offset..........|
配列の観察
1次元配列
000003e0 00 10 01 10 02 10 03 10 04 10 05 10 06 10 07 10 |................|
000003f0 c0 d9 00 a0 01 00 00 00 c8 d9 00 a0 98 d9 00 a0 |................|
00000400 00 00 00 00 00 00 00 00 d8 d9 00 a0 01 00 00 00 |................|
00000410 6f 6e 65 64 61 72 72 61 79 00 00 a0 00 00 00 00 |onedarray.......|
00000420 f0 d9 00 a0 01 00 00 00 f8 d9 00 a0 08 00 00 00 |................|
00000430 00 00 00 00 00 00 00 00 08 da 00 a0 01 00 00 00 |................|
00000440 6f 6e 65 64 61 72 72 61 79 00 00 00 00 00 00 00 |onedarray.......|
00000450 20 da 00 a0 01 00 00 00 28 da 00 a0 00 00 00 00 | .......(.......|
00000460 00 00 00 00 00 00 00 00 38 da 00 a0 01 00 00 00 |........8.......|
00000470 6f 6e 65 64 61 72 72 61 79 00 00 00 00 00 00 00 |onedarray.......|
00000480 50 da 00 a0 01 00 00 00 58 da 00 a0 00 00 00 00 |P.......X.......|
00000490 00 00 00 00 00 00 00 00 68 da 00 a0 01 00 00 00 |........h.......|
000004a0 6f 6e 65 64 61 72 72 61 79 00 00 00 00 00 00 00 |onedarray.......|
000004b0 c8 da 00 a0 01 00 00 00 00 20 10 20 20 20 30 20 |......... . 0 |
4個の変数名に対応するデータとして、順にデータのアドレス 0xa000d998
、要素数 8
、ゼロ、ゼロが格納されている。
配列のデータは、onedarray(0)
から onedarray(7)
が順に格納されている。
2次元配列
000004b0 c8 da 00 a0 01 00 00 00 00 20 10 20 20 20 30 20 |......... . 0 |
000004c0 40 20 50 20 60 20 01 20 11 20 21 20 31 20 41 20 |@ P ` . . ! 1 A |
000004d0 51 20 61 20 02 20 12 20 22 20 32 20 42 20 52 20 |Q a . . " 2 B R |
000004e0 62 20 03 20 13 20 23 20 33 20 43 20 53 20 63 20 |b . . # 3 C S c |
000004f0 04 20 14 20 24 20 34 20 44 20 54 20 64 20 05 20 |. . $ 4 D T d . |
00000500 15 20 25 20 35 20 45 20 55 20 65 20 00 00 00 00 |. % 5 E U e ....|
00000510 e0 da 00 a0 01 00 00 00 e8 da 00 a0 70 da 00 a0 |............p...|
00000520 00 00 00 00 00 00 00 00 f8 da 00 a0 01 00 00 00 |................|
00000530 74 77 6f 64 61 72 72 61 79 00 00 00 00 00 00 00 |twodarray.......|
00000540 10 db 00 a0 01 00 00 00 18 db 00 a0 07 00 00 00 |................|
00000550 00 00 00 00 00 00 00 00 28 db 00 a0 01 00 00 00 |........(.......|
00000560 74 77 6f 64 61 72 72 61 79 00 00 00 00 00 00 00 |twodarray.......|
00000570 40 db 00 a0 01 00 00 00 48 db 00 a0 06 00 00 00 |@.......H.......|
00000580 00 00 00 00 00 00 00 00 58 db 00 a0 01 00 00 00 |........X.......|
00000590 74 77 6f 64 61 72 72 61 79 00 00 a0 00 00 00 00 |twodarray.......|
000005a0 70 db 00 a0 01 00 00 00 78 db 00 a0 00 00 00 00 |p.......x.......|
000005b0 00 00 00 00 00 00 00 00 88 db 00 a0 01 00 00 00 |................|
000005c0 74 77 6f 64 61 72 72 61 79 00 00 00 00 00 00 00 |twodarray.......|
000005d0 08 dc 00 a0 01 00 00 00 00 30 00 31 00 32 00 33 |.........0.1.2.3|
4個の変数名に対応するデータとして、順にデータのアドレス 0xa000da70
、最初の要素数 7
、2番目の要素数 6
、ゼロが格納されている。
配列のデータは、まず twodarray(0,0)
から twodarray(6,0)
が順に格納されている。
続いて twodarray(0,1)
から twodarray(6,1)
が順に格納されている。
その後も、同様に twodarray(i,5)
までが順に格納されている。
3次元配列
000005d0 08 dc 00 a0 01 00 00 00 00 30 00 31 00 32 00 33 |.........0.1.2.3|
000005e0 00 34 10 30 10 31 10 32 10 33 10 34 20 30 20 31 |.4.0.1.2.3.4 0 1|
000005f0 20 32 20 33 20 34 30 30 30 31 30 32 30 33 30 34 | 2 3 40001020304|
00000600 01 30 01 31 01 32 01 33 01 34 11 30 11 31 11 32 |.0.1.2.3.4.0.1.2|
00000610 11 33 11 34 21 30 21 31 21 32 21 33 21 34 31 30 |.3.4!0!1!2!3!410|
00000620 31 31 31 32 31 33 31 34 02 30 02 31 02 32 02 33 |11121314.0.1.2.3|
00000630 02 34 12 30 12 31 12 32 12 33 12 34 22 30 22 31 |.4.0.1.2.3.4"0"1|
00000640 22 32 22 33 22 34 32 30 32 31 32 32 32 33 32 34 |"2"3"42021222324|
00000650 20 dc 00 a0 01 00 00 00 28 dc 00 a0 90 db 00 a0 | .......(.......|
00000660 00 00 00 00 00 00 00 00 38 dc 00 a0 01 00 00 00 |........8.......|
00000670 74 68 72 65 65 64 61 72 72 61 79 00 00 00 00 00 |threedarray.....|
00000680 50 dc 00 a0 01 00 00 00 58 dc 00 a0 05 00 00 00 |P.......X.......|
00000690 00 00 00 00 00 00 00 00 68 dc 00 a0 01 00 00 00 |........h.......|
000006a0 74 68 72 65 65 64 61 72 72 61 79 00 00 00 00 00 |threedarray.....|
000006b0 80 dc 00 a0 01 00 00 00 88 dc 00 a0 04 00 00 00 |................|
000006c0 00 00 00 00 00 00 00 00 98 dc 00 a0 01 00 00 00 |................|
000006d0 74 68 72 65 65 64 61 72 72 61 79 00 00 00 00 00 |threedarray.....|
000006e0 b0 dc 00 a0 01 00 00 00 b8 dc 00 a0 03 00 00 00 |................|
000006f0 00 00 00 00 00 00 00 00 c8 dc 00 a0 01 00 00 00 |................|
00000700 74 68 72 65 65 64 61 72 72 61 79 00 00 00 00 00 |threedarray.....|
00000710 e0 dc 00 a0 01 00 00 00 e8 dc 00 a0 10 07 00 00 |................|
4個の変数名に対応するデータとして、順にデータのアドレス 0xa000db90
、最初の要素数 5
、2番目の要素数 4
、3番目の要素数 3
が格納されている。
配列のデータは、まず threedarray(0,0,0)
から threedarray(4,0,0)
が順に格納されている。
続いて、threedarray(0,1,0)
から threedarray(4,1,0)
が順に格納されている。
さらに、threedarray(i,2,0)
および threedarray(i,3,0)
も同様に順に格納されている。
その後、threedarray(0,0,1)
から threedarray(4,0,1)
が順に格納され、続いて threedarray(i,1,1)
から threedarray(i,3,1)
も順に格納されている。
そして、続いて threedarray(i,j,2)
が同様に格納されている。
まとめ
配列変数の情報は、配列の変数名4個を用い、順にデータの先頭アドレス、1番目の要素数、2番目の要素数、3番目の要素数を変数のデータとして格納する。
なお、要素数はそれぞれ dim
で用いた値に1を足した値となっている。
要素数の枠は dim
で指定する順に前から順に用いられ、3次元未満の配列で使わない要素数には 0
が入る。
配列のデータは1要素につき2バイト、リトルエンディアンで、同じ配列のデータはメモリ上に連続して格納される。
添字の小さいデータがメモリ上で先に、添字の大きいデータがメモリ上で後に格納される。
多次元 (2次元、3次元) 配列の場合、dim
で後 (右) に指定する要素数は、「先 (左) に指定する要素数の配列」を要素とする配列の要素数として動作する。
すなわち、要素数 (dim
で指定した値 + 1) が左から $a$、$b$ の2次元配列では、それぞれの添字を「1」変えると、メモリ上の位置はそれぞれ $2$ バイト、$2a$ バイト変化する。
要素数が左から $a$、$b$、$c$ の3次元配列では、それぞれの添字を「1」変えると、メモリ上の位置はそれぞれ $2$ バイト、$2a$ バイト、$2ab$ バイト変化する。