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
Related
A binary file opened by Sublime HexViewer is like
ffff 0000 0001 0002 0003 0004 0005 0006 0007 0008 0009 000a
000b 000c 000d 000e 000f 0010 0011 0012 0013 0014 0015 0016
0017 0018 0019 .... eeee
Which has 12 columns. But I want to make all my binary files show like this when opened
ffff
0000 00010002
0003 00040005
0006 00070008
0009 000a000b
....
eeee
Is this possible?
I've been trying to learn assembly language (MIPS32) on my own, and I've been following this free online curriculum that teaches it.
There's an exercise that asks me to copy ori $8, $6, 0x20 into $9 by using only or, ori, and shift. Unfortunately, an answer isn't provided, and I have no idea how to do this. Can somebody help me or point me in the right direction? Thank you.
First you have to inspect the format used for the ori instruction:
0011 01ss ssst tttt iiii iiii iiii iiii
Source: MIPS Instruction Reference
sssss the destination register which is $8 = 01000
ttttt the source register which is $6 = 00110
ii... the immediate operand which is 0x20 = ...10 0000
The resulting instruction looks as follows:
0011 01ss ssst tttt iiii iiii iiii iiii
0011 0101 0000 0110 0000 0000 0010 0000
Which we convert to hexadecimal for use in our code: 0x35060020
Since the ori instruction accepts 16 bits for an immediate operand we can combine it with a simple left-shift to populate the higher 16 bits first with 0x3506 and then add the lower 16 bits with another ori instruction.
ori $9, $0, 0x3506 # insert upper 16 bits of instruction
# 0000 0000 0000 0000 0011 0101 0000 0110
sll $9, $9, 0x10 # shift 16 bits to higher part of register
# 0011 0101 0000 0110 0000 0000 0000 0000
ori $9, $9, 0x0020 # insert lower 16 bits of instruction
# 0011 0101 0000 0110 0000 0000 0010 0000
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.
I need help for this problem, I have multiple rows in a text file (.txt) as the example
0001 0002 0005 0007
0004 0005 0009 0004
0004 0009 0006 0004
xxxx 0006 zzzz kkkk
xxxx zzzz ..........
I must check if the third element of each row (0005 in fist row) is the same of second element of next row (0005 in second row), I must continue to check for each row (0009 in second row with 0009 in third row)
How can i check if the value are equal ? maybe put in array ?
thanks for answer
Keeping an array is overkill. Just keep two variables, one which has the elements of one line in an array and one with has the elements of the next. Something like:
Option Explicit
Dim fso,f,problem, i, lineone, linetwo
Set fso = CreateObject("Scripting.FileSystemObject")
Set f = fso.OpenTextFile(WScript.Arguments.Item(0),1,true)
lineone = Split(Trim(f.ReadLine))
i = 1
problem = false
Do While f.AtEndOfStream <> True
linetwo = Split(Trim(f.ReadLine))
i = i+1
If lineone(2) <> linetwo(1) Then
MsgBox "Problem at line " & i
problem = True
Exit Do
End If
lineone = linetwo 'for next pass through the loop
Loop
f.close
If Not problem Then MsgBox "File is Okay"
When I feed the script the following file:
0001 0002 0005 0007
0004 0005 0009 0004
0004 0009 0006 0004
0004 0006 0006 0004
0004 0006 0006 0008
The script prints "File is Okay"
But when I feed it this file:
0001 0002 0005 0007
0004 0005 0009 0004
0004 0006 0006 0004
0004 0006 0006 0004
0004 0006 0006 0008
The script prints "Problem at line 3"
I didn't do any error checking to handle the case where there are fewer than two lines in the file or some of the lines lack the expected number of entries. Obviously, you should make this more robust.
Why is awk not working for the following command instead of sed:
su -c "stdbuf -i0 -o0 -e0 od --width=144 -x /dev/input/event3 | sed 's%^\([a-z0-9]\+ \)\{11\}%%;s%\(....\).*%\1%'"
This prints at every keypress the USB HID ID number of the key that was pressed.
Example output (0028 is for Return and 00e4 for Right-Ctrl):
0028
0028
0028
0028
0028
00e4
00e4
00e4
This are two lines of output without sed/cut/awk filtering when pressing Return two times. The USB HID ID number is in column 12:
0000000 2d6f 511e 0000 0000 051b 0007 0000 0000 0004 0004 0028 0007 2d6f 511e 0000 0000 051d 0007 0000 0000 0001 001c 0000 0000 2d6f 511e 0000 0000 051e 0007 0000 0000 0000 0000 0000 0000 2d73 511e 0000 0000 a150 0007 0000 0000 0004 0004 0028 0007 2d73 511e 0000 0000 a153 0007 0000 0000 0001 001c 0001 0000 2d73 511e 0000 0000 a154 0007 0000 0000 0000 0000 0000 0000
0000220 2d73 511e 0000 0000 9b5a 0008 0000 0000 0004 0004 0028 0007 2d73 511e 0000 0000 9b5d 0008 0000 0000 0001 001c 0000 0000 2d73 511e 0000 0000 9b5e 0008 0000 0000 0000 0000 0000 0000 2d74 511e 0000 0000 4f90 0005 0000 0000 0004 0004 0028 0007 2d74 511e 0000 0000 4f93 0005 0000 0000 0001 001c 0001 0000 2d74 511e 0000 0000 4f94 0005 0000 0000 0000 0000 0000 0000
I tried it with
su -c "stdbuf -i0 -o0 -e0 od --width=144 -x /dev/input/event3 | cut -d' ' -f12"
and it also works. But it only shows the keys pressed after the next two keypresses. stdbuf did not help here, although it should adjusts standard input/output/error stream buffering to 0.
My mawk command looked like this:
su -c "stdbuf -i0 -o0 -e0 od --width=$((48*3)) -x /dev/input/event3 | mawk '{ print $12 }'"
but it only showed me some twos and then some newlines and again twos. It seems very laggy/slow. When I press a key, nothing happens. After pressing different keys multiple times, I always get a bunch of twos and newlines. No matter which keys I pressed! Example:
2
2
2
2
2
2
2
2
2
2
2
2
How can I fix this? Why is this occurring?
Update
When I use gawk instead of mawk it does not lag any more (unbuffered). But I still don’t see the correct values which should be 0028 or 00e4 and so on for Return and Right-Ctrl as example keys.
With all the comments I came to the solution.
#Olivier Dulac brought me to the idea that there are different awk implementations, which solved the problem of the laggyness (using gawk instead of mawk). I don’t even need the stdbuf.
#Ed Morton’s idea brought me to a problem, because he used quotes " in his example. I already had quotes in my statement around the whole thing, because it was executed using su. So I had to escape them.
My whole statement looks like this (notice the escaped dollar sign in front of the 12):
su -c "od --width=144 -x /dev/input/event3 | awk '{ print \$12 }'"
and now it works as expected.