diff --git a/gas/testsuite/gas/riscv/m-ext-32-noarch-m.d b/gas/testsuite/gas/riscv/m-ext-32-noarch-m.d
new file mode 100644
index 00000000000..1d05564125f
--- /dev/null
+++ b/gas/testsuite/gas/riscv/m-ext-32-noarch-m.d
@@ -0,0 +1,4 @@
+#as: -march=rv32i_zmmul -I$srcdir/$subdir -defsym XLEN=32 -defsym NOARCH_ARCH=1
+#source: m-ext.s
+#objdump: -d
+#error_output: m-ext-32-noarch-m.l
diff --git a/gas/testsuite/gas/riscv/m-ext-fail-zmmul-32.l b/gas/testsuite/gas/riscv/m-ext-32-noarch-m.l
similarity index 100%
rename from gas/testsuite/gas/riscv/m-ext-fail-zmmul-32.l
rename to gas/testsuite/gas/riscv/m-ext-32-noarch-m.l
diff --git a/gas/testsuite/gas/riscv/m-ext-32-noarch.d b/gas/testsuite/gas/riscv/m-ext-32-noarch.d
new file mode 100644
index 00000000000..a708d429ac7
--- /dev/null
+++ b/gas/testsuite/gas/riscv/m-ext-32-noarch.d
@@ -0,0 +1,4 @@
+#as: -march=rv32i -I$srcdir/$subdir -defsym XLEN=32 -defsym NOARCH=1
+#source: m-ext.s
+#objdump: -d
+#error_output: m-ext-32-noarch.l
diff --git a/gas/testsuite/gas/riscv/m-ext-32-noarch.l b/gas/testsuite/gas/riscv/m-ext-32-noarch.l
new file mode 100644
index 00000000000..f9179f45bb4
--- /dev/null
+++ b/gas/testsuite/gas/riscv/m-ext-32-noarch.l
@@ -0,0 +1,14 @@
+.*Assembler messages:
+.*: Error: unrecognized opcode `mul a0,a1,a2', extension `m' or `zmmul' required
+.*: Error: unrecognized opcode `mulh a0,a1,a2', extension `m' or `zmmul' required
+.*: Error: unrecognized opcode `mulhsu a0,a1,a2', extension `m' or `zmmul' required
+.*: Error: unrecognized opcode `mulhu a0,a1,a2', extension `m' or `zmmul' required
+.*: Error: unrecognized opcode `div a0,a1,a2', extension `m' required
+.*: Error: unrecognized opcode `divu a0,a1,a2', extension `m' required
+.*: Error: unrecognized opcode `rem a0,a1,a2', extension `m' required
+.*: Error: unrecognized opcode `remu a0,a1,a2', extension `m' required
+.*: Error: unrecognized opcode `mulw a0,a1,a2'
+.*: Error: unrecognized opcode `divw a0,a1,a2'
+.*: Error: unrecognized opcode `divuw a0,a1,a2'
+.*: Error: unrecognized opcode `remw a0,a1,a2'
+.*: Error: unrecognized opcode `remuw a0,a1,a2'
diff --git a/gas/testsuite/gas/riscv/m-ext-32.d b/gas/testsuite/gas/riscv/m-ext-32.d
index fe2ef9af54b..02be2ef9569 100644
--- a/gas/testsuite/gas/riscv/m-ext-32.d
+++ b/gas/testsuite/gas/riscv/m-ext-32.d
@@ -1,4 +1,4 @@
-#as: -march=rv32im
+#as: -march=rv32i -I$srcdir/$subdir -defsym XLEN=32
 #source: m-ext.s
 #objdump: -d
 
diff --git a/gas/testsuite/gas/riscv/m-ext-64-noarch-m.d b/gas/testsuite/gas/riscv/m-ext-64-noarch-m.d
new file mode 100644
index 00000000000..d74fbd0b682
--- /dev/null
+++ b/gas/testsuite/gas/riscv/m-ext-64-noarch-m.d
@@ -0,0 +1,4 @@
+#as: -march=rv64i_zmmul -I$srcdir/$subdir -defsym XLEN=64 -defsym NOARCH_ARCH=1
+#source: m-ext.s
+#objdump: -d
+#error_output: m-ext-64-noarch-m.l
diff --git a/gas/testsuite/gas/riscv/m-ext-fail-zmmul-64.l b/gas/testsuite/gas/riscv/m-ext-64-noarch-m.l
similarity index 100%
rename from gas/testsuite/gas/riscv/m-ext-fail-zmmul-64.l
rename to gas/testsuite/gas/riscv/m-ext-64-noarch-m.l
diff --git a/gas/testsuite/gas/riscv/m-ext-64-noarch.d b/gas/testsuite/gas/riscv/m-ext-64-noarch.d
new file mode 100644
index 00000000000..2d7031e5a35
--- /dev/null
+++ b/gas/testsuite/gas/riscv/m-ext-64-noarch.d
@@ -0,0 +1,4 @@
+#as: -march=rv64i -I$srcdir/$subdir -defsym XLEN=64 -defsym NOARCH=1
+#source: m-ext.s
+#objdump: -d
+#error_output: m-ext-64-noarch.l
diff --git a/gas/testsuite/gas/riscv/m-ext-fail-noarch-64.l b/gas/testsuite/gas/riscv/m-ext-64-noarch.l
similarity index 100%
rename from gas/testsuite/gas/riscv/m-ext-fail-noarch-64.l
rename to gas/testsuite/gas/riscv/m-ext-64-noarch.l
diff --git a/gas/testsuite/gas/riscv/m-ext-64.d b/gas/testsuite/gas/riscv/m-ext-64.d
index 05099b14e9e..ad086829ae5 100644
--- a/gas/testsuite/gas/riscv/m-ext-64.d
+++ b/gas/testsuite/gas/riscv/m-ext-64.d
@@ -1,4 +1,4 @@
-#as: -march=rv64im -defsym rv64=1
+#as: -march=rv64i -I$srcdir/$subdir -defsym XLEN=64
 #source: m-ext.s
 #objdump: -d
 
diff --git a/gas/testsuite/gas/riscv/m-ext-fail-noarch-64.d b/gas/testsuite/gas/riscv/m-ext-fail-noarch-64.d
deleted file mode 100644
index 3c4fc9a0a50..00000000000
--- a/gas/testsuite/gas/riscv/m-ext-fail-noarch-64.d
+++ /dev/null
@@ -1,4 +0,0 @@
-#as: -march=rv64i -defsym rv64=1
-#source: m-ext.s
-#objdump: -d
-#error_output: m-ext-fail-noarch-64.l
diff --git a/gas/testsuite/gas/riscv/m-ext-fail-xlen-32.d b/gas/testsuite/gas/riscv/m-ext-fail-xlen-32.d
deleted file mode 100644
index 54f8b8225dc..00000000000
--- a/gas/testsuite/gas/riscv/m-ext-fail-xlen-32.d
+++ /dev/null
@@ -1,4 +0,0 @@
-#as: -march=rv32im -defsym rv64=1
-#source: m-ext.s
-#objdump: -d
-#error_output: m-ext-fail-xlen-32.l
diff --git a/gas/testsuite/gas/riscv/m-ext-fail-xlen-32.l b/gas/testsuite/gas/riscv/m-ext-fail-xlen-32.l
deleted file mode 100644
index d65ca4980e6..00000000000
--- a/gas/testsuite/gas/riscv/m-ext-fail-xlen-32.l
+++ /dev/null
@@ -1,6 +0,0 @@
-.*Assembler messages:
-.*: Error: unrecognized opcode `mulw a0,a1,a2'
-.*: Error: unrecognized opcode `divw a0,a1,a2'
-.*: Error: unrecognized opcode `divuw a0,a1,a2'
-.*: Error: unrecognized opcode `remw a0,a1,a2'
-.*: Error: unrecognized opcode `remuw a0,a1,a2'
diff --git a/gas/testsuite/gas/riscv/m-ext-fail-zmmul-32.d b/gas/testsuite/gas/riscv/m-ext-fail-zmmul-32.d
deleted file mode 100644
index c164fa96f8f..00000000000
--- a/gas/testsuite/gas/riscv/m-ext-fail-zmmul-32.d
+++ /dev/null
@@ -1,4 +0,0 @@
-#as: -march=rv32i_zmmul
-#source: m-ext.s
-#objdump: -d
-#error_output: m-ext-fail-zmmul-32.l
diff --git a/gas/testsuite/gas/riscv/m-ext-fail-zmmul-64.d b/gas/testsuite/gas/riscv/m-ext-fail-zmmul-64.d
deleted file mode 100644
index f736d9c66c6..00000000000
--- a/gas/testsuite/gas/riscv/m-ext-fail-zmmul-64.d
+++ /dev/null
@@ -1,4 +0,0 @@
-#as: -march=rv64i_zmmul -defsym rv64=1
-#source: m-ext.s
-#objdump: -d
-#error_output: m-ext-fail-zmmul-64.l
diff --git a/gas/testsuite/gas/riscv/m-ext.s b/gas/testsuite/gas/riscv/m-ext.s
index 68baf2ab9c0..8d599f20aef 100644
--- a/gas/testsuite/gas/riscv/m-ext.s
+++ b/gas/testsuite/gas/riscv/m-ext.s
@@ -1,21 +1,26 @@
+.include "testutils.inc"
+
 target:
