September 2017 Update
This commit is contained in:
parent
bba7bfd280
commit
ba8067c3b7
14570 changed files with 153136 additions and 63871 deletions
6
Task/Machine-code/Kotlin/machine-code-1.kotlin
Normal file
6
Task/Machine-code/Kotlin/machine-code-1.kotlin
Normal file
|
|
@ -0,0 +1,6 @@
|
|||
// mcode.def
|
||||
---
|
||||
|
||||
static inline unsigned char runMachineCode(void *code, unsigned char a, unsigned char b) {
|
||||
return ((unsigned char (*) (unsigned char, unsigned char))code)(a, b);
|
||||
}
|
||||
37
Task/Machine-code/Kotlin/machine-code-2.kotlin
Normal file
37
Task/Machine-code/Kotlin/machine-code-2.kotlin
Normal file
|
|
@ -0,0 +1,37 @@
|
|||
// Kotlin Native version 0.3
|
||||
|
||||
import kotlinx.cinterop.*
|
||||
import string.*
|
||||
import mman.*
|
||||
import mcode.*
|
||||
|
||||
fun main(args: Array<String>) {
|
||||
memScoped {
|
||||
val bytes = byteArrayOf(
|
||||
144 - 256, // Align
|
||||
144 - 256,
|
||||
106, 12, // Prepare stack
|
||||
184 - 256, 7, 0, 0, 0,
|
||||
72, 193 - 256, 224 - 256, 32,
|
||||
80,
|
||||
139 - 256, 68, 36, 4, 3, 68, 36, 8, // Rosetta task code
|
||||
76, 137 - 256, 227 - 256, // Get result
|
||||
137 - 256, 195 - 256,
|
||||
72, 193 - 256, 227 - 256, 4,
|
||||
128 - 256, 203 - 256, 2,
|
||||
72, 131 - 256, 196 - 256, 16, // Clean up stack
|
||||
195 - 256 // Return
|
||||
)
|
||||
val len = bytes.size
|
||||
val code = allocArray<ByteVar>(len)
|
||||
for (i in 0 until len) code[i] = bytes[i]
|
||||
val buf = mmap(null, len.toLong(), PROT_READ or PROT_WRITE or PROT_EXEC,
|
||||
MAP_PRIVATE or MAP_ANON, -1, 0)
|
||||
memcpy(buf, code, len.toLong())
|
||||
val a: Byte = 7
|
||||
val b: Byte = 12
|
||||
val c = runMachineCode(buf, a, b)
|
||||
munmap(buf, len.toLong())
|
||||
println("$a + $b = ${if(c >= 0) c.toInt() else c + 256}")
|
||||
}
|
||||
}
|
||||
|
|
@ -2,7 +2,7 @@ atom mem = allocate(9)
|
|||
poke(mem,{#8B,#44,#24,#04,#03,#44,#24,#08,#C3})
|
||||
integer res
|
||||
#ilASM{ mov eax,[mem]
|
||||
call :%pLoadMint -- (in case mem>#3FFFFFFF)
|
||||
call :%pLoadMint -- eax:=(int32)eax, in case mem>#3FFFFFFF
|
||||
push 12
|
||||
push 7
|
||||
call eax
|
||||
|
|
|
|||
24
Task/Machine-code/Phix/machine-code-3.phix
Normal file
24
Task/Machine-code/Phix/machine-code-3.phix
Normal file
|
|
@ -0,0 +1,24 @@
|
|||
integer res
|
||||
#ilASM{ jmp @f
|
||||
::add
|
||||
[32]
|
||||
mov eax,[esp+4]
|
||||
add eax,[esp+8]
|
||||
[64]
|
||||
mov rax,[rsp+8]
|
||||
add rax,[rsp+16]
|
||||
[]
|
||||
ret
|
||||
@@:
|
||||
push 12
|
||||
push 7
|
||||
call :add
|
||||
[32]
|
||||
add esp,8
|
||||
mov [res],eax
|
||||
[64]
|
||||
add rsp,16
|
||||
mov [res],rax
|
||||
[]
|
||||
}
|
||||
?res
|
||||
Loading…
Add table
Add a link
Reference in a new issue