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.

Community
  • 1
  • 1
alalambda
  • 323
  • 3
  • 15

1 Answers1

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.

Ajay Brahmakshatriya
  • 8,993
  • 3
  • 26
  • 49