From 29cd51b76067e02683b9b95f4ce2582eac8669e4 Mon Sep 17 00:00:00 2001 From: Nguyễn Gia Phong Date: Fri, 28 Feb 2020 22:09:59 +0700 Subject: [mips] Finish chapter 3 --- mips/chapter-3/exercise-10.s | 41 ++++++++++++++++++++++++++++++++++++++ mips/chapter-3/exercise-11.s | 41 ++++++++++++++++++++++++++++++++++++++ mips/chapter-3/exercise-13.s | 18 +++++++++++++++++ mips/chapter-3/exercise-14.s | 19 ++++++++++++++++++ mips/chapter-3/exercise-15.s | 19 ++++++++++++++++++ mips/chapter-3/exercise-16.s | 19 ++++++++++++++++++ mips/chapter-3/exercise-3.s | 20 +++++++++++++++++++ mips/chapter-3/exercise-4.s | 20 +++++++++++++++++++ mips/chapter-3/exercise-6.s | 47 ++++++++++++++++++++++++++++++++++++++++++++ mips/chapter-3/exercise-7.s | 47 ++++++++++++++++++++++++++++++++++++++++++++ 10 files changed, 291 insertions(+) create mode 100644 mips/chapter-3/exercise-10.s create mode 100644 mips/chapter-3/exercise-11.s create mode 100644 mips/chapter-3/exercise-13.s create mode 100644 mips/chapter-3/exercise-14.s create mode 100644 mips/chapter-3/exercise-15.s create mode 100644 mips/chapter-3/exercise-16.s create mode 100644 mips/chapter-3/exercise-3.s create mode 100644 mips/chapter-3/exercise-4.s create mode 100644 mips/chapter-3/exercise-6.s create mode 100644 mips/chapter-3/exercise-7.s 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 diff --git a/mips/chapter-3/exercise-11.s b/mips/chapter-3/exercise-11.s new file mode 100644 index 0000000..f47f29f --- /dev/null +++ b/mips/chapter-3/exercise-11.s @@ -0,0 +1,41 @@ +# print -32 in 8-bit two-complement binary + .text +main: + li $t0, -32 # t0 = -32 + 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 diff --git a/mips/chapter-3/exercise-13.s b/mips/chapter-3/exercise-13.s new file mode 100644 index 0000000..6779d11 --- /dev/null +++ b/mips/chapter-3/exercise-13.s @@ -0,0 +1,18 @@ +# print(int('204', 8)) + .text +main: + li $t0, 0 # t0 = 0 + addi $t0, $t0, 2 # t0 += 2 + sll $t0, $t0, 6 # t0 *= 8 * 8 + addi $t0, $t0, 4 # t0 += 4 + + 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-3/exercise-14.s b/mips/chapter-3/exercise-14.s new file mode 100644 index 0000000..b01b76b --- /dev/null +++ b/mips/chapter-3/exercise-14.s @@ -0,0 +1,19 @@ +# print(int('204', 7)) + .text +main: + li $t0, 0 # t0 = 0 + addi $t0, $t0, 2 # t0 += 2 + li $t1, 49 # t1 = 7 * 7 + mul $t0, $t0, $t1 # t0 *= t1 + addi $t0, $t0, 4 # t0 += 4 + + 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-3/exercise-15.s b/mips/chapter-3/exercise-15.s new file mode 100644 index 0000000..3a16b14 --- /dev/null +++ b/mips/chapter-3/exercise-15.s @@ -0,0 +1,19 @@ +# print(int('204', 6)) + .text +main: + li $t0, 0 # t0 = 0 + addi $t0, $t0, 2 # t0 += 2 + li $t1, 36 # t1 = 6 * 6 + mul $t0, $t0, $t1 # t0 *= t1 + addi $t0, $t0, 4 # t0 += 4 + + 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-3/exercise-16.s b/mips/chapter-3/exercise-16.s new file mode 100644 index 0000000..1aa9dec --- /dev/null +++ b/mips/chapter-3/exercise-16.s @@ -0,0 +1,19 @@ +# print(int('204', 5)) + .text +main: + li $t0, 0 # t0 = 0 + addi $t0, $t0, 2 # t0 += 2 + li $t1, 25 # t1 = 5 * 5 + mul $t0, $t0, $t1 # t0 *= t1 + addi $t0, $t0, 4 # t0 += 4 + + 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-3/exercise-3.s b/mips/chapter-3/exercise-3.s new file mode 100644 index 0000000..cb2475c --- /dev/null +++ b/mips/chapter-3/exercise-3.s @@ -0,0 +1,20 @@ +# t0 = 0b10101, using only bit shift and add + .text +main: + li $t0, 0 # t0 = 0 + addi $t0, $t0, 1 # t0++ + sll $t0, $t0, 2 # t0 <<= 2 + addi $t0, $t0, 1 # t0++ + sll $t0, $t0, 2 # t0 <<= 2 + addi $t0, $t0, 1 # t0++ + + 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-3/exercise-4.s b/mips/chapter-3/exercise-4.s new file mode 100644 index 0000000..29888f1 --- /dev/null +++ b/mips/chapter-3/exercise-4.s @@ -0,0 +1,20 @@ +# t0 = 0b11001, using only bit shift and add + .text +main: + li $t0, 0 # t0 = 0 + addi $t0, $t0, 1 # t0++ + sll $t0, $t0, 1 # t0 <<= 1 + addi $t0, $t0, 1 # t0++ + sll $t0, $t0, 3 # t0 <<= 3 + addi $t0, $t0, 1 # t0++ + + 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-3/exercise-6.s b/mips/chapter-3/exercise-6.s new file mode 100644 index 0000000..8c1b6fd --- /dev/null +++ b/mips/chapter-3/exercise-6.s @@ -0,0 +1,47 @@ +# print(hex(0b10101)) + .text +main: + li $t0, 21 # t0 = 0b10101 + li $t1, 0 # t1 = 0 + li $t9, 9 # t9 = 9 + +reverse: + beqz $t0, done # if (!t0) goto done + andi $t2, $t0, 0xf # t2 = t0 & 0xf + sra $t0, $t0, 4 # t0 >>= 4 + sll $t1, $t1, 4 # t1 <<= 4 + add $t1, $t1, $t2 # t1 += t2 + j reverse # goto reverse +done: + + li $v0, 11 # print character + li $a0, 48 # 0 + syscall + li $v0, 11 # print character + li $a0, 120 # x + syscall + bnez $t1, print # if (!t1) goto print + li $v0, 11 # print character + li $a0, 48 # 0 + syscall + j end # goto end + +print: + beqz $t1, end # if (!t1) goto end + andi $t2, $t1, 0xf # t2 = t1 & 0xf + sra $t1, $t1, 4 # t1 >>= 4 + addi $a0, $t2, 48 # a0 = chr(t2), sort of + ble $t2, $t9, put # if (t2 <= 9) goto put + addi $a0, $a0, 7 # a0 += 7 +put: + li $v0, 11 # print character at a0 + syscall + j print # goto print +end: + + li $v0, 11 # print character + li $a0, 10 # newline + syscall + + li $v0, 10 # terminate + syscall diff --git a/mips/chapter-3/exercise-7.s b/mips/chapter-3/exercise-7.s new file mode 100644 index 0000000..924765d --- /dev/null +++ b/mips/chapter-3/exercise-7.s @@ -0,0 +1,47 @@ +# print(hex(0b11001)) + .text +main: + li $t0, 25 # t0 = 0b11001 + li $t1, 0 # t1 = 0 + li $t9, 9 # t9 = 9 + +reverse: + beqz $t0, done # if (!t0) goto done + andi $t2, $t0, 0xf # t2 = t0 & 0xf + sra $t0, $t0, 4 # t0 >>= 4 + sll $t1, $t1, 4 # t1 <<= 4 + add $t1, $t1, $t2 # t1 += t2 + j reverse # goto reverse +done: + + li $v0, 11 # print character + li $a0, 48 # 0 + syscall + li $v0, 11 # print character + li $a0, 120 # x + syscall + bnez $t1, print # if (!t1) goto print + li $v0, 11 # print character + li $a0, 48 # 0 + syscall + j end # goto end + +print: + beqz $t1, end # if (!t1) goto end + andi $t2, $t1, 0xf # t2 = t1 & 0xf + sra $t1, $t1, 4 # t1 >>= 4 + addi $a0, $t2, 48 # a0 = chr(t2), sort of + ble $t2, $t9, put # if (t2 <= 9) goto put + addi $a0, $a0, 7 # a0 += 7 +put: + li $v0, 11 # print character at a0 + syscall + j print # goto print +end: + + li $v0, 11 # print character + li $a0, 10 # newline + syscall + + li $v0, 10 # terminate + syscall -- cgit 1.4.1