Binary subtraction, carry bit not set?












2















I've encountered a situation that seems a bit unintuitive when dealing with binary subtraction and the NZCV flag bits.



Consider the following values



0xF7      0b 1111 0111
0xFF 0b 1111 1111


If these are both considered to be 8B values where



0x000000F7      0b 0000 .... 0000 1111 0111
0x000000FF 0b 0000 .... 0000 1111 1111


When subtracted the end result is



0x FF FF FF F8    0b 1111 .... 1000


I understand how this result is found but I don't understand why the carry bit is not set for this operation.



To my knowledge, the carry bit is set when the MSB is borrowed from, is that not the case here?










share|improve this question























  • Subtract 0xff is the same as add 0x1. And add 0x1 should not set the carry bit for your example. Btw., which CPU arch is your question about? ARM?

    – geza
    Nov 13 '18 at 8:57











  • ARM M0+, is this a 2's complement thing?

    – MKUltra
    Nov 13 '18 at 9:00






  • 1





    On ARM, the carry bit is inverted for subtractions. So carry is set if no borrow happened and vice versa.

    – fuz
    Nov 13 '18 at 9:08
















2















I've encountered a situation that seems a bit unintuitive when dealing with binary subtraction and the NZCV flag bits.



Consider the following values



0xF7      0b 1111 0111
0xFF 0b 1111 1111


If these are both considered to be 8B values where



0x000000F7      0b 0000 .... 0000 1111 0111
0x000000FF 0b 0000 .... 0000 1111 1111


When subtracted the end result is



0x FF FF FF F8    0b 1111 .... 1000


I understand how this result is found but I don't understand why the carry bit is not set for this operation.



To my knowledge, the carry bit is set when the MSB is borrowed from, is that not the case here?










share|improve this question























  • Subtract 0xff is the same as add 0x1. And add 0x1 should not set the carry bit for your example. Btw., which CPU arch is your question about? ARM?

    – geza
    Nov 13 '18 at 8:57











  • ARM M0+, is this a 2's complement thing?

    – MKUltra
    Nov 13 '18 at 9:00






  • 1





    On ARM, the carry bit is inverted for subtractions. So carry is set if no borrow happened and vice versa.

    – fuz
    Nov 13 '18 at 9:08














2












2








2








I've encountered a situation that seems a bit unintuitive when dealing with binary subtraction and the NZCV flag bits.



Consider the following values



0xF7      0b 1111 0111
0xFF 0b 1111 1111


If these are both considered to be 8B values where



0x000000F7      0b 0000 .... 0000 1111 0111
0x000000FF 0b 0000 .... 0000 1111 1111


When subtracted the end result is



0x FF FF FF F8    0b 1111 .... 1000


I understand how this result is found but I don't understand why the carry bit is not set for this operation.



To my knowledge, the carry bit is set when the MSB is borrowed from, is that not the case here?










share|improve this question














I've encountered a situation that seems a bit unintuitive when dealing with binary subtraction and the NZCV flag bits.



Consider the following values



0xF7      0b 1111 0111
0xFF 0b 1111 1111


If these are both considered to be 8B values where



0x000000F7      0b 0000 .... 0000 1111 0111
0x000000FF 0b 0000 .... 0000 1111 1111


When subtracted the end result is



0x FF FF FF F8    0b 1111 .... 1000


I understand how this result is found but I don't understand why the carry bit is not set for this operation.



To my knowledge, the carry bit is set when the MSB is borrowed from, is that not the case here?







assembly binary flags






share|improve this question













share|improve this question











share|improve this question




share|improve this question










asked Nov 13 '18 at 8:39









MKUltraMKUltra

939




939













  • Subtract 0xff is the same as add 0x1. And add 0x1 should not set the carry bit for your example. Btw., which CPU arch is your question about? ARM?

    – geza
    Nov 13 '18 at 8:57











  • ARM M0+, is this a 2's complement thing?

    – MKUltra
    Nov 13 '18 at 9:00






  • 1





    On ARM, the carry bit is inverted for subtractions. So carry is set if no borrow happened and vice versa.

    – fuz
    Nov 13 '18 at 9:08



















  • Subtract 0xff is the same as add 0x1. And add 0x1 should not set the carry bit for your example. Btw., which CPU arch is your question about? ARM?

    – geza
    Nov 13 '18 at 8:57











  • ARM M0+, is this a 2's complement thing?

    – MKUltra
    Nov 13 '18 at 9:00






  • 1





    On ARM, the carry bit is inverted for subtractions. So carry is set if no borrow happened and vice versa.

    – fuz
    Nov 13 '18 at 9:08

















