0

When Im using this bit of the code I get an exception. At least this what I get from the school's compiler.

The value of i,j is given, j= 348 and i= 457830 (i is declared as int 32 and j is declared as int16)

mov(i,eax);
movzx(j,ebx);  
sub(ebx,eax);  
add(3,ebx); 

cdq; 
div(ebx); 
mov(edx,eax); 

The value that I should return in EAX is: (i - j) % (3+j) All I want is the remainder, which I get, but with that exception I cant get the points for my homework.

  • Shouldn't you be initializing EDX to zero (before the DIV) since it will be used with the DIV? your DIV will be doing (EDX:EAX)/EBX . The x86 instruction set explains how [DIV](http://x86.renejeschke.de/html/file_module_x86_id_72.html) works (in this case using 32 bit registers). EDX:EAX represents a 64 bit unsigned integer. If you don't initialize EDX to zero, the division will be using garbage that may be in EDX (EDX is the upper 32 bits of part of the 64-bit dividend – Michael Petch Nov 25 '15 at 21:34
  • 1
    @MichaelPetch: He's using `cdq` to *sign* extend eax into edx:eax. That code should work for those values. (But Juras, you should use `idiv`, or clear edx instead of sign-extending into it). I don't see the problem. Try single-stepping with a debugger, to make sure the values are getting loaded properly. `div` can fault if the result doesn't fit into 32bits. (Maybe somehow the sign bit is set in eax, so EDX:EAX gets a number near 2^64, which would require a very large divisor to bring down to fit in 32 bits?) – Peter Cordes Nov 26 '15 at 00:40
  • @PeterCordes I actually missed the CDQ mainly because it didn't appear as a typical function call (and I think my eye skipped over the line thinking it was a comment) not realizing it was the sign extension instruction. But even if you exclude the fact that there is mixture of signed and unsigned here, the test values given should not have caused issues. What is missing are the declarations and initializations of i and j to know what is really going on. – Michael Petch Nov 26 '15 at 00:54
  • @MichaelPetch: yeah exactly. Like I said, I don't see anything wrong with this code, for those values of `i` and `j`. Probably garbage data is getting loaded due to something outside the posted code. – Peter Cordes Nov 26 '15 at 00:56
  • @MichaelPetch, thank you. Using idiv instead of div, appears to be the solution. – Jurás Bence Nov 26 '15 at 05:30
  • @JurásBence Hmmm `IDIV` works? Are you sure your numbers in i and j are the ones you posted in the question? Although IDIV fixes a bug, IMHO it doesn't fix the bug you would have seen with the code you posted and info given, unless there is another problem somewhere. The other possibility is that your school's server runs test cases of varying values of i and j that would produce that bug (and then they display an error). Just the numbers you give don't seem to be ones that would cause a division overflow. – Michael Petch Nov 26 '15 at 05:44

0 Answers0