How to find the absolute value of an integer in Two's complement in ARM











up vote
2
down vote

favorite












If I have -2 (11111111111111111111111111111110), is there a neat ARM instruction or a series of such that will make it (00000000000000000000000000000010). OR or XOR would not work from what I have tried since I loose the 30th bit.



Thank you










share|improve this question




















  • 1




    ARM's negate instruction is rsb dst, src, #0 (reverse-subtract). You can tst / predicated-rsb. I think that's what C++ compilers will do when inlining / optimizing std::abs, but you should try compiling an abs function with optimization enabled.
    – Peter Cordes
    Nov 15 at 2:21










  • which arm instruction set? did you look at NEG? you do need the compare as shown in the checked answer...
    – old_timer
    Nov 15 at 15:48

















up vote
2
down vote

favorite












If I have -2 (11111111111111111111111111111110), is there a neat ARM instruction or a series of such that will make it (00000000000000000000000000000010). OR or XOR would not work from what I have tried since I loose the 30th bit.



Thank you










share|improve this question




















  • 1




    ARM's negate instruction is rsb dst, src, #0 (reverse-subtract). You can tst / predicated-rsb. I think that's what C++ compilers will do when inlining / optimizing std::abs, but you should try compiling an abs function with optimization enabled.
    – Peter Cordes
    Nov 15 at 2:21










  • which arm instruction set? did you look at NEG? you do need the compare as shown in the checked answer...
    – old_timer
    Nov 15 at 15:48















up vote
2
down vote

favorite









up vote
2
down vote

favorite











If I have -2 (11111111111111111111111111111110), is there a neat ARM instruction or a series of such that will make it (00000000000000000000000000000010). OR or XOR would not work from what I have tried since I loose the 30th bit.



Thank you










share|improve this question















If I have -2 (11111111111111111111111111111110), is there a neat ARM instruction or a series of such that will make it (00000000000000000000000000000010). OR or XOR would not work from what I have tried since I loose the 30th bit.



Thank you







assembly arm twos-complement absolute-value






share|improve this question















share|improve this question













share|improve this question




share|improve this question








edited Nov 15 at 15:52









Peter Cordes

117k16178305




117k16178305










asked Nov 15 at 1:59









mdrjjn

437




437








  • 1




    ARM's negate instruction is rsb dst, src, #0 (reverse-subtract). You can tst / predicated-rsb. I think that's what C++ compilers will do when inlining / optimizing std::abs, but you should try compiling an abs function with optimization enabled.
    – Peter Cordes
    Nov 15 at 2:21










  • which arm instruction set? did you look at NEG? you do need the compare as shown in the checked answer...
    – old_timer
    Nov 15 at 15:48
















  • 1




    ARM's negate instruction is rsb dst, src, #0 (reverse-subtract). You can tst / predicated-rsb. I think that's what C++ compilers will do when inlining / optimizing std::abs, but you should try compiling an abs function with optimization enabled.
    – Peter Cordes
    Nov 15 at 2:21










  • which arm instruction set? did you look at NEG? you do need the compare as shown in the checked answer...
    – old_timer
    Nov 15 at 15:48










1




1




ARM's negate instruction is rsb dst, src, #0 (reverse-subtract). You can tst / predicated-rsb. I think that's what C++ compilers will do when inlining / optimizing std::abs, but you should try compiling an abs function with optimization enabled.
– Peter Cordes
Nov 15 at 2:21




ARM's negate instruction is rsb dst, src, #0 (reverse-subtract). You can tst / predicated-rsb. I think that's what C++ compilers will do when inlining / optimizing std::abs, but you should try compiling an abs function with optimization enabled.
– Peter Cordes
Nov 15 at 2:21












which arm instruction set? did you look at NEG? you do need the compare as shown in the checked answer...
– old_timer
Nov 15 at 15:48






which arm instruction set? did you look at NEG? you do need the compare as shown in the checked answer...
– old_timer
Nov 15 at 15:48














1 Answer
1






active

oldest

votes

















up vote
4
down vote



accepted










For finding the absolute value of an integer, use a comparison and a subtraction.



@ input in r0
cmp r0, #0 @ is r0 < 0?
rsbmi r0, r0 #0 @ if yes, r0 = 0 - r0





