about summary refs log tree commit diff stats
path: root/subx
diff options
context:
space:
mode:
authorKartik Agaram <vc@akkartik.com>2019-07-17 19:11:26 -0700
committerKartik Agaram <vc@akkartik.com>2019-07-17 19:11:26 -0700
commit2a13be5fd73877cc73c0c50ccd3ab7fadb41a682 (patch)
tree681964e386abfa144a420e66834df9a48551cf28 /subx
parent22bb33eb477ab0942be717f8e5499fe339eafc65 (diff)
downloadmu-2a13be5fd73877cc73c0c50ccd3ab7fadb41a682.tar.gz
5413
Bugfix twelve: ModR/M was being incorrectly computed.

This is one of two problems with subx/examples/ex3, so no new passing
examples.
Diffstat (limited to 'subx')
-rw-r--r--subx/010---vm.cc2
-rwxr-xr-xsubx/apps/packbin46508 -> 46791 bytes
-rw-r--r--subx/apps/pack.subx108
3 files changed, 104 insertions, 6 deletions
diff --git a/subx/010---vm.cc b/subx/010---vm.cc
index b54dd9d5..82a7d2f5 100644
--- a/subx/010---vm.cc
+++ b/subx/010---vm.cc
@@ -203,7 +203,6 @@ inline uint8_t* mem_addr_u8(uint32_t addr) {
   if (result == NULL) {
     if (Trace_file) Trace_file.flush();
     raise << "Tried to access uninitialized memory at address 0x" << HEXWORD << addr << '\n' << end();
-    DUMP("");
     exit(1);
   }
   return result;
@@ -224,7 +223,6 @@ inline uint32_t* mem_addr_u32(uint32_t addr) {
     if (Trace_file) Trace_file.flush();
     raise << "Tried to access uninitialized memory at address 0x" << HEXWORD << addr << '\n' << end();
     raise << "The entire 4-byte word should be initialized and lie in a single segment.\n" << end();
-    DUMP("");
     exit(1);
   }
   return result;
diff --git a/subx/apps/pack b/subx/apps/pack
index 705006e5..6dc1529c 100755
--- a/subx/apps/pack
+++ b/subx/apps/pack
Binary files differdiff --git a/subx/apps/pack.subx b/subx/apps/pack.subx
index 8cd24813..2715558a 100644
--- a/subx/apps/pack.subx
+++ b/subx/apps/pack.subx
@@ -2301,7 +2301,7 @@ emit-modrm:  # line : (address stream byte), out : (address buffered-file) -> <v
     #       has-modrm? = true
     #   if has-modrm?
     #     var modrm = mod & 0b11
-    #     modrm <<= 2
+    #     modrm <<= 3
     #     modrm |= r32 & 0b111
     #     modrm <<= 3
     #     modrm |= rm32 & 0b111
@@ -2336,7 +2336,6 @@ emit-modrm:  # line : (address stream byte), out : (address buffered-file) -> <v
     e8/call  rewind-stream/disp32
     # . . discard args
     81          0/subop/add         3/mod/direct    4/rm32/ESP    .           .             .           .           .               4/imm32           # add to ESP
-$emit-modrm:loop:
 #?     # dump line {{{
 #?     # . write(2/stderr, "LL: ")
 #?     # . . push args
@@ -2362,7 +2361,15 @@ $emit-modrm:loop:
 #?     e8/call  write/disp32
 #?     # . . discard args
 #?     81          0/subop/add         3/mod/direct    4/rm32/ESP    .           .             .           .           .               8/imm32           # add to ESP
+#?     # . rewind-stream(line)
+#?     # . . push args
+#?     ff          6/subop/push        1/mod/*+disp8   5/rm32/EBP    .           .             .           .           8/disp8         .                 # push *(EBP+8)
+#?     # . . call
+#?     e8/call  rewind-stream/disp32
+#?     # . . discard args
+#?     81          0/subop/add         3/mod/direct    4/rm32/ESP    .           .             .           .           .               4/imm32           # add to ESP
 #?     # }}}
+$emit-modrm:loop:
     # next-word(line, word-slice)
     # . . push args
     51/push-ECX
@@ -2558,8 +2565,8 @@ $emit-modrm:break:
 $emit-modrm:calculate:
     # modrm/EBX = mod & 0b11
     81          4/subop/and         3/mod/direct    3/rm32/EBX    .           .             .           .           .               3/imm32/0b11      # bitwise and of EBX
-    # modrm <<= 2
-    c1/shift    4/subop/left        3/mod/direct    3/rm32/EBX    .           .             .           .           .               2/imm8            # shift EBX left by 2 bits
+    # modrm <<= 3
+    c1/shift    4/subop/left        3/mod/direct    3/rm32/EBX    .           .             .           .           .               3/imm8            # shift EBX left by 3 bits
     # modrm |= r32 & 0b111
     81          4/subop/and         3/mod/direct    7/rm32/EDI    .           .             .           .           .               7/imm32/0b111     # bitwise and of EDI
     09/or                           3/mod/direct    3/rm32/EBX    .           .             .           7/r32/EDI   .               .                 # EBX = bitwise OR with EDI
@@ -4420,6 +4427,99 @@ test-convert-instruction-emits-modrm-byte:
     5d/pop-to-EBP
     c3/return
 
+test-convert-instruction-emits-modrm-byte-with-non-zero-mod:
+    # . prolog
+    55/push-EBP
+    89/copy                         3/mod/direct    5/rm32/EBP    .           .             .           4/r32/ESP   .               .                 # copy ESP to EBP
+    # setup
+    # . clear-stream(_test-input-stream)
+    # . . push args
+    68/push  _test-input-stream/imm32
+    # . . call
+    e8/call  clear-stream/disp32
+    # . . discard args
+    81          0/subop/add         3/mod/direct    4/rm32/ESP    .           .             .           .           .               4/imm32           # add to ESP
+    # . clear-stream(_test-output-stream)
+    # . . push args
+    68/push  _test-output-stream/imm32
+    # . . call
+    e8/call  clear-stream/disp32
+    # . . discard args
+    81          0/subop/add         3/mod/direct    4/rm32/ESP    .           .             .           .           .               4/imm32           # add to ESP
+    # . clear-stream(_test-output-buffered-file+4)
+    # . . push args
+    b8/copy-to-EAX  _test-output-buffered-file/imm32
+    05/add-to-EAX  4/imm32
+    50/push-EAX
+    # . . call
+    e8/call  clear-stream/disp32
+    # . . discard args
+    81          0/subop/add         3/mod/direct    4/rm32/ESP    .           .             .           .           .               4/imm32           # add to ESP
+    # initialize input
+    # . write(_test-input-stream, "01/add 3/mod/direct 3/rm32/EBX 1/r32/ECX")
+    # . . push args
+    68/push  "01/add 3/mod/direct 3/rm32/EBX 1/r32/ECX"/imm32
+    68/push  _test-input-stream/imm32
+    # . . call
+    e8/call  write/disp32
+    # . . discard args
+    81          0/subop/add         3/mod/direct    4/rm32/ESP    .           .             .           .           .               8/imm32           # add to ESP
+    # convert-instruction(_test-input-stream, _test-output-buffered-file)
+    # . . push args
+    68/push  _test-output-buffered-file/imm32
+    68/push  _test-input-stream/imm32
+    # . . call
+    e8/call  convert-instruction/disp32
+    # . . discard args
+    81          0/subop/add         3/mod/direct    4/rm32/ESP    .           .             .           .           .               8/imm32           # add to ESP
+    # . flush(_test-output-buffered-file)
+    # . . push args
+    68/push  _test-output-buffered-file/imm32
+    # . . call
+    e8/call  flush/disp32
+    # . . discard args
+    81          0/subop/add         3/mod/direct    4/rm32/ESP    .           .             .           .           .               4/imm32           # add to ESP
+#?     # dump output {{{
+#?     # . write(2/stderr, "out: ")
+#?     # . . push args
+#?     68/push  "out: "/imm32
+#?     68/push  2/imm32/stderr
+#?     # . . call
+#?     e8/call  write/disp32
+#?     # . . discard args
+#?     81          0/subop/add         3/mod/direct    4/rm32/ESP    .           .             .           .           .               8/imm32           # add to ESP
+#?     # write-stream(2/stderr, _test-output-stream)
+#?     # . . push args
+#?     68/push  _test-output-stream/imm32
+#?     68/push  2/imm32/stderr
+#?     # . . call
+#?     e8/call  write-stream/disp32
+#?     # . . discard args
+#?     81          0/subop/add         3/mod/direct    4/rm32/ESP    .           .             .           .           .               8/imm32           # add to ESP
+#?     # . write(2/stderr, "$\n")
+#?     # . . push args
+#?     68/push  "$\n"/imm32
+#?     68/push  2/imm32/stderr
+#?     # . . call
+#?     e8/call  write/disp32
+#?     # . . discard args
+#?     81          0/subop/add         3/mod/direct    4/rm32/ESP    .           .             .           .           .               8/imm32           # add to ESP
+#?     # }}}
+    # check output
+    # . check-stream-equal(_test-output-stream, "# abcd", msg)
+    # . . push args
+    68/push  "F - test-convert-instruction-foo"/imm32
+    68/push  "01 cb  # 01/add 3/mod/direct 3/rm32/EBX 1/r32/ECX"/imm32
+    68/push  _test-output-stream/imm32
+    # . . call
+    e8/call  check-stream-equal/disp32
+    # . . discard args
+    81          0/subop/add         3/mod/direct    4/rm32/ESP    .           .             .           .           .               0xc/imm32         # add to ESP
+    # . epilog
+    89/copy                         3/mod/direct    4/rm32/ESP    .           .             .           5/r32/EBP   .               .                 # copy EBP to ESP
+    5d/pop-to-EBP
+    c3/return
+
 test-convert-instruction-emits-modrm-byte-from-subop:
     # pack mod, rm32 and subop operands into ModR/M byte
     # . prolog
7 -0700 1134' href='/akkartik/mu/commit/cpp/002test?h=hlt&id=9e608a7704e79834d7a5a314cec6fe3a5f153711'>9e608a77 ^
6c96a437 ^
46b6e2a3 ^
9e608a77 ^
6ff9413c ^

9e608a77 ^
b39ceb27 ^
5a9e3a11 ^
6ff9413c ^


b39ceb27 ^
683d53ca ^

b39ceb27 ^
df0b469f ^
ccd792da ^
6e1eeeeb ^






51530916 ^
ac0e9db5 ^
7d13531e ^



6573fe1f ^
eb3a527a ^

b5ab709c ^
51530916 ^

53172ce1 ^
17d3003d ^




53172ce1 ^











51530916 ^
f2043a73 ^
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
98
99
100
101
102
103
104
105
106
107
108
109
110
111