Advertisement
Guest User

Untitled

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