start_addr=0x30000000 end_addr=0x30000003 data_write=0x55AA55AA count=0 match_count=0 # 写入数据 for ((addr=$start_addr; addr<$end_addr+1; addr=addr+1)); do echo "addr: $addr" echo -ne "\x55\xAA\x55\xAA" | dd of=/dev/mem bs=4 count=1 seek=$(($addr/8)) conv=notrunc 2>/dev/null data=$(echo -ne "\x55\xAA\x55\xAA" | hexdump -e '/4 "%08x\n"') data_text=$(echo -ne "\x55\xAA\x55\xAA" | hexdump -e '/4 "%08x\n"' | sed 's/../& /g') echo "data_text: $data_text" #echo "data: $data" data_read=$(dd if=/dev/mem bs=4 count=1 skip=$(($addr/4)) 2>/dev/null | hexdump -e '/4 "%08x\n"') data_read_text=$(dd if=/dev/mem bs=4 count=1 skip=$(($addr/4)) 2>/dev/null | hexdump -e '/4 "%08x\n"' | sed 's/../& /g') echo "data_read_text: $data_read_text" # echo "data_read: $data_read" if [ "$data_read" == "$data" ]; then match_count=$((match_count+1)) echo "match_count: $match_count" fi count=$((count+1)) echo "count: $count" done # 输出结果 # 判断结果 if [ $match_count -eq $count ]; then echo "PASS" else echo "FAIL" fi为什么我执行这段代码时输出的addr分别是805306368 805306369 805306370 805306371?根据我的代码addr输出的不应该是30000000 30000001 30000002 30000003吗?
时间: 2024-04-28 08:19:42 浏览: 60
根据你的代码,输出的addr应该是以字节为单位的地址,而不是以4字节为单位的地址。因此,每次addr增加1时,实际上增加了4个字节。而你的起始地址是0x30000000,而结束地址是0x30000003,因此循环中的addr实际上是逐渐增加的,分别是0x30000000、0x30000001、0x30000002和0x30000003对应的字节地址,即805306368、805306369、805306370和805306371。
相关问题
start_addr=0x30000000 end_addr=0x30000003 data_write=0x55AA55AA count=0 match_count=0 # 写入数据 for ((addr=$start_addr; addr<$end_addr+1; addr=addr+1)); do echo -ne "\x55\xAA\x55\xAA" | dd of=/dev/mem bs=4 count=1 seek=$(($addr/8)) conv=notrunc 2>/dev/null data=$(echo -ne "\x55\xAA\x55\xAA" | hexdump -e '/4 "%08x\n"') echo "data: $data" data_read=$(dd if=/dev/mem bs=4 count=1 skip=$(($addr/4)) 2>/dev/null | hexdump -e '/4 "%08x\n"') echo "data_read: $data_read" if [ "$data_read" == "$data" ]; then match_count=$((match_count+1)) echo "match_count: $match_count" fi count=$((count+1)) echo "count: $count" done # 输出结果 # 判断结果 if [ $match_count -eq $count ]; then echo "PASS" else echo "FAIL" fi 为什么这段代码data和data_read输出的都是空白?
这段代码中,数据写入和读取操作都使用了`dd`命令和`hexdump`命令。但是,`dd`命令和`hexdump`命令都是用于二进制数据的传输和显示,而不是文本数据。因此,如果想要在终端中查看二进制数据,需要使用`xxd`命令或者`od`命令。所以,将`hexdump`命令替换成`xxd`命令或者`od`命令即可输出正确的数据。
修改后的代码如下:
```
start_addr=0x30000000 end_addr=0x30000003 data_write=0x55AA55AA count=0 match_count=0
# 写入数据
for ((addr=$start_addr; addr<$end_addr+1; addr=addr+1)); do
echo -ne "\x55\xAA\x55\xAA" | dd of=/dev/mem bs=4 count=1 seek=$(($addr/8)) conv=notrunc 2>/dev/null
data=$(echo -ne "\x55\xAA\x55\xAA" | xxd -p)
echo "data: $data"
data_read=$(dd if=/dev/mem bs=4 count=1 skip=$(($addr/4)) 2>/dev/null | xxd -p)
echo "data_read: $data_read"
if [ "$data_read" == "$data" ]; then
match_count=$((match_count+1))
echo "match_count: $match_count"
fi
count=$((count+1))
echo "count: $count"
done
# 判断结果
if [ $match_count -eq $count ]; then
echo "PASS"
else
echo "FAIL"
fi
```
start_addr=0x05877000 #end_addr=0x3ffff000 end_addr=0x05877002 data_write=0x55AA55AA data_read=$(printf "%08x" $data_write) count=0 match_count=0 for ((addr=$start_addr; addr<$end_addr; addr=addr+4)); do echo $data_write | dd of=/dev/mem bs=4 count=1 seek=$(($addr/8)) conv=notrunc 2>/dev/nul done for ((addr=$start_addr; addr<$end_addr; addr+=4)); do data=$(dd if=/dev/mem bs=4 count=1 skip=$(($addr/4)) 2>/dev/null | hexdump -e '/4 "%08x\n"') if [ "$data_read" == "$data" ]; then match_count=$((match_count+1)) fi count=$((count+1)) done echo $data echo $data_read那么该如何修改程序才能让程序输出PASS
为了让程序输出 PASS,我们需要在循环结束后,判断 match_count 是否等于 count,如果等于,即所有数据都成功读取并且与写入的数据一致,就输出 PASS。
修改后的代码如下:
```
start_addr=0x05877000
end_addr=0x3ffff000
data_write=0x55AA55AA
data_read=$(printf "%08x" $data_write)
count=0
match_count=0
for ((addr=$start_addr; addr<$end_addr; addr=addr+4)); do
echo $data_write | dd of=/dev/mem bs=4 count=1 seek=$(($addr/8)) conv=notrunc 2>/dev/nul
done
for ((addr=$start_addr; addr<$end_addr; addr+=4)); do
data=$(dd if=/dev/mem bs=4 count=1 skip=$(($addr/4)) 2>/dev/null | hexdump -e '/4 "%08x\n"')
if [ "$data_read" == "$data" ]; then
match_count=$((match_count+1))
fi
count=$((count+1))
done
if [ $match_count -eq $count ]; then
echo "PASS"
else
echo "FAIL"
fi
echo $data
echo $data_read
```
这样,当所有数据都成功读取并且与写入的数据一致时,程序就会输出 PASS。
阅读全文