如何在MIPS中提高像素读取和写入功能的效率?

时间:2019-04-30 07:13:30

标签: image-processing assembly mips bmp mars-simulator

以下MIPS过程旨在读取和写入内存中加载的BMP图像中的像素。

.eqv BMP_FILE_SIZE 90122
.eqv BYTES_PER_ROW 1800

put_pixel:
#description: 
#   sets the color of specified pixel
#arguments:
#   $a0 - x coordinate
#   $a1 - y coordinate - (0,0) - bottom left corner
#   $a2 - 0RGB - pixel color
#return value: none

    sub $sp, $sp, 4     #push $ra to the stack
    sw $ra,4($sp)

    la $t1, image + 10  #adress of file offset to pixel array
    lw $t2, ($t1)       #file offset to pixel array in $t2
    la $t1, image       #adress of bitmap
    add $t2, $t1, $t2   #adress of pixel array in $t2

    #pixel address calculation
    mul $t1, $a1, BYTES_PER_ROW #t1= y*BYTES_PER_ROW
    move $t3, $a0       
    sll $a0, $a0, 1
    add $t3, $t3, $a0   #$t3= 3*x
    add $t1, $t1, $t3   #$t1 = 3x + y*BYTES_PER_ROW
    add $t2, $t2, $t1   #pixel address 

    #set new color
    sb $a2,($t2)        #store B
    srl $a2,$a2,8
    sb $a2,1($t2)       #store G
    srl $a2,$a2,8
    sb $a2,2($t2)       #store R

    lw $ra, 4($sp)      #restore (pop) $ra
    add $sp, $sp, 4
    jr $ra
# 

get_pixel:
#description: 
#   returns color of specified pixel
#arguments:
#   $a0 - x coordinate
#   $a1 - y coordinate - (0,0) - bottom left corner
#return value:
#   $v0 - 0RGB - pixel color

    sub $sp, $sp, 4     #push $ra to the stack
    sw $ra,4($sp)

    la $t1, image + 10  #adress of file offset to pixel array
    lw $t2, ($t1)       #file offset to pixel array in $t2
    la $t1, image       #adress of bitmap
    add $t2, $t1, $t2   #adress of pixel array in $t2

    #pixel address calculation
    mul $t1, $a1, BYTES_PER_ROW #t1= y*BYTES_PER_ROW
    move $t3, $a0       
    sll $a0, $a0, 1
    add $t3, $t3, $a0   #$t3= 3*x
    add $t1, $t1, $t3   #$t1 = 3x + y*BYTES_PER_ROW
    add $t2, $t2, $t1   #pixel address 

    #get color
    lbu $v0,($t2)       #load B
    lbu $t1,1($t2)      #load G
    sll $t1,$t1,8
    or $v0, $v0, $t1
    lbu $t1,2($t2)      #load R
        sll $t1,$t1,16
    or $v0, $v0, $t1

    lw $ra, 4($sp)      #restore (pop) $ra
    add $sp, $sp, 4
    jr $ra

这些功能似乎效率不高。

如何在MIPS中提高像素读取和写入功能的效率?

0 个答案:

没有答案