Function definition: Difference between revisions

m
Line 134:
A function is nothing more than a named section of code. A <code>CALL</code> instruction will push the current value of the instruction pointer and then set the instruction pointer to that address. Execution will continue forward until a <code>RET</code> statement is encountered, at which point the top of the stack is popped into the instruction pointer register. Note that the <code>RET</code> statement assumes that the top of the stack contains the actual return address, even though in reality this may not be the case. There is no validation that the return address is correct! This is why it's important for the assembly programmer to ensure the stack is balanced at all times, otherwise your program will go running off to who knows where.
 
It's important to remember that, unlike other languages, execution of assembly code (and this is true for all assembly languages, not just the 8086) is on a purely linear path by default, much like in other "primitive" languages like BASIC, and so there is nothing stopping the instruction pointer from "falling into" subroutines. Often this can be handy if you're trying to code a variation on a function whose only difference is doing a few extra things at the beginning, but it's something you'll need to guard against, either with a return to the operating system or an infinite loop.
 
<lang asm>start:
1,489

edits