Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- .data
- inputMsg: .asciiz "Enter the file name: "
- shape1string: .asciiz "Shape 1\n"
- shape2string: .asciiz "Shape 2\n"
- NameOfFile: .space 128
- errorMsg: .asciiz "Descriptor error. Program restarting.\n"
- bitmapMsg: .asciiz "The file entered is not a bitmap. Program restarting.\n"
- formatMsg: .asciiz "The file entered is a bitmap, but is not 24-bit. Program restarting.\n"
- sizeMsg: .asciiz "The file has wrong width . Program restarting.\n"
- buffer: .space 2 # for proper header alignment in data section
- header: .space 54 # the bmp file header size
- width: .word 320 # header+18
- height: .word 240 # header+22
- .text
- main:
- # begin the program, print prompt
- li $v0, 4 # syscall-4 print string
- la $a0, inputMsg # load address of the input msg
- syscall
- # read the input file name
- li $v0, 8 # syscall-8 read string
- la $a0, NameOfFile # load address of the NameOfFile
- li $a1, 128 # load the maximum number of characters to read
- syscall
- # cut the '\n' from the NameOfFile
- move $t0, $zero # load 0 to $t0 to make sure that it starts from the beginning of the string
- li $t2, '\n' # load the '\n' character to the $t2 register
- # find the '\n'
- findN:
- lb $t1, NameOfFile($t0) # read the NameOfFile
- beq $t1, $t2, removeN # check if '\n'
- addi $t0, $t0, 1
- j findN
- # remove the '\n', swap with '\0'
- removeN:
- li $t1, '\0' # replace '\n' with '\0'
- sb $t1, NameOfFile($t0)
- # open input file for reading
- li $v0, 13 # syscall-13 open file
- la $a0, NameOfFile # load filename address
- li $a1, 0 # 0 flag for reading the file
- li $a2, 0 # mode 0
- syscall
- # $v0 contains the file descriptor
- bltz $v0, fileError
- move $s0, $v0
- # read the header data
- li $v0, 14 # syscall-14 read from file
- move $a0, $s0 # load the file descriptor
- la $a1, header # load header address to store
- li $a2, 54 # read first 54 bytes of the file
- syscall
- # check if our file is a bitmap
- li $t0, 0x4D42
- lhu $t1, header
- bne $t0, $t1, bitmapError
- # check if it is the right size
- lw $t0, width # width (320) = $t0
- lw $s1, header+18
- bne $t0, $s1, sizeError
- lw $t0, height
- lw $s2, header+22
- bne $t0, $s2, sizeError
- # confirm that the bitmap is actually 24 bits
- li $t0, 24 # store 24 into $t0, because it is a 24-bit bitmap (uncompressed)
- lb $t1, header+28
- bne $t0, $t1, formatError
- # Everything seems ok, lets move forward
- lw $s3, header+34 # store the size of the data section of the image
- # read image data into array - allocationg heap memory
- li $v0, 9
- move $a0, $s3
- syscall
- move $s4, $v0
- li $v0, 14 # syscall-14, read from file
- move $a0, $s0 # load the file descriptor
- move $a1, $s4 # load base address of array
- move $a2, $s3 # load size of data section
- syscall
- # close the file
- closeFile:
- li $v0, 16 # syscall-16 close file
- move $a0, $s0
- syscall
- #----------------------------------MAIN PROGRAM--------------------------------------#
- SetUp:
- move $t3, $s4 # load base address of the image
- li $t4, 0 # Position of first column of black image
- move $t6, $s1 # width offset
- mul $t6, $t6, 3 # multiply to get the number of BGR threes in a row
- li $t5, 0 # Position of last colum of black image
- lookforblack: #we are looking for black pixels inside our image
- lb $t0, ($t3)
- beqz $t0,blackappeared
- add $t3, $t3, 3
- j lookforblack
- blackappeared:
- move $t4,$t3 # start of black block - t4
- widthofblack:
- lb $t0, ($t3)
- bnez $t0, pre
- add $t3, $t3, 3
- j widthofblack
- pre: #t4 - beginning of block, t5 - end of block
- sub $t3, $t3, 3
- add $t4,$t4,$t6
- move $t5,$t3
- add $t5,$t5,$t6
- move $t3,$t4 #next row in black box
- loopblack:
- lb $t0, ($t3)
- beq $t3,$t5,nextrow
- bnez $t0,foundwhite
- add $t3, $t3, 3
- j loopblack
- nextrow:
- add $t5,$t5,$t6
- add $t4,$t4,$t6
- move $t3,$t4
- j loopblack
- foundwhite:
- add $t3,$t3,$t6
- lb $t0, ($t3)
- beqz $t0, shape2
- j shape1
- shape1:
- li $v0, 4 # syscall-4 print string
- la $a0, shape1string
- syscall
- j end
- shape2:
- li $v0, 4 # syscall-4 print string
- la $a0, shape2string
- syscall
- #----------------------- THE END OF MAIN----------------------------#
- # end the program
- end:
- li $v0, 10 # syscall-10 exit
- syscall
- # print file error message
- fileError:
- li $v0, 4 # syscall-4 print string
- la $a0, errorMsg
- syscall
- j main # restart the program
- # print bitmap error message
- bitmapError:
- li $v0, 4 # syscall-4 print string
- la $a0, bitmapMsg
- syscall
- j main # restart the program
- # print format error message
- formatError:
- li $v0, 4 # syscall-4 print string
- la $a0, formatMsg
- syscall
- j main # restart the program
- # print size error message
- sizeError:
- li $v0, 4 # syscall-4 print string
- la $a0, sizeMsg
- syscall
- j main # restart the program
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement