I have a piece of assembly code of a function in a program I am analysing.
000081e4 <_ZN7CamTask7executeEv>:
81e4: b580 push {r7, lr}
81e6: b084 sub sp, #16
81e8: af00 add r7, sp, #0
81ea: 6078 str r0, [r7, #4]
81ec: 230a movs r3, #10
81ee: 60fb str r3, [r7, #12]
81f0: 687b ldr r3, [r7, #4]
81f2: 6ddb ldr r3, [r3, #92] ; 0x5c
81f4: 68f9 ldr r1, [r7, #12]
81f6: 4618 mov r0, r3
81f8: f7ff ffb7 bl 816a <_ZN10ImgChannel9pushValueEi>
81fc: bf00 nop
81fe: 3710 adds r7, #16
8200: 46bd mov sp, r7
8202: bd80 pop {r7, pc}
1 - is the scope of registers inside a function of assembly limited to that function for example the 4th line here 81ea: 6078 str r0, [r7, #4]
where did the value inside r0 come from ?.was it passed as an argument by another function that calls this function?. If so is it also called r0 before it is passed?
for example the line bl 816a <_ZN10ImgChannel9pushValueEi>
in the above function <_ZN7CamTask7executeEv> calls function _ZN10ImgChannel9pushValueEi below ..is r0 in line 81f6: 4618 mov r0, r3
in the above function the same as r0 in line 8170: 6078 str r0, [r7, #4]
in the below function.
0000816a <_ZN10ImgChannel9pushValueEi>:
816a: b580 push {r7, lr}
816c: b082 sub sp, #8
816e: af00 add r7, sp, #0
8170: 6078 str r0, [r7, #4]
8172: 6039 str r1, [r7, #0]
8174: 687b ldr r3, [r7, #4]
8176: 683a ldr r2, [r7, #0]
8178: 60da str r2, [r3, #12]
817a: 687b ldr r3, [r7, #4]
817c: 4618 mov r0, r3
817e: f001 ff06 bl 9f8e <_ZN7Tasking7Channel4pushEv>
8182: bf00 nop
8184: 3708 adds r7, #8
8186: 46bd mov sp, r7
8188: bd80 pop {r7, pc}
2- Can the below function change the value of r0 and then return to the calling function with r0 being updated now?
The register usage is set forth by the calling convention, which is part of the ABI. Calling conventions are very detailed in how parameters are passed, return values are returned, and which registers can be used with/without preservation. See here for more information about calling conventions. (Search for ARM.)
was it passed as an argument by another function that calls this function?
Yes
If so is it also called r0 before it is passed?
The registers are physical storage and they are always accessible to any machine code — there is no scope, but rather a sharing agreement between functions. We can think of registers as permanent physical storage, always availability, and globally accessible.
Can the below function change the value of r0 and then return to the calling function with r0 being updated now?
Yes, for simple integer and pointer arguments, functions take their first parameter in r0 (next in r1) and return values in r0 (and r1 as well if longer).