; =================================================================== ; Program: for_loop_sum_nasm.asm ; Date: July 2020 ; Author: Jean-Michel Richer ; Email: jean-michel.richer@univ-angers.fr ; =================================================================== ; Description: ; This program computes and prints the sum of integers from ; 1 to 10 ; =================================================================== global main extern printf ; =================================================================== ; ##### ## ##### ## ; # # # # # # # ; # # # # # # # ; # # ###### # ###### ; # # # # # # # ; ##### # # # # # ; =================================================================== section .data msg_sum: db "sum=%d", 10, 0 ; =================================================================== ; #### #### ##### ###### ; # # # # # # # ; # # # # # ##### ; # # # # # # ; # # # # # # # ; #### #### ##### ###### ; =================================================================== section .text ; ------------------------------------------------------------------- ; 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 ; ; i int loop variable ecx ; sum int sum of values eax ; ; ------------------------------------------------------------------- main: push ebp mov ebp, esp ; ; Code of subprogram ; xor eax, eax ; sum = 0 mov ecx, 1 ; i = 1 .for_i: cmp ecx, 11 jge .endfor_i add eax, ecx ; sum += i inc ecx ; ++i jmp .for_i .endfor_i: push eax push dword msg_sum call printf add esp, 8 xor eax, eax ; 0 of return 0 mov esp, ebp pop ebp ret