Machine code: Difference between revisions

Line 236:
=={{header|M2000 Interpreter}}==
We can execute machine code, in a buffer for code. We can't push to stack and then call, we can use a buffer for data. If eax is non zero then error raised, with error number the eax number. When execute code the code buffer can't be used to write over. so we have to use a buffer for data for read/write data.
This example perform these: At Datamem(1) put 500, eax=5100, eax add Datamem(1), eax add 5, store eax to Datamem(0). We have an option to clear eax, or use it to return value as error code.
We have to leave all other registers, and stack as we found it.
 
 
<lang M2000 Interpreter>
Line 289 ⟶ 292:
}
CheckIt
</lang>
 
Using a lambda function with closures two buffers (buffers are objects in M2000 to handle memory blocks). This also add 12 +7 as the task want (but with no pushing to stack, but poke to data buffer)
 
<lang M2000 Interpreter>
Function MyAdd {
Buffer DataMem as Long*10
Buffer Code ExecMem as byte*1024
Address=0
EmbByte(0x31, 0xC0)
EmbByteLong(0x3,0x5, DataMem(0)) ' add eax, [DataMem(0)]
EmbByteLong(0x3,0x5, DataMem(1)) ' add eax, [DataMem(1)]
EmbLong(0xa3, DataMem(0)) ' mov [DataMem(0)], eax
Rem :
EmbByte(0x31, 0xC0) ' xor eax, eax
Ret() ' Return
Z=lambda ExecMem, DataMem (a as double, b as double)-> {
Return DataMem, 0:=a, 1:=b
Try ok {
Execute Code ExecMem, 0
}
If not ok then {
=Uint(Error)
} Else {
=Eval(DataMem, 0)
}
}
=Z
Sub Ret()
Return ExecMem, Address:=0xC3
Address++
End Sub
Sub EmbByte()
Return ExecMem, Address:=Number, Address+1:=Number
Address+=2
End Sub
Sub EmbLong()
Return ExecMem, Address:=Number, Address+1:=Number as Long
Address+=5
End Sub
Sub EmbByteLong()
Return ExecMem, Address:=Number, Address+1:=Number, Address+2:=Number as Long
Address+=6
End Sub
}
\\ Produce a lambda function with machine code inside
UnsingAdd=MyAdd()
Print UnsingAdd(12, 7), UnsingAdd(500, 100)
</lang>
 
Anonymous user