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
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
|
#include <stdio.h>
#include <stdint.h>
#include <err.h>
#include <stdlib.h>
/*
* 7 6 5 4 3 2 1 0
* +---|---+ +---|---|---+ +---|---|---+
* | MOD | | REG | | R/M |
* +---|---+ +---|---|---+ +---|---|---+
*/
/*
* 0x10 = 00.010.000 (mod=0, reg=2, r/m=0)
* 0x11 = 00.010.001 (mod=0, reg=2, r/m=1)
*/
/*
* byte = 0x10
*
* modrm_byte rm = byte & 7
*
* 0x10 = 00.010.000
* 7 = 00.000.111
*
* 00.010.000
* & 00.000.111
* = 00.000.000
*
* modrm_byte reg = (byte >> 3) & 7;
*
* 0x10 = 00.010.000 >> 3 = 00.000.010
*
* 00.000.010
* & 00.000.111
* = 00.000.010
*
* modrm_byte mod = (byte >> 6) & 3;
*
* 0x10 = 00.010.000 >> 6 = 00.000.000
*
* 00.000.000
* & 00.000.011
* = 00.000.000
*/
/*
* byte = 0x11
*
* modrm_byte rm = byte & 7
*
* 0x11 = 00.010.001
* 7 = 00.000.111
*
* 00.010.001
* & 00.000.111
* = 00.000.001
* = 1
*
* modrm_byte reg = (byte >> 3) & 7;
*
* 0x11 = 00.010.001 >> 3 = 00.000.010
*
* 00.000.010
* & 00.000.111
* = 00.000.010
* = 2
*
* modrm_byte mod = (byte >> 6) & 3;
*
* 0x11 = 00.010.001 >> 6 = 00.000.000
*
* 00.000.000
* & 00.000.011
* = 00.000.000
* = 0
*/
int main(int argc, char *argv[])
{
unsigned byte;
uint8_t modrm_byte;
if (argc != 2)
errx(EXIT_FAILURE, "usage: %s <hex digit>\nusage: %s 0x10", argv[0], argv[0]);
if (sscanf(argv[1], "0x%2x", &byte) != 1)
errx(EXIT_FAILURE, "sscanf(%s)\n", argv[1]);
modrm_byte = byte;
printf("modrm_byte_t rm = 0x%.2x\n", modrm_byte & 7);
printf("modrm_byte_t reg = 0x%.2x\n", (modrm_byte >> 3) & 7);
printf("modrm_byte_t mod = 0x%.2x\n", (modrm_byte >> 6) & 3);
return 0;
}
|