PDA

View Full Version : Help me please....



bazooka joe
09-20-2003, 11:35 AM
What wrong here?


#include <conio.h>
#include <dos.h>

#define WIDTH 640
#define HEIGHT 480

typedef unsigned char byte;

byte *SCR=(byte *) MK_FP (0xA000,0);

void svga() {
asm mov ax,0x4f02;
asm mov bx,0x101;
asm int 0x10;
}

void textmode() {
asm mov ax,0x03;
asm int 0x10;
}

void putpixel (int x, int y, byte color){
unsigned long pixel_offset = 1.0 * (y * 640 + x);
int bank_num = (pixel_offset >> 16);
asm mov ax, 0x4F05;
asm mov bx, 0x0;
asm mov dx, bank_num;
asm int 0x10;
SCR[(pixel_offset & 0xFFFF)] = color;
}

int main(void) {
svga();

byte color=0;
for (int y=0;y<HEIGHT;y++) {
color=0;
for(int x=0;x<WIDTH;x++)
putpixel(x,y,color++);
}

getch();
textmode();
return 0;
}


:confused:

twm
09-20-2003, 11:45 AM
Are you gonna tell us what OS/compiler you use? Or do we have to guess and most likely ignore you due to lack of useful information?

bazooka joe
09-20-2003, 11:49 AM
compiler: turbo c
os: dos

this is the result:
http://uploaded.fresh.co.il/3f65febb34c60223.rar

bazooka joe
09-20-2003, 11:51 AM
....
this code do his job but very slowly


void putpixel(int x, int y, byte color) {
asm mov ah,0x0c;
asm mov al,color;
asm mov cx,x;
asm mov dx,y;
asm mov bx,0x01;
asm int 0x10;
}

VirtualAce
09-20-2003, 04:22 PM
I would say your problem is here w/o even testing the code:



unsigned long pixel_offset = 1.0 * (y * 640 + x);


Why 1.0??? This forces a conversion to double, y is double, 640 is double, and x is double. Then you are assigning a double to an unsigned long which is yet another conversion, probably one in which the result is 0 since you cannot assign a double to an unsigned long.

As well you cannot shift a double left or right but you can an unsigned long -> again, why are you using a double? There is no need.

1*(y*640+x)
or
1*((y<<9)+(y<<7)+x)

is the same as

(y*640+x)
or
((y<<9)+(y<<7)+x)


One times any number is that number. Why the 1.0????

My guess is that if you print out pixel_offset it will be 0.


Here is what you are doing:

Converting all operands to doubles.
Trying to convert the entire result into an unsigned long.


All SVGA functions can be done using integer arithmetic - no need for doubles or floats.



;put original y into eax
mov eax,[y]

;multiply eax by 512
shl eax,9

;put original y into eax
mov ebx,[y]

;multiply ebx by 128
shl ebx,7

;add result of ebx to eax - (y*512)+(y*128) = (y*(512+128))
;=(y*640)
add eax,ebx

;add x to eax = (y*640)+x
;leave result in eax
add eax,x


or in C:


pixel_offset=((y<<9)+(y<<7)+x);