1
0
Fork 0

[mips] Finish the second chapter

This commit is contained in:
Nguyễn Gia Phong 2020-02-10 17:49:10 +07:00
parent 57bfdfe2e5
commit 9e37e768b4
23 changed files with 390 additions and 0 deletions

View File

@ -12,6 +12,7 @@ this README as well as commit messages are duolingo (anglais et vietnamien).
| `codeforces` | [Codeforces][3] |
| `cpptour` | A Tour of C++ |
| `daily` | [/r/dailyprogrammer][4] |
| `mips` | MIPS Assembly Language Programming |
| `ntu` | [Đại học Nha Trang][1] |
| `others` | Các đề bài không rõ nguồn |
| `paip` | Paradigms of Artificial Intelligence Programming |
@ -39,6 +40,7 @@ Phiên bản các trình dịch sử dụng test:
| Common Lisp | SBCL 1.4.8+ |
| Java | OpenJDK 11+ |
| Lua | Lua 5.1+ |
| MIPS ASM | SPIM 8.0 |
| Octave | Octave 6+ |
| Pascal | Free Pascal 2.6.4+ |
| Python | Python 3.5+ |

View File

@ -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

View File

@ -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

View File

@ -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

View File

@ -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

View File

@ -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

View File

@ -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

View File

@ -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

View File

@ -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

View File

@ -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

View File

@ -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

View File

@ -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

View File

@ -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

View File

@ -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

View File

@ -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

View File

@ -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

View File

@ -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

View File

@ -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

View File

@ -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

View File

@ -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

View File

@ -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

View File

@ -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

BIN
mips/mips.pdf Normal file

Binary file not shown.