What is signed division(idiv) instruction?












0















In intel instruction, idiv(integer divsion) means signed division.

I got the result of idiv, but I don't quite understand the result.

- Example



0xffff0000 idiv 0xffff1100



- My wrong prediction

As long as I know, quotient should be 0, and remainder should be 0xffff0000 and because...



0xffff0000 / 0xffff1100 = 0  
0xffff0000 % 0xffff1100 = 0xffff0000



- However, the result was...
Before idiv



eax            0xffff0000            # dividend
esi 0xffff1100 # divisor
edx 0x0


After idiv



eax            0xfffeedcc            # quotient
edx 0x7400 29696 # remainder



- Question.

The result was value I couldn't expected.

Could someone explain about signed division(idiv)?




- Appended.
Here's More information about idiv.
idiv uses the eax register as a source register.

As a result of execution, quotient is stored at eax, and remainder is stored at edx.










share|improve this question

























  • You say "8086 instruction" but then you use "eax" and "edx" which are not 8086 registers. Please clarify. Note also that the idiv instruction uses the (e)dx register as a source register, which you did not specify.

    – Raymond Chen
    Jan 2 at 3:57











  • @RaymondChen Thank you for your advice. I edited my question to be more clear.

    – Jiwon
    Jan 2 at 4:05











  • @Jiwon - the question still doesn't show what is in edx before idiv.

    – rcgldr
    Jan 2 at 4:06






  • 1





    Have you looked at something like (idiv) IA-32 Assembly Language Reference Manual?

    – David C. Rankin
    Jan 2 at 4:18













  • Possible duplicate of IDIV in assembly isn't giving me the wanted result (NASM)

    – Raymond Chen
    Jan 2 at 15:34
















0















In intel instruction, idiv(integer divsion) means signed division.

I got the result of idiv, but I don't quite understand the result.

- Example



0xffff0000 idiv 0xffff1100



- My wrong prediction

As long as I know, quotient should be 0, and remainder should be 0xffff0000 and because...



0xffff0000 / 0xffff1100 = 0  
0xffff0000 % 0xffff1100 = 0xffff0000



- However, the result was...
Before idiv



eax            0xffff0000            # dividend
esi 0xffff1100 # divisor
edx 0x0


After idiv



eax            0xfffeedcc            # quotient
edx 0x7400 29696 # remainder



- Question.

The result was value I couldn't expected.

Could someone explain about signed division(idiv)?




- Appended.
Here's More information about idiv.
idiv uses the eax register as a source register.

As a result of execution, quotient is stored at eax, and remainder is stored at edx.










share|improve this question

























  • You say "8086 instruction" but then you use "eax" and "edx" which are not 8086 registers. Please clarify. Note also that the idiv instruction uses the (e)dx register as a source register, which you did not specify.

    – Raymond Chen
    Jan 2 at 3:57











  • @RaymondChen Thank you for your advice. I edited my question to be more clear.

    – Jiwon
    Jan 2 at 4:05











  • @Jiwon - the question still doesn't show what is in edx before idiv.

    – rcgldr
    Jan 2 at 4:06






  • 1





    Have you looked at something like (idiv) IA-32 Assembly Language Reference Manual?

    – David C. Rankin
    Jan 2 at 4:18













  • Possible duplicate of IDIV in assembly isn't giving me the wanted result (NASM)

    – Raymond Chen
    Jan 2 at 15:34














0












0








0


1






In intel instruction, idiv(integer divsion) means signed division.

I got the result of idiv, but I don't quite understand the result.

- Example



0xffff0000 idiv 0xffff1100



- My wrong prediction

As long as I know, quotient should be 0, and remainder should be 0xffff0000 and because...



0xffff0000 / 0xffff1100 = 0  
0xffff0000 % 0xffff1100 = 0xffff0000



- However, the result was...
Before idiv



eax            0xffff0000            # dividend
esi 0xffff1100 # divisor
edx 0x0


After idiv



eax            0xfffeedcc            # quotient
edx 0x7400 29696 # remainder



- Question.

The result was value I couldn't expected.

Could someone explain about signed division(idiv)?




- Appended.
Here's More information about idiv.
idiv uses the eax register as a source register.

