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 frequent to see small machine language routines incorporated into BASIC programs for these purposes.
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.




Note: This example, using RAM space at $2000, provides a wide range of cross-compatibility across Commodore 8-bit models, and has been tested to work on the Commodore PET (32k), VIC-20 (28k), Commodore 64, Commodore Plus/4, and Commodore 128.
Note: No memory management is performed in this example, which would protect the machine code from being overwritten by BASIC. On some models, machine code can be place in RAM locations that are not affected by BASIC, such as the range from $C000 to $CFFF (49152 to 53247) on the Commodore 64.


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.
This example, using RAM space at $2000, has been tested to work on the Commodore PET (32k), VIC-20 (28k), Commodore 64, Commodore Plus/4, and Commodore 128. Other configurations are possible unique to each machine.


<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.