Results 1 to 6 of 6

Thread: Here's my functions for fixing endianness

Threaded View

  1. #1

    Thread Starter
    Frenzied Member
    Join Date
    Oct 2008
    Posts
    1,181

    Here's my functions for fixing endianness

    This is the ASM code needed to compile a DLL file that provides functions for swapping byte order (BigE to LittleE, or visa versa). The resulting DLL file is very useful in VB6 for code that reads or writes data from files that are used on a machine with BigEndian byte order, as Intel CPUs use LittleEndian byte order, and VB6 doesn't have any built-in functions to accomplish this.

    This code is intended to be assembled with NASM and linked with GoLink.

    Code:
    EXPORT FixShort
    EXPORT FixInt
    
    SEGMENT .text
    
    dllmain:
    mov eax,1
    ret 12
    
    FixShort:
    push ebp
    mov ebp,esp
    push edx
    mov edx,[ebp+8]
    mov eax,0
    mov al,dh
    mov ah,dl
    pop edx
    leave
    ret 4
    
    FixInt:
    push ebp
    mov ebp,esp
    push edx
    mov edx,[ebp+8]
    mov al,dl
    shl eax,8
    shr edx,8
    mov al,dl
    shl eax,8
    shr edx,8
    mov al,dl
    shl eax,8
    shr edx,8
    mov al,dl
    pop edx
    leave
    ret 4
    Note that Short here refers to a 16 bit integer (just like in C and C++), though would be called Integer in VB6.
    Note that Int here refers to a 32 bit integer (just like in C and C++), though would be called Long in VB6 (despite Long meaning a 64 bit integer in C and C++).

    And yes, even when I'm working with the 16 bit integers I use EAX instead of AX when handling the whole number, because VB6 actually uses 32 bit register opcodes for handling 16 bit numbers (if you disassemble a program written in VB6 you can see this) and then just calls additional functions to make sure that the number doesn't exceed the 16 bit limit, and triggers an error if it does. Same with the stack. VB6 uses 32 bit stack opcodes instead of 16 bit opcodes, even for 16 bit values.

    Here are the proper declarations for using this DLL file's functions in VB6.
    Code:
    Private Declare Function FixShort Lib "endianfixer.dll" (ByVal Value As Integer) As Integer
    Private Declare Function FixInt Lib "endianfixer.dll" (ByVal Value As Long) As Long
    Change Private to Public if you are going to declare them in a Module for use elsewhere.
    Last edited by Ben321; May 5th, 2017 at 12:04 AM. Reason: fixed typo

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  



Click Here to Expand Forum to Full Width