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

6
  • 1
    It's initializing part of the BITMAPFILEHEADER. See e.g. en.wikipedia.org/wiki/BMP_file_format#Bitmap_file_header and learn.microsoft.com/en-us/windows/win32/api/wingdi/…
    – Michael
    May 13, 2021 at 17:59
  • 1
    the sw $0... stores 2 half words of zeros using a single instruction. After setting all the fields of the header, it advances the pointer value in the $t0 register by the size of the BMP header (14 bytes). The use of 0x7a suggests that then DIB header is the 108 byte version (0x7a - 14).
    – Erik Eidt
    May 13, 2021 at 18:05
  • Otherwise, if you want to know more, be more specific about your question, since we don't know what you know and don't know.
    – Erik Eidt
    May 13, 2021 at 18:05
  • FYI, that code is only valid if the processor is running in little-endian mode.
    – Jim Rhodes
    May 13, 2021 at 18:18
  • @JimRhodes: Good point; MARS (written in Java) simulates a little-endian MIPS; SPIM (written in C) simulates a MIPS with the same endianness as the host C compiler. May 13, 2021 at 18:27

1 Answer 1

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.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge that you have read and understand our privacy policy and code of conduct.