ASM assistance, if you please.

Game development specific discussions.
kiyotewolf
Posts: 1009
Joined: Oct 11, 2008 7:42
Location: ABQ, NM
Contact:

ASM assistance, if you please.

Post by kiyotewolf »

Dear ASM inline coders.

I know you're out there, I can hear you breathing.

You are all an excellent lot, extremely talented, and I give you ALL, extremely high regard.

I know 6502 assembly, very well, but.. it does me absolutely NO good, in a 8088+ (and far beyond) environment.

I've done a little inline ASM, in Turbo Pascal, so I know some of what is to be expected, and how it does work, BUT.

Being FB is a new beast, (especially compared to TP 4.0), and how to incorporate FB variable into it, and such, it is still quite a monster for me to even begin to understand.

(Yes I am attempting to learn this stuff, and I do want to.)

I am humbly asking, if someone out there, has free time, and I have a very concise, direct, and simple task, which can be implemented in ASM, not just some pipe dream half-baked algo, but a whole idea, ..

Would,.. someone be willing to take a bit of time-out, and help write the ASM routine for me? (Or us.. or them..)

~~~

I know this makes FB code platform dependent, and other various important things of note, yes.



If doing something for nothing is a huge insult to your intelligence, I am willing, to paypal a fee for time to develop, should the need arise.

~~~

I put this out to you FIRST, as an offer to do tiny bits of ASM code, for a named fee, (your choice), but if you feel like being generous, that of course is your decision to make.



~Kiyote!

ASM for money FIRST, ASM around the watercooler, SECOND.

I only offer the money first, because I respect (all you guys) and you do have prior things that are far more important to you, than my pet projects.
relsoft
Posts: 1767
Joined: May 27, 2005 10:34
Location: Philippines
Contact:

Post by relsoft »

What ASM routine? Most people here would do it for free.

And you can make FB spit out ASM instructions. So you can write in FB, let FB spit out ASM, optimize it, then do an:

asm

end asm

statement block.
kiyotewolf
Posts: 1009
Joined: Oct 11, 2008 7:42
Location: ABQ, NM
Contact:

Post by kiyotewolf »

O.O really??

<.<' ! Oh wow.. I didn't know that..

I will sit and write out my thesis on my 1st ASM project I need help with.

Will return asap with the request for help.

Filed in triplicate.

Using only purple ink.



~Kiyote!
kiyotewolf
Posts: 1009
Joined: Oct 11, 2008 7:42
Location: ABQ, NM
Contact:

Post by kiyotewolf »

I'm going to go look at my code, and see exactly what I want, to the letter, so I don't waste anyone's time.

Be back soon.



~Kiyote!
Prime Productions
Posts: 147
Joined: May 24, 2009 23:13
Location: Texas, United States, Planet Earth
Contact:

Post by Prime Productions »

kiyotewolf wrote:I'm going to go look at my code, and see exactly what I want, to the letter, so I don't waste anyone's time.

Be back soon.



~Kiyote!
Sure, I'm with relsoft here, I'm writing an OS in NASM (Useless OS), so I'd help you out depending on what you want. We're mostly here to help, you wouldn't need to pay us or anything.

Sure thing!

David
kiyotewolf
Posts: 1009
Joined: Oct 11, 2008 7:42
Location: ABQ, NM
Contact:

Post by kiyotewolf »

I've made useless OS'es.. in DOS before.

Had all this neat stuff.

Text graphics paint thingy.
El Cheapo Terminal program
Background music (using ON PLAY) with multiple songs
Drop down menus
A "nighttime" thing, where the screen went black and you saw creepy eyes with cricket noises.
And a few other cool things.

Sadly, that nice piece of programming got hit with a corrupted sector on a FLOPPY disk, and I never could find another copy.

It was such a fun toy too!

I'll redo it here very soonish in FB, especially since my ANSI decoding routines finally work now in FB.



~Kiyote!

I'd like to see your useless OS when you're at a build point worth sharing.

I love all that kind of stuff.
Prime Productions
Posts: 147
Joined: May 24, 2009 23:13
Location: Texas, United States, Planet Earth
Contact:

Post by Prime Productions »

kiyotewolf wrote:I've made useless OS'es.. in DOS before.

Had all this neat stuff.

Text graphics paint thingy.
El Cheapo Terminal program
Background music (using ON PLAY) with multiple songs
Drop down menus
A "nighttime" thing, where the screen went black and you saw creepy eyes with cricket noises.
And a few other cool things.

Sadly, that nice piece of programming got hit with a corrupted sector on a FLOPPY disk, and I never could find another copy.

It was such a fun toy too!

I'll redo it here very soonish in FB, especially since my ANSI decoding routines finally work now in FB.



~Kiyote!

I'd like to see your useless OS when you're at a build point worth sharing.

I love all that kind of stuff.
You took the words out of my mouth, the OS is actually called, "The Useless OS" or UOS.

