diff options
author | Nguyễn Gia Phong <mcsinyx@disroot.org> | 2021-02-28 17:25:57 +0700 |
---|---|---|
committer | Nguyễn Gia Phong <mcsinyx@disroot.org> | 2021-02-28 17:25:57 +0700 |
commit | 67bb27e9f5beec4d8ca0a6c29f045d5f23d6f40f (patch) | |
tree | b677228d71b638dd6e423b037311ef0913ba66e7 /lang/mips | |
parent | ee9b8fc921f48dc893808e1c9dbfbef321aa362c (diff) | |
download | cp-67bb27e9f5beec4d8ca0a6c29f045d5f23d6f40f.tar.gz |
[lang] Reorganize language learning archive
Diffstat (limited to 'lang/mips')
32 files changed, 677 insertions, 0 deletions
diff --git a/lang/mips/chapter-2/exercise-1/a.s b/lang/mips/chapter-2/exercise-1/a.s new file mode 100644 index 0000000..4da4645 --- /dev/null +++ b/lang/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/lang/mips/chapter-2/exercise-1/b.s b/lang/mips/chapter-2/exercise-1/b.s new file mode 100644 index 0000000..fa590b5 --- /dev/null +++ b/lang/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/lang/mips/chapter-2/exercise-1/c.s b/lang/mips/chapter-2/exercise-1/c.s new file mode 100644 index 0000000..370af96 --- /dev/null +++ b/lang/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/lang/mips/chapter-2/exercise-1/d.s b/lang/mips/chapter-2/exercise-1/d.s new file mode 100644 index 0000000..e20cd4c --- /dev/null +++ b/lang/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/lang/mips/chapter-2/exercise-1/e.s b/lang/mips/chapter-2/exercise-1/e.s new file mode 100644 index 0000000..d35702d --- /dev/null +++ b/lang/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/lang/mips/chapter-2/exercise-1/f.s b/lang/mips/chapter-2/exercise-1/f.s new file mode 100644 index 0000000..2ec14ef --- /dev/null +++ b/lang/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/lang/mips/chapter-2/exercise-1/g.s b/lang/mips/chapter-2/exercise-1/g.s new file mode 100644 index 0000000..e9d8f8f --- /dev/null +++ b/lang/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/lang/mips/chapter-2/exercise-1/h.s b/lang/mips/chapter-2/exercise-1/h.s new file mode 100644 index 0000000..eaf63bd --- /dev/null +++ b/lang/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/lang/mips/chapter-2/exercise-1/i.s b/lang/mips/chapter-2/exercise-1/i.s new file mode 100644 index 0000000..8754525 --- /dev/null +++ b/lang/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/lang/mips/chapter-2/exercise-1/j.s b/lang/mips/chapter-2/exercise-1/j.s new file mode 100644 index 0000000..95eccc7 --- /dev/null +++ b/lang/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/lang/mips/chapter-2/exercise-1/k.s b/lang/mips/chapter-2/exercise-1/k.s new file mode 100644 index 0000000..60c0038 --- /dev/null +++ b/lang/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/lang/mips/chapter-2/exercise-1/l.s b/lang/mips/chapter-2/exercise-1/l.s new file mode 100644 index 0000000..73b999d --- /dev/null +++ b/lang/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/lang/mips/chapter-2/exercise-1/m.s b/lang/mips/chapter-2/exercise-1/m.s new file mode 100644 index 0000000..e02f924 --- /dev/null +++ b/lang/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/lang/mips/chapter-2/exercise-1/n.s b/lang/mips/chapter-2/exercise-1/n.s new file mode 100644 index 0000000..4394ebf --- /dev/null +++ b/lang/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/lang/mips/chapter-2/exercise-1/o.s b/lang/mips/chapter-2/exercise-1/o.s new file mode 100644 index 0000000..06dfc5c --- /dev/null +++ b/lang/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/lang/mips/chapter-2/exercise-1/p.s b/lang/mips/chapter-2/exercise-1/p.s new file mode 100644 index 0000000..a15958f --- /dev/null +++ b/lang/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/lang/mips/chapter-2/exercise-1/q.s b/lang/mips/chapter-2/exercise-1/q.s new file mode 100644 index 0000000..47069cd --- /dev/null +++ b/lang/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/lang/mips/chapter-2/exercise-1/r.s b/lang/mips/chapter-2/exercise-1/r.s new file mode 100644 index 0000000..c362e64 --- /dev/null +++ b/lang/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/lang/mips/chapter-2/exercise-1/s.s b/lang/mips/chapter-2/exercise-1/s.s new file mode 100644 index 0000000..07c8a12 --- /dev/null +++ b/lang/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/lang/mips/chapter-2/exercise-3.s b/lang/mips/chapter-2/exercise-3.s new file mode 100644 index 0000000..d82da72 --- /dev/null +++ b/lang/mips/chapter-2/exercise-3.s @@ -0,0 +1,22 @@ +# 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/lang/mips/chapter-2/exercise-5.s b/lang/mips/chapter-2/exercise-5.s new file mode 100644 index 0000000..ffb4058 --- /dev/null +++ b/lang/mips/chapter-2/exercise-5.s @@ -0,0 +1,22 @@ +# 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 diff --git a/lang/mips/chapter-3/exercise-10.s b/lang/mips/chapter-3/exercise-10.s new file mode 100644 index 0000000..f3ddeef --- /dev/null +++ b/lang/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/lang/mips/chapter-3/exercise-11.s b/lang/mips/chapter-3/exercise-11.s new file mode 100644 index 0000000..f47f29f --- /dev/null +++ b/lang/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/lang/mips/chapter-3/exercise-13.s b/lang/mips/chapter-3/exercise-13.s new file mode 100644 index 0000000..6779d11 --- /dev/null +++ b/lang/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/lang/mips/chapter-3/exercise-14.s b/lang/mips/chapter-3/exercise-14.s new file mode 100644 index 0000000..b01b76b --- /dev/null +++ b/lang/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/lang/mips/chapter-3/exercise-15.s b/lang/mips/chapter-3/exercise-15.s new file mode 100644 index 0000000..3a16b14 --- /dev/null +++ b/lang/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/lang/mips/chapter-3/exercise-16.s b/lang/mips/chapter-3/exercise-16.s new file mode 100644 index 0000000..1aa9dec --- /dev/null +++ b/lang/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/lang/mips/chapter-3/exercise-3.s b/lang/mips/chapter-3/exercise-3.s new file mode 100644 index 0000000..cb2475c --- /dev/null +++ b/lang/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/lang/mips/chapter-3/exercise-4.s b/lang/mips/chapter-3/exercise-4.s new file mode 100644 index 0000000..29888f1 --- /dev/null +++ b/lang/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/lang/mips/chapter-3/exercise-6.s b/lang/mips/chapter-3/exercise-6.s new file mode 100644 index 0000000..8c1b6fd --- /dev/null +++ b/lang/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/lang/mips/chapter-3/exercise-7.s b/lang/mips/chapter-3/exercise-7.s new file mode 100644 index 0000000..924765d --- /dev/null +++ b/lang/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 diff --git a/lang/mips/mips.pdf b/lang/mips/mips.pdf new file mode 100644 index 0000000..4a38047 --- /dev/null +++ b/lang/mips/mips.pdf Binary files differ |