about summary refs log tree commit diff
path: root/mips/chapter-3/exercise-10.s
diff options
context:
space:
mode:
Diffstat (limited to 'mips/chapter-3/exercise-10.s')
-rw-r--r--mips/chapter-3/exercise-10.s41
1 files changed, 41 insertions, 0 deletions
diff --git a/mips/chapter-3/exercise-10.s b/mips/chapter-3/exercise-10.s
new file mode 100644
index 0000000..f3ddeef
--- /dev/null
+++ b/mips/chapter-3/exercise-10.s
@@ -0,0 +1,41 @@
+# print -35 in 8-bit two-complement binary
+	.text
+main:
+	li	$t0,	-35		# t0 = -35
+	li	$t1,	0		# t1 = 0
+	li	$t8,	8		# t8 = 8
+
+reverse:
+	beqz	$t8,	prefix		# if (!t8) goto prefix
+	andi	$t2,	$t0,	1	# t2 = t0 & 1
+	sra	$t0,	$t0,	1	# t0 >>= 1
+	sll	$t1,	$t1,	1	# t1 <<= 1
+	add	$t1,	$t1,	$t2	# t1 += t2
+	addi	$t8,	$t8,	-1	# t8--
+	j	reverse			# goto reverse
+
+prefix:
+	li	$v0,	11		# print character
+	li	$a0,	48		# 0
+	syscall
+	li	$v0,	11		# print character
+	li	$a0,	98		# b
+	syscall
+	li	$t8,	8		# t8 = 8
+
+print:
+	beqz	$t8,	end		# if (!t8) goto end
+	li	$v0,	1		# print integre
+	andi	$a0,	$t1,	1	# a0 = t1 & 1
+	syscall
+	sra	$t1,	$t1,	1	# t1 >>= 1
+	addi	$t8,	$t8,	-1	# t8--
+	j	print			# goto print
+
+end:
+	li	$v0,	11		# print character
+	li	$a0,	10		# newline
+	syscall
+
+	li	$v0,	10		# terminate
+	syscall