How to run LuaJIT bytecode generated from `luajit -bl`? - luajit

I have a LuaJIT function in binary bytecode format (i.e. I can run it with luajit)
I ran luajit -bl on it to convert it to text bytecode, and I rewrote some of the bytecode.
How can I run the modified text bytecode?
For example, here's an excerpt from my text bytecode
-- BYTECODE -- innerfunction:0-0
0001 IST 1
0002 JMP 2 => 0004
0003 KSHORT 1 0
0004 => GGET 2 0 ; "pairs"
0005 MOV 3 0
0006 CALL 2 4 2
0007 ISNEXT 5 => 0080
0008 => GGET 7 1 ; "type"
0009 MOV 8 5
0010 CALL 7 2 2
0011 ISNES 7 2 ; "string"
0012 JMP 7 => 0019
0013 GGET 7 2 ; "string"
0014 TGETS 7 7 3 ; "format"
0015 KSTR 8 4 ; "%q"

Related

What makes `while` loop in Ruby faster than others?

I am using this code:
require 'benchmark'
LOOPS = 100_000_000
def while_loop
i = 0
while i < LOOPS do i += 1 end
end
def times_loop
i = 0
LOOPS.times { i += 1 }
end
Benchmark.benchmark do |b|
b.report('while') { while_loop }
b.report('times') { times_loop }
end
The output is (Ruby 2.6.0):
while 2.419529 0.000000 2.419529 ( 2.426470)
times 7.225500 0.005673 7.231173 ( 7.252794)
Why is while loop faster than others?
One of the reasons is because times - it's a block. And it introduces new local variable scope. And it creates some sort of local variable, look:
RubyVM::InstructionSequence.disasm(method(:times_loop)) returns
== disasm: #<ISeq:times_loop#1.rb:23 (23,0)-(26,3)>=====================
== catch table
| catch type: break st: 0003 ed: 0014 sp: 0000 cont: 0014
== disasm: #<ISeq:block in times_loop#1.rb:25 (25,14)-(25,24)>==========
== catch table
| catch type: redo st: 0001 ed: 0010 sp: 0000 cont: 0001
| catch type: next st: 0001 ed: 0010 sp: 0000 cont: 0010
|------------------------------------------------------------------------
0000 nop ( 25)[Bc]
0001 getlocal_OP__WC__1 i[Li]
0003 putobject_OP_INT2FIX_O_1_C_
0004 opt_plus <callinfo!mid:+, argc:1, ARGS_SIMPLE>, <callcache>
0007 dup
0008 setlocal_OP__WC__1 i
0010 leave [Br]
|------------------------------------------------------------------------
local table (size: 1, argc: 0 [opts: 0, rest: -1, post: 0, block: -1, kw: -1#-1, kwrest: -1])
[ 1] i
0000 putobject_OP_INT2FIX_O_0_C_ ( 24)[LiCa]
0001 setlocal_OP__WC__0 i
0003 getinlinecache 10, <is:0> ( 25)[Li]
0006 getconstant :LOOPS
0008 setinlinecache <is:0>
0010 send <callinfo!mid:times, argc:0>, <callcache>, block in times_loop
0014 leave ( 26)[Re]
There are two local variables (setlocal_OP__WC__1 and setlocal_OP__WC__0) in this case.
In opposite while use just one, RubyVM::InstructionSequence.disasm(method(:while_loop)) returns
== disasm: #<ISeq:while_loop#1.rb:15 (15,0)-(20,3)>=====================
== catch table
| catch type: break st: 0009 ed: 0032 sp: 0000 cont: 0032
| catch type: next st: 0009 ed: 0032 sp: 0000 cont: 0006
| catch type: redo st: 0009 ed: 0032 sp: 0000 cont: 0009
|------------------------------------------------------------------------
local table (size: 1, argc: 0 [opts: 0, rest: -1, post: 0, block: -1, kw: -1#-1, kwrest: -1])
[ 1] i
0000 putobject_OP_INT2FIX_O_0_C_ ( 16)[LiCa]
0001 setlocal_OP__WC__0 i
0003 jump 17 ( 17)[Li]
0005 putnil
0006 pop
0007 jump 17
0009 getlocal_OP__WC__0 i ( 18)[Li]
0011 putobject_OP_INT2FIX_O_1_C_
0012 opt_plus <callinfo!mid:+, argc:1, ARGS_SIMPLE>, <callcache>
0015 setlocal_OP__WC__0 i
0017 getlocal_OP__WC__0 i ( 17)
0019 getinlinecache 26, <is:0>
0022 getconstant :LOOPS
0024 setinlinecache <is:0>
0026 opt_lt <callinfo!mid:<, argc:1, ARGS_SIMPLE>, <callcache>
0029 branchif 9
0031 putnil
0032 leave ( 20)[Re]
I think that the reason is not the only one.
But setting/getting new local variable slow down operation.

BaseOfCode present in PE+ executable

The MS documentation says that the BaseOfCode value is only present in PE files, not in PE+. Looking at notepad.exe with dotPeek and with PE Viewer seems to indicate that the BaseOfCode is present and consumed.
0 1 2 3 4 5 6 7 8 9 A B C D E F
0x00E0 | 5045 0000 6486 0600 6a98 8957 0000 0000
0x00F0 | 0000 0000 f000 2200 0b02 0e00 0086 0100
0x0100 | 004e 0200 0000 0000 d087 0100 0010 0000
The two bytes at 0x00F8 signify that this is a PE+ header. The BaseOfCode is the four bytes at 0x010C.
Is the documentation (and myself) incorrect or are dotPeek and PE View
incorrect?
The fact that these bytes aren't zeroed out would imply that it the bytes are significant in some way.

How to increment a hexadecimal loop in expect

How can I make a loop that increment a hexadecimal variable in expect ?
I would like something like that.
while min < max do
print hexValue
hexValue++
This:
#!/usr/bin/expect -f
set min 0x0000;
set max 0xFFFF ;
while {$min < $max } {
puts [format %04X $min]
sleep 1;
set min [expr $min+1];
}
Will output:
debian#debian:~/Desktop$ ./test.sh
0000
0001
0002
0003
0004
0005
0006
0007
0008
0009
000A
000B
000C
000D
000E
000F
0010

IEEE Float input to BCD convertion

If i use one std_logic_vector (31 downto 0) as input of my entity.
Exists any form of using this 32 bits (IEEE Format) to convert them to ASCII form ?
I have 3.14:
input ----> 0100 0000 0100 1000 1111 0101 1100 0011 (in IEEE 32 bits form)
output <---- 0011 0011 0010 1110 0011 0001 0011 0100
3 3 2 E 3 1 3 4
\__/ \__/ \__/ \__/
3 . 1 4 (in ASCII form)
The number 3.14 is only a example. May be is any number in 32 bits used as input
of my entity.

BCD to ASCII checksum

I have a very old device that I am connecting to through serial. When I am sending data it wants a checksum to be calculated with it. I add up all of the ascii valuesof the characters of the string and convert the sum to BCD. This results in illegal BCD characters such as 1011. In the only example that is provided they convert 1011 to ";". When I sent the data in the example the checksum clears fine. But when I use ";" for other illegal characters it fails. Has anyone seen the use of ";" before and if so does anyone have any idea what the values for the other illegal characters are?
edit : The Example I have:
STX 000 0010
1 011 0001
2 011 0010
3 011 0011
CR 000 1101
A 100 0001
B 100 0010
C 100 0011
CR 000 1101
EXT 000 0011
Total 10111 1011
Convert To BCD 1 0111 1011
Checksum 1 7 ;
Looks like they're using the next six ASCII characters:
DEC HEX1 HEX2 BIN1 BIN2 CHAR
48 3 0 0011 0000 0
49 3 1 0011 0001 1
50 3 2 0011 0010 2
51 3 3 0011 0011 3
52 3 4 0011 0100 4
53 3 5 0011 0101 5
54 3 6 0011 0110 6
55 3 7 0011 0111 7
56 3 8 0011 1000 8
57 3 9 0011 1001 9
58 3 A 0011 1010 :
59 3 B 0011 1011 ;
60 3 C 0011 1100 <
61 3 D 0011 1101 =
62 3 E 0011 1110 >
63 3 F 0011 1111 ?

Resources