Check it out here: http://www.uselessos.site11.com/

The website is far from done. I also haven't really got very far on it, but it sounds sort of like what yours was, kinda like DOS.

It has a bootloader, etc. Also a to ASM BASIC translator, (far from complete) written in FreeBASIC. But it is a true OS. No need for another. Just reboot the computer with the floppy.

I'd have loved to see yours. Too bad you lost it.

David

P.S. I don't want to steal your thread though, remeber, when you get what you want laid out, I'll try my best to help.
kiyotewolf
Posts: 1009
Joined: Oct 11, 2008 7:42
Location: ABQ, NM
Contact:

Post by kiyotewolf »

Sometimes the best things in this forum is when a thread goes slightly off track, I've found.

Seen it plenty of times, and I'm glad you took over a lil bit.

I don't mind.



~Kiyote!

Yeah. I wanna learn ASM, but it's as mindboggling to me as C.

I mean, ASM itself, I really do understand 6502, but 8088 flavored cookies..

I have issues wrapping my brain around how the code can run from anywhere in memory, and variables.. how are they stored in ASM,.. other things that I shouldn't be confused on,..
kiyotewolf
Posts: 1009
Joined: Oct 11, 2008 7:42
Location: ABQ, NM
Contact:

Post by kiyotewolf »

http://www.freebasic.net/forum/viewtopi ... 622#140622

Run the code I wrote to demo my routines for this guy here.

Go to the last demo, the screensaver.

It's a FB implementation of my "AMAZING CHEAP TEXTUREMAPPING"

I would love to see THAT, my texturemapping routine, implemented in ASM, so that I could use it with a decent framerate.



~Kiyote!

That would be a HUGE undertaking, I can only guess.

*** NOTE ***

This is NOT the first ASM project I had in mind, I have a super simple one that I'm still working on writing up first.

*** NOTE ***

This is a mountain of a task for later, to make into ASM.[/b]
1000101
Posts: 2556
Joined: Jun 13, 2005 23:14
Location: SK, Canada

Post by 1000101 »

Until you are more familiar with inline asm and 386 protected mode programming, I would start with very small parts which are easily replicated by using rel's trick of letting fb generate the asm for you to begin with. I use this trick when debugging my asm sometimes, ie: "hrm, how would fbc do this?"

Another trick when you want to look for specific code segments is to encapsulate it in a specific code sequence which fbc wouldn't output. I use int3 which is the debugger interrupt. When executed in a debugger the execution will stop at that point and the debugger with focus on it. Outside a debugger it will crash (no interrupt 3 handler). Int3 helps with debugging as it's easy to find when looking at fbc's asm output (-r).

eg: (compile as "fbc -r something.bas")

Code: Select all

Dim As Integer i

'' Use int3 to help locate the fbc asm output of what we want.
asm int3

i += i

'' Now cap it so you know where it ends.
asm int3
The resultant asm will look like this:

Code: Select all

.
.
.
int3
mov eax, dword ptr [ebp-4]
add dword ptr [ebp-4], eax
int3
.
.
.
Also, FBEdit is a pretty solid IDE for fb and debugging but for any real asm coding you want to use OllyDbg.

Assuming you are on a win32 compatible platform. ¬¸¬
Prime Productions
Posts: 147
Joined: May 24, 2009 23:13
Location: Texas, United States, Planet Earth
Contact:

Post by Prime Productions »

kiyotewolf wrote:http://www.freebasic.net/forum/viewtopi ... 622#140622

Run the code I wrote to demo my routines for this guy here.

Go to the last demo, the screensaver.

It's a FB implementation of my "AMAZING CHEAP TEXTUREMAPPING"

I would love to see THAT, my texturemapping routine, implemented in ASM, so that I could use it with a decent framerate.



~Kiyote!

That would be a HUGE undertaking, I can only guess.

*** NOTE ***

This is NOT the first ASM project I had in mind, I have a super simple one that I'm still working on writing up first.

*** NOTE ***

This is a mountain of a task for later, to make into ASM.[/b]
Hi Kiyote!

Okay, I'm going to be away for few days, so I'll check when I'm back. (Monday) But I don't think it will be too hard. I actually have an MASM texture mapping routine I wrote a while back. But I can just look at your code line by line and port it to ASM.

I'll try when I'm back.
relsoft
Posts: 1767
Joined: May 27, 2005 10:34
Location: Philippines
Contact:

Post by relsoft »

Try this one: You could modify it with little effort to work on FB.

Code: Select all


;;;Fix point Textured triangle routine. :*)
;;;Call with:
;;;Declare sub (Byval Layer%, Byval x1%, Byval y1%,Byval u1%,Byval v1%, Byval x2%,_
;;; 	Byval y2%,Byval u2%,Byval v2%, Byval x3%, Byval y3%,Byval u3%,Byval v3%, Byval TSEG%, Byval TOFF%)

