As my understand, register %r12 ~ %r15 are preserved thought calling function, but as below my source code, it seems value of %r12 is changed when function f is called. The problem is at ★★ position as below asm code. Does anyone know reason?
.text
.globl f
.type f, @function
f:
pushq %rbp
movq %rsp, %rbp
subq $16, %rsp
movq %rdi, -8(%rbp)
movq -8(%rbp), %r12
movq $2, %r13
cmpq %r13, %r12
setl %r13b
andq $255,%r13
cmpq $0, %r13
je L1
movq $1, %r13 //★ increase input value + 1, and return
movq %r13, %rax
jmp L0
L1:
movq -8(%rbp), %r12
movq $1, %r14
addq %r12, %r14
movq %r14, %rax
jmp L0
L0:
addq $16, %rsp
popq %rbp
ret
.LS3:
.string "return %d\n"
.text
.globl main
.type main, @function
main:
pushq %rbp
movq %rsp, %rbp
subq $16, %rsp
movq $7, %r12 //★ store 7 to register r12
movq $10, %r15
movq %r15, %rdi //★ pass value 10 to function f
call f
movq %rax, %r15
addq %r12, %r15 //★★ expect value is 18 but it is 21, it seem value r12 is changed across call function f
movq %r15, -8(%rbp)
movq $.LS3, %rdi
movq -8(%rbp), %r15
movq %r15, %rsi
call printf
movq %rax, %r15
movq $0, %r12
movq %r12, %rax
jmp L2
L2:
addq $16, %rsp
popq %rbp
ret