// =======================================================
// Chapter 3, Example 1
// Demonstrating Static Arrays.
// =======================================================
// -------------------------------------------------------
// Name: ArrayFunction
// Description: sets array[0] to 10.
// Arguments: - p_array: the array
// -------------------------------------------------------
void ArrayFunction( int p_array[] )
{
p_array[0] = 10;
}
void main()
{
// declare an array with 10 cells.
int array1[10];
// declare x
int x;
// set the first cell to 5, then set the second
// cell to the first cell.
array1[0] = 5;
array1[1] = array1[0];
// DONT EVER DO THIS:
// this next line of code writes past the end
// of the array, potentially causing harm.
// array1[10] = 0;
// pass the array to a function.
ArrayFunction( array1 );
// set cell 5 to 42.
array1[5] = 42;
// retrieve the value of cell 5 using 3 different methods.
// x should be 42 after each operation.
x = array1[5];
x = *( array1 + 5 );
x = 5[array1];
// declare a second array and initialise it.
int array2[5] = { 1, 2, 3, 4, 5 };
// declare a third array and initilise it without a specific size
int array3[] = { 1, 2, 3, 4, 5, 6 };
// retrieve the number of cells in array3:
int size = sizeof( array3 ) / sizeof( int );
}
//debug VC++6.0
TITLE D:\program\game program\Data Structures for Game Programmers\examples\ch03\01 - Static Arrays\e03-01.cpp
.386P
include listing.inc
if @Version gt 510
.model FLAT
else
_TEXT SEGMENT PARA USE32 PUBLIC 'CODE'
_TEXT ENDS
_DATA SEGMENT DWORD USE32 PUBLIC 'DATA'
_DATA ENDS
CONST SEGMENT DWORD USE32 PUBLIC 'CONST'
CONST ENDS
_BSS SEGMENT DWORD USE32 PUBLIC 'BSS'
_BSS ENDS
$$SYMBOLS SEGMENT BYTE USE32 'DEBSYM'
$$SYMBOLS ENDS
$$TYPES SEGMENT BYTE USE32 'DEBTYP'
$$TYPES ENDS
_TLS SEGMENT DWORD USE32 PUBLIC 'TLS'
_TLS ENDS
; COMDAT ?ArrayFunction@@YAXQAH@Z
_TEXT SEGMENT PARA USE32 PUBLIC 'CODE'
_TEXT ENDS
; COMDAT _main
_TEXT SEGMENT PARA USE32 PUBLIC 'CODE'
_TEXT ENDS
FLAT GROUP _DATA, CONST, _BSS
ASSUME CS: FLAT, DS: FLAT, SS: FLAT
endif
PUBLIC ?ArrayFunction@@YAXQAH@Z ; ArrayFunction
; COMDAT ?ArrayFunction@@YAXQAH@Z
_TEXT SEGMENT
_p_array$ = 8
?ArrayFunction@@YAXQAH@Z PROC NEAR ; ArrayFunction, COMDAT
; 13 : {
00000 55 push ebp
00001 8b ec mov ebp, esp
00003 83 ec 40 sub esp, 64 ; 00000040H
00006 53 push ebx
00007 56 push esi
00008 57 push edi
00009 8d 7d c0 lea edi, DWORD PTR [ebp-64]
0000c b9 10 00 00 00 mov ecx, 16 ; 00000010H
00011 b8 cc cc cc cc mov eax, -858993460 ; ccccccccH
00016 f3 ab rep stosd
; 14 : p_array[0] = 10;
00018 8b 45 08 mov eax, DWORD PTR _p_array$[ebp]
0001b c7 00 0a 00 00
00 mov DWORD PTR [eax], 10 ; 0000000aH
; 15 : }
00021 5f pop edi
00022 5e pop esi
00023 5b pop ebx
00024 8b e5 mov esp, ebp
00026 5d pop ebp
00027 c3 ret 0
?ArrayFunction@@YAXQAH@Z ENDP ; ArrayFunction
_TEXT ENDS
PUBLIC _main
EXTRN __chkesp:NEAR
; COMDAT _main
_TEXT SEGMENT
_array1$ = -40
_x$ = -44
_array2$ = -64
_array3$ = -88
_size$ = -92
_main PROC NEAR ; COMDAT
; 20 : {
00000 55 push ebp
00001 8b ec mov ebp, esp
00003 81 ec 9c 00 00
00 sub esp, 156 ; 0000009cH
00009 53 push ebx
0000a 56 push esi
0000b 57 push edi
0000c 8d bd 64 ff ff
ff lea edi, DWORD PTR [ebp-156]
00012 b9 27 00 00 00 mov ecx, 39 ; 00000027H
00017 b8 cc cc cc cc mov eax, -858993460 ; ccccccccH
0001c f3 ab rep stosd
; 21 : // declare an array with 10 cells.
; 22 : int array1[10];
; 23 :
; 24 : // declare x
; 25 : int x;
; 26 :
; 27 : // set the first cell to 5, then set the second
; 28 : // cell to the first cell.
; 29 : array1[0] = 5;
0001e c7 45 d8 05 00
00 00 mov DWORD PTR _array1$[ebp], 5
; 30 : array1[1] = array1[0];
00025 8b 45 d8 mov eax, DWORD PTR _array1$[ebp]
00028 89 45 dc mov DWORD PTR _array1$[ebp+4], eax
; 31 :
; 32 : // DONT EVER DO THIS:
; 33 : // this next line of code writes past the end
; 34 : // of the array, potentially causing harm.
; 35 : // array1[10] = 0;
; 36 :
; 37 :
; 38 : // pass the array to a function.
; 39 : ArrayFunction( array1 );
0002b 8d 4d d8 lea ecx, DWORD PTR _array1$[ebp]
0002e 51 push ecx
0002f e8 00 00 00 00 call ?ArrayFunction@@YAXQAH@Z ; ArrayFunction
00034 83 c4 04 add esp, 4
; 40 :
; 41 : // set cell 5 to 42.
; 42 : array1[5] = 42;
00037 c7 45 ec 2a 00
00 00 mov DWORD PTR _array1$[ebp+20], 42 ; 0000002aH
; 43 :
; 44 : // retrieve the value of cell 5 using 3 different methods.
; 45 : // x should be 42 after each operation.
; 46 : x = array1[5];
0003e 8b 55 ec mov edx, DWORD PTR _array1$[ebp+20]
00041 89 55 d4 mov DWORD PTR _x$[ebp], edx
; 47 : x = *( array1 + 5 );
00044 8b 45 ec mov eax, DWORD PTR _array1$[ebp+20]
00047 89 45 d4 mov DWORD PTR _x$[ebp], eax
; 48 : x = 5[array1];
0004a 8b 4d ec mov ecx, DWORD PTR _array1$[ebp+20]
0004d 89 4d d4 mov DWORD PTR _x$[ebp], ecx
; 49 :
; 50 :
; 51 :
; 52 : // declare a second array and initialise it.
; 53 : int array2[5] = { 1, 2, 3, 4, 5 };
00050 c7 45 c0 01 00
00 00 mov DWORD PTR _array2$[ebp], 1
00057 c7 45 c4 02 00
00 00 mov DWORD PTR _array2$[ebp+4], 2
0005e c7 45 c8 03 00
00 00 mov DWORD PTR _array2$[ebp+8], 3
00065 c7 45 cc 04 00
00 00 mov DWORD PTR _array2$[ebp+12], 4
0006c c7 45 d0 05 00
00 00 mov DWORD PTR _array2$[ebp+16], 5
; 54 :
; 55 : // declare a third array and initilise it without a specific size
; 56 : int array3[] = { 1, 2, 3, 4, 5, 6 };
00073 c7 45 a8 01 00
00 00 mov DWORD PTR _array3$[ebp], 1
0007a c7 45 ac 02 00
00 00 mov DWORD PTR _array3$[ebp+4], 2
00081 c7 45 b0 03 00
00 00 mov DWORD PTR _array3$[ebp+8], 3
00088 c7 45 b4 04 00
00 00 mov DWORD PTR _array3$[ebp+12], 4
0008f c7 45 b8 05 00
00 00 mov DWORD PTR _array3$[ebp+16], 5
00096 c7 45 bc 06 00
00 00 mov DWORD PTR _array3$[ebp+20], 6
; 57 :
; 58 :
; 59 : // retrieve the number of cells in array3:
; 60 : int size = sizeof( array3 ) / sizeof( int );
0009d c7 45 a4 06 00
00 00 mov DWORD PTR _size$[ebp], 6
; 61 : }
000a4 5f pop edi
000a5 5e pop esi
000a6 5b pop ebx
000a7 81 c4 9c 00 00
00 add esp, 156 ; 0000009cH
000ad 3b ec cmp ebp, esp
000af e8 00 00 00 00 call __chkesp
000b4 8b e5 mov esp, ebp
000b6 5d pop ebp
000b7 c3 ret 0
_main ENDP
_TEXT ENDS
END
//debug ollydbg
//function :ArrayFunction
00401020 > 55 push ebp
00401021 8BEC mov ebp,esp
00401023 83EC 40 sub esp,0x40
00401026 53 push ebx
00401027 56 push esi
00401028 57 push edi
00401029 8D7D C0 lea edi,dword ptr ss:[ebp-0x40]
0040102C B9 10000000 mov ecx,0x10
00401031 B8 CCCCCCCC mov eax,0xCCCCCCCC
00401036 F3:AB rep stos dword ptr es:[edi]
00401038 8B45 08 mov eax,dword ptr ss:[ebp+0x8]
0040103B C700 0A000000 mov dword ptr ds:[eax],0xA
00401041 5F pop edi
00401042 5E pop esi
00401043 5B pop ebx
00401044 8BE5 mov esp,ebp
00401046 5D pop ebp
00401047 C3 retn
//function: main
00401060 > 55 push ebp
00401061 8BEC mov ebp,esp
00401063 81EC 9C000000 sub esp,0x9C
00401069 53 push ebx
0040106A 56 push esi
0040106B 57 push edi
0040106C 8DBD 64FFFFFF lea edi,dword ptr ss:[ebp-0x9C]
00401072 B9 27000000 mov ecx,0x27
00401077 B8 CCCCCCCC mov eax,0xCCCCCCCC
0040107C F3:AB rep stos dword ptr es:[edi]
0040107E C745 D8 0500000>mov dword ptr ss:[ebp-0x28],0x5
00401085 8B45 D8 mov eax,dword ptr ss:[ebp-0x28]
00401088 8945 DC mov dword ptr ss:[ebp-0x24],eax
0040108B 8D4D D8 lea ecx,dword ptr ss:[ebp-0x28]
0040108E 51 push ecx
0040108F E8 76FFFFFF call e03-01.0040100A
00401094 83C4 04 add esp,0x4
00401097 C745 EC 2A00000>mov dword ptr ss:[ebp-0x14],0x2A
0040109E 8B55 EC mov edx,dword ptr ss:[ebp-0x14]
004010A1 8955 D4 mov dword ptr ss:[ebp-0x2C],edx
004010A4 8B45 EC mov eax,dword ptr ss:[ebp-0x14]
004010A7 8945 D4 mov dword ptr ss:[ebp-0x2C],eax
004010AA 8B4D EC mov ecx,dword ptr ss:[ebp-0x14]
004010AD 894D D4 mov dword ptr ss:[ebp-0x2C],ecx
004010B0 C745 C0 0100000>mov dword ptr ss:[ebp-0x40],0x1
004010B7 C745 C4 0200000>mov dword ptr ss:[ebp-0x3C],0x2
004010BE C745 C8 0300000>mov dword ptr ss:[ebp-0x38],0x3
004010C5 C745 CC 0400000>mov dword ptr ss:[ebp-0x34],0x4
004010CC C745 D0 0500000>mov dword ptr ss:[ebp-0x30],0x5
004010D3 C745 A8 0100000>mov dword ptr ss:[ebp-0x58],0x1
004010DA C745 AC 0200000>mov dword ptr ss:[ebp-0x54],0x2
004010E1 C745 B0 0300000>mov dword ptr ss:[ebp-0x50],0x3
004010E8 C745 B4 0400000>mov dword ptr ss:[ebp-0x4C],0x4
004010EF C745 B8 0500000>mov dword ptr ss:[ebp-0x48],0x5
004010F6 C745 BC 0600000>mov dword ptr ss:[ebp-0x44],0x6
004010FD C745 A4 0600000>mov dword ptr ss:[ebp-0x5C],0x6
00401104 5F pop edi
00401105 5E pop esi
00401106 5B pop ebx
00401107 81C4 9C000000 add esp,0x9C
0040110D 3BEC cmp ebp,esp
0040110F E8 3C000000 call e03-01._chkespleBufferstringsWtetAp>
00401114 8BE5 mov esp,ebp
00401116 5D pop ebp
00401117 C3 retn
//release VC++6.0
TITLE D:\program\game program\Data Structures for Game Programmers\examples\ch03\01 - Static Arrays\e03-01.cpp
.386P
include listing.inc
if @Version gt 510
.model FLAT
else
_TEXT SEGMENT PARA USE32 PUBLIC 'CODE'
_TEXT ENDS
_DATA SEGMENT DWORD USE32 PUBLIC 'DATA'
_DATA ENDS
CONST SEGMENT DWORD USE32 PUBLIC 'CONST'
CONST ENDS
_BSS SEGMENT DWORD USE32 PUBLIC 'BSS'
_BSS ENDS
_TLS SEGMENT DWORD USE32 PUBLIC 'TLS'
_TLS ENDS
; COMDAT ?ArrayFunction@@YAXQAH@Z
_TEXT SEGMENT PARA USE32 PUBLIC 'CODE'
_TEXT ENDS
; COMDAT _main
_TEXT SEGMENT PARA USE32 PUBLIC 'CODE'
_TEXT ENDS
FLAT GROUP _DATA, CONST, _BSS
ASSUME CS: FLAT, DS: FLAT, SS: FLAT
endif
PUBLIC ?ArrayFunction@@YAXQAH@Z ; ArrayFunction
; COMDAT ?ArrayFunction@@YAXQAH@Z
_TEXT SEGMENT
_p_array$ = 8
?ArrayFunction@@YAXQAH@Z PROC NEAR ; ArrayFunction, COMDAT
; 14 : p_array[0] = 10;
00000 8b 44 24 04 mov eax, DWORD PTR _p_array$[esp-4]
00004 c7 00 0a 00 00
00 mov DWORD PTR [eax], 10 ; 0000000aH
; 15 : }
0000a c3 ret 0
?ArrayFunction@@YAXQAH@Z ENDP ; ArrayFunction
_TEXT ENDS
PUBLIC _main
; COMDAT _main
_TEXT SEGMENT
_array1$ = -40
_main PROC NEAR ; COMDAT
; 20 : {
00000 83 ec 28 sub esp, 40 ; 00000028H
; 21 : // declare an array with 10 cells.
; 22 : int array1[10];
; 23 :
; 24 : // declare x
; 25 : int x;
; 26 :
; 27 : // set the first cell to 5, then set the second
; 28 : // cell to the first cell.
; 29 : array1[0] = 5;
00003 b8 05 00 00 00 mov eax, 5
00008 89 44 24 00 mov DWORD PTR _array1$[esp+40], eax
; 30 : array1[1] = array1[0];
0000c 89 44 24 04 mov DWORD PTR _array1$[esp+44], eax
; 31 :
; 32 : // DONT EVER DO THIS:
; 33 : // this next line of code writes past the end
; 34 : // of the array, potentially causing harm.
; 35 : // array1[10] = 0;
; 36 :
; 37 :
; 38 : // pass the array to a function.
; 39 : ArrayFunction( array1 );
00010 8d 44 24 00 lea eax, DWORD PTR _array1$[esp+40]
00014 50 push eax
00015 e8 00 00 00 00 call ?ArrayFunction@@YAXQAH@Z ; ArrayFunction
; 40 :
; 41 : // set cell 5 to 42.
; 42 : array1[5] = 42;
; 43 :
; 44 : // retrieve the value of cell 5 using 3 different methods.
; 45 : // x should be 42 after each operation.
; 46 : x = array1[5];
; 47 : x = *( array1 + 5 );
; 48 : x = 5[array1];
; 49 :
; 50 :
; 51 :
; 52 : // declare a second array and initialise it.
; 53 : int array2[5] = { 1, 2, 3, 4, 5 };
; 54 :
; 55 : // declare a third array and initilise it without a specific size
; 56 : int array3[] = { 1, 2, 3, 4, 5, 6 };
; 57 :
; 58 :
; 59 : // retrieve the number of cells in array3:
; 60 : int size = sizeof( array3 ) / sizeof( int );
; 61 : }
0001a 83 c4 2c add esp, 44 ; 0000002cH
0001d c3 ret 0
_main ENDP
_TEXT ENDS
END
//release ollydbg
//function : ArrayFunction
00401000 8B4424 04 mov eax,dword ptr ss:[esp+0x4]
00401004 C700 0A000000 mov dword ptr ds:[eax],0xA
0040100A C3 retn
//function :main
00401010 83EC 28 sub esp,0x28
00401013 B8 05000000 mov eax,0x5
00401018 894424 00 mov dword ptr ss:[esp],eax
0040101C 894424 04 mov dword ptr ss:[esp+0x4],eax
00401020 8D4424 00 lea eax,dword ptr ss:[esp]
00401024 50 push eax
00401025 E8 D6FFFFFF call e03-01.00401000
0040102A 83C4 2C add esp,0x2C
0040102D C3 retn