about summary refs log tree commit diff
path: root/mips/chapter-2
diff options
context:
space:
mode:
authorNguyễn Gia Phong <vn.mcsinyx@gmail.com>2020-02-10 17:49:10 +0700
committerNguyễn Gia Phong <vn.mcsinyx@gmail.com>2020-02-10 17:49:10 +0700
commit9e37e768b4694038a757366fcdf8e4c6e3ca788d (patch)
tree87d7d54499471e7694d87743fcbb5c178c8ed159 /mips/chapter-2
parent57bfdfe2e58a9dcf95f94d38cbd654b18035735a (diff)
downloadcp-9e37e768b4694038a757366fcdf8e4c6e3ca788d.tar.gz
[mips] Finish the second chapter
Diffstat (limited to 'mips/chapter-2')
-rw-r--r--mips/chapter-2/exercise-1/a.s20
-rw-r--r--mips/chapter-2/exercise-1/b.s19
-rw-r--r--mips/chapter-2/exercise-1/c.s15
-rw-r--r--mips/chapter-2/exercise-1/d.s13
-rw-r--r--mips/chapter-2/exercise-1/e.s18
-rw-r--r--mips/chapter-2/exercise-1/f.s17
-rw-r--r--mips/chapter-2/exercise-1/g.s19
-rw-r--r--mips/chapter-2/exercise-1/h.s20
-rw-r--r--mips/chapter-2/exercise-1/i.s12
-rw-r--r--mips/chapter-2/exercise-1/j.s16
-rw-r--r--mips/chapter-2/exercise-1/k.s28
-rw-r--r--mips/chapter-2/exercise-1/l.s22
-rw-r--r--mips/chapter-2/exercise-1/m.s17
-rw-r--r--mips/chapter-2/exercise-1/n.s16
-rw-r--r--mips/chapter-2/exercise-1/o.s17
-rw-r--r--mips/chapter-2/exercise-1/p.s21
-rw-r--r--mips/chapter-2/exercise-1/q.s20
-rw-r--r--mips/chapter-2/exercise-1/r.s16
-rw-r--r--mips/chapter-2/exercise-1/s.s16
-rw-r--r--mips/chapter-2/exercise-3.s23
-rw-r--r--mips/chapter-2/exercise-5.s23
21 files changed, 388 insertions, 0 deletions
diff --git a/mips/chapter-2/exercise-1/a.s b/mips/chapter-2/exercise-1/a.s
new file mode 100644
index 0000000..4da4645
--- /dev/null
+++ b/mips/chapter-2/exercise-1/a.s
@@ -0,0 +1,20 @@
+# t3 = t4 + t5 - t6
+	.text
+main:
+	li	$t4,	4		# t4 = 4
+	li	$t5,	5		# t5 = 5
+	li	$t6,	6		# t6 = 6
+
+	add	$t3,	$t4,	$t5	# t3 = t4 + t5
+	sub	$t3,	$t3,	$t6	# t3 -= t6
+
+	li	$v0,	1		# print integer
+	move	$a0,	$t3		# at t3
+	syscall
+
+	li	$v0,	11		# print character
+	li	$a0,	10		# newline
+	syscall
+
+	li	$v0,	10		# terminate program run
+	syscall
diff --git a/mips/chapter-2/exercise-1/b.s b/mips/chapter-2/exercise-1/b.s
new file mode 100644
index 0000000..fa590b5
--- /dev/null
+++ b/mips/chapter-2/exercise-1/b.s
@@ -0,0 +1,19 @@
+# s3 = t2 / (s1 - 54321)
+	.text
+main:
+	li	$t2,	69		# t2 = 69
+	li	$s1,	54324		# s1 = 54324
+
+	sub	$s1,	$s1,	54321	# s1 -= 54321
+	div	$t3,	$t2,	$s1	# t3 = t2 / s1
+
+	li	$v0,	1		# print integer
+	move	$a0,	$t3		# at a0
+	syscall
+
+	li	$v0,	11		# print character
+	li	$a0,	10		# newline
+	syscall
+
+	li	$v0,	10		# terminate program run
+	syscall
diff --git a/mips/chapter-2/exercise-1/c.s b/mips/chapter-2/exercise-1/c.s
new file mode 100644
index 0000000..370af96
--- /dev/null
+++ b/mips/chapter-2/exercise-1/c.s
@@ -0,0 +1,15 @@
+# sp -= 16
+	.text
+main:
+	addi	$sp,	$sp,	-16	# sp -= 16, may underflow
+
+	li	$v0,	1		# print integer
+	move	$a0,	$sp		# at sp
+	syscall
+
+	li	$v0,	11		# print character
+	li	$a0,	10		# newline
+	syscall
+
+	li	$v0,	10		# terminate program run
+	syscall
diff --git a/mips/chapter-2/exercise-1/d.s b/mips/chapter-2/exercise-1/d.s
new file mode 100644
index 0000000..e20cd4c
--- /dev/null
+++ b/mips/chapter-2/exercise-1/d.s
@@ -0,0 +1,13 @@
+# print t3
+	.text
+main:
+	li	$v0,	1		# print integer
+	move	$a0,	$t3		# at t3
+	syscall
+
+	li	$v0,	11		# print character
+	li	$a0,	10		# newline
+	syscall
+
+	li	$v0,	10		# terminate program run
+	syscall
diff --git a/mips/chapter-2/exercise-1/e.s b/mips/chapter-2/exercise-1/e.s
new file mode 100644
index 0000000..d35702d
--- /dev/null
+++ b/mips/chapter-2/exercise-1/e.s
@@ -0,0 +1,18 @@
+# read to and echo t0
+	.text
+main:
+	li	$v0,	5		# read integer to v0
+	syscall
+
+	move	$t0,	$v0		# t0 = v0
+
+	li	$v0,	1		# print integer
+	move	$a0,	$t0		# at t0
+	syscall
+
+	li	$v0,	11		# print character
+	li	$a0,	10		# newline
+	syscall
+
+	li	$v0,	10		# terminate program run
+	syscall
diff --git a/mips/chapter-2/exercise-1/f.s b/mips/chapter-2/exercise-1/f.s
new file mode 100644
index 0000000..2ec14ef
--- /dev/null
+++ b/mips/chapter-2/exercise-1/f.s
@@ -0,0 +1,17 @@
+# a0 = array
+	.data
+array:	.word	4, 20, 6, 9
+
+	.text
+main:
+
+	li	$v0,	1		# print integer
+	la	$a0,	array		# address of array
+	syscall
+
+	li	$v0,	11		# print character
+	li	$a0,	10		# newline
+	syscall
+
+	li	$v0,	10		# terminate program run
+	syscall
diff --git a/mips/chapter-2/exercise-1/g.s b/mips/chapter-2/exercise-1/g.s
new file mode 100644
index 0000000..e9d8f8f
--- /dev/null
+++ b/mips/chapter-2/exercise-1/g.s
@@ -0,0 +1,19 @@
+# t8 = *a0
+	.data
+array:	.word	4, 20, 6, 9
+
+	.text
+main:
+	la	$a0,	array		# a0 = array
+	lw	$t8,	($a0)		# t8 = *a0
+
+	li	$v0,	1		# print integer
+	move	$a0,	$t8		# at t8
+	syscall
+
+	li	$v0,	11		# print character
+	li	$a0,	10		# newline
+	syscall
+
+	li	$v0,	10		# terminate program run
+	syscall
diff --git a/mips/chapter-2/exercise-1/h.s b/mips/chapter-2/exercise-1/h.s
new file mode 100644
index 0000000..eaf63bd
--- /dev/null
+++ b/mips/chapter-2/exercise-1/h.s
@@ -0,0 +1,20 @@
+# a0[4] = 32768
+	.data
+array:	.word	4, 2, 0, 6, 9
+
+	.text
+main:
+	la	$a0,	array		# t0 = array
+	li	$t1,	32768		# t1 = 32768
+	sw	$t1,	16($a0)		# t0[4] = t1
+
+	li	$v0,	1		# print integer
+	lw	$a0,	16($a0)		# at t0[4]
+	syscall
+
+	li	$v0,	11		# print character
+	li	$a0,	10		# newline
+	syscall
+
+	li	$v0,	10		# terminate program run
+	syscall
diff --git a/mips/chapter-2/exercise-1/i.s b/mips/chapter-2/exercise-1/i.s
new file mode 100644
index 0000000..8754525
--- /dev/null
+++ b/mips/chapter-2/exercise-1/i.s
@@ -0,0 +1,12 @@
+# print Hello, World!
+	.data
+hello:	.asciiz "Hello, World!\n"
+
+	.text
+main:
+	li	$v0,	4		# print string
+	la	$a0,	hello		# hello
+	syscall
+
+	li	$v0,	10		# terminate program run
+	syscall
diff --git a/mips/chapter-2/exercise-1/j.s b/mips/chapter-2/exercise-1/j.s
new file mode 100644
index 0000000..95eccc7
--- /dev/null
+++ b/mips/chapter-2/exercise-1/j.s
@@ -0,0 +1,16 @@
+# t7 = abs(t0)
+	.text
+main:
+	li	$t0,	-420		# t0 = -420
+	abs	$t7,	$t0		# t7 = abs(t0)
+
+	li	$v0,	1		# print integer
+	move	$a0,	$t7		# at t7
+	syscall
+
+	li	$v0,	11		# print character
+	li	$a0,	10		# newline
+	syscall
+
+	li	$v0,	10		# terminate program run
+	syscall
diff --git a/mips/chapter-2/exercise-1/k.s b/mips/chapter-2/exercise-1/k.s
new file mode 100644
index 0000000..60c0038
--- /dev/null
+++ b/mips/chapter-2/exercise-1/k.s
@@ -0,0 +1,28 @@
+# while (t0) { s1 += t0; t0 = *++t2; }
+	.data
+array:	.word	4, 2, 0, 6, 9
+
+	.text
+main:
+	la	$t2,	array		# t2 = array
+	lw	$t0,	($t2)		# t0 = *t2
+	li	$s1,	0		# s1 = 0
+
+while:
+	beqz	$t0,	end		# if (!t0) goto end
+	add	$s1,	$s1,	$t0	# s1 += t0
+	addi	$t2,	$t2,	4	# t2++
+	lw	$t0,	($t2)		# t0 = *t2
+	j	while			# goto while
+end:
+
+	li	$v0,	1		# print integer
+	move	$a0,	$s1		# at s1
+	syscall
+
+	li	$v0,	11		# print character
+	li	$a0,	10		# newline
+	syscall
+
+	li	$v0,	10		# terminate program run
+	syscall
diff --git a/mips/chapter-2/exercise-1/l.s b/mips/chapter-2/exercise-1/l.s
new file mode 100644
index 0000000..73b999d
--- /dev/null
+++ b/mips/chapter-2/exercise-1/l.s
@@ -0,0 +1,22 @@
+# for (t1 = 99; t1 > 0; v0 += t1--)
+	.text
+main:
+	li	$v0,	0		# v0 = 0
+	li	$t1,	99		# t1 = 99
+for:
+	blez	$t1,	end		# if (t1 <= 0) goto end
+	add	$v0,	$v0,	$t1	# v0 += t1
+	addi	$t1,	$t1,	-1	# t1--
+	j	for			# goto for
+end:
+
+	move	$a0,	$v0		# a0 = v0
+	li	$v0,	1		# print integer at a0
+	syscall
+
+	li	$v0,	11		# print character
+	li	$a0,	10		# newline
+	syscall
+
+	li	$v0,	10		# terminate program run
+	syscall
diff --git a/mips/chapter-2/exercise-1/m.s b/mips/chapter-2/exercise-1/m.s
new file mode 100644
index 0000000..e02f924
--- /dev/null
+++ b/mips/chapter-2/exercise-1/m.s
@@ -0,0 +1,17 @@
+# t0 = 0x7fffffff - 0x80000000
+	.text
+main:
+	li	$t2,	-0x80000000	# t2 = 0x80000000
+	li	$t1,	0x7fffffff	# t1 = 0x7fffffff
+	add	$t0,	$t1,	$t2	# t0 = t1 - t2
+
+	li	$v0,	1		# print integer
+	move	$a0,	$t0		# at t0
+	syscall
+
+	li	$v0,	11		# print character
+	li	$a0,	10		# newline
+	syscall
+
+	li	$v0,	10		# terminate program run
+	syscall
diff --git a/mips/chapter-2/exercise-1/n.s b/mips/chapter-2/exercise-1/n.s
new file mode 100644
index 0000000..4394ebf
--- /dev/null
+++ b/mips/chapter-2/exercise-1/n.s
@@ -0,0 +1,16 @@
+# s0 *= -1
+	.text
+main:
+	li	$s0,	420		# s0 = 420
+	neg	$s0,	$s0		# s0 = -s0
+
+	li	$v0,	1		# print integer
+	move	$a0,	$s0		# at s0
+	syscall
+
+	li	$v0,	11		# print character
+	li	$a0,	10		# newline
+	syscall
+
+	li	$v0,	10		# terminate program run
+	syscall
diff --git a/mips/chapter-2/exercise-1/o.s b/mips/chapter-2/exercise-1/o.s
new file mode 100644
index 0000000..06dfc5c
--- /dev/null
+++ b/mips/chapter-2/exercise-1/o.s
@@ -0,0 +1,17 @@
+# s1 *= a0
+	.text
+main:
+	li	$s1,	420		# s1 = 420
+	li	$a0,	69		# a0 = 69
+	mul	$s1,	$s1,	$a0	# s1 *= a0
+
+	li	$v0,	1		# print integer
+	move	$a0,	$s1		# at s1
+	syscall
+
+	li	$v0,	11		# print character
+	li	$a0,	10		# newline
+	syscall
+
+	li	$v0,	10		# terminate program run
+	syscall
diff --git a/mips/chapter-2/exercise-1/p.s b/mips/chapter-2/exercise-1/p.s
new file mode 100644
index 0000000..a15958f
--- /dev/null
+++ b/mips/chapter-2/exercise-1/p.s
@@ -0,0 +1,21 @@
+# s2 = srt(s0**2 + 56) / a3
+	.text
+main:
+	li	$s0,	420		# s0 = 420
+	li	$a3,	69		# a3 = 69
+
+	mul	$t0,	$s0,	$s0	# t0 = s0 ** 2
+	addi	$a0,	$t0,	56	# a0 = t0 + 56
+	jal	srt			# v0 = srt(a0)	# srt is undefined
+	div	$s2,	$v0,	$a3	# s2 = v0 / a3
+
+	li	$v0,	1		# print integer
+	move	$a0,	$s0		# at s2
+	syscall
+
+	li	$v0,	11		# print character
+	li	$a0,	10		# newline
+	syscall
+
+	li	$v0,	10		# terminate program run
+	syscall
diff --git a/mips/chapter-2/exercise-1/q.s b/mips/chapter-2/exercise-1/q.s
new file mode 100644
index 0000000..47069cd
--- /dev/null
+++ b/mips/chapter-2/exercise-1/q.s
@@ -0,0 +1,20 @@
+# s3 = s1 - s2 / s3
+	.text
+main:
+	li	$s1,	69		# s1 = 69
+	li	$s2,	20		# s2 = 20
+	li	$s3,	4		# s3 = 4
+
+	div	$s3,	$s2,	$s3	# s3 = s2 / s3
+	sub	$s3,	$s1,	$s3	# s3 = s1 - s3
+
+	li	$v0,	1		# print integer
+	move	$a0,	$s3		# at s3
+	syscall
+
+	li	$v0,	11		# print character
+	li	$a0,	10		# newline
+	syscall
+
+	li	$v0,	10		# terminate program run
+	syscall
diff --git a/mips/chapter-2/exercise-1/r.s b/mips/chapter-2/exercise-1/r.s
new file mode 100644
index 0000000..c362e64
--- /dev/null
+++ b/mips/chapter-2/exercise-1/r.s
@@ -0,0 +1,16 @@
+# s4 <<= 3
+	.text
+main:
+	li	$s4,	420		# s4 = 420
+	sll	$s4,	$s4,	3	# s4 <<= 3
+
+	li	$v0,	1		# print integer
+	move	$a0,	$s4		# at s4
+	syscall
+
+	li	$v0,	11		# print character
+	li	$a0,	10		# newline
+	syscall
+
+	li	$v0,	10		# terminate program run
+	syscall
diff --git a/mips/chapter-2/exercise-1/s.s b/mips/chapter-2/exercise-1/s.s
new file mode 100644
index 0000000..07c8a12
--- /dev/null
+++ b/mips/chapter-2/exercise-1/s.s
@@ -0,0 +1,16 @@
+# s5 *= pi
+	.text
+main:
+	li	$s5,	420		# s5 = 420
+	mul	$s5,	$s5,	3	# s5 *= 3
+
+	li	$v0,	1		# print integer
+	move	$a0,	$s5		# at s5
+	syscall
+
+	li	$v0,	11		# print character
+	li	$a0,	10		# newline
+	syscall
+
+	li	$v0,	10		# terminate program run
+	syscall
diff --git a/mips/chapter-2/exercise-3.s b/mips/chapter-2/exercise-3.s
new file mode 100644
index 0000000..11252b7
--- /dev/null
+++ b/mips/chapter-2/exercise-3.s
@@ -0,0 +1,23 @@
+# t0 = (s1 - s0 / s2) * s4
+	.text
+main:
+	li	$s1,	4		# s1 = 4
+	li	$s0,	20		# s0 = 20
+	li	$s2,	6		# s2 = 6
+	li	$s4,	9		# s4 = 9
+
+	div	$t0,	$s0,	$s2	# t0 = s0 / s2
+	sub	$t0,	$s1,	$t0	# t0 = s1 - t0
+	mul	$t0,	$t0,	$s4	# t0 *= s4
+
+	li	$v0,	1		# print integer
+	move	$a0,	$t0		# at t0
+	syscall
+
+	li	$v0,	11		# print character
+	li	$a0,	10		# newline
+	syscall
+
+
+	li	$v0,	10		# terminate
+	syscall
diff --git a/mips/chapter-2/exercise-5.s b/mips/chapter-2/exercise-5.s
new file mode 100644
index 0000000..84d274d
--- /dev/null
+++ b/mips/chapter-2/exercise-5.s
@@ -0,0 +1,23 @@
+# t0 = s0/8 - s1*2 + s2
+	.text
+main:
+	li	$s0,	69		# s0 = 20
+	li	$s1,	4		# s1 = 4
+	li	$s2,	20		# s2 = 20
+
+	sra	$t0,	$s0,	3	# t0 = s0 >> 3
+	sll	$t1,	$s1,	1	# t1 = s1 << 1
+	sub	$t0,	$t0,	$t1	# t0 -= t1
+	add	$t0,	$t0,	$s2	# t0 += s2
+
+	li	$v0,	1		# print integer
+	move	$a0,	$t0		# at t0
+	syscall
+
+	li	$v0,	11		# print character
+	li	$a0,	10		# newline
+	syscall
+
+
+	li	$v0,	10		# terminate
+	syscall