Valhalla Legends Archive

Programming => General Programming => Topic started by: TheMinistered on May 13, 2003, 11:16 PM

Title: Bitwise Operations [VB 6.0]
Post by: TheMinistered on May 13, 2003, 11:16 PM
I got tired of trying to implement bitwise operations into visual basic using only visual basic.  Thus, this is a result of my frustration!  This will only work in visual basic 6.0 -- have fun!


'Class: CBitwise
Option Explicit

Private Type TPROC
   Memory As Long
   Pointer As Long
End Type
Private Proc() As TPROC

Private Sub Class_Initialize()
   Dim a As Long, b As Long
   
   ShiftLeft VarPtr(a), VarPtr(b)
   ShiftRight VarPtr(a), VarPtr(b)
   RotateLeft VarPtr(a), VarPtr(b)
End Sub

Public Function ShiftLeft(ByVal Value As Long, ByVal Shift As Long) As Long
   ExecuteMe "8B5C2408 8B44240C 8B542410 8B1B 8A08 D3E3 891A 31C0 C20A00"
End Function

Public Function ShiftRight(ByVal Value As Long, ByVal Shift As Long) As Long
   ExecuteMe "8B5C24088B44240C8B5424108B1B8A08D3EB891A31C0C20A00"
End Function

Public Function RotateLeft(ByVal Value As Long, ByVal Shift As Long) As Long
   ExecuteMe "8B5C2408 8B44240C 8B542410 8B1B 8A08 D3C3 891A 31C0 C20A00"
End Function

Private Sub ExecuteMe(ByVal YourCode As String)
   Dim i As Long, size As Long, b() As Byte
   
   YourCode = Replace$(YourCode, " ", "")
   size = LenB(YourCode)

   ReDim Preserve b(1 To size)
   For i = 1 To size
       b(i) = Val("&H" & Mid$(YourCode, i * 2 - 1, 2))
   Next
   
   Static p As Long
   ReDim Preserve Proc(p)
   Dim Mem As Long, Ptr As Long
   Mem = GlobalAlloc(0, size)
   Ptr = GlobalLock(Mem)
   RtlMoveMemory ByVal Ptr, b(1), size
   GlobalUnlock Mem
   
   Proc(p).Memory = Mem
   Proc(p).Pointer = VirtualTable(p)
   VirtualTable(p) = Ptr
   p = p + 1
End Sub

Private Property Let VirtualTable(ByVal Index As Long, ByVal ProcPtr As Long)
   Dim i As Long
   
   Index = &H1C + Index * 4
   RtlMoveMemory i, ByVal ObjPtr(Me), 4
   RtlMoveMemory ByVal i + Index, ProcPtr, 4
End Property
Private Property Get VirtualTable(ByVal Index As Long) As Long
   Dim i As Long
   
   Index = &H1C + Index * 4
   RtlMoveMemory i, ByVal ObjPtr(Me), 4
   RtlMoveMemory VirtualTable, ByVal i + Index, 4
End Property

Private Sub Class_Terminate()
   On Error GoTo Error
   Dim i As Long
   For i = 0 To UBound(Proc)
       VirtualTable(i) = Proc(i).Pointer
       GlobalFree Proc(i).Memory
   Next
Error:
End Sub



mov ebx, dword [esp+08h] ; value
mov eax, dword [esp+0Ch] ; shift
mov edx, dword [esp+10h] ; hresult

mov ebx, [ebx]
mov cl, byte [eax]
rol ebx, cl

mov dword [edx], ebx
xor eax, eax
ret 10



mov ebx, dword [esp+08h] ; value
mov eax, dword [esp+0Ch] ; shift
mov edx, dword [esp+10h] ; hresult

mov ebx, [ebx]
mov cl, byte [eax]
shl ebx, cl

mov dword [edx], ebx
xor eax, eax
ret 10



mov ebx, dword [esp+08h] ; value
mov eax, dword [esp+0Ch] ; shift
mov edx, dword [esp+10h] ; hresult

mov ebx, [ebx]
mov cl, byte [eax]
shr ebx, cl

mov dword [edx], ebx
xor eax, eax
ret 10


The above three snippets are using nasm! have fun, and don't forget you need the following declares in a module!

'Memory allocation functions
Public Declare Function GlobalAlloc Lib "kernel32" (ByVal wFlags As Long, ByVal dwBytes As Long) As Long
Public Declare Function GlobalFree Lib "kernel32" (ByVal hMem As Long) As Long
Public Declare Function GlobalLock Lib "kernel32" (ByVal hMem As Long) As Long
Public Declare Function GlobalUnlock Lib "kernel32" (ByVal hMem As Long) As Long

'Memory copy and move functions
Public Declare Sub RtlMoveMemory Lib "kernel32" (Destination As Any, Source As Any, ByVal Length As Long)
Title: Re:Bitwise Operations [VB 6.0]
Post by: TheMinistered on May 13, 2003, 11:23 PM
I implemented a version of CalcHashBuf using the aid of the bitwise functions above, I will post it sometime in the future (hopefully).
Title: Re:Bitwise Operations [VB 6.0]
Post by: Yoni on May 14, 2003, 08:49 AM
You are writing VB code in the same style I wrote VB code just before I switched to VC++.
(i.e. highly exaggerated low level stuff beyond the power of VB)

Switch to VC++.
Title: Re:Bitwise Operations [VB 6.0]
Post by: iago on May 14, 2003, 11:22 AM
That's what I was going to say.  Clearly you've pretty much grown beyond the scope of what you can do with vb and move on to something more powerful :-)
Title: Re:Bitwise Operations [VB 6.0]
Post by: Dumb_Canadian on May 15, 2003, 10:42 AM
Sounds like you're ready for VB.NET, TheMinistered:)
Title: Re:Bitwise Operations [VB 6.0]
Post by: Banana fanna fo fanna on May 15, 2003, 07:08 PM
He intended to get BEYOND VB6, not lower himself farther.