[list -] %INCLUDE "Along32.inc" %INCLUDE "Macros_Along.inc" [list +] ;--------------------------------------------------------------------- extern FillArray ; HLL prototype: void FillArray(byte *array, int n); ; Sets array[0] = array[1] = 0 and array[i] = 1, i = 2, 3, ..., (n-1) ; Receives: ESI = starting offset of array ; ECX = # of elements in array ; Returns: nothing ;--------------------------------------------------------------------- ;--------------------------------------------------------------------- extern EliminateMultiples ; HLL prototype: void EliminateNultiples(byte *array, int n, int k); ; Implements the following loop: ; for (i = k + k; i < n; i += k) ; array[i] = 0 ; For example, if k = 2, sets array[4], array[6], array[8], ... to 0 ; Receives: ESI = starting offset of array ; ECX = # of elements in array ; EDX = value of k ; Returns: nothing ;--------------------------------------------------------------------- ;--------------------------------------------------------------------- extern DisplayArray ; HLL prototype: void DisplayArray(byte *array, int n); ; Implements the following loop: ; for (i = 2; i < n; ++i) ; if array[i] == 1 then print i ; Receives: ESI = starting offset of array ; ECX = # of elements in array ; Returns: nothing ;--------------------------------------------------------------------- SECTION .data array times 1024 db '#' k dd 2 SECTION .bss n resd 1 SECTION .text global _start _start: call ReadDec ; read an unsigned integer mov [n],eax ; move the integer to n inc dword [n] ; increment n by 1 mov esi,array mov ecx,[n] call FillArray .L0: mov eax,[k] ; while (k * k <= n) do mul dword [k] cmp eax,[n] ja .L2 mov esi,array add esi,[k] cmp byte [esi],0 ; if (array[k] != 0) then je .L1 mov esi,array ; call EliminateMultiples mov ecx,[n] mov edx,[k] call EliminateMultiples ; end if .L1: inc dword [k] ; increment k jmp .L0 ; end while .L2: mov esi,array mov ecx,[n] call DisplayArray Exit {0}