; for a tutorial, look after the code.. ;) .model tiny .386 .data .code org 100h START: jmp @go ; params c:word; p2 proc near mov bp,sp ; still using BP for indexing into SS mov ax,ss:[bp+2] ; takes 30h+50h off the stack, NO POP pop ax ; pops caller-address into ax. stack: FFF6h add sp,2 ; two bytes parameter, clean it up by changing ; SP. remember that the stack grow downwards, so ; we ADD, not SUB ; stack: FFF8h push ax ; push callers address back on. stack: FFF6h ret ; return from procedure. stack: FFF8h p2 endp ; params: a,b:word p1 proc near mov bp,sp ; use BP for indexing into SS mov ax,ss:[bp+2] ; puts 30h into AX from the stack, NO POP mov bx,ss:[bp+4] ; puts 50h into BX from the stack, NO POP add ax,bx ; does something.. whatever.. ;push bp <- we could do that.. but we dont in this prog:) push ax ; push parameter for p2 on... stack: FFF6h call p2 ; stack FFF4h ;pop bp pop ax ; pops caller-address into ax. stack: FFFAh add sp,4 ; we had 4 bytes of parametres here, clean up. ; stack: FFFEh push ax ; push caller-address back on. stack: FFFCh ret ; return from procedure. stack: FFFEh p1 endp @go: ; stack: FFFEh push 50h ; parametres for p1. stack: FFFCh push 30h ; stack: FFFAh call p1 ; stack: FFF8h ; and we're back. stack: FFFEh, like nothing happened :) mov ax,4c00h int 21h END START ;------ Explaining a bit more ------; this .asm is made for tasm 2.01, and organized as a .com file. To compile it: c:\>tasm proctut c:\>tlink -t proctut -Calling with parameters This demonstrates exactly how to pass parametres to procedures via the stack. When your .com starts, the Stack Pointer (SP) is FFFEh. Before you call a procedure, simply push the parametres. Remember to only push words or dwords, bytes wont work. When the pushing is done, you call the procedure. A call (near, by default) pushes 1 word onto the stack before it jumps to the procedure (a far call pushes 2 words). That word is the offset of the next instruction after the call, in other words where the procedure should return -Handling the parameters in the procedure As mentioned, there is 1 word of data on the stack before the parametres. So the first word parameter on the stack is on ss:sp+2. If you wonder why we add two instead of subtract, just remember that the stack grows DOWNWARDS (it starts on FFFEh and subtracts when you push data). In the procedure, we'll use BP for indexing into the stack, we'll want to keep SP as it is. So, you MOV bp,sp, and then you can MOV ax,ss:[bp+2] to put the first parameter into AX. When that is done, you can code whatever you want, as long SP is not changed when the procedure should return. So how to make it return? Simple. I told you that CALL pushes the offset of the intruction after the CALL onto the stack, AFTER your parameters. So you might think you can just RET right away... No, it's not that simple ;) But almost. POP ax, to get that offset. Now the only thing on the stack is your parameters. Clean them up simply by ADD sp,SizeOfParameters. When that is done, PUSH ax, to put the return-offset back on the stack. Now you can RET, to get back from the procedure. The RET will take that offset away from the stack, so everything is cleaned up afterwards. One more thing... it's a good idea to put a comment before a procedure, saying what parameters it takes! -Calling a procedure from a procedure ...is very simple. You have to save BP (unless you know you wont need it later in your procedure). So, PUSH bp. Then PUSH your parameters, and call the procedure. Since you've built the other procedure correctly, it will clean up the stack before it returns. Therefore all you have to do after the CALL, is POP bp. -more? just a note... POP eats cycles :) you should avoid it.. I'll leave it up to you to figure out how ;) And one more thing... Simply passing parameters in registers is easier and faster, if it's possible. But you can't pass very many parameters that way, of course. good luck! send me a mail if you didn't understand this, or have comments. solvesle at gmail com http://loonix.technigga.net/osdev.php Feel free to distribute this file, as long as you don't change anything (and please send me a mail if you put it on your site, I wanna keep track of it:) )