+	SET_ARCH_START	+zmmul
 	mul	a0, a1, a2
 	mulh	a0, a1, a2
 	mulhsu	a0, a1, a2
 	mulhu	a0, a1, a2
-.ifndef zmmul
+	SET_ARCH_START	+m
 	div	a0, a1, a2
 	divu	a0, a1, a2
 	rem	a0, a1, a2
 	remu	a0, a1, a2
-.endif
-
-.ifdef rv64
+	SET_ARCH_END
+	SET_ARCH_END
+.if	XLEN_GE_64
+	SET_ARCH_START	+zmmul
 	mulw	a0, a1, a2
-.ifndef zmmul
+	SET_ARCH_START	+m
 	divw	a0, a1, a2
 	divuw	a0, a1, a2
 	remw	a0, a1, a2
 	remuw	a0, a1, a2
-.endif
+	SET_ARCH_END
+	SET_ARCH_END
 .endif
diff --git a/gas/testsuite/gas/riscv/zmmul-32.d b/gas/testsuite/gas/riscv/zmmul-32.d
deleted file mode 100644
index c9cf56ab33f..00000000000
--- a/gas/testsuite/gas/riscv/zmmul-32.d
+++ /dev/null
@@ -1,14 +0,0 @@
-#as: -march=rv32im -defsym zmmul=1
-#source: m-ext.s
-#objdump: -d
-
-.*:[ 	]+file format .*
-
-
-Disassembly of section .text:
-
-0+000 <target>:
-[ 	]+[0-9a-f]+:[ 	]+02c58533[ 	]+mul[  	]+a0,a1,a2
-[ 	]+[0-9a-f]+:[ 	]+02c59533[ 	]+mulh[ 	]+a0,a1,a2
-[ 	]+[0-9a-f]+:[ 	]+02c5a533[ 	]+mulhsu[ 	]+a0,a1,a2
-[ 	]+[0-9a-f]+:[ 	]+02c5b533[ 	]+mulhu[ 	]+a0,a1,a2
diff --git a/gas/testsuite/gas/riscv/zmmul-64.d b/gas/testsuite/gas/riscv/zmmul-64.d
deleted file mode 100644
index 67ef3604755..00000000000
--- a/gas/testsuite/gas/riscv/zmmul-64.d
+++ /dev/null
@@ -1,15 +0,0 @@
-#as: -march=rv64im -defsym zmmul=1 -defsym rv64=1
-#source: m-ext.s
-#objdump: -d
-
-.*:[ 	]+file format .*
-
-
-Disassembly of section .text:
-
-0+000 <target>:
-[ 	]+[0-9a-f]+:[ 	]+02c58533[ 	]+mul[  	]+a0,a1,a2
-[ 	]+[0-9a-f]+:[ 	]+02c59533[ 	]+mulh[ 	]+a0,a1,a2
-[ 	]+[0-9a-f]+:[ 	]+02c5a533[ 	]+mulhsu[ 	]+a0,a1,a2
-[ 	]+[0-9a-f]+:[ 	]+02c5b533[ 	]+mulhu[ 	]+a0,a1,a2
-[ 	]+[0-9a-f]+:[ 	]+02c5853b[ 	]+mulw[ 	]+a0,a1,a2