.Model Medium, BASIC
.386
.Code

align 2

;;*****************Near procedure for preclipped Textured Hline*****************************************
HTline     Proc  near\
            x1:word, x2:word, y:word, lu:dword, ru:dword, lv:dword, rv:dword, Tsize:word

			Local udelta:   dword
			Local vdelta:   dword
			Local u: 		dword
			Local v: 		dword
			Local dy:		word
			Local TsizeM1:	word
			Local Toff:		word
			Local wid:		word

			;GS = Xshift


    mov ax, x1
    mov bx, x2
    cmp ax,bx
    jle Short @Postswapx
    	xchg ax, bx
    	mov ecx, lu
    	mov edx, ru
    	xchg ecx, edx
    	mov x1, ax
    	mov x2, bx
    	mov eax, lv
    	mov ebx, rv
    	xchg eax, ebx
    	mov lu, ecx
    	mov ru, edx
    	mov lv, eax
    	mov rv, ebx
@PostSwapX:



	mov ecx, lu
	mov edx, lv
	mov u, ecx
	mov v, edx
	;U
    mov ax, x1
    mov bx, x2
    mov edx, ru
    mov ecx, lu
    								;Gdx% = (Gx2% - Gx1%) + 1       'Get Xdelta(+1) for the Div by 0 error
    								;Cdx& = Clr2& - Clr1&           'Color delta    								;deltac& = Cdx& \ Gdx%          'Interpolate
    								;col& = Clr1&                   'save orig color to be safe
    sub edx, ecx

    sub bx, ax
    jz  @NolineH
    mov dy, bx				;save ydelta
    mov ax, bx
    cwde					;eax = gdx
    mov ecx, edx			;ecx = cdx
    xchg eax, ecx			;eax = cdx  : ecx = gdx
    cdq						;eax:edx = cdx

    idiv ecx				                ;eax = edxedx\ecx
    mov udelta, eax


	;V
	mov edx, rv
    mov ecx, lv
    								;Gdx% = (Gx2% - Gx1%) + 1       'Get Xdelta(+1) for the Div by 0 error
    								;Cdx& = Clr2& - Clr1&           'Color delta    								;deltac& = Cdx& \ Gdx%          'Interpolate
    								;col& = Clr1&                   'save orig color to be safe
    sub edx, ecx
    mov ax, dy
    cwde					;eax = gdx
    mov ecx, edx			;ecx = cdx
    xchg eax, ecx			;eax = cdx  : ecx = gdx
    cdq						;eax:edx = cdx

    idiv ecx				                ;eax = edxedx\ecx
    mov vdelta, eax



    mov ax, x1
    mov bx, x2


;;;;;;;;;;
    cmp ax,319
    jg @NolineH
    cmp ax,0
    jl  @clipLeft
@PostclipLeft:
    cmp bx,0
    jl  @NolineH
    cmp bx,319
    jg  @clipRight
@PostclipRight:



	mov cx, Tsize
    mov dx, bx							;dx = (bx - ax)+1
    inc dx
    dec cx
    mov TsizeM1, cx
    sub dx, ax
    jz   @NolineH
    mov cx, y				;put in y value
    xchg ch, cl				;y = y *256 ;di& = cx * 320& + ax
    mov di, cx				;di = 256
    shr di, 2				;di = 64
    add di, cx				;di = 256+64 = 320
    add di, ax				;di = y*320+x


    									;FOR l% = Gx1% TO Gx2%                   'Rasterizer loop
    									;    PSET (l%, yy%), col& \ FIXPOINT     'Use poke for speed
    									;    col& = col& + deltac&               'DDA
    									;NEXT l%


;;xloop okay
Mov Toff, fs
mov bx, dx				;do divide
cmp bx, 4
jle @Less4
and bl, 3
shr dx, 2
mov ch, bl				;odd pixels I used dy to conserve mem. :*)
mov wid, dx
mov ax, gs				;shift counter
mov cl, al				;use cl
mov ebx, u				;save u and v
mov edx, v

@xloop:
	;1.
	;;v
    mov eax, edx
    shr eax, 16
    and ax, TsizeM1
    shl ax, cl
    mov si, ax
    add si, Toff
    ;;u
    mov eax, ebx
    shr eax, 16
    and ax, TsizeM1
    add si, ax
    mov al,[si+4]
    mov es:[di], al
    add ebx, udelta
    add edx, vdelta
    ;2.
	;;v
    mov eax, edx
    shr eax, 16
    and ax, TsizeM1
    shl ax, cl
    mov si, ax
    add si, Toff
    ;;u
    mov eax, ebx
    shr eax, 16
    and ax, TsizeM1
    add si, ax
    mov al,[si+4]
    mov es:[di+1], al
    add ebx, udelta
    add edx, vdelta
    ;3.
	;;v
    mov eax, edx
    shr eax, 16
    and ax, TsizeM1
    shl ax, cl
    mov si, ax
    add si, Toff
    ;;u
    mov eax, ebx
    shr eax, 16
    and ax, TsizeM1
    add si, ax
    mov al,[si+4]
    mov es:[di+2], al
    add ebx, udelta
    add edx, vdelta
    ;4.
	;;v
    mov eax, edx
    shr eax, 16
    and ax, TsizeM1
    shl ax, cl
    mov si, ax
    add si, Toff
    ;;u
    mov eax, ebx
    shr eax, 16
    and ax, TsizeM1
    add si, ax
    mov al,[si+4]
    mov es:[di+3], al
    add ebx, udelta
    add edx, vdelta
    add di, 4
    dec wid
    jnz @xloop

