how to get value at an address with radare - radare2

If I'm using radare2, and I run, lets say dr while debugging, it'll print pointers for some of the registers.
Lets pretend like esp is resolving to 0x04084308 or something similar.
If I want to get the value that esp is pointing to, how could I do that?
Thanks in advance.

print rsp register value
[0x560207c7275a]> dr?rsp
0x7fffa5e429c8
print 4 bytes hex at 0x7fffa5e429c8
[0x560207c7275a]> px 4 #rsp
- offset - 0 1 2 3 4 5 6 7 8 9 A B C D E F 0123456789ABCDEF
0x7fffa5e429c8 9b00 dae7 ....
print 8 bytes hex at 0x7fffa5e429c8 ( command px == x )
[0x560207c7275a]> x 8 #rsp
- offset - 0 1 2 3 4 5 6 7 8 9 A B C D E F 0123456789ABCDEF
0x7fffa5e429c8 9b00 dae7 347f 0000 ....4...
[0x560207c7275a]>

This can be solved with drr, which will show more information about the registers, such as where they point :).
Otherwise, if you want to get a value in the programs memory, you can s 0xaddr and then V to show information near there.

Related

Converting integer to binary for SPI transfer

I am trying to convert an integer to 3 bytes to send via the SPI protocol to control a MAX6921 serial chip. I am using spi.xfer2() but cannot get it to work as expected.
In my code, I can call spi.xfer2([0b00000000, 0b11101100, 0b10000000]) it displays the letter "H" on my display, but when I try and convert the int value for this 79616, it doesn't give the correct output:
val = 79616
spi.xfer2(val.to_bytes(3, 'little'))
My full code so far is on GitHub, and for comparison, this is my working code for Arduino.
More details
I have an IV-18 VFD tube driver module, which came with some example code for an ESP32 board. The driver module has a 20 output MAX6921 serial chip to drive the vacuum tube.
To sent "H" to the second grid position (as the first grid only displays a dot or a dash) the bits are sent to MAX6921 in order OUT19 --> OUT0, so using the LSB in my table below. The letter "H" has an int value 79616
I can successfully send this, manually, via SPI using:
spi.xfer2([0b00000000, 0b11101100, 0b10000000])
The problem I have is trying to convert other letters in a string to the correct bits. I can retrieve the integer value for any character (0-9, A-Z) in a string, but can't then work out how to convert it to the right format for spi.xfer() / spi.xfer2()
My Code
def display_write(val):
spi.xfer2(val.to_bytes(3, 'little'))
# Loops over the grid positions
def update_display():
global grid_index
val = grids[grid_index] | char_to_code(message[grid_index:grid_index+1])
display_write(val)
grid_index += 1
if (grid_index >= 9):
grid_index = 0
The full source for my code so far is on GitHub
Map for MAX6921 Data out pins to the IV-18 tube pins:
BIT
24
23
22
21
20
19
18
17
16
15
14
13
12
11
10
9
8
7
6
5
4
3
2
1
IV-18
G9
G8
G7
G6
G5
G4
G3
G2
G1
A
B
C
D
E
F
G
DP
MAX6921
0
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
IV-18 Pinout diagram

How to rename values in order in matlab?

Say I have this vector A:
A=[2
92
91
91
91
92
9
92
-1
91];
I want to write a code to rename the smallest entry as 1, the next smallest entry as 2, and so on. So, I want the output to be:
B=[2
5
4
4
4
5
3
5
1
4];
How do I do that with a short and efficient code? The code I have been able to write is a "check one by one and rename" kind of code, which is highly inefficient.
U=unique(A);
for a=1:size(U,1)
for b=1:size(A,1)
if A(b,1)==U(a,1)
B(b,1)=a;
end
end
end
Is it possible to write one without using for loops, or one that is otherwise efficient?
As a consequence of unique sorting the output, your desired array is automatically built by the function and is accessible via the third output:
>> A=[2;92;91;91;91;92;9;92;-1;91];
>> [~,~,B] = unique(A)
B =
2
5
4
4
4
5
3
5
1
4

How to select a submatrix in Matlab?

Hello and happy new year!
I have a problem, and I can't find a solution.
I need to create these 2 matrices, C from B with submatrix.
B = [ 1 2 3 4 5 6 7
9 7 5 3 1 -1 -3
4 8 16 32 64 128 256];
And I want to extract this matrix from it:
C = [ 2 3 4 5
32 64 128 256]
First I created the matrix B:
B = (1:7; 9:-2:-3; 2.^(2:8));
But with this I get an error:
C = B([1,(2:5)]; [3,(4:7)]);
Any ideas?
That's just a grammar issue.
Just try this:
C = [B(1, 2:5); B(3, 4:7)];

How to convert members of a matrix in a specific constrains value in MATLAB?

