Raoul722
Raoul722

Reputation: 1264

Stack management in assembly code

I am noob in assembly language and I am trying to understand an assembly code. All seems clear to me except a few operations and specifically these on the stack pointer register (r1).

Here is the code:

#define A           r2
#define B           r3
#define R           r6


.section .text, "ax"
.global u32mul16

u32mul16:
    ; R = AxB :
    ;       A <= input  r2 (16bits expected)
    ;       B <= input  r3 (16bits expected)
    ;       R <= output r6 (32bits)

    ; stack update and save
    sub     r1, #0x10
    stq     r4, [r1]

    ; multiplication computation
             ...
    ; after computation

    ; prepare the 'return' value
    mov     r2, R

    ldq     r4, [r1]
    add     r1, #0x10

    jmp     [r15]

Not that all registers (r0,...,r15) are 32-bit registers.

My question is the following: why the operations

sub r1, #0x10
stq r4, [r1]

at the beginning and

ldq r4, [r1]
add r1,#0x10

at the end are needed? I don't understand what it does.

Also, is there any reason to put the result in the register r2?

Upvotes: 0

Views: 176

Answers (1)

Jester
Jester

Reputation: 58822

As Michael said, you have to find out what the procedure call standard for that architecture is.

The sub allocates space from the stack, which is needed for storing r4 and possibly also used for local variables in the computation code that is omitted. Presumably r4 is also modified in that block of code, and the calling convention may designate that as a callee-saved register, hence its value must be preserved. If you don't need locals and you don't modify r4, then you most probably do not need those instructions.

Similarly, the calling convention specifies which register to use for passing and returning arguments.

Upvotes: 1

Related Questions