test ch,ch
jz short @NolineH
@xloop2:
	;1.
	;;v
    mov eax, edx
    shr eax, 16
    and ax, TsizeM1
    shl ax, cl
    mov si, ax
    add si, Toff
    ;;u
    mov eax, ebx
    shr eax, 16
    and ax, TsizeM1
    add si, ax
    mov al,[si+4]
    mov es:[di], al
    add ebx, udelta
    add edx, vdelta
    inc di
    dec ch
    jnz @xloop2

@NolineH:

Ret

@clipLeft:
	mov cx, ax							;save in cx
	neg cx								;cx = clipped width
    mov dx,bx
    sub dx,ax
    add ax,dx
    cmp ax,0
    jl short @NolineH
    	xor eax, eax
    	mov ax, cx
    	imul eax, udelta				;eax=wid*udelta
    	add u, eax	    				;add to u to clip
        xor eax,eax
        mov ax, cx
        imul eax, vdelta				;eax=wid*vdelta
    	add v, eax						;add to v to clip
    	xor eax,eax
    Jmp  @PostclipLeft

@clipRight:
    mov bx,319
    Jmp  @PostclipRight


;Less then 4
@Less4:
mov wid, dx
mov ax, gs				;shift counter
mov cl, al				;use cl
mov ebx, u				;save u and v
mov edx, v

@xloopLess4:
	;;v
    mov eax, edx
    shr eax, 16
    and ax, TsizeM1
    shl ax, cl
    mov si, ax
    add si, Toff
    ;;u
    mov eax, ebx
    shr eax, 16
    and ax, TsizeM1
    add si, ax
    mov al,[si+4]
    mov es:[di], al
    add ebx, udelta
    add edx, vdelta
    inc di
    dec wid
    jnz short @xloopLess4
Ret

EndP


;;*****************Procedure for Textured Triangle Filler**************************************************

Public  RelTriT
RelTriT     Proc  Uses es di ds si,\
            Layer:word, x1:word, y1:word, u1:word, v1:word, x2:word, y2:word, u2:word, v2:word,\
            x3:word, y3:word, u3:word, v3:word, Tseg: word, Toff: word

            Local delta1:   dword
            Local delta2:   dword
            Local delta3:   dword
            Local Lx:       dword
            Local Rx:       dword
            Local udelta1:  dword
            Local udelta2:  dword
            Local udelta3:  dword
            Local Lu:       dword
            Local Ru:       dword
            Local vdelta1:  dword
            Local vdelta2:  dword
            Local vdelta3:  dword
            Local Lv:       dword
            Local Rv:       dword
            Local ycounter: word
            Local Tsize:	word
            Local xshift:	word

mov ax, y1
mov bx, y2
cmp bx, ax			;cmp y2, y1
jge short @noswapy1
	xchg ax, bx		;xchg y1, y2
	mov cx, x1
	mov dx, x2
	xchg cx, dx		;xchg x1, x2
	mov y1, ax
	mov y2, bx
	mov x1, cx
	mov x2, dx
	mov cx, u1
	mov dx, u2
	xchg cx, dx
	mov u1, cx
	mov u2, dx
	mov cx, v1
	mov dx, v2
	xchg cx, dx
	mov v1, cx
	mov v2, dx
@noswapy1:


mov ax, y3
mov bx, y1
cmp ax, bx			;cmp y3, y1
jge short @noswapy2
	xchg ax, bx		;xchg y3, y1
	mov cx, x3
	mov dx, x1
	xchg cx, dx		;xchg x3, x1
	mov y3, ax
	mov y1, bx
	mov x3, cx
	mov x1, dx
	mov cx, u3
	mov dx, u1
	xchg cx, dx
	mov u3, cx
	mov u1, dx
	mov cx, v3
	mov dx, v1
	xchg cx, dx
	mov v3, cx
	mov v1, dx
@noswapy2:


mov ax, y3
mov bx, y2
cmp ax, bx			;cmp y3, y2
jge short @noswapy3
	xchg ax, bx		;xchg y3, y2
	mov cx, x3
	mov dx, x2
	xchg cx, dx		;xchg x3, x2
	mov y3, ax
	mov y2, bx
	mov x3, cx
	mov x2, dx
	mov cx, u3
	mov dx, u2
	xchg cx, dx
	mov u3, cx
	mov u2, dx
	mov cx, v3
	mov dx, v2
	xchg cx, dx
	mov v3, cx
	mov v2, dx
