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

  • SEARCH
  • Home
  • 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 7519183
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 30, 20262026-05-30T01:51:38+00:00 2026-05-30T01:51:38+00:00

I have some trouble with some inline assembly code. I know what should be

  • 0

I have some trouble with some inline assembly code. I know what should be done but I miss the “how” !

I have this checksum function that is “almost” working :

static unsigned long cksum_unroll( unsigned short **w, int *mlen)
{
  int len;
  unsigned short *w0;
  unsigned long sum=0;

  len = *mlen;
  w0 = *w;

  while( len >= 8) {
    asm volatile (
          "ldmia %[w0]!, {v1, v2}\n\t"
          "adds %[sum], %[sum], v1\n\t"
          "adcs %[sum], %[sum], v2\n\t"
          "adcs %[sum], %[sum], #0"
          : [sum] "+r" (sum) : [w0] "r" (w0)
          );
    len -= 8;
  }
  *mlen = len;
  *w = w0;
  return (sum);
}

My problem, I believe, is on the line “: [sum] “+r” (sum) : [w0] “r” (w0)”
On the first assembly line, w0 is processed correctly by ldmia (when the line is executed, data are in r4,r5 and w0 is incremented). But the incremented value of w0 is not saved somewhere and when the code loop, the original value of w0 is loaded again (see assembly code below).
My guess is that I should store the value of w0 on the line “: [sum] “+r” (sum) : [w0] “r” (w0)” but I don’t know how…

Here’s the disassembly code of the inline assembly part of the function :

Note that :

len is stored at r11, #-16
w0 is stored at r11, #-20
sum is stored at r11, #-24

Compiled, the following code :

asm volatile (
          "ldmia %[w0]!, {v1, v2}\n\t"
          "adds %[sum], %[sum], v1\n\t"
          "adcs %[sum], %[sum], v2\n\t"
          "adcs %[sum], %[sum], #0"
          : [sum] "+r" (sum) : [w0] "r" (w0)
);
len -= 8;

Generate :

00031910:   ldr r3, [r11, #-20]
00031914:   ldr r2, [r11, #-24]
00031918:   mov r4, r2
0003191c:   ldm r3!, {r4, r5}
00031920:   adds r4, r4, r4
00031924:   adcs r4, r4, r5
00031928:   adcs r4, r4, #0
0003192c:   str r4, [r11, #-24]
00031930:   ldr r3, [r11, #-16]
00031934:   sub r3, r3, #8
00031938:   str r3, [r11, #-16]

As you can see, I would like to add something like “str r3, [r11, #-20]” between lines 31928 and 3192c because when the program loop to the line 31910, r3 is loaded with the initial value of r3…

I think this is an easy one for the inline assembly expert of the stack overflow community!

By the way, I’m working on a ARM7TDMI processor (but this may not be relevant for this question…)

Thanks in advance!

EDIT:

To verify my idea, I tested the following :

asm volatile ( 
"ldmia %[w0]!, {v1, v2}\n\t" 
"adds %[sum], %[sum], v1\n\t" 
"adcs %[sum], %[sum], v2\n\t" 
"adcs %[sum], %[sum], #0\n\t" 
"str %[w0], [r11, #-20]" 
: [sum] "+r" (sum) : [w0] "r" (w0) 
); 

And this works. Maybe this is the solution, but what do I use to replace “r11, #20” that will probably change if I modify the function?

  • 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-05-30T01:51:39+00:00Added an answer on May 30, 2026 at 1:51 am

    The problem would seem to be that you specify w0 as an INPUT operand, when it actually should be a read-write OUTPUT operand, like sum. In addition, you need to specify that it clobbers v1 and v2 as you use those registers (otherwise, gcc might put some other var into these regs and expect them to be preserved.)

    So you should have:

    asm volatile (
          "ldmia %[w0]!, {v1, v2}\n\t"
          "adds %[sum], %[sum], v1\n\t"
          "adcs %[sum], %[sum], v2\n\t"
          "adcs %[sum], %[sum], #0"
          : [sum] "+r" (sum) , [w0] "+r" (w0) : : "v1", "v2"
          );
    

    that is, two read-write input/output operands, no exclusively input operands, and two register clobbers

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

Sidebar

Related Questions

I have some trouble with pointers. I have a function that should return the
i am using c++ and i have some trouble about pointers i know that
I have some trouble with my trac installation (version 11.4). What should I do
I'm migrating to Nhibernate 2.0 GA but have some trouble with setting cache expirations
I am developing a small windows app, but have some trouble deciding whether to
iI have some trouble getting sth like this to work in java: public class
I have some trouble understanding this one so here it is. I'm trying to
I have some trouble using decltype for member function pointers: #include <iostream> #include <type_traits>
I have some trouble with a git repository that contains several submodules. The super
I have some trouble with #define statements in my C++ code, however I'm not

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.