Please convert to ASM

Just starting out? Need help? Post your questions and find answers here.
Wladek
User
User
Posts: 98
Joined: Mon Feb 23, 2009 4:01 pm
Location: Poland
Contact:

Post by Wladek »

Meaby some this:

Code: Select all

Procedure Odbierz(liczba.b, numer)     ;32-Bit
  ;value-range liczba: 0-255, value-range numer: 1-8
  !MOVZX eax,byte[p.v_liczba]
  !CMP dword[p.v_numer],8    ;if numer is long
  !JNE @f
  !SHR eax,7
  !NOT eax
  !AND eax,1
 ProcedureReturn   ;integer!
  !@@:
  !MOV cl,[p.v_numer]
  !DEC cl
  !SHR eax,cl
  !AND eax,1
 ProcedureReturn   ;integer!
EndProcedure 

Procedure goodOdbierz(liczba.b, numer)
a=Odbierz(liczba.b, numer)
if numer = 8
a=a~
endif
ProcedureReturn  a
EndProcedure 
Helle
Enthusiast
Enthusiast
Posts: 178
Joined: Wed Apr 12, 2006 7:59 pm
Location: Germany
Contact:

Post by Helle »

@Michael: I get 1 0 1 0 1 0 1 1 :shock: .
My test-routine:

Code: Select all

Procedure OdbierzOriginal(liczba.b,numer) 
liczba.b+128 
a1.d=liczba.b/2 
a2.d=Round(a1, #PB_Round_Down)/2 
a3.d=Round(a2, #PB_Round_Down)/2 
a4.d=Round(a3, #PB_Round_Down)/2 
a5.d=Round(a4, #PB_Round_Down)/2 
a6.d=Round(a5, #PB_Round_Down)/2 
a7.d=Round(a6, #PB_Round_Down)/2 
a8.d=Round(a7, #PB_Round_Down)/2 

Select numer 
Case 1 
a=Round(a1, #PB_Round_Up)-Round(a1, #PB_Round_Down) 
Case 2 
a=Round(a2, #PB_Round_Up)-Round(a2, #PB_Round_Down) 
Case 3 
a=Round(a3, #PB_Round_Up)-Round(a3, #PB_Round_Down) 
Case 4 
a=Round(a4, #PB_Round_Up)-Round(a4, #PB_Round_Down) 
Case 5 
a=Round(a5, #PB_Round_Up)-Round(a5, #PB_Round_Down) 
Case 6 
a=Round(a6, #PB_Round_Up)-Round(a6, #PB_Round_Down) 
Case 7 
a=Round(a7, #PB_Round_Up)-Round(a7, #PB_Round_Down) 
Case 8 
a=Round(a8, #PB_Round_Up)-Round(a8, #PB_Round_Down) 
EndSelect 

ProcedureReturn a 
EndProcedure 

Procedure OdbierzASM(liczba.b, numer)     ;32-Bit
  ;value-range liczba: 0-255, value-range numer: 1-8 
  !MOVZX eax,byte[p.v_liczba]
  !CMP dword[p.v_numer],8    ;if numer is long
  !JNE @f
  !SHR eax,7
  !NOT eax
  !AND eax,1
 ProcedureReturn   ;integer!
  !@@:
  !MOV cl,[p.v_numer]
  !DEC cl
  !SHR eax,cl
  !AND eax,1
 ProcedureReturn   ;integer!
EndProcedure

For numer = 1 To 8
  For liczba = 0 To 255
    Debug numer
    Debug liczba
    x = OdbierzOriginal(liczba, numer)
    y = OdbierzASM(liczba, numer)
    Debug x
    Debug y
    If x <> y
      Debug "Error!"
      Delay(1000)
    EndIf
    Debug "==========="
  Next
Next
I get no errors! PB 4.31 Beta2 (x64).

Gruss
Helle
Wladek
User
User
Posts: 98
Joined: Mon Feb 23, 2009 4:01 pm
Location: Poland
Contact:

Post by Wladek »

Helle- you right
Thanks
User avatar
Michael Vogel
Addict
Addict
Posts: 2797
Joined: Thu Feb 09, 2006 11:27 pm
Contact:

Post by Michael Vogel »

Helle wrote:@Michael: I get 1 0 1 0 1 0 1 1 :shock: .
The only error can be seen(?) in my brain, I haven't read careful enough :( and thought, something like that has to be found...

Code: Select all

liczba=25
numer=3
Debug Asc(Mid(RSet(Bin(liczba),8,"0"),9-numer))-'0'
But the first bit has to be inverted, so now I got it (hopefully;)...

Even when Helles code is the best solution for the given problem, I'll try to add one additional idea for optimizing similar problems, it may be useful (especially when slow math functions are involved) to create a result table at the beginning...

Code: Select all

Procedure OdbierzASM(liczba.b, numer)     ;32-Bit
	;value-range liczba: 0-255, value-range numer: 1-8
	!MOVZX eax,byte[p.v_liczba]
	!CMP dword[p.v_numer],8    ;if numer is long
	!JNE @f
	!SHR eax,7
	!NOT eax
	!AND eax,1
	ProcedureReturn   ;integer!
	!@@:
	!MOV cl,[p.v_numer]
	!DEC cl
	!SHR eax,cl
	!AND eax,1
	ProcedureReturn   ;integer!
EndProcedure

Global Dim SBits.b(255,8)
For l = 0 To 255
	s.s=RSet(Bin(l),8,"0")
	PokeB(@s,'0'+'1'-PeekB(@s))
	For i=1 To 8
		SBits(l,i)=Asc(mid(s,9-i,1))-'0'
	Next i
Next l

For liczba = 0 To 255
	a.s=""
	b.s=""
	For numer = 1 To 8
		a=Str(OdbierzASM(liczba, numer))+a
		b=Str(Sbits(liczba,numer))+b
	Next numer
	If a<>b
		Debug Str(liczba)+": "+a+"~"+b
	EndIf
Next
Wladek
User
User
Posts: 98
Joined: Mon Feb 23, 2009 4:01 pm
Location: Poland
Contact:

Post by Wladek »

Meaby, I 'm make mistake in my code.:oops:
Thanks all ,who help me.
Post Reply