Call a function: Difference between revisions

m
Line 84:
The return value is usually stored in the accumulator if it will fit in 8 bits. If not, it's often stored in a dedicated section of the zero page. Since the 6502 has very few registers and all are 8-bit, it's common to set aside a few zero page memory addresses for holding 16-bit return values.
 
Statements like <code>PRINT SIN(45)</code>, or expressions where you assign a variable to a function's output don't exist in assembly. You have to perform the functions individually, working from the inside out, and process them one at a time. In other words, a BASIC statement like <code>PRINT SIN(45)</code> would have to compute <code>SIN(45)</code> first, then pass the return value to <code>PRINT</code>. This is just as true with modern CPUs as it was with the 6502.
The closest thing 6502 has to "built-in functions" are the interrupt vectors whose pointers are stored at the very end of memory. They are, in order: Non-maskable interrupt (NMI), reset, and IRQ (Interrupt Request). They are no different than other functions except they end in <code>RTI</code> rather than <code>RTS</code>. With "bare-metal programming" like on the NES this is all you have, but most computers of the 80s had some sort of kernel or operating system that had pre-defined functions you could use simply by <code>JSR</code>ing their memory address. The actual memory locations of these, and what they did, varies by implementation.
 
The closest thing 6502 has to true "built-in functions" are the interrupt vectors whose pointers are stored at the very end of memory. They are, in order: Non-maskable interrupt (NMI), reset, and IRQ (Interrupt Request). They are no different than other functions except they end in <code>RTI</code> rather than <code>RTS</code>. With "bare-metal programming" like on the NES this is all you have, but most computers of the 80s had some sort of kernel or operating system that had pre-defined functions you could use simply by <code>JSR</code>ing their memory address. The actual memory locations of these, and what they did, varies by implementation.
 
=={{header|68000 Assembly}}==
To call a function, you use <code>JSR</code> followed by the pointer to its beginning. Most of the time this will be a labeled line of code that your assembler will convert to an actual memory address for you during the assembly process.
1,489

edits