diff --git a/src/task2_2.asm b/src/task2_2.asm new file mode 100644 index 0000000000000000000000000000000000000000..096917902a044738c7e1567c42e2fc84b6420419 --- /dev/null +++ b/src/task2_2.asm @@ -0,0 +1,48 @@ +segment .data + prompt_sum db "The sum of the integers 1 to 100 is: %d", 0 + nl db 10,0 + +segment .bss + array resd 100 ; Reserve space for 100 integers + +segment .text + extern printf + extern scnaf + +global asm_main +asm_main: + ; Initialize array: array[i] = i+1 for i=0 to 99 + mov ecx, 100 ; loop counter + mov edi, array ; edi points to start of array + xor eax, eax ; eax = 0 +init_loop: + inc eax ; eax = i+1 + mov [edi], eax + add edi, 4 ; advance by 4 bytes (size of int) + loop init_loop + + ; Sum the array + mov ecx, 100 + mov edi, array + xor eax, eax ; eax = sum +sum_loop: + add eax, [edi] + add edi, 4 + loop sum_loop + + ; Print the result + ; printf("The sum of the integers 1 to 100 is: %d", eax) + push eax ; argument: sum + push prompt_sum + call printf + add esp, 8 ; pop 2 arguments + + ; Print newline + push nl ; '\n' + call printf ; we can call putchar directly if desired (define extern putchar if needed) + add esp,4 + + ; Exit the program + mov eax, 1 ; sys_exit + xor ebx, ebx + int 0x80 \ No newline at end of file