As a result of execution, quotient is stored at eax, and remainder is stored at edx.










share|improve this question
















In intel instruction, idiv(integer divsion) means signed division.

I got the result of idiv, but I don't quite understand the result.

- Example



0xffff0000 idiv 0xffff1100



- My wrong prediction

As long as I know, quotient should be 0, and remainder should be 0xffff0000 and because...



0xffff0000 / 0xffff1100 = 0  
0xffff0000 % 0xffff1100 = 0xffff0000



- However, the result was...
Before idiv



eax            0xffff0000            # dividend
esi 0xffff1100 # divisor
edx 0x0


After idiv



eax            0xfffeedcc            # quotient
edx 0x7400 29696 # remainder



- Question.

The result was value I couldn't expected.

Could someone explain about signed division(idiv)?




- Appended.
Here's More information about idiv.
idiv uses the eax register as a source register.

As a result of execution, quotient is stored at eax, and remainder is stored at edx.







assembly x86 division integer-division signed-integer






share|improve this question















share|improve this question













share|improve this question




share|improve this question








edited Jan 2 at 4:22









Peter Cordes

129k18194329




129k18194329










asked Jan 2 at 3:41









JiwonJiwon

329213




329213













  • You say "8086 instruction" but then you use "eax" and "edx" which are not 8086 registers. Please clarify. Note also that the idiv instruction uses the (e)dx register as a source register, which you did not specify.

    – Raymond Chen
    Jan 2 at 3:57











  • @RaymondChen Thank you for your advice. I edited my question to be more clear.

    – Jiwon
    Jan 2 at 4:05











  • @Jiwon - the question still doesn't show what is in edx before idiv.

    – rcgldr
    Jan 2 at 4:06






  • 1





    Have you looked at something like (idiv) IA-32 Assembly Language Reference Manual?

    – David C. Rankin
    Jan 2 at 4:18













  • Possible duplicate of IDIV in assembly isn't giving me the wanted result (NASM)

    – Raymond Chen
    Jan 2 at 15:34



















  • You say "8086 instruction" but then you use "eax" and "edx" which are not 8086 registers. Please clarify. Note also that the idiv instruction uses the (e)dx register as a source register, which you did not specify.

    – Raymond Chen
    Jan 2 at 3:57











  • @RaymondChen Thank you for your advice. I edited my question to be more clear.

    – Jiwon
    Jan 2 at 4:05











  • @Jiwon - the question still doesn't show what is in edx before idiv.

    – rcgldr
    Jan 2 at 4:06






  • 1





    Have you looked at something like (idiv) IA-32 Assembly Language Reference Manual?

    – David C. Rankin
    Jan 2 at 4:18













  • Possible duplicate of IDIV in assembly isn't giving me the wanted result (NASM)

    – Raymond Chen
    Jan 2 at 15:34

















You say "8086 instruction" but then you use "eax" and "edx" which are not 8086 registers. Please clarify. Note also that the idiv instruction uses the (e)dx register as a source register, which you did not specify.

– Raymond Chen
Jan 2 at 3:57





You say "8086 instruction" but then you use "eax" and "edx" which are not 8086 registers. Please clarify. Note also that the idiv instruction uses the (e)dx register as a source register, which you did not specify.

– Raymond Chen
Jan 2 at 3:57













@RaymondChen Thank you for your advice. I edited my question to be more clear.

– Jiwon
Jan 2 at 4:05





@RaymondChen Thank you for your advice. I edited my question to be more clear.

– Jiwon
Jan 2 at 4:05













@Jiwon - the question still doesn't show what is in edx before idiv.

– rcgldr
Jan 2 at 4:06





@Jiwon - the question still doesn't show what is in edx before idiv.

– rcgldr
Jan 2 at 4:06




1




1





Have you looked at something like (idiv) IA-32 Assembly Language Reference Manual?

– David C. Rankin
Jan 2 at 4:18







Have you looked at something like (idiv) IA-32 Assembly Language Reference Manual?

– David C. Rankin
Jan 2 at 4:18















Possible duplicate of IDIV in assembly isn't giving me the wanted result (NASM)