@noswapy3:

mov delta1, 0
mov udelta1, 0
mov vdelta1, 0

mov bx, y2				;bx = y2
sub bx, y1				;bx = bx - y1

jz short @delta1_is_zero		;IF bx <> 0 THEN
    mov ax, x2			;ax = x2
    sub ax, x1			;ax = ax - x1

    shl eax, 16			;eax& = eax& * FIXPOINT
    cdq					;edx& = eax&
    					;edxeax& = edx&

    movsx ecx, bx		;ecx& = bx
    idiv ecx 			;delta1& = edxeax& \ ecx&
    mov delta1, eax

    					;uDelta1& = du1 * FIXPOINT \ dy1
    mov ax, u2			;ax = u2
    sub ax, u1			;ax = ax - u1

    shl eax, 16			;eax& = eax& * FIXPOINT
    cdq					;edx& = eax&
    					;edxeax& = edx&
    idiv ecx 			;delta1& = edxeax& \ ecx&
    mov udelta1, eax

    					;vDelta1& = dv1 * FIXPOINT \ dy1
    mov ax, v2			;ax = u2
    sub ax, v1			;ax = ax - u1

    shl eax, 16			;eax& = eax& * FIXPOINT
    cdq					;edx& = eax&
    					;edxeax& = edx&
    idiv ecx 			;delta1& = edxeax& \ ecx&
    mov vdelta1, eax

@delta1_is_zero:



mov delta2, 0
mov udelta2, 0
mov vdelta2, 0

mov bx, y3				;bx = y3
sub bx, y2				;bx = bx - y2

jz short @delta2_is_zero		;IF bx <> 0 THEN
    mov ax, x3			;ax = x3
    sub ax, x2			;ax = ax - x2

    shl eax, 16			;eax& = eax& * FIXPOINT
    cdq					;edx& = eax&
    					;edxeax& = edx&

    movsx ecx, bx		;ecx& = bx
    idiv ecx 			;delta1& = edxeax& \ ecx&
    mov delta2, eax

        				;uDelta1& = du2 * FIXPOINT \ dy2
    mov ax, u3			;ax = u3
    sub ax, u2			;ax = ax - u3

    shl eax, 16			;eax& = eax& * FIXPOINT
    cdq					;edx& = eax&
    					;edxeax& = edx&
    idiv ecx 			;delta1& = edxeax& \ ecx&
    mov udelta2, eax

           				;vDelta1& = dv2 * FIXPOINT \ dy2
    mov ax, v3			;ax = v3
    sub ax, v2			;ax = ax - v3

    shl eax, 16			;eax& = eax& * FIXPOINT
    cdq					;edx& = eax&
    					;edxeax& = edx&
    idiv ecx 			;delta1& = edxeax& \ ecx&
    mov vdelta2, eax


@delta2_is_zero:


mov delta3, 0
mov udelta3, 0
mov vdelta3, 0

mov bx, y1				;bx = y1
sub bx, y3				;bx = bx - y3

jz short @delta3_is_zero		;IF bx <> 0 THEN
    mov ax, x1			;ax = x1
    sub ax, x3			;ax = ax - x3

    shl eax, 16			;eax& = eax& * FIXPOINT
    cdq					;edx& = eax&
    					;edxeax& = edx&

    movsx ecx, bx		;ecx& = bx
    idiv ecx 			;delta1& = edxeax& \ ecx&
    mov delta3, eax

        				;uDelta1& = du3 * FIXPOINT \ dy3
    mov ax, u1			;ax = u1
    sub ax, u3			;ax = ax - u3

    shl eax, 16			;eax& = eax& * FIXPOINT
    cdq					;edx& = eax&
    					;edxeax& = edx&
    idiv ecx 			;delta1& = edxeax& \ ecx&
    mov udelta3, eax

        				;vDelta1& = dv3 * FIXPOINT \ dy3
    mov ax, v1			;ax = v1
    sub ax, v3			;ax = ax - v3

    shl eax, 16			;eax& = eax& * FIXPOINT
    cdq					;edx& = eax&
    					;edxeax& = edx&
    idiv ecx 			;delta1& = edxeax& \ ecx&
    mov vdelta3, eax

@delta3_is_zero:


Mov es, Layer
Mov ds, TSEG
mov ax, TOFF
mov fs, ax
mov si, ax
mov bx, [si]
shr bx,3 				;width
mov Tsize, bx


;calculate shifts

mov ax, bx
    cmp ax, 2
    jne short @xpow4
        mov bx, 1
        jmp short @SizeOK
@xpow4:
    cmp ax, 4
    jne short @xpow8
        mov bx, 2
        jmp short @SizeOK
