PDA

View Full Version : Asembly Language Help needed..Urgent..plzzzz


paritoshcool
11-27-07, 01:14 AM
Hi every body..i am not sure whats wrong with the code. The program crashes..i dont know why?Please help me out. here is the program requirement:
************************************************** ***************
Overview
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.



Details
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)

<!--[endif]-->

<!--[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)

; 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

Comment !
mov ebx, 2
mov [esi], ebx
add esi, 4
mov ebx, 3
mov [esi], ebx
sub esi, 4
add ebx, [esi]
add esi, 4
push ebx

;sub esi, 4
mov edx, 0
mov eax, ebx
mov ecx, [esi]
div ecx

call crlf
call crlf
call crlf
call writedec

cmp eax, ecx
jnge L1
mov edx,OFFSET myMessage
call WriteString


L1: add esi, 4
pop ebx
mov [esi], ebx
ret
!
Comment ^
push ebp
mov ebp,esp ; set ebp for this proc
mov esi, OFFSET parray
;call crlf
;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

mov eax, 0
mov ecx, 2
mov esi, OFFSET parray
L1: add eax, [esi]
add esi, 4
loop L1
mov edx,OFFSET myMessage
call WriteString

Comment %
mov [esi], eax
pop eax
cmp [esi], eax
jle back
%

;add esi, 4
;sub esi, 4
mov ebx, [esi]
again: add ebx, 2 ;increment the value by 2
sub esi, 4
mov edx, 0 ;division to know if next number is prime
mov eax, ebx
mov ecx, [esi]
div ecx
sub esi, 4
cmp eax, [esi]
ja back

;add esi, 4
;mov [esi], ebx
;pop eax
;cmp [esi], eax
;push eax
;jne again
back:;mov edx,OFFSET myMessage
;call WriteString
^

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
;jle rtn
sub esi, 4
mov ebx, [esi]
add esi, 4
add ebx, [esi]

;push eax
mov eax, ebx
push ebx ;save next element
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 ebx, eax ;save eax
push eax
mov eax, ebx
mov ecx, [esi]
div ecx

;cmp eax, [esi]


incre: add esi, 4 ;increment the number in array
pop ebx
mov [esi], ebx
pop eax
cmp [esi], eax
push eax
jge divagain
mov edx,OFFSET myMessage
call WriteString

rtn: pop ebp
pop ebx
;pop eax
ret
fillarray ENDP

END main

mab
11-27-07, 08:07 AM
First of all, no programming help forum is going to do or debug your assignment for you. Therefore, posting your detailed list of requirements is irrelevant, because programming help forums can only help you with specific programming questions, specific programming errors, or specific programming problems with your code.

Secondly, "The program crashes.." is not a specific statement of a question, error, or problem you need help with. If you can pin down where the problem is occurring in the code and ask a specific question like "after line wwww I am getting xxxx for a value in yyyy instead of zzzz" then someone can probably help you solve the problem with that specific section of code, but just posting all your code and stating that it crashes is not going to get you a helpful response, that is not how programming help forums work.