How does endianess influence drawing pixels in BMP file using Bresenham algorithm in MIPS?

459 Views Asked by At

I want to create a program, in MIPS assembly, which makes and saves a 1 bpp BMP file in which a circle of varying radius' length (provided by the user on the console) will be drawn using Bresenham algorithm.

For the moment being, I have a well-defined BMP file for integer values of diameters, however I am struggling with two more things:

  • drawing pixels at a right address of a bitmap
  • implementing correct Bresenham algorithm applied 8 points at a time

Here is part of my code in the text segment responsible for drawing pixels in the correct address places:

    # $t2 - length of row + padding
    # $t3 - initial address of a bitmap (header)
    # $t4 - pointer to move values from BMP header parameters onto the heap

     .macro drawPixel(%x,%y)
        addiu   $t4, $t3, 62    # starting address of a pixel map
        mult    %y, $t2     # pixel y-value multiplied by row length
        mflo    $a2     
        addu    $t4, $t4, $a2   # y-value pixel address computation
        divu    $a2, %x, 8  # pixel x-value divided by 8 to obtain number of bytes
        addu    $t4, $t4, $a2   # x-value pixel address computation
        mfhi    $a3

        #addiu  $a3, $a3, -1 
        li  $a2, 1      
        srlv    $a2, $a2, $a3   
        lb  $a3, ($t4)  # loading byte at the address of pixel map start (="white" byte)
        or  $a3, $a3, $a2   # adds a new coloured byte to a "white" byte
        sb  $a3, ($t4)  # loads byte with a coloured pixel  
    .end_macro
enter code here

Part below is a simple example, for testing purposes, showing that pixels are displayed not in the inteded places:

    li  $a0, 0
    li  $a1, 0
    drawPixel($a0, $a1)

    li  $a0, 4
    li  $a1, 0
    drawPixel($a0, $a1)

Although, my program draws pixels, they are somehow in a reverse order. I wonder whether this is connected with different big/little-endian in MIPS assembly and if so, how can I fix it?

Thank you very much in advance for your help and perhaps if you manage to guide me with this part, the second question about Bresenham algorithm will not be needed.

1

There are 1 best solutions below

0
On BEST ANSWER

Big/little-endiannes is for byte order in word, not for bits.

If your pixels are reflected left-to right in every 8-pixel group, then look at example of setting a pixel in monochrome bitmap (x86 asm). Important part for you - MaskToSetABit = 0x80 shr (X mod 8)

 mov ecx,edx //X coordinate
 and ecx, 7   //X mod 8
 mov edx, $80
 shr edx,cl   //mask to isolate needed bit 
 or ebx,ebx
 jz @@IsZero  
 or eax,edx   //set bit to 1

If all the lines are reflected top-to-down, then you have to consider that the most of bitmaps contain bottom-up DIBs, its' origin is the lower-left corner, and line-line offset is negative (when biHeight is positive)