0

I finally managed to draw a pixel on the screen using the VESA BIOS Extensions in the protected mode. But I'm having a problem putting it in a different position (for example I want it to be in x=100, y=50).

According to http://www.brokenthorn.com/Resources/OSDevVid2.html, we plot pixels using this C code:

void pixel_16RGB (unsigned short color, unsigned short x, unsigned short y) {
    unsigned short* fb = (unsigned short*) _modeInfo.physBasePtr;
    unsigned short offset = x + y * (_modeInfo.bytesPerScanLine / 2);
    fb [offset] = color;
}

I tried to write it in assembly, so the result looks like this:

;first part
mov si, word[ModeInfoBlock + 10h]

mov edx, 0
lea eax, [si]
mov ecx, 2
div ecx
mov ecx, 100
mul ecx
add eax, 50

;second part
mov edx, dword[ModeInfoBlock + 28h]
add edx, eax
mov ebx, 0x3296fa

mov [edx], ebx
jmp $

I expected a blue pixel showing up in the coordinates (100, 50) but I'm seeing just a red one in a random position.

I think I'm wrong in this part:

add edx, eax

Am I right?

MARSHMALLOW
  • 1,315
  • 2
  • 12
  • 24
  • 2
    Which mode are you in? If you are in a 24 bpp mode as we can guess from your color constant, you should be scaling your `x` by 3 as each pixel is 3 bytes. You would want to write 3 bytes too not 4. If you are in a 32 bpp mode, then you need to scale by 4 but can use a dword write. Also it's unclear why you divide the `bytesPerScanLine` by 2. – Jester May 25 '20 at 22:03
  • @Jester I divide the `bytesPerScanLine` by 2 because the algorithm says that I should do that: `_modeInfo.bytesPerScanLine / 2`. I'm in 24bpp mode. – MARSHMALLOW May 25 '20 at 22:05
  • 1
    Ah, they are dividing because C scales by 2 automatically due to the `unsigned short*` pointer type. You don't have that in assembly. Also they seem to be using a 16bpp mode. Drop the division by 2, and instead multiply `x` by 3. – Jester May 25 '20 at 22:08
  • @Jester Ok, I'll try that. – MARSHMALLOW May 25 '20 at 22:09
  • @Jester What do you mean with "Drop the division by 2"? – MARSHMALLOW May 25 '20 at 22:13
  • Remove it. You do not need to divide by 2. – Jester May 25 '20 at 22:13
  • @Jester Is this part `add edx, eax` in my code right? – MARSHMALLOW May 25 '20 at 22:14
  • Sure. That's an addition :) – Jester May 25 '20 at 22:15
  • @Jester Obviously, how can I write `fb [offset]` in assembly? I think this method: `add edx, eax`? – MARSHMALLOW May 25 '20 at 22:18
  • Yes, that is correct. `a[b]` is `*(a+b)`. – Jester May 25 '20 at 22:19
  • 1
    @Jester Oh thanks Jester! You're a legend! How can I reciprocate? – MARSHMALLOW May 25 '20 at 22:34

0 Answers0