I have been attempting to debug my program, but it always seems to get caught up when I call my function. I get an error saying
“Unhandled exception at 0x0018fed8 in windows32.exe: 0xC0000005:
Access violation.”
I tried researching and what I gathered is that it is a stack error. Is there any other possibilities?
.586
.MODEL FLAT
INCLUDE io.h
.STACK 4096
.DATA ; reserve storage for data
number1 WORD ?
anArray WORD 100 DUP (?)
count WORD ?
search WORD ?
prompt1 BYTE "Enter a number or -1 to quit.", 0
prompt2 BYTE "Enter a number to search for", 0
prompt3 BYTE "Search for another number Y/N",0
inString BYTE 40 DUP (?)
searchString BYTE 16 DUP (?)
outMsgLabel BYTE "Search Result", 0
frontOut1 BYTE 6 DUP (?)
outMsg1 BYTE " is element"
rearOut1 BYTE 6 DUP (?),0
frontOut2 BYTE 6 DUP (?)
outMsg2 BYTE " is not in array",0
EXTERN function1:PROC
.CODE ; start of main program code
_MainProc PROC
lea ebx, anArray
mov cx, count
moveThrough: input prompt1, inString, 40 ; read ASCII characters
atow inString ; convert to integer
cmp ax,0 ; check for -1
jl next
mov [ebx], eax ; store in memory
add ebx,2 ; move to next location in array
inc ecx
cmp ecx,50 ; check to make sure array isn't over 50
je next
jmp moveThrough ; jump to add more numbers
next: lea ebx, anArray ; get address of array
mov count, cx
input prompt2, inString, 40 ; prompt for number to search for
atow inString
mov dx,ax
mov cx,count ; prep cx to be the counter
lea eax, anArray
push eax
push edx
push ecx
call function1
add esp,6
cmp eax,0
je notThere
jmp equalTo
notThere: wtoa frontOut2, search
output outMsgLabel,frontOut2 ; output message
jmp searchAgain
equalTo: wtoa frontOut1, search
wtoa rearOut1, ax
output outMsgLabel,frontOut1 ; output message
mov cx,count
jmp searchAgain
searchAgain: input prompt3, searchString, 16 ; prompt for search again input
cmp searchString,"n" ; check for n
je end1
cmp searchString,"N" ; check forN
je end1
jmp next
end1:
mov eax, 0 ; exit with return code 0
ret
_MainProc ENDP
END ; end of source code
.586
.MODEL FLAT
.CODE
;void function1(int count, int search, int array[])
;outputs whether the search is in the array
function1 PROC
push ebp
mov ebp,esp
push ebx
push ecx
push edx
push esi
mov ecx,[ebp]
mov edx,[ebp+2]
mov esi,[ebp+4]
arraySearch: mov ax, dx
cmp [esi],ax ; check if number is in array
je equalTo
add ebx,2 ; move to next number in array
loop arraySearch ; loop back to top
notThere: pop esi
pop edx
pop ecx
pop ebx
mov ax,0
ret
equalTo: pop esi
pop edx
pop ecx
pop ebx
inc cx
mov ax,cx
ret
ret
function1 ENDP
END
Why are you adding 6 to esp after your call to function1? This is 32bit Assembly and the stack is DWORD aligned, you pushed 3 32bit registers so you should be adding 12 to esp after your call to function1.
In function1, you set up a stack frame with:
push ebp
mov ebp, esp
Where do you pop ebp and restore the stack pointer? These 2 errors will mess your program up big time!
Oh, and that being said, the parameters to your function start at [ebp + 8] and for each parameter add 4 to that offset.
1st param = [ebp + 8]
2nd = [ebp + 12]
3rd = [ebp + 16]
etc…