:(
quick summary if you care, i wrote this over the course of the day yesterday. it's my project due today. (why did i wait till last minute?) taking 4 classes w/ fulltime work schedule sucks. was up till 3am trying to fix this and i seem to have gone in circles.
anyway here's what im trying to do in plain english:
1) Create an array of randomly ordered integers. Using the Swap procedure (this is using the xchg instruction). write a loop that exchanges each consecutive pair of integers in the array.
2) Write a procedure named findMax that find the largest number from an array of doublewords. It receives two stack parameters, the offset of the array and size of the array. Write a program to test the procedure.
3) Write a recursive impelementation of Euclid's algorithm for finding the greatest common divisor (GCD) of two integers.
Euclid's GCD algorithm:
int gcd(int m,int n)
{
if(m==n)
return m;
else if(m>n)
return gcd(m-n,n);
else
return gcd(m,n-m);
}
here's what i have written:
TITLE Assignment #1 (assignment_1.asm)
; Assignment #1
;---------------------------------------------
;1) Create random array of int's and sort.
;2) Findmax val from array of doublewords.
;3) Euclids algorithm to find gcd of two ints.
INCLUDE Irvine32.inc
GenerateArray PROTO,
ptrArray:PTR DWORD
SwapArray PROTO,
pValX:PTR DWORD, pValY:PTR DWORD
FindMax PROTO
GCDAlg PROTO,
m:DWORD, n:DWORD
.data
dArray DWORD 10 dup(?)
num1 DWORD 120
num2 DWORD 30
.code
main PROC
INVOKE GenerateArray, ADDR dArray
INVOKE Swap, ADDR dArray, ADDR [dArray+4]
;push OFFSET dArray
;push lengthof dArray
;call FindMax
;INVOKE GCDAlg, num1, num2
exit
main ENDP
GCDAlg PROC m:DWORD, n:DWORD
mov eax,n
cmp m,eax
je endGCDAlg
jg mMinusNRet
mov eax,n
sub eax,m
mov n,eax
jmp callGCDAlg
mMinusNRet:
mov eax,n
sub m,eax
mov m,eax
callGCDAlg:
INVOKE GCDAlg, m, n
endGCDAlg:
ret
GCDAlg ENDP
FindMax PROC
LOCAL vari:PTR DWORD
push ebp
mov ebp, esp
mov esi,[ebp+16]
mov ecx, [ebp+12]
mov vari, esi
L1:
mov eax, [esi]
add esi,4
cmp eax,[esi]
jle nextStep
mov vari,eax
nextStep:
loop L1
ret 2
FindMax ENDP
GenerateArray PROC dataPtr:PTR DWORD
call Randomize
mov edi, dataPtr ; address of dataPtr
mov ecx, LENGTHOF dataPtr ; loop counter
mov eax, 100
L1:
call Random32
mov [edi],eax ;move random value to array pointer
call WriteInt
add edi, TYPE dataPtr ; point to address
loop L1 ; repeat until ECX = 0
ret
GenerateArray ENDP
SwapArray PROC pValX:PTR DWORD, pValY:PTR DWORD
LOCAL flag:DWORD
mov esi, [pValX]
mov ecx,LENGTHOF dataPtr ; loop counter
swapLoop:
mov esi, pValX
mov edi, pValY
mov eax, [esi]
xchg eax,[edi]
mov [esi],eax
loop swapLoop
ret
SwapArray ENDP
END main
i am having a problem with the first generate random numbers into array logic. i seem to be generating only 4 numbers and then receiving an exception.
basically thats as far as i have gotten, i think im almost there but just my stupid logic errors.
any help is greatly appreciated.
thanks,
Ersan