Given:
typedef struct __attribute__((packed)) _Node{
int data;
struct _Node *left;
struct _Node *right;
} Node;
and the following assembly code which searches for a value in a tree. (Same code as How this assembly code will be translated into c?)
.section .text
.global _start
_start:
mov $8, %esi
mov $A, %rdi
call func
movq $60, %rax
movq $0, %rdi
syscall
func:
pushq %rbp
movq %rsp, %rbp
cmp (%rdi), %esi
jne continue
mov $1, %eax
jmp finish
continue: # go left
cmpq $0, 4(%rdi)
je next
pushq %rdi # 3
mov 4(%rdi), %rdi
call func
pop %rdi # 4
cmp $1, %eax
je finish
next: # go right
cmpq $0, 12(%rdi)
je fail
pushq %rdi # 1
mov 12(%rdi), %rdi
call func
pop %rdi # 2
cmp $1, %eax
je finish
fail:
mov $0, %rax
finish:
leave
ret
I want to know what will be the impact of this change and if it will cause the program to not work as expected:
adding push %rdi
right after continue.
From what I understand this will cause a problem since we are pushing some extra value to the stack so the caller for this iteration might pop a wrong value of %rdi for example the caller in this case:
pushq %rdi # 1
mov 12(%rdi), %rdi
call func
pop %rdi # 2
might pop 12+%rdi instead of poping %rdi, But I ran many tests and all of them seem to return correct value in RAX, why is that?
Note: can this line cause stack overflow too? I think the answer is probably yes.