Machine code: Difference between revisions

Added S-BASIC example
(Scheme version)
(Added S-BASIC example)
 
Line 1,482:
}
</syntaxhighlight>
 
=={{header|S-BASIC}}==
The target CPUs for the 8-bit CP/M machines which supported S-BASIC
are the 8080 and the Z80. The documented approach to
including machine code is to declare a common array sufficient in
size to hold the machine language routine. The common data area starts
at 011A hex. The byte values for the op codes could either
be directly stashed in the array, as here, or more conveniently in the case
of a longer routine, assembled at a starting address of
011Ah and then overlaid into the compiled S-BASIC program using
the debugger DDT.COM. The machine code would be invoked
using the CALL statement, which takes one of two forms. In the
first, the only argument is the starting address of the machine code.
In the second, used here, four variables following the address are mapped to
the 8080's HL, DE, BC, and A_PSW register pairs.
<syntaxhighlight lang = "BASIC">
$constant CODESIZE = 4 rem - size of our ml routine
$constant ML_LOC = 011AH rem - beginning of common data area
 
dim common byte ml_code(CODESIZE)
var hl, de, bc, a_psw = integer
 
comment
The 8080 machine language routine adds two unsigned
8-bit numbers passed as the low-order bytes in the
BC and DE registers, and leaves the result in HL.
end
 
ml_code(1) = 79H rem MOV A,C
ml_code(2) = 83H rem ADD E
ml_code(3) = 6FH rem MOV L,A
ml_code(4) = 0C9H rem RET
 
bc = 7 rem first number
de = 12 rem second number
 
rem - call the routine and display the result (returned in HL)
call (ML_LOC, hl, de, bc, a_psw)
print bc; " plus"; de; " equals"; hl
 
end
</syntaxhighlight>
{{out}}
<pre>
7 plus 12 equals 19
</pre>
 
 
=={{header|Scala}}==
13

edits