Mail Archives: djgpp/1997/09/03/17:37:15
Vic writes:
>Hi. I'm just starting in ASM, and I'd like to know if this piece of
>code is correct (I know it works, I just wanna make sure)
>(it's a putpixel routine for an Allegro memory bitmap)
[...]
>gcc2_compiled.:
>___gnu_compiled_c:
You don't need that in handwritten asm. I don't know what it does, but
it implies that the code was produced by gcc, so you probably shouldn't
lie by using it in things you write yourself :-)
>.text
>.globl _put_pixel //put_pixel(BITMAP*,x,y,color);must be memory bitmap!!
>_put_pixel:
>
>movl 4(%esp),%eax //move BITMAP* in %eax
>movl 12(%esp),%ebx //move y in %ebx
>movl 60(%eax,%ebx,4),%ecx //move the addres of BITMAP->line[y] in %ecx
>movl 8(%esp),%eax //move x in %eax
>movl 16(%esp),%ebx //move color in %ebx
>movb %ebx,(%ecx,%eax,1) //put color in BITMAP->line[y][x]
>ret
Basically correct, but the movb should be from %bl, an 8 bit register,
rather than %ebx, which is 32 bits in size. Also you must be sure to
restore %ebx, %esi, and %edi at the end of your function (you are
allowed to clobber %eax, %ecx, and %edx though). In your case this just
means you need to "pushl %ebx" at the start of the routine, and "popl
%ebx" at the end, and then adjust all your offsets from %esp by four
since you just put an extra dword on the stack.
Just out of interest, a really simple C routine:
void pp(BITMAP *bmp, int x, int y, int color)
{
bmp->line[y][x] = color;
}
compiled with -O3, -m486, -fomit-frame-pointer, produces the code:
_pp:
pushl %ebx
movl 8(%esp),%ecx
movl 12(%esp),%ebx
movl 16(%esp),%eax
movl 20(%esp),%edx
movl 60(%ecx,%eax,4),%eax
movb %dl,(%ebx,%eax)
popl %ebx
ret
This is almost identical to your hand-optimised asm, just using
different registers! Since gcc does such a good job of optimising this
kind of code, and since 99% of the overhead is in the function call
rather than the actual pixel write, it would probably be much more
efficient to declare the C routine as an inline function in one of your
header files...
--
Shawn Hargreaves - shawn AT talula DOT demon DOT co DOT uk - http://www.talula.demon.co.uk/
Beauty is a French phonetic corruption of a short cloth neck ornament.
- Raw text -