Advertisement
Guest User

Untitled

a guest
Dec 18th, 2017
59
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 7.43 KB | None | 0 0
  1.  
  2. .data
  3. FileName: .space 128
  4. buffer: .space 2 # for proper header alignment in data section
  5. header: .space 54 # the bmp file header size
  6. width: .word 320 # width of the bmp, header+18
  7. height: .word 240 # height of the bmp, header+22
  8.  
  9. input: .asciiz "Enter the file name: "
  10. output1: .asciiz "Shape 1\n"
  11. output2: .asciiz "Shape 2\n"
  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. allwhiteMsg: .asciiz "All white!\n"
  17.  
  18.  
  19. .text
  20. main:
  21. # begin, print prompt
  22. li $v0, 4
  23. la $a0, input # load address of the input msg
  24. syscall
  25.  
  26. # read the input file name
  27. li $v0, 8
  28. la $a0, FileName
  29. li $a1, 128 # load the maximum number of characters to read
  30. syscall
  31.  
  32. # cut the '\n' from the FileName
  33. move $t0, $zero # load 0 to $t0 to make sure that it starts from the beginning of the string
  34. li $t2, '\n' # load the '\n' character to the $t2 register
  35.  
  36. # find the '\n'
  37. Find_n:
  38. lb $t1, FileName($t0) # read the FileName
  39. beq $t1, $t2, remove_n # check if '\n'
  40. addi $t0, $t0, 1
  41. j Find_n
  42.  
  43. # remove the '\n', swap with '\0'
  44. remove_n:
  45. li $t1, '\0' # replace '\n' with '\0'
  46. sb $t1, FileName($t0)
  47.  
  48. # open input file for reading
  49. li $v0, 13 # open file
  50. la $a0, FileName # load filename address
  51. li $a1, 0 # 0 flag for reading the file
  52. li $a2, 0 # mode 0
  53. syscall
  54.  
  55. # $v0 contains the file descriptor
  56. bltz $v0, fileError # if $v0=-1, there is a descriptor error, go to fileError and the file cannot be read
  57. move $s0, $v0 # save the file descriptor from $v0 for closing the file
  58.  
  59. # read the header data
  60. li $v0, 14
  61. move $a0, $s0 # load the file descriptor
  62. la $a1, header # load header address to store
  63. li $a2, 54 # read first 54 bytes of the file
  64. syscall
  65.  
  66. # check if our file is a bitmap
  67. li $t0, 0x4D42 # 0X4D42 is the signature for a bitmap (hex for "BM")
  68. lhu $t1, header # the signature is stored in the first two bytes (header+0) # lhu - load halfword unsigned - loads the first 2 bytes into $t1 register
  69. bne $t0, $t1, bitmapError # if these two aren't equal then the input is not a bitmap
  70.  
  71. # check if it is the right size
  72. lw $t0, width # width (320) = $t0
  73. lw $s1, header+18 # read the file width from the header information (offset of 18) - need to read only 2 bytes
  74. bne $t0, $s1, sizeError # if not equal, go to sizeError
  75. lw $t0, height # height (240) = $t0
  76. lw $s2, header+22 # read the file height from the header information (offset of 22) - need to read only 2 bytes
  77. bne $t0, $s2, sizeError # if not equal, go to sizeError
  78.  
  79. # confirm that the bitmap is actually 24 bits
  80. li $t0, 24 # store 24 into $t0, because it is a 24-bit bitmap (uncompressed)
  81. lb $t1, header+28
  82.  
  83. bne $t0, $t1, formatError
  84.  
  85. # Everything seems ok, lets move forward
  86. lw $s3, header+34 # store the size of the data section of the image
  87.  
  88. # read image data into array - allocationg heap memory
  89. li $v0, 9
  90. move $a0, $s3
  91. syscall
  92. move $s4, $v0
  93.  
  94. li $v0, 14 # read from file
  95. move $a0, $s0 # load the file descriptor
  96. move $a1, $s4 # load base address of array
  97. move $a2, $s3 # load size of data section
  98. syscall
  99.  
  100. # close the file
  101. closeFile:
  102. li $v0, 16 # close file
  103. move $a0, $s0
  104. syscall
  105.  
  106. #--MAIN PROGRAM--#
  107.  
  108.  
  109. shapeRecognition:
  110. move $t3, $s4 # load base address of the image
  111. li $t4, 0 # Position of first column of black image, width counter
  112. move $t6, $s1 # width offset
  113. mul $t6, $t6, 3 # multiply to get the number of BGR threes in a row
  114. li $t5, 0 # Position of last colum of black image
  115. # calculating the [w-1][h-1] pixel to use it as the ending point, otherwise the program might go out of bounds
  116. move $t2, $s3 # move the size of the array to $t2 (0x38400 = 230400)
  117. sub $t2, $t2, 3 # reference to [w-1]
  118. sub $t2, $t2, $t6 # reference to [h-1]
  119. add $t2,$s4,$t2
  120.  
  121.  
  122. look_black:
  123. lb $t0, ($t3)
  124. beq $t3, $t2, allwhite #t2 has the size, if equal to t3, all white
  125. beqz $t0,blackappeared #checking if black pixel appeared
  126. add $t3, $t3, 3
  127. j look_black
  128.  
  129. blackappeared:
  130. move $t4,$t3 # start of black block - t4
  131.  
  132. width_black:
  133. lb $t0, ($t3)
  134. bnez $t0, recognition #looking for width of our black box
  135. add $t3, $t3, 3
  136. j width_black
  137.  
  138. recognition: #t4 - beginning of block, t5 - end of block
  139. add $t4,$t4,$t6
  140. sub $t3, $t3, 3
  141. move $t5,$t3
  142. add $t5,$t5,$t6
  143. move $t3,$t4 #next row in black box
  144.  
  145. loopblack:
  146. lb $t0, ($t3)
  147. beq $t3,$t5, nextrow #if t3 reaches t5
  148. bnez $t0, foundwhite # if not equal to zero
  149. add $t3, $t3, 3
  150. j loopblack
  151.  
  152. nextrow:
  153. add $t5,$t5,$t6 # add width offset to end of block
  154. add $t4,$t4,$t6 # add width offset to begining of block
  155. move $t3,$t4
  156. j loopblack
  157.  
  158. foundwhite:
  159. sub $t3,$t3,3
  160.  
  161. checkingshape:
  162. lb $t0, ($t3)
  163. bnez $t0, shape2 #if not equal to zero
  164. add $t3,$t3,3
  165. lb $t0, ($t3)
  166. beqz $t0, shape1 # if equal to zero
  167. sub $t3,$t3,3
  168. add $t3,$t3,$t6
  169. j checkingshape
  170.  
  171. shape1:
  172. li $v0, 4 # print string
  173. la $a0, output1
  174. syscall
  175. j end
  176.  
  177. shape2:
  178. li $v0, 4 # print string
  179. la $a0, output2
  180. syscall
  181. j end
  182.  
  183. allwhite:
  184. li $v0, 4 # print string
  185. la $a0,allwhiteMsg
  186. syscall
  187.  
  188.  
  189. #END#
  190.  
  191. # end the program
  192. end:
  193. li $v0, 10 # exit
  194. syscall
  195.  
  196.  
  197. # print file error message
  198. fileError:
  199. li $v0, 4 # print string
  200. la $a0, errorMsg
  201. syscall
  202. j main # restart the program
  203.  
  204. # print bitmap error message
  205. bitmapError:
  206. li $v0, 4 # print string
  207. la $a0, bitmapMsg
  208. syscall
  209. j main # restart the program
  210.  
  211. # print format error message
  212. formatError:
  213. li $v0, 4 # print string
  214. la $a0, formatMsg
  215. syscall
  216. j main # restart the program
  217.  
  218. # print size error message
  219. sizeError:
  220. li $v0, 4 # print string
  221. la $a0, sizeMsg
  222. syscall
  223. j main # restart the program
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement