; =================================================================== ; Program: for_loop_sum_nasm.asm ; Date: July 2020 ; Author: Jean-Michel Richer ; Email: jean-michel.richer@univ-angers.fr ; =================================================================== ; Description: ; This program creates an array of random values and computes ; and prints the sum of the values ; =================================================================== global main extern printf, rand, srand, time, malloc, free ; =================================================================== ; ##### ## ##### ## ; # # # # # # # ; # # # # # # # ; # # ###### # ###### ; # # # # # # # ; ##### # # # # # ; =================================================================== section .data MAX_ELEMENTS equ 8 array: dd 0 ; int *array msg_print_a_i: db "%d ", 0 msg_sum: db "sum=%d", 10, 0 new_line: db 10, 0 ; =================================================================== ; #### #### ##### ###### ; # # # # # # # ; # # # # # ##### ; # # # # # # ; # # # # # # # ; #### #### ##### ###### ; =================================================================== section .text ; ------------------------------------------------------------------- ; SUBPROGRAM ; ; void random_init(int *a, int size) ; ; DESCRIPTION ; ; initialize integer array 'a' of size 'size' with random values ; between 1 and 10 ; ; PARAMETERS ; ; a int * array of integers ; size int size of the array ; ; RETURN VALUE ; ; none ; ; VARIABLES / REGISTERS ; ; a int * array of integers [ebp +8] ebx ; size int size of the array [ebp+12] ; i int for loop variable esi ; eax int return value of rand() ; ecx int constant = 10 ; ; ------------------------------------------------------------------- random_init: push ebp mov ebp, esp push ebx push esi mov ebx, [ebp + 8] ; for (int i = 0; i < size; ++i) xor esi, esi .for_i: cmp esi, [ebp + 12] jge .endfor_i ; a[i] = (rand() % 10) + 1; call rand xor edx, edx mov ecx, 10 div ecx add edx, 1 mov [ebx + esi * 4], edx inc esi jmp .for_i .endfor_i: pop esi pop ebx mov esp, ebp pop ebp ret ; ------------------------------------------------------------------- ; SUBPROGRAM ; ; void print_values(int *a, int size) ; ; DESCRIPTION ; ; print values of array 'a' of size 'size' ; ; PARAMETERS ; ; a int * array of integers ; size int size of the array ; ; RETURN VALUE ; ; none ; ; VARIABLES / REGISTERS ; ; a int * array of integers [ebp +8] ebx ; size int size of the array [ebp+12] ; i int for loop variable esi ; ; ------------------------------------------------------------------- print_values: push ebp mov ebp, esp push ebx push esi mov ebx, [ebp + 8] ; for (int i = 0; i < size; ++i) xor esi, esi .for_i: cmp esi, [ebp + 12] jge .endfor_i ; printf("%d ", a[i]); push dword [ebx + esi * 4] push dword msg_print_a_i call printf add esp, 8 inc esi jmp .for_i .endfor_i: ; printf("\n"); push dword new_line call printf add esp, 4 pop esi pop ebx mov esp, ebp pop ebp ret ; ------------------------------------------------------------------- ; SUBPROGRAM ; ; int array_sum(int *a, int size) ; ; DESCRIPTION ; ; compute sum of value of the array 'a' of size 'size' ; ; PARAMETERS ; ; a int * array of integers [ebp+8] ebx ; size int size of the array [ebp+12] esi ; ; RETURN VALUE ; ; the sum of a[0] + ... + a[size-1] ; ; VARIABLES / REGISTERS ; ; a int * array of integers [ebp +8] ebx ; size int size of the array [ebp+12] ; i int for loop variable esi ; sum int sum of a[i]'s eax ; ; ------------------------------------------------------------------- array_sum: push ebp mov ebp, esp push ebx push esi mov ebx, [ebp + 8] ; sum = 0 xor eax, eax ; for (int i = 0; i < size; ++i) xor esi, esi .for_i: cmp esi, [ebp + 12] jge .endfor_i ; sum += a[i] add eax, [ebx + esi * 4] inc esi jmp .for_i .endfor_i: pop esi pop ebx mov esp, ebp pop ebp ret ; ------------------------------------------------------------------- ; SUBPROGRAM ; ; int main(int argc, char *argv[]) ; ; DESCRIPTION ; ; Main subprogram that computes sum of 1 to 10 and prints result ; ; PARAMETERS ; ; argc int number of arguments of the program ; argv char *[] array of arguments as strings ; ; RETURN VALUE ; ; 0 if everything is ok, ; >0 if error ; ; VARIABLES / REGISTERS ; ; argc int number of commandl line [ebp + 8] ; arguments ; argv char *[] command line arguments [ebp + 12] ; ; ------------------------------------------------------------------- main: push ebp mov ebp, esp ; srand( time(nullptr) ); xor eax, eax push eax call time add esp, 4 push eax call srand add esp, 4 ; array = new int [ MAX_ELEMENTS ]; ; array = (int *) malloc( MAX_ELEMENTS * sizeof(int) ); mov eax, MAX_ELEMENTS shl eax, 2 push eax call malloc add esp, 4 mov [array], eax ; random_init( array, MAX_ELEMENTS ); push dword MAX_ELEMENTS push dword [array] call random_init add esp, 8 ; print_values( array, MAX_ELEMENTS ); push dword MAX_ELEMENTS push dword [array] call print_values add esp, 8 ; printf( "sum=%d\n", array_sum( array, MAX_ELEMENTS ) ); push dword MAX_ELEMENTS push dword [array] call array_sum add esp, 8 push eax push dword msg_sum call printf add esp, 8 ; delete [] array push dword [array] call free add esp, 4 ; return 0 xor eax, eax ; 0 of return 0 mov esp, ebp pop ebp ret