Jump to content

Machine code: Difference between revisions

Added Commodore BASIC with MOS 65xx/85xx machine code.
m (Doh.. Grammar...)
(Added Commodore BASIC with MOS 65xx/85xx machine code.)
Line 186:
{{out}}
+0000000019
 
=={{header|Commodore BASIC}}==
 
For Commodore computers, machine language routines were often written to increase speed of a program, provide functionality not otherwise provided for in BASIC, or execute code in the background. It was frequent to see small 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:
 
<lang>Assembly Hexadecimal Decimal
 
CLC 18 24
LDA $2000 AD 00 20 173 0 32
ADC $2001 6D 01 20 109 1 32
STA $2002 8D 02 20 141 2 32
RTS 60 96</lang>
 
# The numbers to be added must be poked into locations $2000 and $2001 (8192 and 8193)
# 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 machine code must issue a RTS (ReTurn from Subroutine) to allow BASIC to continue.
 
 
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.
 
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);
15 ml=8192
20 if peek(ml+3)<>173 and peek(ml+12)<>96 then gosub 100
30 for ad=ml to ml+2:poke ad,0:next
40 poke ml,7:poke ml+1,12
50 print "before:";peek(ml+2)
60 sys ml+3
70 print "after:";peek(ml+2)
80 end
100 rem machine language loader
105 for ad=ml+3 to ml+12
110 read b
115 poke ad,b
120 next
125 return
2003 data 24 :rem clc
2004 data 173,0,32 :rem lda $2000
2007 data 109,1,32 :rem adc $2001
2010 data 141,2,32 :rem sta $2002
2013 data 96 :rem rts</lang>
 
{{Output}}
<pre>
Before: 0
After: 19
</pre>
 
=={{header|Common Lisp}}==
113

edits

Cookies help us deliver our services. By using our services, you agree to our use of cookies.