Write a program that will generate and print all prime numbers that are less than or equal to a user-defined limit. The program will contain 4 procedures: main and 3 procedures to read the user limit, generate the prime numbers, and print the prime numbers. In addition, the program will have 2 macros to make it more convenient to print text strings and print numeric output.
The program will define an array of 1000 unsigned integers. The main procedure will call each of the other 3 procedures to do the following 3 tasks.
1. This procedure will call the macro to ask the user for an upper limit for the prime numbers. Then procedure will read in and check that the limit is not larger than 7919, the 1000th prime number. If it is larger, the procedure will call the macro to print a warning message that the limit is reset to 7919. Then the procedure will return the limit back to the main procedure.
2. This procedure will fill the array with prime numbers up to the limit. The array is passed into this procedure by address.
Algorithm to generate the prime numbers:
a. initialize the first 2 elements of the array to 2 and 3 (the first 2 prime numbers)
<!--[if !supportLists]--> b. <!--[endif]-->loop through all odd numbers less than or equal to the limit, starting with the number 5, and test each number this way:
<!--[if !supportLists]--> - Divide the test number by each array element (starting at 3) until the quotient is smaller than the divisor, or until the test number is divisible by the array element.
<!--[if !supportLists]--> - If the test number is divisible by the array element, it is not a prime number. Start the loop again with the next test number.
<!--[if !supportLists]--> - If the quotient becomes smaller than the divisor, the test number is a prime. Put it in the next available spot in the array and update the next available index. (Note that when you first start the loop, the next available index is at the third element of the array)
To see the calculation of the first several prime numbers, click here.
<!--[if !supportLists]--> 3. <!--[endif]-->This procedure will call the macro to print a header, and then print all the prime numbers in the array, 10 numbers per line. To print each prime number, the procedure will call a macro to print the number and a space. Again, the array should be passed by address to this procedure.
In addition to the 4 procedures, the program also has 2 macros.
The first macro makes printing text strings easier. If the name of the macro is ‘print’, the programmer can code: print str1 where str1 is a string which has been defined in the data segment.
The second macro prints a numeric value and a space. If the name of the macro is 'printNum', the programmer can code: printNum eax and the numeric value in eax will be printed followed by a space character.
To think about
- Don't forget to save and restore registers that are used by each macro and each procedure
- In 32-bit mode, the size of each data value on the stack is 32 bits or 4 bytes. This means that to access data in the stack, you should use multiples of 4 for the offset from EBP.
- For the array of prime numbers, you can use word or doubleword data. When you walk the array, just remember to increment by the right number for your data size.
Absolute must haves
- Use the stack to pass input arguments and return values. Except for the main procedure, all other procedures must get input data from the stack and return data through the stack. These procedures should not use any memory variables defined in the .data section, with the exception of string constants used to print to screen.1
- Implement the prime number algorithm given in this lab. Do not use a different algorithm.
- As before, document your program clearly. When it comes to parameter passing through the stack, clear documentation will save you a lot of headaches.
- Programs that do not assemble successfully will receive 1/3 credit at most.
1 Extra Credit (2 pts)
- Implement the procedures so the string constants are passed to them through the stack. This means these procedure does not directly fetch any data from the .data section
TITLE Lab 5 (lab5.asm) ; Description: Dot product calculation for 2D, 3D, or 4D vectors ; Name: Dhruv Gogna ; Date: 11/14/2007 INCLUDE Irvine32.inc DIM EQU 1000 ; DIM is dimension of vector, can be 2, 3, or 4 .stack ; set up stack segment for procedure call (more details in module 7) .data myMessage BYTE "MASM program example",0dh,0ah,0 parray dword DIM DUP(?) sum dword ? .code main PROC call Clrscr mov edx,OFFSET myMessage call WriteString sub esp, 4 call getnumber call writedec ;mov esi, OFFSET parray push OFFSET parray call fillarray ;pop OFFSET parray ;sub esi, 4 ;mov eax, parray+1 ;mov eax, parray+ mov esi, OFFSET parray mov eax, [esi] add esi, 4 add esi, 4 mov eax, [esi] call crlf call crlf call crlf ;call writedec exit main ENDP getnumber PROC push ebp mov ebp,esp ; set ebp for this proc mov edx,OFFSET myMessage call WriteString call readdec cmp eax, 7919 jng L1 mov eax, 7919 L1: mov [ebp+8], eax pop ebp ;call writedec ret ; pop return addr ; and input params getnumber ENDP fillarray PROC push ebp mov ebp,esp ; set ebp for this proc mov esi, OFFSET parray ;add esi, 4 ;mov eax, [esi] call crlf call crlf ;call writedec ;mov ebx, eax ;eax has the limit of the array push eax ;saving eax mov ebx, 2 mov [esi], ebx add esi, 4 mov ebx, 3 mov [esi], ebx pop eax cmp eax, [esi] push eax ;push eax jle rtn ;push eax mov esi, OFFSET parray mov ebx, [esi] add esi, 4 add ebx, [esi] ;mov ebx, eax push ebx ;save eax mov eax, ebx mov edx, 0 ;mov eax, ebx mov ecx, [esi] ;divisor i.e. 3 is in [esi] div ecx ;so mov it to ecx cmp eax, [esi] jle incre divagain: mov edx, 0 ;start division again mov eax, [esi] add eax, 2 push eax sub esi, 4 mov ecx, [esi] add esi, 4 div ecx cmp edx, 0 jz divagain ;sub esi, 8 cmp eax, 3 jnz incr cmp eax, 5 ;add esi, 4 ;cmp eax, [esi] jnz incr cmp eax, 7 jnz incr mov edx,OFFSET myMessage call WriteString incre: add esi, 4 ;increment the number in array ;pop eax ;mov [esi], eax pop ebx jmp inr incr: pop ebx ;jmp ir inr: mov [esi], ebx ;pop eax L1: pop ebx cmp [esi], ebx push ebx ;jle divagain jna divagain mov edx,OFFSET myMessage call WriteString rtn: pop ebp pop eax mov esi, OFFSET parray ;add esi, 4 ;add esi, 4 ;add esi, 4 add esi, 16 mov eax, [esi] call crlf call writedec ret fillarray ENDP END main