covert a bit of C to purebasic help please ;)

Just starting out? Need help? Post your questions and find answers here.
ehowington
Enthusiast
Enthusiast
Posts: 114
Joined: Sat Sep 12, 2009 3:06 pm

covert a bit of C to purebasic help please ;)

Post by ehowington »

Trying to convert this bit of C to purebasic any help would be appricated as i have tried a few times with this bit of code.

Code: Select all

/* -------------------------------------------------------------------- */
/*  Establish the byte order on this machine.           */
/* -------------------------------------------------------------------- */
#if !defined(bBigEndian)
    i = 1;
    if( *((uchar *) &i) == 1 )
        bBigEndian = FALSE;
    else
        bBigEndian = TRUE;
#endif
User avatar
skywalk
Addict
Addict
Posts: 3972
Joined: Wed Dec 23, 2009 10:14 pm
Location: Boston, MA

Re: covert a bit of C to purebasic help please ;)

Post by skywalk »

My try using a global instead of constant.

Code: Select all

CompilerIf Defined(BigEndian, #PB_Variable) = 0
  Global.i BigEndian = 1
  If PeekA(@BigEndian) = 1
    BigEndian = 0
  EndIf
CompilerEndIf
Debug BigEndian
The nice thing about standards is there are so many to choose from. ~ Andrew Tanenbaum
ehowington
Enthusiast
Enthusiast
Posts: 114
Joined: Sat Sep 12, 2009 3:06 pm

Re: covert a bit of C to purebasic help please ;)

Post by ehowington »

Tried with both 86 compiler and 64 both produce same result?
User avatar
skywalk
Addict
Addict
Posts: 3972
Joined: Wed Dec 23, 2009 10:14 pm
Location: Boston, MA

Re: covert a bit of C to purebasic help please ;)

Post by skywalk »

Endian is an architecture setting, not processor bit depth dependent.

Code: Select all

Procedure.l Endian(x.l)
  ;ProcedureReturn (x & $FF) << 24 + (x & $FF00) << 8 + (x >> 8) & $FF00 + (x >> 24) & $FF
  ;Debug Endian(32) ; 536870912
  !MOV eax, dword[p.v_x]
  !BSWAP eax                  ; 32 bit little endian <-> big endian
  ProcedureReturn             ; eax is returned by default
EndProcedure

CompilerIf Defined(BigEndian, #PB_Variable) = 0
  Global.i BigEndian = 1
  BigEndian = Endian(BigEndian) ;<-- Comment this out. DEBUG only.
  If PeekA(@BigEndian) = 1
    BigEndian = 0
  EndIf
CompilerEndIf
Debug BigEndian

Procedure.i IsBigEndian(DoNotChange1.i=1)
  ; Macro's cannot get address of a literal value. Must use procedure.
  PeekA(@DoNotChange1)
EndProcedure
Debug IsBigEndian()
The nice thing about standards is there are so many to choose from. ~ Andrew Tanenbaum
ehowington
Enthusiast
Enthusiast
Posts: 114
Joined: Sat Sep 12, 2009 3:06 pm

Re: covert a bit of C to purebasic help please ;)

Post by ehowington »

You have been most helpful.

Any thoughts on this bit of c code?

Code: Select all

/************************************************************************/
/*                              SwapWord()                              */
/*                                                                      */
/*      Swap a 2, 4 or 8 byte word.                                     */
/************************************************************************/

static void	SwapWord( int length, void * wordP )

{
    int		i;
    uchar	temp;

    for( i=0; i < length/2; i++ )
    {
	temp = ((uchar *) wordP)[i];
	((uchar *)wordP)[i] = ((uchar *) wordP)[length-i-1];
	((uchar *) wordP)[length-i-1] = temp;
    }
}
User avatar
skywalk
Addict
Addict
Posts: 3972
Joined: Wed Dec 23, 2009 10:14 pm
Location: Boston, MA

Re: covert a bit of C to purebasic help please ;)

Post by skywalk »

This is what I use for byte swapping Q(8),L(4),W(2).

Code: Select all

Procedure.q EndianQ(x.q)
  CompilerIf #PB_Compiler_Processor = #PB_Processor_x64
    !MOV rax, qword[p.v_x]
    !BSWAP rax
  CompilerElse
    !MOV edx, dword[p.v_x]
    !MOV eax, dword[p.v_x+4]
    !BSWAP edx
    !BSWAP eax
  CompilerEndIf
  ProcedureReturn             ; eax is returned by default with quad picking up edx also.
EndProcedure
Procedure.l Endian(x.l)
  ;ProcedureReturn (x & $FF) << 24 + (x & $FF00) << 8 + (x >> 8) & $FF00 + (x >> 24) & $FF
  ;Debug Endian(32) ; 536870912
  !MOV eax, dword[p.v_x]
  !BSWAP eax                  ; 32 bit little endian <-> big endian
  ProcedureReturn             ; eax is returned by default
EndProcedure
Procedure.w EndianW(x.w)
  ; ProcedureReturn (e & $FF) << 8 + (e >> 8) & $FF
  !MOV ax, word[p.v_x]
  !XCHG al, ah                ; Swap Lo byte <-> Hi byte
  !MOV word[p.v_x], ax
  ProcedureReturn x
EndProcedure
Define.u x=300, y=11265
Debug EndianW(x)  ;<-- 11265
Debug EndianW(y)  ;<-- 300
The nice thing about standards is there are so many to choose from. ~ Andrew Tanenbaum
ehowington
Enthusiast
Enthusiast
Posts: 114
Joined: Sat Sep 12, 2009 3:06 pm

Re: covert a bit of C to purebasic help please ;)

Post by ehowington »

I could kiss you lol but ill just shake your hand good sir

Been driving me nuts messing with some code that done in C
Post Reply