Advertisement
Guest User

Untitled

a guest
Dec 13th, 2017
64
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 5.11 KB | None | 0 0
  1. # ECOAR LAB - MIPS PROJECT #
  2. # Michał Kamiński - A6
  3. # Determining shape #
  4.  
  5.  
  6. .data
  7. inputMsg: .asciiz "Enter the file name: "
  8. shape1string: .asciiz "Shape 1\n"
  9. shape2string: .asciiz "Shape 2\n"
  10. NameOfFile: .space 128
  11.  
  12. errorMsg: .asciiz "Descriptor error. Program restarting.\n"
  13. bitmapMsg: .asciiz "The file entered is not a bitmap. Program restarting.\n"
  14. formatMsg: .asciiz "The file entered is a bitmap, but is not 24-bit. Program restarting.\n"
  15. sizeMsg: .asciiz "The file has wrong width . Program restarting.\n"
  16.  
  17. buffer: .space 2 # for proper header alignment in data section
  18. header: .space 54 # the bmp file header size
  19. width: .word 320 # header+18
  20. height: .word 240 # header+22
  21. .text
  22. main:
  23. # begin the program, print prompt
  24. li $v0, 4 # syscall-4 print string
  25. la $a0, inputMsg # load address of the input msg
  26. syscall
  27.  
  28. # read the input file name
  29. li $v0, 8 # syscall-8 read string
  30. la $a0, NameOfFile # load address of the NameOfFile
  31. li $a1, 128 # load the maximum number of characters to read
  32. syscall
  33.  
  34. # cut the '\n' from the NameOfFile
  35. move $t0, $zero # load 0 to $t0 to make sure that it starts from the beginning of the string
  36. li $t2, '\n' # load the '\n' character to the $t2 register
  37.  
  38. # find the '\n'
  39. findN:
  40. lb $t1, NameOfFile($t0) # read the NameOfFile
  41. beq $t1, $t2, removeN # check if '\n'
  42. addi $t0, $t0, 1
  43. j findN
  44.  
  45. # remove the '\n', swap with '\0'
  46. removeN:
  47. li $t1, '\0' # replace '\n' with '\0'
  48. sb $t1, NameOfFile($t0)
  49.  
  50. # open input file for reading
  51. li $v0, 13 # syscall-13 open file
  52. la $a0, NameOfFile # load filename address
  53. li $a1, 0 # 0 flag for reading the file
  54. li $a2, 0 # mode 0
  55. syscall
  56. # $v0 contains the file descriptor
  57. bltz $v0, fileError
  58.  
  59. move $s0, $v0
  60.  
  61. # read the header data
  62. li $v0, 14 # syscall-14 read from file
  63. move $a0, $s0 # load the file descriptor
  64. la $a1, header # load header address to store
  65. li $a2, 54 # read first 54 bytes of the file
  66. syscall
  67.  
  68. # check if our file is a bitmap
  69. li $t0, 0x4D42
  70. lhu $t1, header
  71.  
  72. bne $t0, $t1, bitmapError
  73.  
  74. # check if it is the right size
  75. lw $t0, width # width (320) = $t0
  76. lw $s1, header+18
  77. bne $t0, $s1, sizeError
  78. lw $t0, height
  79. lw $s2, header+22
  80. bne $t0, $s2, sizeError
  81.  
  82. # confirm that the bitmap is actually 24 bits
  83. li $t0, 24 # store 24 into $t0, because it is a 24-bit bitmap (uncompressed)
  84. lb $t1, header+28
  85.  
  86. bne $t0, $t1, formatError
  87.  
  88. # Everything seems ok, lets move forward
  89. lw $s3, header+34 # store the size of the data section of the image
  90.  
  91. # read image data into array - allocationg heap memory
  92. li $v0, 9
  93. move $a0, $s3
  94. syscall
  95. move $s4, $v0
  96.  
  97. li $v0, 14 # syscall-14, read from file
  98. move $a0, $s0 # load the file descriptor
  99. move $a1, $s4 # load base address of array
  100. move $a2, $s3 # load size of data section
  101. syscall
  102.  
  103. # close the file
  104. closeFile:
  105. li $v0, 16 # syscall-16 close file
  106. move $a0, $s0
  107. syscall
  108.  
  109. #----------------------------------MAIN PROGRAM--------------------------------------#
  110.  
  111.  
  112. SetUp:
  113. move $t3, $s4 # load base address of the image
  114. li $t4, 0 # Position of first column of black image
  115. move $t6, $s1 # width offset
  116. mul $t6, $t6, 3 # multiply to get the number of BGR threes in a row
  117. li $t5, 0 # Position of last colum of black image
  118.  
  119.  
  120. lookforblack: #we are looking for black pixels inside our image
  121. lb $t0, ($t3)
  122. beqz $t0,blackappeared
  123. add $t3, $t3, 3
  124. j lookforblack
  125.  
  126. blackappeared:
  127. move $t4,$t3 # start of black block - t4
  128. widthofblack:
  129. lb $t0, ($t3)
  130. bnez $t0, pre
  131. add $t3, $t3, 3
  132. j widthofblack
  133.  
  134. pre: #t4 - beginning of block, t5 - end of block
  135. sub $t3, $t3, 3
  136. add $t4,$t4,$t6
  137.  
  138. move $t5,$t3
  139. add $t5,$t5,$t6
  140. move $t3,$t4 #next row in black box
  141. loopblack:
  142. lb $t0, ($t3)
  143. beq $t3,$t5,nextrow
  144. bnez $t0,foundwhite
  145. add $t3, $t3, 3
  146. j loopblack
  147. nextrow:
  148. add $t5,$t5,$t6
  149. add $t4,$t4,$t6
  150. move $t3,$t4
  151. j loopblack
  152. foundwhite:
  153. add $t3,$t3,$t6
  154. lb $t0, ($t3)
  155. beqz $t0, shape2
  156. j shape1
  157.  
  158. shape1:
  159. li $v0, 4 # syscall-4 print string
  160. la $a0, shape1string
  161. syscall
  162. j end
  163.  
  164. shape2:
  165. li $v0, 4 # syscall-4 print string
  166. la $a0, shape2string
  167. syscall
  168.  
  169. #----------------------- THE END OF MAIN----------------------------#
  170.  
  171.  
  172. # end the program
  173. end:
  174. li $v0, 10 # syscall-10 exit
  175. syscall
  176.  
  177.  
  178. # print file error message
  179. fileError:
  180. li $v0, 4 # syscall-4 print string
  181. la $a0, errorMsg
  182. syscall
  183. j main # restart the program
  184.  
  185. # print bitmap error message
  186. bitmapError:
  187. li $v0, 4 # syscall-4 print string
  188. la $a0, bitmapMsg
  189. syscall
  190. j main # restart the program
  191.  
  192. # print format error message
  193. formatError:
  194. li $v0, 4 # syscall-4 print string
  195. la $a0, formatMsg
  196. syscall
  197. j main # restart the program
  198.  
  199. # print size error message
  200. sizeError:
  201. li $v0, 4 # syscall-4 print string
  202. la $a0, sizeMsg
  203. syscall
  204. j main # restart the program
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement