Page 1 of 1

Assembler Recursion

Posted: Wed May 09, 2012 2:23 pm
by VolTeK
Am looking into designing directory support with assembler for my os. It will involve the function calling itself many times, in situations like; deleting directories. My problem, in order to do this, i want to save my variables on to the stack, and then getting them back from a call. If anyone else did this, what was your approach? Do you have a link?

My current idea sits at

Code: Select all

MyFunction: ;We take two letters, A and Z al and ah

bLetterA equ 0
bLetterZ equ 1

mov [sp-bLetterA], byte al
mov [sp-bLetterZ], byte ah 



But say i wanted to call MyFunction again?

Re: Assembler Recursion

Posted: Wed May 09, 2012 2:52 pm
by iansjack
Well, as you say, you store the variables on the stack. The conventional way to do this is to use bp to mark the top of the stack frame and then decrement the stack pointer by the appropriate number of bytes to hold the variables. The variables are then referred to as indexes from bp. This way each iteration of the function gets its own uniques set of variables.

If you want to see this in action, try looking at the assembler output produced when compiling a C function.

Re: Assembler Recursion

Posted: Wed May 09, 2012 3:43 pm
by VolTeK
Thank you.

Re: Assembler Recursion

Posted: Wed May 09, 2012 4:00 pm
by bubach
Do you really need to use local variables, without it all you need to do is make sure to leave the registers unaffected when you return from your function. If you run out of registers you could solve that too, push [var], call myself, pop [var]. But make sure you put the registers to full use first - since basically all you need to worry about is doing pushad & popad at the beginning and end of your function.

EDIT: Unless you need to follow for example C calling conventions, for that see previous answer.