Initializing a bmp file in mips

0

This code creates a bmp file with all the white pixels:

initialize_bmp:
#description: 
#   fill the header of the bmp file an initialize all pixels with white
#arguments:
#   none
#return value: none
    la $t0, image  #image is a buffer or we can say array that stores all the 90122 bytes (equivalent to the image size) in register t0.
    li $t1, 'B'    #Loading the first letter 'B' in the register t1
    sb $t1, ($t0)  #Storing the letter 'B' from t1 to the 0th byte of t0
    li $t1, 'M'    #Storing the letter 'M' in t1
    sb $t1, 1($t0) #Loading the letter 'M' in the 1st byte of register t0.
    li $t1, BMP_FILE_SIZE 
    sw $t1, 2($t0)
    sw $0, 6($t0)
    li $t1, 0x7a
    sw $t1, 10($t0)
    addi $t0, $t0, 14
    li $t1, 27          # loop counter  
    la $t2, header
    copy_loop:          # loop to copy header in the image
        lw $t3, ($t2)
        sw $t3, ($t0)
        addi $t0, $t0, 4
        addi $t2, $t2, 4
        addi $t1, $t1, -1
        bnez $t1, copy_loop
    li $t1, 5625            # loop counter      
    li $t3, 0xFFFFFFFF      # white color
    initialize_loop:        # loop to initialize the image with all white pixels
        sw $t3, ($t0)
        sw $t3, 4($t0)
        sw $t3, 8($t0)
        sw $t3, 12($t0)
        addi $t0, $t0, 16
        addi $t1, $t1, -1
        bnez $t1, initialize_loop
    jr $ra

I have this code of creating a bmp file with all the white pixels in mips. I don't understand what is going on in this part of the code:

li $t1, BMP_FILE_SIZE 
sw $t1, 2($t0)
sw $0, 6($t0)
li $t1, 0x7a
sw $t1, 10($t0)
addi $t0, $t0, 14

Can somebody explain me the code?

This is the bmp file format so in accordance with this image can somebody explain me the code that how are we actually initializing the bmp file here with the white pixels?

annotated hexdump of BMP header

assembly
mips
bmp
asked on Stack Overflow May 13, 2021 by Hawra Saif • edited May 13, 2021 by Peter Cordes

1 Answer

2
  1. li $t1, BMP_FILE_SIZE Total size of the file. In this case it is 14 for the bitmap file header, 108 for the bitmap header (4 * 27) plus 22500 for the pixel data (4 * 5625) = 22,622.
  2. sw $t1, 2($t0) Stores the total file size into the file header.
  3. sw $0, 6($t0) Sets the next four bytes to zero.
  4. li $t1, 0x7a Offset within the file of the start of the pixel data.
  5. sw $t1, 10($t0) Stores the pixel data offset into the file header.
  6. addi $t0, $t0, 14 Bumps the pointer to the start of the bitmap header.

The next block of code copies the 108 byte bitmap header to the buffer after the file header:

li $t1, 27          # loop counter  
la $t2, header
copy_loop:          # loop to copy header in the image
    lw $t3, ($t2)
    sw $t3, ($t0)
    addi $t0, $t0, 4
    addi $t2, $t2, 4
    addi $t1, $t1, -1
    bnez $t1, copy_loop

The remaining code fills the pixel data with white pixels. Since the loop stores 4 bytes 5625 times, that is a total of 22500 bytes.

answered on Stack Overflow May 13, 2021 by Jim Rhodes

User contributions licensed under CC BY-SA 3.0