If
a =
1
2
3
3.5
5
6
6.25
8
9
9.75
11
12
13
14
15
How it is possible to switch numbers which are in a specific constrains, into the smaller part of constrain?
In other word, if b=min(a) & c=b+threshold
b<a<c => a=b.
It means, in this example, let's assume threshold=3. Then "a" should change into a matrix which min(a):min(a)+threshold =[min(a)]. This procedure should continue to the last array of "a"matrix by adding each time the amount of threshold. The result should be like this:
a=
1
1
1
1
5
5
5
5
9
9
9
9
13
13
13
I guess finally I found a solution for that. Sorry if I couldn't explain better my intention and mislead you. Here is the code:
a = [1 2 3 3.5 5 6 6.25 8 9 9.75 11 12 13 14 15]';
b = min(a);
threshold=4;
c=b;
for i=1:size(a,1)
c =c+threshold;
a((a > b & a < c)) = b;
b=b+threshold;
if c==max(a);
break
end
end
P.S. threshold change to "threshold=4" instead of "3"
Logical indexing
Edit, based on the comment thread
I can't think of a way to avoid a loop here, nor am I really sure how to constrain it efficiently, but the general process is the same:
% Initial conditions
threshold = 3;
a = [1 2 3 3.5 5 6 6.25 8 9 9.75 11 12 13 14 15];
b = min(a);
c = b + threshold;
a((a > b & a < c)) = b;
for/while/if % Need some way to constrain the loop
b = c;
c = c+threshold;
a((a > b & a < c)) = b;
end

Conditionally replacing cell values with column names

I have a 165 x 165 rank matrix such that each row has values ranging from 1-165. I want to parse each row and delete all values >= 5, sort each row in increasing order, then replace the values 1-5 with the name of the column from the original matrix.
For example, for row k the values 1 ,2 3, 4, 5, would result after the first two transformations and would be replaced by p,d, m, n, a.
I am assuming that your array consists of an array of arrays...
Neither Awk, Sed, or Perl have multi-dimensional arrays. However, they can be emulated in Perl by using arrays of arrays.
$a[0]->[0] = xx;
$a[0]->[1] = yy;
[...]
$a[0]->[164] = zz;
$a[1]->[0] = qq;
$a[1]->[1] = rr;
[...]
$a[164]->[164] = vv;
Does this make sense?
I'm calling the row $x and columns $y, so an element in your array will be $array[$x]->[$y]. Is that good?
Okay, your column names will be in row $array[0], so if we find a value less than five in $array[$x]->[$y], we know the column name is in $array[0]->[$y]. Is that good?
for my $x (1..164) { #First row is column names
for my $y (0..164) {
if ($array[$x]->[$y] <= 5) {
$array[$x]->[$y] = $array[0]->[$y];
}
}
}
I'm simply going through all the rows, and for each row, all the columns, and checking the value. If the value is less than or equal to five, I replace it with the column name.
I hope I'm not doing your homework for you.
This GNU sed solution might work although it will need scaling up as I only used a 10x10 matrix for testing purposes:
# { echo {a..j};for x in {1..10};do seq 1 10 | shuf |sed 'N;N;N;N;N;N;N;N;N;s/\n/ /g';done; }> test_data
# cat test_data
a b c d e f g h i j
4 5 9 3 6 2 10 8 7 1
3 7 4 2 1 6 10 5 8 9
10 9 3 1 2 7 8 5 6 4
5 10 4 9 7 8 1 3 6 2
8 6 5 9 1 4 3 2 7 10
2 8 9 3 5 6 10 1 4 7
3 9 8 2 1 4 10 6 7 5
3 7 2 1 8 6 10 4 5 9
1 10 8 3 6 5 4 2 7 9
7 2 3 5 6 1 10 4 8 9
# cat test_data |
sed -rn '1{h;d};s/[0-9]{2,}|[6-9]/0/g;G;s/\n|$/ &/g;s/$/&1 2 3 4 5 /;:a;s/^(\S*) (.*\n)(\S* )(.*)/\2\4\1\3/;ta;s/\n//;s/0[^ ]? //g;:b;s/([1-5])(.*)\1(.)/\3\2/;tb;p'
j f d a b
e d a c h
d e c j h
g j h c a
e h g f c
h a d i e
e d a f j
d c a h i
a h d g f
f b c h d
The sed command works as follows.
The first line of the data file contains the column headings is stored in the hold space then the pattern space (current line) is deleted. For all subsequent data lines all two or more digit numbers and values 6 to 9 are converted to 0. The column names are appended, along with a newline to the data values. Spaces are inserted before the newline and end of string. The data is transformed into a lookup and the sorted values i.e.. 1 2 3 4 5 is prepended to it. The newline is removed along with any 0 values and associated lookups. The values 1 to 5 are replaced by the column names in the lookup.
EDIT:
I may have misunderstood the problem regarding sorting columns or rows, if so it's a minimal fix - replace 1 2 3 4 5 by the original values and perform a numeric sort prior to replacing the numeric data with column names from the lookup.