Call a function: Difference between revisions

(Call a function in PureBasic)
Line 67:
CALL (R15),(X,Y) call MULTPLIC(X,Y)
ST R0,Z Z=MULTPLIC(X,Y)</lang>
 
=={{header|6502 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.
 
<lang 6502asm>JSR myFunction</lang>
 
Function arguments are often passed in through registers, the zero page, or the stack. Given how awkward it is to work with the stack on the 6502, it's best not to use the hardware stack as a means of parameter passing. CC65 uses a software stack in the upper half of zero page, which is indexed using the X register.
<lang 6502asm>sum:
;adds the values in zero page address $00 and $01, outputs to accumulator.
LDA $00 ;load the byte stored at memory address $0000
CLC
ADC $01 ;add the byte at memory address $0001
RTS ;return</lang>
 
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.
 
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.
=={{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.
 
<lang 68000devpac>JSR myFunction</lang>
 
Function arguments are often passed in through the stack. When looking at the function in C or a similar language that compiles to 68000 Assembly, the arguments are pushed in the reverse order they are listed. Return values typically go into the D0 register if they're 32-bit or smaller. The CPU does not enforce this, so it's up to the programmer or compiler to use calling conventions to ensure compatibility between software.
 
 
 
=={{header|8086 Assembly}}==
1,489

edits