share|improve this answer























  • mvn a bitwise NOT, ones-complement inversion rather than two's complement. infocenter.arm.com/help/index.jsp?topic=/com.arm.doc.dui0204j/…. Like I commented, you can use rsbmi r0, #0
    – Peter Cordes
    Nov 15 at 11:36










  • @PeterCordes Clearly you are right. I though for a second that mvn stood for “move negated.” That said, did you get my email?
    – fuz
    Nov 15 at 11:39










  • I did, thanks for the reminder. I should have some time to look at that today.
    – Peter Cordes
    Nov 15 at 11:43










  • @PeterCordes I'm looking forwards to your comments!
    – fuz
    Nov 15 at 11:44






  • 2




    Worth noting that if you're using an ARMv7 or ARMv8 device, you will be using the Thumb-2 instruction set which does not include conditional execution of non-branch instructions. You will need an it mi before the rsbmi. Many assemblers will insert this for you, though some will warn because you're getting an instruction you didn't explicitly request. See community.arm.com/processors/b/blog/posts/…
    – cooperised
    Nov 15 at 13:44













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',
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%2f53311360%2fhow-to-find-the-absolute-value-of-an-integer-in-twos-complement-in-arm%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








up vote
4
down vote



accepted










For finding the absolute value of an integer, use a comparison and a subtraction.



@ input in r0
cmp r0, #0 @ is r0 < 0?
rsbmi r0, r0 #0 @ if yes, r0 = 0 - r0





share|improve this answer























  • mvn a bitwise NOT, ones-complement inversion rather than two's complement. infocenter.arm.com/help/index.jsp?topic=/com.arm.doc.dui0204j/…. Like I commented, you can use rsbmi r0, #0
    – Peter Cordes
    Nov 15 at 11:36










  • @PeterCordes Clearly you are right. I though for a second that mvn stood for “move negated.” That said, did you get my email?
    – fuz
    Nov 15 at 11:39










  • I did, thanks for the reminder. I should have some time to look at that today.
    – Peter Cordes
    Nov 15 at 11:43










  • @PeterCordes I'm looking forwards to your comments!
    – fuz
    Nov 15 at 11:44






  • 2




    Worth noting that if you're using an ARMv7 or ARMv8 device, you will be using the Thumb-2 instruction set which does not include conditional execution of non-branch instructions. You will need an it mi before the rsbmi. Many assemblers will insert this for you, though some will warn because you're getting an instruction you didn't explicitly request. See community.arm.com/processors/b/blog/posts/…
    – cooperised
    Nov 15 at 13:44

















up vote
4
down vote



accepted










For finding the absolute value of an integer, use a comparison and a subtraction.



@ input in r0
cmp r0, #0 @ is r0 < 0?
rsbmi r0, r0 #0 @ if yes, r0 = 0 - r0





share|improve this answer























  • mvn a bitwise NOT, ones-complement inversion rather than two's complement. infocenter.arm.com/help/index.jsp?topic=/com.arm.doc.dui0204j/…. Like I commented, you can use rsbmi r0, #0
    – Peter Cordes
    Nov 15 at 11:36










  • @PeterCordes Clearly you are right. I though for a second that mvn stood for “move negated.” That said, did you get my email?
    – fuz
    Nov 15 at 11:39










  • I did, thanks for the reminder. I should have some time to look at that today.
    – Peter Cordes
    Nov 15 at 11:43










  • @PeterCordes I'm looking forwards to your comments!
    – fuz
    Nov 15 at 11:44






  • 2




    Worth noting that if you're using an ARMv7 or ARMv8 device, you will be using the Thumb-2 instruction set which does not include conditional execution of non-branch instructions. You will need an it mi before the rsbmi. Many assemblers will insert this for you, though some will warn because you're getting an instruction you didn't explicitly request. See community.arm.com/processors/b/blog/posts/…
    – cooperised
    Nov 15 at 13:44















up vote
4
down vote



accepted







up vote
4
down vote



accepted






For finding the absolute value of an integer, use a comparison and a subtraction.



@ input in r0
cmp r0, #0 @ is r0 < 0?
rsbmi r0, r0 #0 @ if yes, r0 = 0 - r0





share|improve this answer














For finding the absolute value of an integer, use a comparison and a subtraction.



@ input in r0
cmp r0, #0 @ is r0 < 0?
rsbmi r0, r0 #0 @ if yes, r0 = 0 - r0






share|improve this answer














share|improve this answer



share|improve this answer








edited Nov 15 at 14:09

























answered Nov 15 at 7:13









fuz

56.8k14124253




