Is this the rigth way to write power and factorial functions with nasm?
sub rsp, 0x10 ; Allocate 2 bytes on the stack



The comment is not correct! 0x10 is an hexadecimal number equal to 16 in decimal.


cmp rdi, 0 ; if == 0
jne power_init



If you need to compare with zero, it's usually better to test the register to itself:

test rdi, rdi
jnz  power_init



dec edi
cmp edi, 0
je power_end



The cmp edi, 0 instruction is redundant since the dec edi instruction already provides the necessary zero condition status.




  For example, by executing the functions I make some changes in the registers and I don't know if I have to revert them to their original value a the end with push and pop.


If you write these functions and your program uses these functions, then it's up to you to decide if you deem it useful to preserve any or all of the registers. If someone else will be using your functions your comments should make it absolutely clear what registers get clobbered!



The recursive factorial function can do without the prologue/epilogue code and is a bit complicated.

; IN (esi) OUT (eax) MOD ()
; Calculate eax = esi!
Factorial:
    mov  eax, 1
    cmp  esi, eax
    je   .return       ; esi was 1 -&gt; eax = 1
    push esi           ; (1)
    dec  esi
    call Factorial     ; -&gt; EAX
    pop  esi           ; (1)
    imul eax, esi
.return:
    ret


The power function can do without the prologue/epilogue code. There's no need to put ESI on the stack. If you want you could preserve the EDI register, but that's up to you to decide.

; IN (esi,edi) OUT (eax) MOD (edi)
; Calculate eax = esi ^ edi
Power:
                      ; push edi
    mov  eax, 1
    sub  edi, eax
    jb   .return      ; edi was 0 -&gt; eax = esi^0 = 1
    mov  eax, esi
    jz   .return      ; edi was 1 -&gt; eax = esi^1 = esi
.more:
    imul eax, esi
    dec  edi
    jnz  .more
.return
                      ; pop edi
    ret