【问题标题】:Initializing a bmp file in mips在 mips 中初始化 bmp 文件
【发布时间】:2021-05-13 17:54:23
【问题描述】:

此代码创建一个包含所有白色像素的 bmp 文件:

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

我有这段代码可以创建一个包含 mips 中所有白色像素的 bmp 文件。我不明白这部分代码发生了什么:

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

谁能解释一下代码?

这是bmp文件格式,所以根据这张图片,有人可以解释一下我们如何用白色像素在这里初始化bmp文件的代码吗?

【问题讨论】:

  • sw $0... 使用一条指令存储 2 个半字零。设置头的所有字段后,它将$t0 寄存器中的指针值前移 BMP 头的大小(14 字节)。使用 0x7a 表明那么 DIB 头是 108 字节版本(0x7a - 14)。
  • 否则,如果您想了解更多,请更具体地说明您的问题,因为我们不知道您知道什么和不知道什么。
  • 仅供参考,该代码仅在处理器以 little-endian 模式运行时才有效。
  • @JimRhodes:好点; MARS(用 Java 编写)模拟 little-endian MIPS; SPIM(用 C 编写)模拟 MIPS,其字节顺序与主机 C 编译器相同。

标签: assembly mips bmp


【解决方案1】:
  1. li $t1, BMP_FILE_SIZE 文件的总大小。在这种情况下,位图文件头为 14,位图头 (4 * 27) 为 108,像素数据为 22500 (4 * 5625) = 22,622。
  2. sw $t1, 2($t0) 将总文件大小存储到文件头中。
  3. sw $0, 6($t0) 将接下来的四个字节设置为零。
  4. li $t1, 0x7a 文件内像素数据开始处的偏移量。
  5. sw $t1, 10($t0) 将像素数据偏移存储到文件头中。
  6. addi $t0, $t0, 14 将指针指向位图标题的开头。

下一段代码将108字节的位图头复制到文件头之后的缓冲区中:

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

剩下的代码用白色像素填充像素数据。由于循环存储4个字节5625次,一共22500个字节。

【讨论】:

    猜你喜欢
    • 2011-11-24
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多