xlinuks
September 2nd, 2007, 11:42 AM
Hi, I'm currently learning assembly.
I have the following recursion (from a book) which computes the factorial of 4 (the code itself is not too important in our case, the main idea is that it contains an example of a recursion)::
.section .data
.section .text
.globl _start
.globl factorial
_start:
pushl $4
call factorial
addl $4, %esp
movl %eax, %ebx
movl $1, %eax
int $0x80
.type factorial, @function
factorial:
pushl %ebp
movl %esp, %ebp
movl 8(%ebp), %eax
cmpl $1, %eax
je end_factorial
decl %eax
push %eax
call factorial
movl 8(%ebp), %ebx
imull %ebx, %eax
end_factorial:
movl %ebp, %esp
popl %ebp
ret
Every time when the function gets called (in this case from inside itself) a new stack frame is created/allocated, and the old stack frame of the functions called before don't get dismissed because they will quit after the control returns to them. Does that mean that we have an explosion of portions of RAM consumption? I mean - for a while we create a bunch of stack frames. What if it wasn't 4 but 2000000000 (2 billion) ?
Am I missing some point? Please help.
I have the following recursion (from a book) which computes the factorial of 4 (the code itself is not too important in our case, the main idea is that it contains an example of a recursion)::
.section .data
.section .text
.globl _start
.globl factorial
_start:
pushl $4
call factorial
addl $4, %esp
movl %eax, %ebx
movl $1, %eax
int $0x80
.type factorial, @function
factorial:
pushl %ebp
movl %esp, %ebp
movl 8(%ebp), %eax
cmpl $1, %eax
je end_factorial
decl %eax
push %eax
call factorial
movl 8(%ebp), %ebx
imull %ebx, %eax
end_factorial:
movl %ebp, %esp
popl %ebp
ret
Every time when the function gets called (in this case from inside itself) a new stack frame is created/allocated, and the old stack frame of the functions called before don't get dismissed because they will quit after the control returns to them. Does that mean that we have an explosion of portions of RAM consumption? I mean - for a while we create a bunch of stack frames. What if it wasn't 4 but 2000000000 (2 billion) ?
Am I missing some point? Please help.