AVR Assembly: Writing to arrays without returning anything

I have to write a program in AVR assembly that takes in a pointer to an integer array when being called by a C program and performs operations on its elements without actually outputting values. For simplicity's sake, let's say I want my program to double the value of each element - Such that given the array <2, 4, 6, 8>, calling a print method in C separately from what I wrote will print . My problem is I don't understand how to alter the values of the array's elements and have those changes persist after the function doing so has finished executing. I can't return anything through register r24 , because I need to return a different number for a different purpose. My idea was, since the input on register r24 comes in as a pointer to the first element of the array, I would mov r26 r24 , associating(?) the array with the X pointer, and then ld that to another register so that I can use the X pointer to increment through the array, as in ld r18, X+ . And while I have little trouble navigating the array, I don't understand how to give my changes permanence, if that makes sense. I'm under the impression that I'm expected to use st and/or sts to solve this, but I'm struggling to understand how they work. My attempt was to reserve a pointer like Z to be associated with the input array, and every time I had a value ready to replace an old element in the array, I would write st Z+, rXX , putting the value at index Z and subsequently pointing to the next index. This didn't work, so I'm left wondering: what do I need to do to link the memory of my local registers with that of the inputs provided to the program?

asked Apr 26, 2019 at 3:24 user2709168 user2709168 117 1 1 silver badge 15 15 bronze badges

I don't understand how to give my changes permanence, if that makes sense - this sounds weird. Your array is in memory, at sequential addresses. You pass the pointer to the start address and overwrite the content of this memory with new values - what can be more permanent than that. :) It is worth specifying which C compiler you use as you will need to understand the compiler-specific 'calling convention'. And, if in doubt, you can always implement your function in C instead of asm and see what compiler produced.

Commented Apr 26, 2019 at 6:17

This didn't work, so I'm left wondering - well, as this really tells us almost nothing it is always recommended to show the actual code (by copy&paste into the question body, using Edit) Since you sound as someone with little experience there maybe dozens of reasons why something didn't work for you, no point guessing. Please also specify the exact chip that you are programming for (or the emulator if you're using one). since the input on register r24 - i'm not experienced with AVR but a pointer is 16 bits, isn't it? can't be passed via r24