56.8k14124253












  • mvn a bitwise NOT, ones-complement inversion rather than two's complement. infocenter.arm.com/help/index.jsp?topic=/com.arm.doc.dui0204j/…. Like I commented, you can use rsbmi r0, #0
    – Peter Cordes
    Nov 15 at 11:36










  • @PeterCordes Clearly you are right. I though for a second that mvn stood for “move negated.” That said, did you get my email?
    – fuz
    Nov 15 at 11:39










  • I did, thanks for the reminder. I should have some time to look at that today.
    – Peter Cordes
    Nov 15 at 11:43










  • @PeterCordes I'm looking forwards to your comments!
    – fuz
    Nov 15 at 11:44






  • 2




    Worth noting that if you're using an ARMv7 or ARMv8 device, you will be using the Thumb-2 instruction set which does not include conditional execution of non-branch instructions. You will need an it mi before the rsbmi. Many assemblers will insert this for you, though some will warn because you're getting an instruction you didn't explicitly request. See community.arm.com/processors/b/blog/posts/…
    – cooperised
    Nov 15 at 13:44




















  • mvn a bitwise NOT, ones-complement inversion rather than two's complement. infocenter.arm.com/help/index.jsp?topic=/com.arm.doc.dui0204j/…. Like I commented, you can use rsbmi r0, #0
    – Peter Cordes
    Nov 15 at 11:36










  • @PeterCordes Clearly you are right. I though for a second that mvn stood for “move negated.” That said, did you get my email?
    – fuz
    Nov 15 at 11:39










  • I did, thanks for the reminder. I should have some time to look at that today.
    – Peter Cordes
    Nov 15 at 11:43










  • @PeterCordes I'm looking forwards to your comments!
    – fuz
    Nov 15 at 11:44






  • 2




    Worth noting that if you're using an ARMv7 or ARMv8 device, you will be using the Thumb-2 instruction set which does not include conditional execution of non-branch instructions. You will need an it mi before the rsbmi. Many assemblers will insert this for you, though some will warn because you're getting an instruction you didn't explicitly request. See community.arm.com/processors/b/blog/posts/…
    – cooperised
    Nov 15 at 13:44


















mvn a bitwise NOT, ones-complement inversion rather than two's complement. infocenter.arm.com/help/index.jsp?topic=/com.arm.doc.dui0204j/…. Like I commented, you can use rsbmi r0, #0
– Peter Cordes
Nov 15 at 11:36




mvn a bitwise NOT, ones-complement inversion rather than two's complement. infocenter.arm.com/help/index.jsp?topic=/com.arm.doc.dui0204j/…. Like I commented, you can use rsbmi r0, #0
– Peter Cordes
Nov 15 at 11:36












@PeterCordes Clearly you are right. I though for a second that mvn stood for “move negated.” That said, did you get my email?
– fuz
Nov 15 at 11:39




@PeterCordes Clearly you are right. I though for a second that mvn stood for “move negated.” That said, did you get my email?
– fuz
Nov 15 at 11:39












I did, thanks for the reminder. I should have some time to look at that today.
– Peter Cordes
Nov 15 at 11:43




I did, thanks for the reminder. I should have some time to look at that today.
– Peter Cordes
Nov 15 at 11:43












@PeterCordes I'm looking forwards to your comments!
– fuz
Nov 15 at 11:44




@PeterCordes I'm looking forwards to your comments!
– fuz
Nov 15 at 11:44




2




2




Worth noting that if you're using an ARMv7 or ARMv8 device, you will be using the Thumb-2 instruction set which does not include conditional execution of non-branch instructions. You will need an it mi before the rsbmi. Many assemblers will insert this for you, though some will warn because you're getting an instruction you didn't explicitly request. See community.arm.com/processors/b/blog/posts/…
– cooperised
Nov 15 at 13:44






Worth noting that if you're using an ARMv7 or ARMv8 device, you will be using the Thumb-2 instruction set which does not include conditional execution of non-branch instructions. You will need an it mi before the rsbmi. Many assemblers will insert this for you, though some will warn because you're getting an instruction you didn't explicitly request. See community.arm.com/processors/b/blog/posts/…
– cooperised
Nov 15 at 13:44




















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.





Some of your past answers have not been well-received, and you're in danger of being blocked from answering.


Please pay close attention to the following guidance:


  • 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%2f53311360%2fhow-to-find-the-absolute-value-of-an-integer-in-twos-complement-in-arm%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

How to change which sound is reproduced for terminal bell?

Can I use Tabulator js library in my java Spring + Thymeleaf project?

Title Spacing in Bjornstrup Chapter, Removing Chapter Number From Contents