@xpow8:
    cmp ax, 8
    jne short @xpow16
        mov bx, 3
        jmp short @SizeOK
@xpow16:
    cmp ax, 16
    jne short @xpow32
        mov bx, 4
        jmp short @SizeOK
@xpow32:
    cmp ax, 32
    jne short @xpow64
        mov bx, 5
        jmp short @SizeOK
@xpow64:
    cmp ax, 64
    jne short @xpow128
        mov bx, 6
        jmp short @SizeOK
@xpow128:
    cmp ax, 128
    jne short @xpow256
        mov bx, 7
        jmp short @SizeOK
@xpow256:
    cmp ax, 256
    jne @exit
    mov bx, 8


@SizeOK:
;Flat bottom
;Top part of triangle

mov gs, bx				;save xshift

mov ax, x1				;ax = x1
cwde					;eax& = ax
sal eax, 16				;eax& = eax& * FIXPOINT
mov ebx, eax			;ebx& = eax&
mov lx, eax
mov rx, ebx

mov ax, u1				;ax = u1
cwde					;eax& = ax
sal eax, 16				;eax& = eax& * FIXPOINT
mov ebx, eax			;ebx& = eax&
mov lu, eax
mov ru, ebx

mov ax, v1				;ax = v1
cwde					;eax& = ax
sal eax, 16				;eax& = eax& * FIXPOINT
mov ebx, eax			;ebx& = eax&
mov lv, eax
mov rv, ebx



mov cx, y1					;cx = y1
mov ycounter, cx
@Loop1:
	cmp ycounter, 0
    jl short @Noline1
    cmp ycounter, 199
    jg short @Noline1
    mov eax, lx
    mov ebx, rx
    sar	eax, 16				;ax = eax& \ FIXPOINT
    sar ebx, 16				;bx = ebx& \ FIXPOINT
    Push ax 				;x1
    push bx	    			;x2
    mov ecx, lu				;u1
    mov edx, ru             ;u2
    mov ax, ycounter
    push ax
    Push ecx				;Lu
    push edx				;Ru
    mov eax, lv
    mov ebx, rv
    Push eax				;Lv
    push ebx				;Rv
    mov cx, Tsize
    push cx


    call HTline

@Noline1:
    mov eax, delta1
    mov ebx, delta3
    mov ecx, udelta1
    mov edx, udelta3

    add lx, eax 			;eax& = eax& + delta1&
    add rx, ebx 			;ebx& = ebx& + delta3&
    add lu, ecx 			;eax& = eax& + udelta1&
    add ru, edx 			;ebx& = ebx& + udelta3&

    mov eax, vdelta1
    mov ebx, vdelta3
    add lv, eax 			;eax& = eax& + vdelta1&
    add rv, ebx 			;ebx& = ebx& + vdelta3&


    inc ycounter
    mov cx, ycounter
    cmp cx, y2
    jle short @Loop1

    mov ebx, delta3
    sub rx, ebx  			;subtexel accuracy
    mov edx, udelta3
    sub ru, edx
    mov eax, vdelta3
    sub rv, eax


;Flat top
;Lower part of triangle

mov ax, x2				;ax = x2
cwde					;eax& = ax
sal eax, 16				;eax& = eax& * FIXPOINT
mov lx, eax

mov ax, u2				;ax = u2
cwde					;eax& = ax
sal eax, 16				;eax& = eax& * FIXPOINT
mov lu, eax

mov ax, v2				;ax = v2
cwde					;eax& = ax
sal eax, 16				;eax& = eax& * FIXPOINT
mov lv, eax


mov cx, y2				;cx = y2
mov ycounter, cx
@Loop2:
	cmp ycounter, 0
    jl short @Noline2
    cmp ycounter, 199
    jg short @Noline2
    mov eax, lx
    mov ebx, rx
    sar	eax, 16				;ax = eax& \ FIXPOINT
    sar ebx, 16				;bx = ebx& \ FIXPOINT
    Push ax	    			;x1
    push bx 				;x2
    mov ecx, lu				;u1
    mov edx, ru             ;u2
    mov ax, ycounter
    push ax
    Push ecx				;Lu
    push edx				;Ru
    mov eax, lv
    mov ebx, rv
    Push eax				;Lv
    push ebx				;Rv

    mov cx, Tsize
    push cx


    call HTline

@Noline2:
    mov eax, delta2
    mov ebx, delta3
    mov ecx, udelta2
    mov edx, udelta3

    add lx, eax 			;eax& = eax& + delta2&
    add rx, ebx 			;ebx& = ebx& + delta3&
    add lu, ecx 			;eax& = eax& + udelta2&
    add ru, edx 			;ebx& = ebx& + udelta3&

    mov eax, vdelta2
    mov ebx, vdelta3
    add lv, eax 			;eax& = eax& + vdelta2&
    add rv, ebx 			;ebx& = ebx& + vdelta3&


    inc ycounter
    mov cx, ycounter
    cmp cx, y3
    jl short @Loop2

