Search code examples
assemblymipsfopenfwritebmp

writing a bmp file of a checkerboard in mips assembler


hi i am required to write a program in mips assembler where i am to create a checkerboard which is too be saved and written to a bmp file, compile this program it has a problem with writing the file because it jumps straight to my error message could someone please help me with this problem. Here is the code:

.data

file_open_error:    .asciiz "Open file error\n"
bmp_file:   .asciiz "chkboard.bmp"

.text
main:
    #----Write .BMP file (header+data)----

    li  $v0, 13         # open file
    la  $a0, bmp_file   # file path
    li  $a1, 0x8301    # flags WRITE|CREATE|TRUNCATE|BINARY

    li  $a2, 0x1a4      # 0644 UNIX mode (rw-r--r--)
    syscall
    bgez    $v0, write_file
    li  $v0, 4          # print string
    la  $a0, file_open_error
    syscall

write_file:
    li  $v0, 10         # exit the program
    syscall

Solution

  • Your problem is this line:

    li  $a1, 0x8301    # flags WRITE|CREATE|TRUNCATE|BINARY
    

    Not sure where you got the values from, but there is no BINARY in standard unix, and the others are O_WRONLY=1, O_CREAT=0x40 and O_TRUNC=0x200, so the correct value is 0x241.

    This applies only if the given environment uses the unix values of course. SPIM does, but MARS for example is documented to only implement three flag values: 0 for read-only, 1 for write-only with create, and 9 for write-only with create and append. It ignores mode.

    You forgot to say what you are using.