1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
pub struct SIBByte {
    pub base_reg: u8,
    pub index_reg: u8,
    pub scale: u8,
}

impl SIBByte {
    pub fn base_field(byte: u8) -> u8 {
        byte
    }
    pub fn index_field(byte: u8) -> u8 {
        byte << 3
    }
    pub fn scale_field(byte: u8) -> u8 {
        match byte {
            0x1 => 0b00,
            0x2 => 0b01 << 6,
            0x4 => 0b10 << 6,
            0x8 => 0b11 << 6,
            _ => panic!("scale must 0x1, 0x2, 0x4 or 0x8"),
        }
    }

    pub fn to_byte(&self) -> u8 {
        Self::base_field(self.base_reg) | Self::index_field(self.index_reg) | Self::scale_field(self.scale)
    }
}