Page 1 of 1
icon drawing problem
Posted: Mon Mar 26, 2012 2:06 pm
by assembler01
I recently found out how to draw pixels without the bios, you need to set es to 0xa000 and set di to the offeset (x/y position). So I made a icon which is 50 x 50 pixels (bytes) large and was the going to print it to the screen with this routine:
Code: Select all
; in: ds:si = icon (IMPORTANT: 1st word is height, 2st word is width and the rest are the pixels/bytes), dx = x/y start position
draw_icon: ; NOTE: we are in screen mode 0x13
pusha
mov ax,0xa000 ; set es to 0xa000 (place to write to)
mov es,ax
mov di,dx ; di = start x/y position
lodsw ; get 1st word
mov cx,ax ; cx = height
lodsw ; get 2st word
mov dx,ax ; dx = width
.do:
push cx
push di
mov cx,dx ; repeat width
repz movsb
pop di
pop cx
add di,320 ; newline
loop .do
popa
ret
When I run it it draws the pixels correctly until it is almost at the end where it just stops. I tried it with a 40x40 icon and it draw the whole icon, I tried it with a 60x60 but it allways stops drawing at the same place, why?
Re: icon drawing problem
Posted: Mon Mar 26, 2012 3:36 pm
by DavidCooper
assembler01 wrote:When I run it it draws the pixels correctly until it is almost at the end where it just stops. I tried it with a 40x40 icon and it draw the whole icon, I tried it with a 60x60 but it allways stops drawing at the same place, why?
Not directly an OS-dev problem, and the real problem is that you don't know how to debug, so you're unlikely to get a lot of help with it here. However, I'll try to help you to help yourself. The code you've shown looks as if it should work, assuming that your ds:si values aren't causing it to jump back 64K through memory in the middle of drawing the icon. I'm guessing you've checked the four bytes at the start of your icon data carefully enough to be sure that they're correct. You say it always stops drawing at the same place, but you haven't said exactly where that place is - I want to know which line and which pixel on that line it's stopping at, and I want to know if it's stopping at that exact same pixel when you do a 60x60 rather than a 50x50.
You ought to think about building yourself a debugging tool - a subroutine which you can call from anywhere to print out the values of registers at that time. That would enable you to see where the icon-drawing routine is going wrong if there's a fault in it. Alternatively, you may be able to run through it step by step in Bochs and see the contents of all the registers as each instruction is run - I've heard that it provides such a facility.
Re: icon drawing problem
Posted: Tue Mar 27, 2012 2:01 am
by Brendan
Hi,
assembler01 wrote:When I run it it draws the pixels correctly until it is almost at the end where it just stops. I tried it with a 40x40 icon and it draw the whole icon, I tried it with a 60x60 but it allways stops drawing at the same place, why?
The code looks correct (as far as I can tell), so I'd be tempted to assume the problem is the second word of the icon data (the "icon.height" field).
I was bored, so I optimised a little:
Code: Select all
; in: ds:si = icon (IMPORTANT: 1st word is height, 2st word is width and the rest are the pixels/bytes), dx = x/y start position
; NOTE: we are in screen mode 0x13
draw_icon:
pusha
mov ax,0xa000 ; set es to 0xa000 (place to write to)
mov es,ax
mov di,dx ; di = start x/y position
mov bp,320
mov bx,[si] ; bx = height
mov dx,[si+2] ; dx = width
sub bp,dx ; bp = 320 - width
add si,4
.nextLine:
mov cx,dx ; repeat width
rep movsb
add di,bp
sub bx,1
jne .nextLine
popa
ret
Cheers,
Brendan
Re: icon drawing problem
Posted: Tue Mar 27, 2012 4:22 am
by assembler01
What an idiot I am.
The problem was in my bootloader, it does not search for my kernel or do stuff like that, it only loads a specified amount of sectors starting from the second sector (at the end of my bootloader code (after dw 0xaa55) I put incbin "mykerne.bin"). The problem was that when the icon was at the end of the kernel my bootloader didn't load that much into RAM so all I had to do was to heighten the amount of sectors to read in the bootloader and it worked fine.
Re: icon drawing problem
Posted: Tue Mar 27, 2012 4:49 am
by Solar
Do not assume. Test, verify, assert. How exactly you do that depends on personal tastes and environment, but never just "assume" that everything went well. Hunting down the cause when things eventually fail simply takes too much time. Checksum at the end of the binary. Having the kernel Makefile write the number of sectors-to-load into the bootloader. Stuff like that.
Re: icon drawing problem
Posted: Tue Mar 27, 2012 4:52 am
by assembler01
I didn't assume, I have tested and can verify that that was the problem.
Re: icon drawing problem
Posted: Tue Mar 27, 2012 5:15 am
by Solar
I meant that your
bootloader shouldn't have assumed that loading 'n' sectors would load the whole kernel. Have a "magic number" at the end of your kernel. Do a checksum. Have the build process of the kernel image "inject" the correct number of sectors to be loaded into the bootloader. Something like that. I think I have written just that before.
Give the software the capability to report when things are "strange". Fail early, fail loud. Don't wait for the icon rendering to tell you that things aren't going according to plan.
It was an on-the-side remark regarding a design lesson I have learned the hard way. Nothing more.
Re: icon drawing problem
Posted: Tue Mar 27, 2012 1:49 pm
by DavidCooper
berkus wrote:DavidCooper wrote:ds:si values aren't causing it to jump back 64K
Sounds kinda unlikely, looks like this is 320x200 mode (64000 bytes total).
It's where it's reading from that might have been the issue rather than where it's writing to, but not surprisingly the fault turned out to be much simpler.
Great tip from Solar.
Re: icon drawing problem
Posted: Tue Mar 27, 2012 6:40 pm
by Hoozim
Eventually, you are probably going to want to have your boot device formatted with something like FAT12. The file you want to load can then be parsed from the filesystem which is not that difficult. This means that when you eventually format the device with a filesystem, you will not have to rely on reserved sectors for you kernel which is one less thing to worry about.
Hoozim