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 6909945
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 27, 20262026-05-27T08:45:11+00:00 2026-05-27T08:45:11+00:00

I defined a function called multiply and it takes arguments R0 and R1 and

  • 0

I defined a function called multiply and it takes arguments R0 and R1 and saves the result in R3. This sucks because whenever I call multiply I have to put the operands in R0 and R1 and move what was in R0, R1 and R3 somewhere else. Is there a way to make the function take the form multiply R4, R5, R6 similar to how add R1, R2, R3

ldr R0, =snakes
ldr R1, [R0], #4
mov R2, #15
mov R3, #6

If I want to multiply R2 and R3 this would require extra work. I call multiply often and am wondering if there’s a better way?

Multiply:
    stmfd   sp!,{r0-r2, lr}
    mov R2, #1
    mov R3, #0 
    repeat:
        add R3, R1, R3  
        add R2, R2, #1
        cmp R2, R0
        ble repeat
    mov R2, R3
    LDMFD   sp!,{r0-r1, pc}
  • 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-27T08:45:11+00:00Added an answer on May 27, 2026 at 8:45 am

    You can use macros:

    .macro multiply A B C
    {body}
    .endm
    

    Values of parameters in the body are avaliable by \A, \B, \C

    there is a little mistake in your code:

    Multiply:
        stmfd   sp!,{r0-r2, lr}
        mov R2, #1
        mov R3, #0 
        repeat:
            add R3, R1, R3  
            add R2, R2, #1
            cmp R2, R0
            ble repeat
        mov R2, R3
        LDMFD   sp!,{r0-r1, pc}
    

    When R0 is 0, the result would be R1 instead of 0. So you have to check it before it enters the loop. The correct code will be:

    stmfd   sp!,{r0-r2, lr}
    mov R2, #0
    mov R3, #0 
    repeat:
        cmp R2, R0
        be exit_loop
        add R3, R1, R3  
        add R2, R2, #1
        b repeat
    exit_loop:
    mov R2, R3
    LDMFD   sp!,{r0-r1, pc}
    

    But this is not the most optimal method because there are 5 instructions in loop’s body instead of 4, and 2 branches instead of 1, so it will be difficult to predict them both. That’s why we going to do the following:

    stmfd   sp!,{r0-r2, lr}
    mov R2, #-1
    rsb R3, R1, #0 
    repeat:
        add R3, R1, R3  
        add R2, R2, #1
        cmp R2, R0
        bne repeat
    mov R2, R3
    LDMFD   sp!,{r0-r1, pc}
    

    And finally, with macros it will look like this:

    .macro multiply C B A
    stmfd   sp!,{\A, \B, \C, r2, lr}
    mov R2, #-1
    rsb \C, \B, #0 
    repeat:
        add \C, \B, \C  
        add R2, R2, #1
        cmp R2, \A
        bne repeat
    LDMFD   sp!,{\A, \B, \C, r2, pc}
    .endm
    
    multiply R3 R2 R1 will save the result into R3
    

    But I don’t know why you want to write your own multiply macro, I hope you know, that there is a set of multiply instructions…

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

Sidebar

Related Questions

I have a user defined function in SQL called getBuisnessDays it takes @startdate and
I have a function, where if a variable is not defined, exit; is called,
I have this user defined function. public partial class UserDefinedFunctions { static int i;
The linker is reporting multiply defined errors for an inline function. I have the
I have defined a function called is_logged_in? in a user defined library stored in
Assume you have a programme with multiple functions defined. Each function is called in
I have a class whose function defined like this. My intention is to send
I've defined a function called hash_swap inside the User model, but when I call
I have a User Defined Function (UDF) written in Java to parse lines in
I am trying to call a user defined function in jQuery: $(document).ready(function() { $('#btnSun').click(function()

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.