0

I am new to ARM Assembler. Using qemu emulator.

This solution didn't work for me.

I have this C file md1_main.c:

#include <stdio.h>
#include <stdlib.h>
#include "md1.h"

    int main (void)
    {
            int n;
            scanf("%d", &n);
            printf("Result = %u\n", asum(n));
            return 0;
    }

and .h file contains function prototype unsigned int asum(unsigned int n);

I am really confused, how to pass n into the assembler code.

Assembler code is md1.s:

.text
.align  2
.global asum
.type   asum, %function

asum:
    mov r1, #0
    mov r2, #1

loop:
    cmp r2, #3 ; instead of 3 there should be my input
    bgt end
    add r1, r1, r2
    add r2, r2, #1
    b loop

end:
    mov r0, r1
    bx lr

Just can't get it.

8
  • 2
    What target are you compiling this C code for? Platform - Linux, Windows or something else? Commented Mar 18, 2017 at 15:47
  • @AjayBrahmakshatriya, linux. Commented Mar 18, 2017 at 15:49
  • @Benny, that solution(?) didn't worked for me. Commented Mar 18, 2017 at 15:49
  • 1
    Okay, So we will have to follow the Linux calling convention. Now the target? What assembly is that? X86? X64? ARM? Commented Mar 18, 2017 at 15:51
  • 1
    Are you sure? Because those registers don't look like x86_64 registers. Neither do the instructions. Commented Mar 18, 2017 at 15:53

1 Answer 1

4

OP has mentioned the architecture as ARM 64. So I will answer according the calling convention.

The first 4 arguments are passed in r0, r1, r2, r3.

That is what the compiler will also do for you when compiling the C file. So you can expect your parameter n in the r0 register and you can directly use it.

I also see that your function returns an unsigned value. That will be returned in the r0 register.

See this, for a more detailed description of the calling convention.

Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.