Skip to main content

decode

Function decode 

Source
pub fn decode(code: &[u8], offset: u32) -> Result<Instruction, DecodeError>
Expand description

Decode the instruction at offset in code.

code starts at the function’s method header, so offset and all branch targets are relative to the header.

Examples found in repository?
examples/probe.rs (line 22)
8fn main() {
9    let args: Vec<String> = std::env::args().skip(1).collect();
10    let data = std::fs::read(&args[0]).expect("cannot read image");
11    let image = Image::parse(&data).expect("cannot parse image");
12    let address = u32::from_str_radix(args[1].trim_start_matches("0x"), 16).expect("bad address");
13    let count: usize = args.get(2).map_or(40, |n| n.parse().expect("bad count"));
14    let page = image
15        .code_pool
16        .at(address)
17        .expect("address is not in the code pool");
18    println!("{:?}", MethodHeader::parse(page));
19    println!("{:02x?}", &page[..page.len().min(16)]);
20    let mut offset = u32::from(image.entry_point.method_header_size);
21    for _ in 0..count {
22        match decode(page, offset) {
23            Ok(i) => {
24                println!(
25                    "{:04x}  {:<14} {:?} {:?}",
26                    i.offset,
27                    i.opcode.name(),
28                    i.operands,
29                    i.payload
30                );
31                offset += i.len;
32            }
33            Err(error) => {
34                println!("{error}");
35                break;
36            }
37        }
38    }
39}