I am working on an assignment for my class. My C++ code calls the _Divide funtion to take in 2 values to divide and return into 'Result'. To start I move -1 into eax. once I'm done with the process, my eax keeps returning '-1' as the value. What could I be doing wrong. Here's my assem code

public _Divide
.386
.model flat
.code
_Divide proc
                mov eax, -1
mov ebx, 1

cdq
idiv ebx
ret
_Divide endp
end

if you would need to see my C++ code please let me know.
Thanks
Posted on 2010-10-05 11:18:32 by justbrianr
justbrianr,

You shouldn't be surprised, x/1=x for any reasonable x.
Posted on 2010-10-05 11:28:48 by baldr
We probably don't need to see your C++ code, but you need to pay attention to it! The assignment asks you to "take in two values". How are you doing that? What "type" are the values? Then, in your asm function, you need to acquire those values - not just -1! Your code for "idiv" is correct, ASSuming signed integer types. The calling convention expects ebx to be preserved, so either preserve it (push/pop), or easier, use a register your function is allowed to trash - ecx would do nicely.

If you need help doing some part of that, ask again.

Best,
Frank

Posted on 2010-10-06 14:15:37 by fbkotler