– Raymond Chen
Jan 2 at 15:34





Possible duplicate of IDIV in assembly isn't giving me the wanted result (NASM)

– Raymond Chen
Jan 2 at 15:34












1 Answer
1






active

oldest

votes


















3














idiv divides edx:eax by the explicit source operand. See Intel's instruction manual entry.



Since edx is 0, edx:eax is a positive number. You are dividing 4294901760 by -61184, giving -70196 with a remainder of 29696.



Remember that both dividend (EDX:EAX) and divisor (ESI in your case) are interpreted as 2's complement signed numbers, so any bit-pattern with the high bit set is negative.



00000000ffff0000 = 4294901760
ffff1100 = -61184
fffeedcc = -70196
7400 = 29696


You should sign extend eax into edx using cdq before using idiv, instead of zero-extending by zeroing EDX.



However, that still won't give the results you were expecting, because -65536 divided by -61184 equals 1 with a remainder of -4352.





(A negative dividend and positive divisor would give a negative remainder: X86 IDIV sign of remainder depends on sign of dividend for 8/-3 and -8/3?)






share|improve this answer





















  • 1





    Thank you for your detailed answer!

    – Jiwon
    Jan 2 at 8:12











Your Answer






StackExchange.ifUsing("editor", function () {
StackExchange.using("externalEditor", function () {
StackExchange.using("snippets", function () {
StackExchange.snippets.init();
});
});
}, "code-snippets");

StackExchange.ready(function() {
var channelOptions = {
tags: "".split(" "),
id: "1"
};
initTagRenderer("".split(" "), "".split(" "), channelOptions);

StackExchange.using("externalEditor", function() {
// Have to fire editor after snippets, if snippets enabled
if (StackExchange.settings.snippets.snippetsEnabled) {
StackExchange.using("snippets", function() {
createEditor();
});
}
else {
createEditor();
}
});

function createEditor() {
StackExchange.prepareEditor({
heartbeatType: 'answer',
autoActivateHeartbeat: false,
convertImagesToLinks: true,
noModals: true,
showLowRepImageUploadWarning: true,
reputationToPostImages: 10,
bindNavPrevention: true,
postfix: "",
imageUploader: {
brandingHtml: "Powered by u003ca class="icon-imgur-white" href="https://imgur.com/"u003eu003c/au003e",
contentPolicyHtml: "User contributions licensed under u003ca href="https://creativecommons.org/licenses/by-sa/3.0/"u003ecc by-sa 3.0 with attribution requiredu003c/au003e u003ca href="https://stackoverflow.com/legal/content-policy"u003e(content policy)u003c/au003e",
allowUrls: true
},
onDemand: true,
discardSelector: ".discard-answer"
,immediatelyShowMarkdownHelp:true
});


}
});














draft saved

draft discarded


















StackExchange.ready(
function () {
StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fstackoverflow.com%2fquestions%2f54000965%2fwhat-is-signed-divisionidiv-instruction%23new-answer', 'question_page');
}
);

Post as a guest















Required, but never shown

























1 Answer
1






active

oldest

votes








1 Answer
1






active

oldest

votes









active

oldest

votes






active

oldest

votes









3














idiv divides edx:eax by the explicit source operand. See Intel's instruction manual entry.



Since edx is 0, edx:eax is a positive number. You are dividing 4294901760 by -61184, giving -70196 with a remainder of 29696.



Remember that both dividend (EDX:EAX) and divisor (ESI in your case) are interpreted as 2's complement signed numbers, so any bit-pattern with the high bit set is negative.



00000000ffff0000 = 4294901760
ffff1100 = -61184
fffeedcc = -70196
7400 = 29696


You should sign extend eax into edx using cdq before using idiv, instead of zero-extending by zeroing EDX.



However, that still won't give the results you were expecting, because -65536 divided by -61184 equals 1 with a remainder of -4352.





(A negative dividend and positive divisor would give a negative remainder: X86 IDIV sign of remainder depends on sign of dividend for 8/-3 and -8/3?)






share|improve this answer





















  • 1





    Thank you for your detailed answer!

    – Jiwon
    Jan 2 at 8:12
















3