@exit:
Ret

EndP

End


BasicCoder2
Posts: 3954
Joined: Jan 01, 2009 7:03
Location: Australia

Post by BasicCoder2 »

I might be interested in asm to speed up some proceedures.

Asm is best when a simple operation is applied to a large
amount of data which can be the case in image processing.

The code below is an example of some of the procedures that
I might like to speed up.

Note if you want to run it you need to change the file path
to your own 640x480x32 image. I will probably change it all
to handle any size image, it is just that the code is meant
to be used with a webcam/robot and I use 320x240 or 640x480
as that seems to be a webcam standard. The lower resolution
can be useful if the images are not being processed fast
enough in the higher resolution.

It also requires an include written by DJPeters which I use
to capture webcam images.

#include "escapi.bi"

Note also the procedures assume images are in the form of
uinteger arrays.

I have programmed in asm (Z-80, 6502, 68000, 8086) although
mostly with 16bit dos. I have not used the floating point
instructions or the newer blit like instructions. I did
start using the 32 bit register stuff with a dos extender
before I converted to C.

I am a self taught occasional programmer so there are big
gaps in what I know :)

JohnC

Code: Select all

#include "escapi.bi"
Const iWIDTH = 640
Const iHEIGHT = 480

ScreenRes 640,480,32

Dim as string file


Dim As Any Ptr lpImage=ImageCreate(iWIDTH,iHEIGHT)

'global variables
Dim Shared As Uinteger image1(iWIDTH,iHEIGHT)
Dim Shared As Uinteger image2(iWIDTH,iHEIGHT)
Dim Shared As Uinteger image3(iWIDTH,iHEIGHT)

Dim Shared As SimpleCapParams Params

Params.mWidth  = iWIDTH
Params.mHeight = iHEIGHT
Params.mTargetBuf=cptr(Integer Ptr,lpImage)
Params.mTargetBuf+=8

'must be intialized
dim shared as integer HFmask(3,3)

Sub loadImage(image1() as uinteger,file as string,lpImage as any ptr)
    Dim As integer i
    bload file,lpImage
    i = 0
    For y As Integer = 0 To iHEIGHT-1
        For x As Integer = 0 To iWIDTH-1
            image1(x,y) = Params.mTargetBuf[i]
            i = i + 1
        Next x
    Next y    
End Sub

Sub displayImage(image1() As Uinteger,lpImage as any ptr)
    Dim As Integer i
    i=0
    For y As Integer = 0 To iHEIGHT-1
        For x As Integer = 0 To iWIDTH-1
            Params.mTargetBuf[i] = image1(x,y)
            i = i + 1
        Next x
    Next y
    Put (0,0), lpImage,Pset
End Sub

Sub invertImage(image1() as uinteger, image2() as uinteger)
   dim as integer v,r,g,b
   for y as integer = 0 to iHEIGHT-1
      for x as integer = 0 to iWIDTH-1
         v = image2(x,y)
         r = 255 - (v shr 16 and 255)
         g = 255 - (v shr  8 and 255)
         b = 255 - (v and 255)
         v = (r shl 16) or (g shl 8) or (b)
         image1(x,y) = v
      next x
   next y
End Sub

Sub bwImage(image1() as uinteger, image2() as uinteger)
   dim as integer v,r,g,b
   for y as integer = 0 to iHEIGHT-1
      for x as integer = 0 to iWIDTH-1
         v = image2(x,y)
         r = v shr 16 and 255
         g = v shr  8 and 255
         b = v and 255
         v = (r+g+b)/3
         v = (v shl 16) or (v shl 8) or (v)
         image1(x,y) = v
      next x
   next y
End Sub

Sub posteralizeImage(image1() as uinteger, image2() as uinteger)
   dim as integer v,r,g,b
   for y as integer = 0 to iHEIGHT-1
      for x as integer = 0 to iWIDTH-1
         v = image2(x,y)
         r = (v shr 16 and 255) and &HC0
         g = (v shr  8 and 255) and &HC0
         b = (v and 255) and &H80
         v = (r shl 16) or (g shl 8) or (b)
         image1(x,y) = v
      next x
   next y
End Sub

Sub AverageImage(image1() as uinteger, image2() as uinteger)
   dim as integer v,r,g,b,avgRed,avgGrn,avgBlu
   for y as integer = 1 to iHEIGHT-2
      for x as integer = 1 to iWIDTH-2
          avgRed = 0
          avgGrn = 0
          avgBlu = 0
          for q as integer = -1 to 1
              for p as integer = -1 to 1
                  
                  v = image2(x+p,y+q)
                  r = v shr 16 and 255
                  g = v shr  8 and 255
                  b = v and 255
                  
                  avgRed = avgRed + r
                  avgGrn = avgGrn + g
                  avgBlu = avgBlu + b
                  
              next p
          next q
          r = avgRed/9
          g = avgGrn/9
          b = avgBlu/9
          v = (r shl 16) or (g shl 8) or (b)
         image1(x,y) = v
      next x
   next y
