Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • Home
  • SEARCH
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 8561449
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 11, 20262026-06-11T16:30:19+00:00 2026-06-11T16:30:19+00:00

Please note: I’m just trying to learn. Please do not yell at me for

  • 0

Please note: I’m just trying to learn. Please do not yell at me for toying with assembly.

I have the following method:

uint32 test(int16 a, int16 b)
{
  return ( a + b ) & 0xffff;
}

I created a .s file based on details I found here.

My .s file contains the following:

.macro BEGIN_FUNCTION
.align 2        // Align the function code to a 4-byte (2^n) word boundary.
.arm            // Use ARM instructions instead of Thumb.
.globl _$0      // Make the function globally accessible.
.no_dead_strip _$0  // Stop the optimizer from ignoring this function!
.private_extern _$0
_$0:                // Declare the function.
.endmacro

.macro END_FUNCTION
bx  lr      // Jump back to the caller.
.endmacro


BEGIN_FUNCTION addFunction
  add       r0, r0, r1      // Return the sum of the first 2 function parameters
END_FUNCTION

BEGIN_FUNCTION addAndFunction
  add       r0, r0, r1      // Return the sum of the first 2 function parameters
  ands      r0, r0, r2      // Ands the result of r0 and the third parameter passed
END_FUNCTION

So if I call the following:

addFunction(10,20)

I get what I would expect. But then if I try

int addOne = addFunction(0xffff,0xffff); // Result = -2
int addTwo = 0xffff + 0xffff;            // Result = 131070

Screenshot of the two values

My addOne does not end up being the same value as my add two. Any ideas on what I am doing wrong here?

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-06-11T16:30:21+00:00Added an answer on June 11, 2026 at 4:30 pm

    When you pass the int16_t parameter 0xffff to addFunction the compiler sign-extends it to fit in the 32-bit register (as it’s a signed integer) making it 0xffffffff. You add two of these together to get 0xfffffffe and return it. When you do 0xffff + 0xffff both constants are already 32-bits and so there’s no need to sign extend, the result is 0x0001fffe.

    What you need to understand is that when you’re using a signed integer and passing the 16-bit value 0xffff you’re actually passing in -1, so it’s no surprise that -1 + -1 = -2. It’s also no suprise that 0xffff + 0xffff = 0xfffffffe.

    If you change the (not shown) C declaration of addFunction to take two unsigned ints, then you’ll get the result you desire (the compiler won’t do the sign extension).

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

PLEASE NOTE: This is a CODE QUESTION NOT AN SYSTEM MANIPULATION ONE. I have
Please note that I am trying to learn iphone and for this problem, i
EDIT: Please note that the app comes up just fine in Passenger/Apache2. Just not
Please note, user_id in my plans collection is NOT an object_id. I have it
*Please note that I am just testing to understand this. I am trying to
Please note that I'm not asking how but why. And I don't know if
(Please note that I have seen a similar question on StackOverflow recently, however I
Please note: This is a question about the Eclipse plugin Subversive , and not
Please note this is not a question about online/hosted SVN services. I am working
Please note that this is not homework and i did search before starting this

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.