Clear Array using indexing

void ClearUsingIndex(int[], int);

static int Array[10] ={1,2,3,4,5,6,7,8,9,-1};

int main()

{

int size = 10;

ClearUsingIndex( Array, size);

// or you can use this ClearUsingIndex( &Array[0], size);

// call function that Clears static Array

}

// Clears array using indexing.

void ClearUsingIndex(int Array[], int size)

{

int i;

for (i = 0; i < size; i +=1)

Array[i] = 0;

}

Before Calling Clear Array

After Calling the function ClearArray

Clear Array Using Pointers

#define SIZE 10 /* number of integers in an Array */

void ClearUsingPointers(int *, int);

static int Array[10] ={1,2,3,4,5,6,7,8,9,-1};

int main()

{


ClearUsingPointers( &Array[0], SIZE);

// call function that Clears static Array

}

// Clears array using pointers.

void ClearUsingPointers(int *Array, int size)

{

int *p;

for (p = &Array[0]; p < &Array[size]; p = p+1)

*p= 0;

};

After two iteration s inFunction that uses pointers

After 4 Iteration in Function that uses pointers

Clear Array Function in ASM as generated

By MS Compiler from C-code (using indexing)

; Copyright (C) 2000 Microsoft Corporation. All rights reserved.

.386

.model flat, c

; Custom Build Step, including a listing file placed in intermediate directory

; but without Source Browser information

; debug:

; ml -c -Zi "-Fl$(IntDir)\$(InputName).lst" "-Fo$(IntDir)\$(InputName).obj" "$(InputPath)"

; release:

; ml -c "-Fl$(IntDir)\$(InputName).lst" "-Fo$(IntDir)\$(InputName).obj" "$(InputPath)"

; outputs:

; $(IntDir)\$(InputName).obj

; Custom Build Step, including a listing file placed in intermediate directory

; and Source Browser information also placed in intermediate directory

; debug:

; ml -c -Zi "-Fl$(IntDir)\$(InputName).lst" "-FR$(IntDir)\$(InputName).sbr" "-Fo$(IntDir)\$(InputName).obj" "$(InputPath)"

; release:

; ml -c "-Fl$(IntDir)\$(InputName).lst" "-FR$(IntDir)\$(InputName).sbr" "-Fo$(IntDir)\$(InputName).obj" "$(InputPath)"

; outputs:

; $(IntDir)\$(InputName).obj

; $(IntDir)\$(InputName).sbr

.code

; Clear array using index as compiler generated.

;

; x:SDWORD - signed double word ( 4 bytes)

_TEXT SEGMENT

_i$ = -8

_Array$ = 8

_size$ = 12

ClearUsingIndex PROC NEAR ; ClearUsingIndex, COMDAT

; Line 14

00000 55 push ebp

00001 8b ec mov ebp, esp

00003 81 ec cc 00 00

00 sub esp, 204 ; 000000ccH

00009 53 push ebx

0000a 56 push esi

0000b 57 push edi

0000c 8d bd 34 ff ff

ff lea edi, DWORD PTR [ebp-204]

00012 b9 33 00 00 00 mov ecx, 51 ; 00000033H

00017 b8 cc cc cc cc mov eax, -858993460 ; ccccccccH

0001c f3 ab rep stosd

; Line 16

0001e c7 45 f8 00 00

00 00 mov DWORD PTR _i$[ebp], 0

00025 eb 09 jmp SHORT $L281

$L282:

00027 8b 45 f8 mov eax, DWORD PTR _i$[ebp]

0002a 83 c0 01 add eax, 1

0002d 89 45 f8 mov DWORD PTR _i$[ebp], eax

$L281:

00030 8b 45 f8 mov eax, DWORD PTR _i$[ebp]

00033 3b 45 0c cmp eax, DWORD PTR _size$[ebp]

00036 7d 0f jge SHORT $L279

; Line 17

00038 8b 45 f8 mov eax, DWORD PTR _i$[ebp]

0003b 8b 4d 08 mov ecx, DWORD PTR _Array$[ebp]

0003e c7 04 81 00 00

00 00 mov DWORD PTR [ecx+eax*4], 0

00045 eb e0 jmp SHORT $L282

$L279:

; Line 18

00047 5f pop edi

00048 5e pop esi

00049 5b pop ebx

0004a 8b e5 mov esp, ebp

0004c 5d pop ebp

0004d c3 ret 0

ClearUsingIndex ENDP ; ClearUsingIndex

_TEXT ENDS

END


end

Clear Array using Pointers as

generated By MS Compiler

.386

.model flat, c

; Custom Build Step, including a listing file placed in intermediate directory

; but without Source Browser information

; debug:

; ml -c -Zi "-Fl$(IntDir)\$(InputName).lst" "-Fo$(IntDir)\$(InputName).obj" "$(InputPath)"

; release:

; ml -c "-Fl$(IntDir)\$(InputName).lst" "-Fo$(IntDir)\$(InputName).obj" "$(InputPath)"

; outputs:

; $(IntDir)\$(InputName).obj

; Custom Build Step, including a listing file placed in intermediate directory

; and Source Browser information also placed in intermediate directory

; debug:

; ml -c -Zi "-Fl$(IntDir)\$(InputName).lst" "-FR$(IntDir)\$(InputName).sbr" "-Fo$(IntDir)\$(InputName).obj" "$(InputPath)"

; release:

; ml -c "-Fl$(IntDir)\$(InputName).lst" "-FR$(IntDir)\$(InputName).sbr" "-Fo$(IntDir)\$(InputName).obj" "$(InputPath)"

; outputs:

; $(IntDir)\$(InputName).obj

; $(IntDir)\$(InputName).sbr

.code

_TEXT SEGMENT

_p$ = -8

_Array$ = 8

_size$ = 12

ClearUsingPointers PROC NEAR ; ClearUsingPointers, COMDAT

; Line 15

push ebp

mov ebp, esp

sub esp, 204 ; 000000ccH

push ebx

push esi

push edi

lea edi, DWORD PTR [ebp-204]

mov ecx, 51 ; 00000033H

mov eax, -858993460 ; ccccccccH

rep stosd

; Line 17

mov eax, DWORD PTR _Array$[ebp]

mov DWORD PTR _p$[ebp], eax

jmp SHORT $L280

$L281:

mov eax, DWORD PTR _p$[ebp]

add eax, 4

mov DWORD PTR _p$[ebp], eax

$L280:

mov eax, DWORD PTR _size$[ebp]

mov ecx, DWORD PTR _Array$[ebp]

lea edx, DWORD PTR [ecx+eax*4]

cmp DWORD PTR _p$[ebp], edx

jae SHORT $L278

; Line 18

mov eax, DWORD PTR _p$[ebp]

mov DWORD PTR [eax], 0

jmp SHORT $L281

$L278:

; Line 19

pop edi

pop esi

pop ebx

mov esp, ebp

pop ebp

ret 0

ClearUsingPointers ENDP ; ClearUsingPointers

_TEXT ENDS

END

Optimized ASM using indexes

snapshot INDX after 6 Iterations

snapshot of Optimized INDX after 3 Iterations

Optimized ASM using pointers

snapshot using POINTERS after 7 iteratios

snapshot OPTIMZED using POINTERS after 7 iterations

Review the above points for SPARC and MIPS (SPIM) processors. Your home work assignments is

Home Assignment

Tutorial on how to run a program in DEBUG mode on a SPARC processor.