End Sub

Sub colorRatio(image2() as uinteger, image1() as uinteger)
    dim as integer v,r,g,b
    dim as double mult
    for y as integer = 0 to iHEIGHT-1
        for x as integer = 0 to iWIDTH-1
            v = image1(x,y)
            r = v shr 16 and 255
            g = v shr  8 and 255
            b = v and 255
            if r>=g and r>=b then
                mult = 255/r
            else
                if g>=r and g>=b then
                    mult = 255/g
                else
                    if b>=r and b>=g then
                        mult = 255/b
                    end if
                end if
            end if
            r = int(r * mult)
            g = int(g * mult)
            b = int(b * mult)
            image2(x,y) = rgb(r/2,g/2,b/2)
        next x
    next
end sub

sub edgeImage(image2() as uinteger, image1() as uinteger)
    dim as integer v,r,g,b,r1,g1,b1,r2,g2,b2,d1,d2
    For y As Integer = 1 To 478
        For x As Integer = 1 To 638
            v = image1(x,y)
            r1 = v shr 16 and 255
            g1 = v shr  8 and 255
            b1 = v and 255
            v = image1(x+1,y)
            r2 = v shr 16 and 255
            g2 = v shr  8 and 255
            b2 = v and 255
            r = Abs(r1-r2)
            g = Abs(g1-g2)
            b = Abs(b1-b2)
            d1 = Sqr(r*r+g*g+b*b)
            v = image1(x,y+1)
            r2 = v shr 16 and 255
            g2 = v shr  8 and 255
            b2 = v and 255
            r = Abs(r1-r2)
            g = Abs(g1-g2)
            b = Abs(b1-b2)
            d2 = Sqr(r*r+g*g+b*b)
            v = abs(d1+d2)
            if v>255 then v=255
            if v>8 then v = 0 else v=255
            image2(x,y) = rgb(v,v,v)
        Next x
    Next y
end sub

Sub clearImage(image1() as uinteger)
   dim as integer v,r1,g1,b1,r2,g2,b2
   for y as integer = 0 to iHEIGHT-1
      for x as integer = 0 to iWIDTH-1
         image1(x,y) = 0
      next x
   next y
End Sub

Sub HFbypass(image1() as uinteger, image2() as uinteger)
   dim as integer v,r,g,b,sum
   for y as integer = 0 to iHEIGHT-3
      for x as integer = 0 to iWIDTH-3
          sum = 0
          for yy as integer = 0 to 2
              for xx as integer = 0 to 2                  
                  v = image2(x+xx,y+yy)
                  r = v shr 16 and 255
                  g = v shr  8 and 255
                  b = v and 255
                  v = (r+g+b)\3
                  sum = sum + v * HFmask(xx,yy)
              next xx
          next yy
          if sum<0 then sum = 0
          if sum>255 then sum = 255
          image1(x,y) = rgb(sum,sum,sum) 
      next x
   next y
End Sub

Dim As Integer imgW, imgH

Dim As Integer fileHnd = Freefile()
    
'MAIN PROGRAM
'intialize masks

for j as integer = 0 to 2
    for i as integer = 0 to 2
        read HFmask(i,j)
    next i
next j

for i as integer = 1 to 17
    file = "C:\FreeBasic\bitmaps\Views\view" + str(i) + ".bmp"

    Open file For Binary As #fileHnd
    '' image width/height are stored in bytes 19 and 23 of a bitmap file, respectively
    Get #fileHnd, 19, imgW
    Get #fileHnd, 23, imgH
    Close #fileHnd

    loadImage(image1(),file,lpImage)
    displayImage(image1(),lpImage)
    HFbypass(image2(),image1())
    displayImage(image2(),lpImage)
    sleep

next i

imageDestroy(lpImage)

End

data -1,-1,-1
data -1, 9,-1
data -1,-1,-1

kiyotewolf
Posts: 1009
Joined: Oct 11, 2008 7:42
Location: ABQ, NM
Contact:

Post by kiyotewolf »

Oooh! Filter code that's not written in OOP style!

Neat neat neat!



~Kiyote!
kiyotewolf
Posts: 1009
Joined: Oct 11, 2008 7:42
Location: ABQ, NM
Contact:

Post by kiyotewolf »

@BasicCoder2
Note also the procedures assume images are in the form of
uinteger arrays.
Do you stuff an entire Red of 8 bits, Green of 8 bits, and a Blue of 8 bits in one element of the UINTEGER array?

How physically big IS a UINTEGER anyways?

Are you stuffing 24-bit RGB full color pixel in thar?

Details man.. details..

Also, do those filters operate with UINTEGER pixels as well?



~Kiyote!
Post Reply