-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathmips_q7.asm
71 lines (58 loc) · 1.87 KB
/
mips_q7.asm
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
.text
# Open (for writing) a file that does not exist
addi $v0,$zero,13 # system call for open file
la $a0, fout # output file name
addi $a1,$zero,1 # Open for writing (flags are 0: read, 1: write)
addi $a2,$zero,0 # mode is ignored
syscall # open a file (file descriptor returned in $v0)
move $s6, $v0 # save the file descriptor
# Open (for reading) a file
addi $v0,$zero,13 # system call for open file
la $a0, fin # input file name
addi $a1,$zero,0 # Open for reading
addi $a2,$zero,0 # mode is ignored
syscall # open a file (file descriptor returned in $v0)
move $s7, $v0 # save the file descriptor
addi $t0,$zero,0 # counter
addi $t1,$zero,0 # fib first element
addi $t2,$zero,1 # fib sec element
loop:
# Read from file
addi $v0,$zero,14 # system call for read
move $a0, $s7 # file descriptor
la $a1, buffer # address of buffer from which to read
addi $a2,$zero,1 # read file
syscall
beq $v0,$zero,end # if end of file is reached
beq $t0,$t2,is_fib # equals to fib num
beq $t0,$t1,is_fib # equals to fib num
# Write to file
addi $v0,$zero,15 # system call for write to file
move $a0, $s6 # file descriptor
la $a1, buffer # address of buffer from which to write
addi $a2,$zero,1 # hardcoded buffer length
syscall # write to file
addi $t0,$t0,1 # counter ++
j loop # iterate over file
end:
# Close the file
addi $v0,$zero,16 # system call for close file
move $a0, $s6 # file descriptor to close
syscall # close file
# Close the file
addi $v0,$zero,16 # system call for close file
move $a0, $s7 # file descriptor to close
syscall # close file
# close running
addi $v0,$zero,10 # end of code
syscall
is_fib:
move $t3,$t2 # find next fib_pair numbers
add $t2,$t2,$t1
move $t1,$t3
addi $t0,$t0,1 # counter ++
j loop
.data
fout: .asciiz "testout.txt" # filename for output
fin: .asciiz "testin.txt" # filename for input
buffer: .space 2