Data update
This commit is contained in:
parent
4bb20c9b71
commit
cbaf4c4b64
12390 changed files with 318560 additions and 27248 deletions
68
Task/Machine-code/Ada/machine-code.adb
Normal file
68
Task/Machine-code/Ada/machine-code.adb
Normal file
|
|
@ -0,0 +1,68 @@
|
|||
-- Linux x86_64 with GNAT 15.1.0
|
||||
|
||||
with Ada.Unchecked_Conversion;
|
||||
with Ada.Text_IO;
|
||||
with Interfaces.C;
|
||||
with System;
|
||||
|
||||
-- Pull in primitive operations for unsigned int types to combine
|
||||
-- flags with "or"
|
||||
use type Interfaces.C.unsigned;
|
||||
|
||||
procedure Machine_Code_Add is
|
||||
-- The mmap interface provided by GNAT is limited, expecting only
|
||||
-- reading and writing files rather than grabbing anonymous
|
||||
-- executable memory regions. At that point it's easier just to
|
||||
-- define our own interface.
|
||||
|
||||
PROT_READ : constant Interfaces.C.unsigned := 1;
|
||||
PROT_WRITE : constant Interfaces.C.unsigned := 2;
|
||||
PROT_EXEC : constant Interfaces.C.unsigned := 4;
|
||||
MAP_PRIVATE : constant Interfaces.C.unsigned := 2;
|
||||
MAP_ANON : constant Interfaces.C.unsigned := 32;
|
||||
|
||||
function Mmap (addr : System.Address;
|
||||
len : Interfaces.C.size_t;
|
||||
prot : Interfaces.C.unsigned;
|
||||
flags : Interfaces.C.unsigned;
|
||||
fd : Interfaces.C.unsigned;
|
||||
off : Interfaces.C.long) return System.Address;
|
||||
pragma Import (C, Mmap, "mmap");
|
||||
|
||||
function Munmap (addr : System.Address;
|
||||
len : Interfaces.C.size_t) return Interfaces.C.int;
|
||||
pragma Import (C, Munmap, "munmap");
|
||||
|
||||
function Memcpy (dest, src : System.Address; n : Interfaces.C.size_t)
|
||||
return System.Address;
|
||||
pragma Import (C, Memcpy, "memcpy");
|
||||
|
||||
type Add_Fn is access
|
||||
function (A, B : Interfaces.C.long) return Interfaces.C.long
|
||||
with Convention => C;
|
||||
|
||||
function To_Add_Fn is
|
||||
new Ada.Unchecked_Conversion (System.Address, Add_Fn);
|
||||
|
||||
Code : constant array (Positive range 1 .. 7)
|
||||
of Interfaces.C.unsigned_char :=
|
||||
(16#48#, 16#89#, 16#F8#, 16#48#, 16#01#, 16#F0#, 16#C3#);
|
||||
|
||||
Len : constant Interfaces.C.size_t := Code'Length;
|
||||
|
||||
Mem : constant System.Address :=
|
||||
Mmap (System.Null_Address,
|
||||
Len,
|
||||
PROT_READ or PROT_WRITE or PROT_EXEC,
|
||||
MAP_PRIVATE or MAP_ANON,
|
||||
-1,
|
||||
0);
|
||||
|
||||
Add : constant Add_Fn := To_Add_Fn (Mem);
|
||||
Dummy : System.Address := Memcpy (Mem, Code'Address, Len);
|
||||
Dummy2 : Interfaces.C.int;
|
||||
Sum : Interfaces.C.long := Add (12, 7);
|
||||
begin
|
||||
Ada.Text_IO.Put_Line ("12 + 7 =" & Interfaces.C.long'Image (Sum));
|
||||
Dummy2 := Munmap (Mem, Len);
|
||||
end Machine_Code_Add;
|
||||
10
Task/Machine-code/COBOL/machine-code-1.cob
Normal file
10
Task/Machine-code/COBOL/machine-code-1.cob
Normal file
|
|
@ -0,0 +1,10 @@
|
|||
pushq %rbp
|
||||
movq %rsp, %rbp
|
||||
movl %edi, -0x4(%rbp)
|
||||
movl %esi, -0x8(%rbp)
|
||||
movl -0x4(%rbp), %esi
|
||||
addl -0x8(%rbp), %esi
|
||||
movl %esi, -0xc(%rbp)
|
||||
movl -0xc(%rbp), %eax
|
||||
popq %rbp
|
||||
retq
|
||||
71
Task/Machine-code/COBOL/machine-code-2.cob
Normal file
71
Task/Machine-code/COBOL/machine-code-2.cob
Normal file
|
|
@ -0,0 +1,71 @@
|
|||
>>SOURCE FORMAT IS FIXED
|
||||
IDENTIFICATION DIVISION.
|
||||
PROGRAM-ID. MC.
|
||||
DATA DIVISION.
|
||||
WORKING-STORAGE SECTION.
|
||||
01 INSTRUCTIONS.
|
||||
03 USAGE BINARY-CHAR UNSIGNED VALUE H'55'.
|
||||
03 USAGE BINARY-CHAR UNSIGNED VALUE H'48'.
|
||||
03 USAGE BINARY-CHAR UNSIGNED VALUE H'89'.
|
||||
03 USAGE BINARY-CHAR UNSIGNED VALUE H'E5'.
|
||||
03 USAGE BINARY-CHAR UNSIGNED VALUE H'89'.
|
||||
03 USAGE BINARY-CHAR UNSIGNED VALUE H'7D'.
|
||||
03 USAGE BINARY-CHAR UNSIGNED VALUE H'FC'.
|
||||
03 USAGE BINARY-CHAR UNSIGNED VALUE H'89'.
|
||||
03 USAGE BINARY-CHAR UNSIGNED VALUE H'75'.
|
||||
03 USAGE BINARY-CHAR UNSIGNED VALUE H'F8'.
|
||||
03 USAGE BINARY-CHAR UNSIGNED VALUE H'8B'.
|
||||
03 USAGE BINARY-CHAR UNSIGNED VALUE H'75'.
|
||||
03 USAGE BINARY-CHAR UNSIGNED VALUE H'FC'.
|
||||
03 USAGE BINARY-CHAR UNSIGNED VALUE H'03'.
|
||||
03 USAGE BINARY-CHAR UNSIGNED VALUE H'75'.
|
||||
03 USAGE BINARY-CHAR UNSIGNED VALUE H'F8'.
|
||||
03 USAGE BINARY-CHAR UNSIGNED VALUE H'89'.
|
||||
03 USAGE BINARY-CHAR UNSIGNED VALUE H'75'.
|
||||
03 USAGE BINARY-CHAR UNSIGNED VALUE H'F4'.
|
||||
03 USAGE BINARY-CHAR UNSIGNED VALUE H'8B'.
|
||||
03 USAGE BINARY-CHAR UNSIGNED VALUE H'45'.
|
||||
03 USAGE BINARY-CHAR UNSIGNED VALUE H'F4'.
|
||||
03 USAGE BINARY-CHAR UNSIGNED VALUE H'5D'.
|
||||
03 USAGE BINARY-CHAR UNSIGNED VALUE H'C3'.
|
||||
01 MMAP.
|
||||
03 MMAP-ADDR USAGE POINTER VALUE NULL.
|
||||
03 MMAP-LEN USAGE BINARY-LONG UNSIGNED VALUE 24.
|
||||
03 MMAP-PROT USAGE BINARY-INT VALUE H'0007'.
|
||||
03 MMAP-FLAGS USAGE BINARY-INT VALUE H'1002'.
|
||||
03 MMAP-FD USAGE BINARY-INT VALUE -1.
|
||||
03 MMAP-OFFSET USAGE BINARY-LONG VALUE 0.
|
||||
03 CODE-PTR USAGE PROCEDURE-POINTER.
|
||||
01 ARG-A USAGE BINARY-INT VALUE 7.
|
||||
01 ARG-B USAGE BINARY-INT VALUE 12.
|
||||
01 RESULT USAGE BINARY-INT.
|
||||
LINKAGE SECTION.
|
||||
01 MACHINE-CODE PIC X(24).
|
||||
PROCEDURE DIVISION.
|
||||
MAIN SECTION.
|
||||
PERFORM SET-UP.
|
||||
CALL CODE-PTR USING
|
||||
BY VALUE ARG-A
|
||||
BY VALUE ARG-B
|
||||
RETURNING RESULT.
|
||||
DISPLAY RESULT.
|
||||
PERFORM TEAR-DOWN.
|
||||
STOP RUN.
|
||||
|
||||
SET-UP SECTION.
|
||||
CALL 'mmap' USING
|
||||
BY VALUE MMAP-ADDR
|
||||
BY VALUE MMAP-LEN
|
||||
BY VALUE MMAP-PROT
|
||||
BY VALUE MMAP-FLAGS
|
||||
BY VALUE MMAP-FD
|
||||
BY VALUE MMAP-OFFSET
|
||||
RETURNING CODE-PTR.
|
||||
SET ADDRESS OF MACHINE-CODE TO CODE-PTR.
|
||||
MOVE INSTRUCTIONS TO MACHINE-CODE.
|
||||
|
||||
TEAR-DOWN SECTION.
|
||||
SET ADDRESS OF MACHINE-CODE TO NULL.
|
||||
CALL 'munmap' USING
|
||||
BY VALUE CODE-PTR
|
||||
BY VALUE MMAP-LEN.
|
||||
|
|
@ -24,7 +24,7 @@ Module Checkit {
|
|||
Print m=5605
|
||||
Print Error=0, ok=False
|
||||
|
||||
Print Eval(DataMem, 0)=5605, Eval(DataMem, 0)
|
||||
Print DataMem[0]=5605, Eval(DataMem, 0)
|
||||
\\ sub used as Exit here
|
||||
Sub Ret()
|
||||
Return ExecMem, Address:=0xC3
|
||||
|
|
|
|||
|
|
@ -1,42 +1,38 @@
|
|||
Function MyAdd {
|
||||
Buffer DataMem as Long*2
|
||||
Buffer Code ExecMem as byte*32
|
||||
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
|
||||
=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)
|
||||
}
|
||||
}
|
||||
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
|
||||
Buffer DataMem as Long*2
|
||||
Buffer Code ExecMem as byte*32
|
||||
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
|
||||
=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 =DataMem[0]
|
||||
}
|
||||
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
|
||||
UnsingedAdd=MyAdd()
|
||||
Print UnsingedAdd(12, 7), UnsingedAdd(500, 100)
|
||||
Print UnsingedAdd(12, 7)=19, UnsingedAdd(0x8000_0000, 0x8000_0001)=1
|
||||
|
|
|
|||
37
Task/Machine-code/Perl/machine-code.pl
Normal file
37
Task/Machine-code/Perl/machine-code.pl
Normal file
|
|
@ -0,0 +1,37 @@
|
|||
#!/usr/bin/perl
|
||||
use strict;
|
||||
use warnings;
|
||||
|
||||
use Inline "C";
|
||||
printf("%d\n", add(7, 12));
|
||||
|
||||
__END__
|
||||
__C__
|
||||
#include <stdio.h>
|
||||
#include <sys/mman.h>
|
||||
#include <string.h>
|
||||
|
||||
int add (int a, int b)
|
||||
{
|
||||
#ifdef __x86_64__
|
||||
char code[] = {0x55, 0x48, 0x89, 0xe5, 0x89, 0x7d,
|
||||
0xfc, 0x89, 0x75, 0xf8, 0x8b, 0x75,
|
||||
0xfc, 0x03, 0x75, 0xf8, 0x89, 0x75,
|
||||
0xf4, 0x8b, 0x45, 0xf4, 0x5d, 0xc3};
|
||||
#else
|
||||
char code[] = {0x8B, 0x44, 0x24, 0x4, 0x3, 0x44, 0x24, 0x8, 0xC3};
|
||||
#endif
|
||||
|
||||
void *buf;
|
||||
int c;
|
||||
/* copy code to executable buffer */
|
||||
buf = mmap (0,sizeof(code),PROT_READ|PROT_WRITE|PROT_EXEC,
|
||||
MAP_PRIVATE|MAP_ANON,-1,0);
|
||||
|
||||
memcpy (buf, code, sizeof(code));
|
||||
/* run code */
|
||||
c = ((int (*) (int, int))buf)(a, b);
|
||||
/* free buffer */
|
||||
munmap (buf, sizeof(code));
|
||||
return c;
|
||||
}
|
||||
|
|
@ -1,7 +1,5 @@
|
|||
(notonline)-->
|
||||
<span style="color: #004080;">atom</span> <span style="color: #000000;">mem</span> <span style="color: #0000FF;">=</span> <span style="color: #000000;">allocate</span><span style="color: #0000FF;">(</span><span style="color: #000000;">9</span><span style="color: #0000FF;">)</span>
|
||||
<span style="color: #000000;">poke</span><span style="color: #0000FF;">(</span><span style="color: #000000;">mem</span><span style="color: #0000FF;">,{</span><span style="color: #000000;">#8B</span><span style="color: #0000FF;">,</span><span style="color: #000000;">#44</span><span style="color: #0000FF;">,</span><span style="color: #000000;">#24</span><span style="color: #0000FF;">,</span><span style="color: #000000;">#04</span><span style="color: #0000FF;">,</span><span style="color: #000000;">#03</span><span style="color: #0000FF;">,</span><span style="color: #000000;">#44</span><span style="color: #0000FF;">,</span><span style="color: #000000;">#24</span><span style="color: #0000FF;">,</span><span style="color: #000000;">#08</span><span style="color: #0000FF;">,</span><span style="color: #000000;">#C3</span><span style="color: #0000FF;">})</span>
|
||||
<span style="color: #008080;">constant</span> <span style="color: #000000;">mfunc</span> <span style="color: #0000FF;">=</span> <span style="color: #000000;">define_c_func</span><span style="color: #0000FF;">({},</span><span style="color: #000000;">mem</span><span style="color: #0000FF;">,{</span><span style="color: #000000;">C_INT</span><span style="color: #0000FF;">,</span><span style="color: #000000;">C_INT</span><span style="color: #0000FF;">},</span><span style="color: #000000;">C_INT</span><span style="color: #0000FF;">)</span>
|
||||
<span style="color: #0000FF;">?</span><span style="color: #000000;">c_func</span><span style="color: #0000FF;">(</span><span style="color: #000000;">mfunc</span><span style="color: #0000FF;">,{</span><span style="color: #000000;">12</span><span style="color: #0000FF;">,</span><span style="color: #000000;">7</span><span style="color: #0000FF;">})</span>
|
||||
<span style="color: #000000;">free</span><span style="color: #0000FF;">(</span><span style="color: #000000;">mem</span><span style="color: #0000FF;">)</span>
|
||||
<!--
|
||||
atom mem = allocate(9)
|
||||
poke(mem,{#8B,#44,#24,#04,#03,#44,#24,#08,#C3})
|
||||
constant mfunc = define_c_func({},mem,{C_INT,C_INT},C_INT)
|
||||
?c_func(mfunc,{12,7})
|
||||
free(mem)
|
||||
|
|
|
|||
|
|
@ -1,14 +1,12 @@
|
|||
-->
|
||||
<span style="color: #004080;">atom</span> <span style="color: #000000;">mem</span> <span style="color: #0000FF;">=</span> <span style="color: #000000;">allocate</span><span style="color: #0000FF;">(</span><span style="color: #000000;">9</span><span style="color: #0000FF;">)</span>
|
||||
<span style="color: #000000;">poke</span><span style="color: #0000FF;">(</span><span style="color: #000000;">mem</span><span style="color: #0000FF;">,{</span><span style="color: #000000;">#8B</span><span style="color: #0000FF;">,</span><span style="color: #000000;">#44</span><span style="color: #0000FF;">,</span><span style="color: #000000;">#24</span><span style="color: #0000FF;">,</span><span style="color: #000000;">#04</span><span style="color: #0000FF;">,</span><span style="color: #000000;">#03</span><span style="color: #0000FF;">,</span><span style="color: #000000;">#44</span><span style="color: #0000FF;">,</span><span style="color: #000000;">#24</span><span style="color: #0000FF;">,</span><span style="color: #000000;">#08</span><span style="color: #0000FF;">,</span><span style="color: #000000;">#C3</span><span style="color: #0000FF;">})</span>
|
||||
<span style="color: #004080;">integer</span> <span style="color: #000000;">res</span>
|
||||
#ilASM{ mov eax,[mem]
|
||||
call :%pLoadMint -- eax:=(int32)eax, in case mem>#3FFFFFFF
|
||||
push 12
|
||||
push 7
|
||||
call eax
|
||||
add esp,8
|
||||
mov [res],eax }
|
||||
<span style="color: #0000FF;">?</span><span style="color: #000000;">res</span>
|
||||
<span style="color: #000000;">free</span><span style="color: #0000FF;">(</span><span style="color: #000000;">mem</span><span style="color: #0000FF;">)</span>
|
||||
<!--
|
||||
atom mem = allocate(9)
|
||||
poke(mem,{#8B,#44,#24,#04,#03,#44,#24,#08,#C3})
|
||||
integer res
|
||||
#ilASM{ mov eax,[mem]
|
||||
call :%pLoadMint -- eax:=(int32)eax, in case mem>#3FFFFFFF
|
||||
push 12
|
||||
push 7
|
||||
call eax
|
||||
add esp,8
|
||||
mov [res],eax }
|
||||
?res
|
||||
free(mem)
|
||||
|
|
|
|||
|
|
@ -1,26 +1,24 @@
|
|||
-->
|
||||
<span style="color: #004080;">integer</span> <span style="color: #000000;">res</span>
|
||||
#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
|
||||
[]
|
||||
}
|
||||
<span style="color: #0000FF;">?</span><span style="color: #000000;">res</span>
|
||||
<!--
|
||||
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
|
||||
|
|
|
|||
|
|
@ -1,17 +1,15 @@
|
|||
-->
|
||||
<span style="color: #004080;">integer</span> <span style="color: #000000;">res</span>
|
||||
#ilASM{
|
||||
[32]
|
||||
mov eax,12
|
||||
mov edx,7
|
||||
add eax,edx
|
||||
mov [res],eax
|
||||
[64]
|
||||
mov rax,12
|
||||
mov rdx,7
|
||||
add rax,rdx
|
||||
mov [res],rax
|
||||
[]
|
||||
}
|
||||
<span style="color: #0000FF;">?</span><span style="color: #000000;">res</span>
|
||||
<!--
|
||||
integer res
|
||||
#ilASM{
|
||||
[32]
|
||||
mov eax,12
|
||||
mov edx,7
|
||||
add eax,edx
|
||||
mov [res],eax
|
||||
[64]
|
||||
mov rax,12
|
||||
mov rdx,7
|
||||
add rax,rdx
|
||||
mov [res],rax
|
||||
[]
|
||||
}
|
||||
?res
|
||||
|
|
|
|||
Loading…
Add table
Add a link
Reference in a new issue