Greetings,
I've been working on this program for quite some time, it is designed to calculate the area of the shadow of a triangle on a 3d environment, my first problem was using the fpu for some square root operations, but after some reading I finally was able to make an executable but now it shows a divide by 0 exception, I used OllyDbg as suggested on another thread to find out what was wrong , but found out that somehow my program after doing some divisions go back at doing them again, this time with the divisor as 0, and thus trigering the exception, the problem is that I don't understand why it does that or maybe I am using it wrong?.
I was hoping you could check my code to see if you could suggest something that could throw me in the right direction, but the code is quite big so I don't know if I should post it using the code tags or just upload it in a rar file?.
Thanks in advance
Use a zip file (it is a supported attachment file type) for the source and
post a snippet of the relevent section of code using the code tag.
Ok, here it goes, I hope it's not too unorganized :red.
I am not sure if you need this information but just in case I created the executable using: ml /c /coff shadow.asm
and: link /susbsystem:console /entry:start /output:anyname.exe io.obj shadow.obj kernel32.lib
thanks once again for any tip or advice you can give me about this :P
PS: I am including the excutable, the asm, the io.obj and kernel32.lib files
[attachment deleted by admin]
Crackers,
Use OllyDebug to make sure that you are popping a nonzero value into eax here:
[pre]
pop eax
mul ebx
div dos
[/pre]
If eax does not contain a valid value then the results of the multipy will be zero and then the next instruction will throw an exception.
BTW: What are you using for an editor? There sure are a bundle of trailing TABS on each line.
-- Paul
Thanks, I checked but the 0 exception happens 2 divs earlier according to the OllyDebug
I was using visual studio 2005 as my editor, also I just tried to create the executable, it seems I accidentaly erased the ; of a comment when I was translating it, and a tag:
for it to work you must add:
_calculoPunto1:
after this comment:
with t I find the x and z coordinates of the first point:
on line 183
and add the ; on that comment too.
sorry about that
QuoteCode:
pop eax
mul ebx
div dos
If eax does not contain a valid value then the results of the multipy will be zero and then the next instruction will throw an exception.
Regardless of what you have in EAX, the mul instruction will ALWAYS return a valid result. The div instruction will throw an exception IF and ONLY IF the divisor is less than or equal to the content of EDX, regardless of the content of EAX.
Crackers:
Your problem is here:
;************ FPU ***********
mov raiz,eax
fld raiz
FSQRT
fstp raiz
;************** ***************************
; I finish findind the ecuation for the line
pop eax
mul x3n
sub eax, z3n
add eax, ebx
cdq
div raiz
Your first mistake is not being familiar with FPU instructions.
The fld instruction treats the content of the source as a value in floating point format. If the integer content of "raiz" is relatively small (such as less than 100,000,000), it will be considered as almost zero as a float. Its square root will thus be 0 which you store again as a float (but would have the same effect even stored as an integer) and use later as a divisor. (The proper instruction to load an integer to the FPU is "fild").
If you are really interested in using the FPU, I would suggest you study the following:
http://www.ray.masmcode.com/fpu.html
As a side note, I also noticed the following instructions in two areas:
mov c2, eax
; Code to find the second line's t
cdq
mov eax, y2
neg eax
mov ebx, b2
mov divisor,ebx
nop
idiv divisor
The "cdq" instruction will extend the sign to EDX of the value in EAX which you stored in c2. Then you move some other value in EAX, change its sign and do a signed division without extending the sign of that EAX. It somehow doesn't make much sense to me.
Thanks a lot!, I will check that page now :bg
I hadn't noticed the sign extension problem, thanks for bringing it out too