【发布时间】: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文件的代码吗?
【问题讨论】:
-
它正在初始化 BITMAPFILEHEADER 的一部分。参见例如en.wikipedia.org/wiki/BMP_file_format#Bitmap_file_header 和 docs.microsoft.com/en-us/windows/win32/api/wingdi/…
-
sw $0...使用一条指令存储 2 个半字零。设置头的所有字段后,它将$t0寄存器中的指针值前移 BMP 头的大小(14 字节)。使用 0x7a 表明那么 DIB 头是 108 字节版本(0x7a - 14)。 -
否则,如果您想了解更多,请更具体地说明您的问题,因为我们不知道您知道什么和不知道什么。
-
仅供参考,该代码仅在处理器以 little-endian 模式运行时才有效。
-
@JimRhodes:好点; MARS(用 Java 编写)模拟 little-endian MIPS; SPIM(用 C 编写)模拟 MIPS,其字节顺序与主机 C 编译器相同。