Subtract 0xff is the same as add 0x1. And add 0x1 should not set the carry bit for your example. Btw., which CPU arch is your question about? ARM?

– geza
Nov 13 '18 at 8:57





Subtract 0xff is the same as add 0x1. And add 0x1 should not set the carry bit for your example. Btw., which CPU arch is your question about? ARM?

– geza
Nov 13 '18 at 8:57













ARM M0+, is this a 2's complement thing?

– MKUltra
Nov 13 '18 at 9:00





ARM M0+, is this a 2's complement thing?

– MKUltra
Nov 13 '18 at 9:00




1




1





On ARM, the carry bit is inverted for subtractions. So carry is set if no borrow happened and vice versa.

– fuz
Nov 13 '18 at 9:08





On ARM, the carry bit is inverted for subtractions. So carry is set if no borrow happened and vice versa.

– fuz
Nov 13 '18 at 9:08












1 Answer
1






active

oldest

votes


















3














The ARM subtraction instructions with carry (SBC, RSC) interpret the carry flag (C) as:




0: means borrow



1: means no borrow




So in your calculation the MSB is borrowed and the carry ist NOT set!






share|improve this answer
























  • Yes, and this is the opposite of how x86's CF works. On x86 it works as the OP was thinking, a non-inverted borrow flag.

    – Peter Cordes
    Nov 13 '18 at 15:24













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%2f53276919%2fbinary-subtraction-carry-bit-not-set%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














The ARM subtraction instructions with carry (SBC, RSC) interpret the carry flag (C) as:




0: means borrow



1: means no borrow




So in your calculation the MSB is borrowed and the carry ist NOT set!






share|improve this answer
























  • Yes, and this is the opposite of how x86's CF works. On x86 it works as the OP was thinking, a non-inverted borrow flag.

    – Peter Cordes
    Nov 13 '18 at 15:24


















3














The ARM subtraction instructions with carry (SBC, RSC) interpret the carry flag (C) as:




0: means borrow



1: means no borrow




So in your calculation the MSB is borrowed and the carry ist NOT set!






share|improve this answer
























  • Yes, and this is the opposite of how x86's CF works. On x86 it works as the OP was thinking, a non-inverted borrow flag.

    – Peter Cordes
    Nov 13 '18 at 15:24
















3












3








3







The ARM subtraction instructions with carry (SBC, RSC) interpret the carry flag (C) as:




0: means borrow



1: means no borrow




So in your calculation the MSB is borrowed and the carry ist NOT set!






share|improve this answer













The ARM subtraction instructions with carry (SBC, RSC) interpret the carry flag (C) as:




0: means borrow



1: means no borrow




So in your calculation the MSB is borrowed and the carry ist NOT set!







share|improve this answer












share|improve this answer



share|improve this answer










answered Nov 13 '18 at 9:28









MikeMike

2,0071621




2,0071621













  • Yes, and this is the opposite of how x86's CF works. On x86 it works as the OP was thinking, a non-inverted borrow flag.

    – Peter Cordes
    Nov 13 '18 at 15:24





















  • Yes, and this is the opposite of how x86's CF works. On x86 it works as the OP was thinking, a non-inverted borrow flag.

    – Peter Cordes
    Nov 13 '18 at 15:24



















Yes, and this is the opposite of how x86's CF works. On x86 it works as the OP was thinking, a non-inverted borrow flag.

– Peter Cordes
Nov 13 '18 at 15:24







Yes, and this is the opposite of how x86's CF works. On x86 it works as the OP was thinking, a non-inverted borrow flag.

– Peter Cordes
Nov 13 '18 at 15:24




















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%2f53276919%2fbinary-subtraction-carry-bit-not-set%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







這個網誌中的熱門文章

Hercules Kyvelos

Tangent Lines Diagram Along Smooth Curve

Yusuf al-Mu'taman ibn Hud