idiv divides edx:eax by the explicit source operand. See Intel's instruction manual entry.



Since edx is 0, edx:eax is a positive number. You are dividing 4294901760 by -61184, giving -70196 with a remainder of 29696.



Remember that both dividend (EDX:EAX) and divisor (ESI in your case) are interpreted as 2's complement signed numbers, so any bit-pattern with the high bit set is negative.



00000000ffff0000 = 4294901760
ffff1100 = -61184
fffeedcc = -70196
7400 = 29696


You should sign extend eax into edx using cdq before using idiv, instead of zero-extending by zeroing EDX.



However, that still won't give the results you were expecting, because -65536 divided by -61184 equals 1 with a remainder of -4352.





(A negative dividend and positive divisor would give a negative remainder: X86 IDIV sign of remainder depends on sign of dividend for 8/-3 and -8/3?)






share|improve this answer





















  • 1





    Thank you for your detailed answer!

    – Jiwon
    Jan 2 at 8:12














3












3








3







idiv divides edx:eax by the explicit source operand. See Intel's instruction manual entry.



Since edx is 0, edx:eax is a positive number. You are dividing 4294901760 by -61184, giving -70196 with a remainder of 29696.



Remember that both dividend (EDX:EAX) and divisor (ESI in your case) are interpreted as 2's complement signed numbers, so any bit-pattern with the high bit set is negative.



00000000ffff0000 = 4294901760
ffff1100 = -61184
fffeedcc = -70196
7400 = 29696


You should sign extend eax into edx using cdq before using idiv, instead of zero-extending by zeroing EDX.



However, that still won't give the results you were expecting, because -65536 divided by -61184 equals 1 with a remainder of -4352.





(A negative dividend and positive divisor would give a negative remainder: X86 IDIV sign of remainder depends on sign of dividend for 8/-3 and -8/3?)






share|improve this answer















idiv divides edx:eax by the explicit source operand. See Intel's instruction manual entry.



Since edx is 0, edx:eax is a positive number. You are dividing 4294901760 by -61184, giving -70196 with a remainder of 29696.



Remember that both dividend (EDX:EAX) and divisor (ESI in your case) are interpreted as 2's complement signed numbers, so any bit-pattern with the high bit set is negative.



00000000ffff0000 = 4294901760
ffff1100 = -61184
fffeedcc = -70196
7400 = 29696


You should sign extend eax into edx using cdq before using idiv, instead of zero-extending by zeroing EDX.



However, that still won't give the results you were expecting, because -65536 divided by -61184 equals 1 with a remainder of -4352.





(A negative dividend and positive divisor would give a negative remainder: X86 IDIV sign of remainder depends on sign of dividend for 8/-3 and -8/3?)







share|improve this answer














share|improve this answer



share|improve this answer








edited Jan 2 at 4:41









Peter Cordes

129k18194329




129k18194329










answered Jan 2 at 4:10









prlprl

4,9631316




4,9631316








  • 1





    Thank you for your detailed answer!

    – Jiwon
    Jan 2 at 8:12














  • 1





    Thank you for your detailed answer!

    – Jiwon
    Jan 2 at 8:12








1




1





Thank you for your detailed answer!

– Jiwon
Jan 2 at 8:12





Thank you for your detailed answer!

– Jiwon
Jan 2 at 8:12




















draft saved

draft discarded




















































Thanks for contributing an answer to Stack Overflow!


  • Please be sure to answer the question. Provide details and share your research!

But avoid



  • Asking for help, clarification, or responding to other answers.

  • Making statements based on opinion; back them up with references or personal experience.


To learn more, see our tips on writing great answers.




draft saved


draft discarded














StackExchange.ready(
function () {
StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fstackoverflow.com%2fquestions%2f54000965%2fwhat-is-signed-divisionidiv-instruction%23new-answer', 'question_page');
}
);

Post as a guest















Required, but never shown





















































Required, but never shown














Required, but never shown












Required, but never shown







Required, but never shown

































Required, but never shown














Required, but never shown












Required, but never shown







Required, but never shown







Popular posts from this blog

Monofisismo

Angular Downloading a file using contenturl with Basic Authentication

Olmecas