Assembler Recursion

Programming, for all ages and all languages.
Post Reply
User avatar
VolTeK
Member
Member
Posts: 815
Joined: Sat Nov 15, 2008 2:37 pm
Location: The Fire Nation

Assembler Recursion

Post 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?
User avatar
iansjack
Member
Member
Posts: 4687
Joined: Sat Mar 31, 2012 3:07 am
Location: Chichester, UK

Re: Assembler Recursion

Post 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.
User avatar
VolTeK
Member
Member
Posts: 815
Joined: Sat Nov 15, 2008 2:37 pm
Location: The Fire Nation

Re: Assembler Recursion

Post by VolTeK »

Thank you.
User avatar
bubach
Member
Member
Posts: 1223
Joined: Sat Oct 23, 2004 11:00 pm
Location: Sweden
Contact:

Re: Assembler Recursion

Post 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.
"Simplicity is the ultimate sophistication."
http://bos.asmhackers.net/ - GitHub
Post Reply