Machine code: Difference between revisions
Content added Content deleted
(Simplify Nim example (make it more similar to the original C version)) |
m (Commodore BASIC editorial changes.) |
||
Line 189: | Line 189: | ||
=={{header|Commodore BASIC}}== |
=={{header|Commodore BASIC}}== |
||
For Commodore computers, machine language routines are often written to increase speed of a program, provide functionality not otherwise provided for in BASIC, or execute code in the background. It is |
For Commodore computers, machine language routines are often written to increase speed of a program, provide functionality not otherwise provided for in BASIC, or execute code in the background. It is common to see machine language routines incorporated into BASIC programs for these purposes. |
||
The machine code shown for adding two numbers targets the MOS 65xx/85xx architecture (6502, 6510, etc.) and is given as follows: |
The machine code shown for adding two numbers targets the MOS 65xx/85xx architecture (6502, 6510, etc.) and is given as follows: |
||
Line 204: | Line 204: | ||
# The machine code is called for execution at $2003 with the SYS statement. (Note, since we are using '''AD'''d with '''C'''arry, we should '''CL'''ear the '''C'''arry flag first to be sure the result is accurate.) |
# The machine code is called for execution at $2003 with the SYS statement. (Note, since we are using '''AD'''d with '''C'''arry, we should '''CL'''ear the '''C'''arry flag first to be sure the result is accurate.) |
||
# The result is stored in location $2002 (8194). |
# The result is stored in location $2002 (8194). |
||
# The machine code must issue a RTS (ReTurn from Subroutine) to allow BASIC to continue. |
# The machine code ''must'' issue a RTS (ReTurn from Subroutine) to allow BASIC to continue. |
||
⚫ | |||
⚫ | |||
⚫ | No memory management is performed in this example, which would protect the machine code from being overwritten by BASIC. There are more optimal memory locations for the machine code to reside that are not affected by BASIC, however, the locations of such are unique to each model. For example, the range from $C000 to $CFFF (49152 to 53247) on the Commodore 64 is one such ideal location since BASIC memory ends at $9FFF, and there is no overlapping ROM to interfere. |
||
⚫ | |||
<lang gwbasic>10 print chr$(147); |
<lang gwbasic>10 print chr$(147); |
||
Line 232: | Line 232: | ||
8205 data 96 :rem rts</lang> |
8205 data 96 :rem rts</lang> |
||
'''Notes''' |
'''Notes about Program''' |
||
* Line 20 checks to see if the ML routine has already been loaded into memory. If not, visit the loader routine. |
* Line 20 checks to see if the ML routine has already been loaded into memory. If not, visit the loader routine. |