Dark Bit Factory & Gravity

PROGRAMMING => Freebasic => Topic started by: MrP on August 25, 2006

Title: Inline ASM....
Post by: MrP on August 25, 2006
been messing around with the inline asm thingy for the last few days.... jeez brings back memories, the last assembly i did was back on my good old atari ST....... Anyways heres my first little asm function that did something, (progressed a little now). Nothing special it just plots a pixel at a designated co-ordinate on screen. Well in fairness it actually writes 640x480 pixels in whatever color you pass to it. Nothing much I know but you've gotta start somewhere and you never know it might even be useful to someone....

screenres 640, 480, 32, 2, 1, 75

dim scr_ptr as integer ptr
screenset 0, 1
declare sub put_pixel(byval x as integer, byval y as integer, byval col as integer)


dim angle as single

dim shared fps_timer
fps_timer = timer
fps_counter = 0
while inkey$ = ""
    'cls
    for y = 0 to 480
        for x = 0 to 640
            col = 255
            put_pixel(x, y, rgb(col, col, col))
        next x
    next y

    fps_counter = fps_counter + 1
    if timer - fps_timer > 1 then
        act_fps = fps_counter
        fps_counter = 0
        fps_timer = timer
    end if
    locate 1,1 : print act_fps
    screensync
    flip
wend
end

sub put_pixel(byval x as integer, byval y as integer, byval col as integer)
    dim scr_ptr as integer ptr
    scr_ptr = screenptr()
    asm
        mov eax, [x]            'pixel_x into eax
        mov ebx, [y]            'pixel_y into ebx
        mov ecx, [col]          'color into ecx
        mov edx, [scr_ptr]      'beginning of screen into edx
        imul ebx, 640           'evaluate screen y
        add eax, ebx            'then screen x
        imul eax, 4               'using integers so jump by 4
        add edx, eax            'update our pointer
        mov [edx], ecx          'write the pixel
    end asm
   
end sub
Title: Re: Inline ASM....
Post by: DrewPee on August 25, 2006
MrP

Again - fantastic code - didnt realise you could do ASM inline!!!!!! Ive made one change to your code - made the colour a random amount and now it looks like white noise - cool!

Thanks for the code - Im learning so much at the moment - my head hurts
Title: Re: Inline ASM....
Post by: Shockwave on August 25, 2006
You can have some good Karma for that Mr.P :)
Title: Re: Inline ASM....
Post by: MrP on August 25, 2006
I know what it feels like to have a sore head DrewPee. mines been like that for a few days now. Just started learning asm for the pc myself. As mentioned in my first post, i've just got some  mmx code going to calculate alpha on one pixel at a time, its really cool as you can execute the calculations on the red, green and blue elements of the pixel at the same time. The codes a bit messy right now, going to tidy it up tonight and i'll post back what I have later......

@Shockwave - Cheers for the karma matey, never hurts to have some good karma.....
Title: Re: Inline ASM....
Post by: Jim on August 25, 2006
Nice job!  I didn't even know freebasic had inline asm support!

btw, you can replace

        imul eax, 4               'using integers so jump by 4
        add edx, eax            'update our pointer

with

        lea edx,[edx+eax*4]

That should be a fraction faster.

Jim
Title: Re: Inline ASM....
Post by: MrP on August 26, 2006
Cheers for that Jim. Any help I can get while i'm learning is more than appreciated. Looking up that lea command now..... I'm also of the opinion that using a bitwise shift to the left by 2 on register eax will eliminate the need for that nasty multiply by 4. I'm starting to think that being able to do something in asm isn't the be all and end all of things. From what I've gathered its how you optimise the code that counts.......
Title: Re: Inline ASM....
Post by: Jim on August 26, 2006
The lea is only 1 cycle, even though it has a *4 in it.  You get it for free.  You can do *2 and *4 with lea, while adding another register.
Jim

Title: Re: Inline ASM....
Post by: Blitz Amateur on August 26, 2006
This is really handy! I already used it in a piece of code I wrote (Already posted my code)

Thanks for posting this, finally got me to dabble in ASM a bit :D