Common functions in VB [ZT]

Source: Internet
Author: User
Tags first string

; //////////////////////////////////////// //////////////////

VB module declare:

Option explicit

'Bits
Declare function movebitl lib "utils. dll "_
(Byval dwval as long ,_
Byval dwmovenum as long) as long

Declare function movebitr lib "utils. dll "_
(Byval dwval as long ,_
Byval dwmovenum as long) as long

'Numeric Converter
Declare function bin2dec lib "utils. dll "_
(Byref pbin as any) as long

Declare function dec2bin lib "utils. dll "_
(Byref pbin as any ,_
Byval dwval as long) as long

Declare function dec2hex lib "utils. dll "_
(Byref Phex as any ,_
Byval dwval as long) as long

Declare function hex2dec lib "utils. dll "_
(Byref Phex as any) as long

'Strings
Declare function mstrlen lib "utils. dll "_
(Byref pstring as any) as long

Declare function mstrcopy lib "utils. dll "_
(Byref pdest as any ,_
Byref psrc as any) as long

Declare function mstrcat lib "utils. dll "_
(Byref pdest as any ,_
Byref psrc as any) as long

'By Borland. Return a pointer of position in pdest
Declare function mstrpos lib "utils. dll "_
(Byref pdest as any ,_
Byref psrc as any) as long

Declare function mstrupper lib "utils. dll "_
(Byref psrc as any) As long 'by Borland.

Declare function mstrlower lib "utils. dll "_
(Byref psrc as any) As long 'by Borland.

'Memory
Declare function mmemcopy lib "utils. dll "_
(Byref pdst as any ,_
Byref psrc as any ,_
Byval nlength as long) as long

Declare function mmemfill lib "utils. dll "_
(Byref pdst as any ,_
Byval cfill as long ,_
Byval nlength as long) as long

Declare function mmemzero lib "utils. dll "_
(Byref pdst as any ,_
Byval ucount as long) as long

; //////////////////////////////////////// /////////////////////
; Masm32 in dll:

. 386; create 32 bit code
. Model flat, stdcall; 32 bit Memory Model
Option Casemap: none; case sensitive

Include windows. inc
Include masm32.inc
Include gdi32.inc
Include user32.inc
Include kernel32.inc

Includelib masm32.lib
Includelib gdi32.lib
Includelib user32.lib
Includelib kernel32.lib
; Includelib comctl32.lib
; Includelib comdlg32.lib
; Includelib shell32.lib

Dllentry proto hinstance: DWORD, dwfunction: DWORD, lpreserve: DWORD

Movebitl proto dwval: DWORD, dwmovenum: DWORD
Movebitr proto dwval: DWORD, dwmovenum: DWORD

Bin2dec proto pbin: DWORD
Dec2bin proto pbin: DWORD, dwval: DWORD
Dec2hex proto Phex: DWORD, ndec: DWORD
Hex2dec proto Phex: DWORD

Mstrlen proto pstring: DWORD; for borland
Mstrcopy proto pdest: DWORD, psrc: DWORD; for borland
Mstrcat proto pdest: DWORD, psrc: DWORD; for borland
Mstrpos proto pszdest: DWORD, pszsrc: DWORD; for borland
Mstrupper proto pszsrc: DWORD; for borland
Mstrlower proto pszsrc: DWORD; for borland

Mmemcopy proto pdst: DWORD, psrc: DWORD, nlength: DWORD
Mmemfill proto pdst: DWORD, cfill: DWORD, nlength: DWORD
Mmemzero proto pdest: DWORD, ucount: DWORD

; //////////////////////////////////////// ///////////

. Code
Align 4
; //////////////////////////////////////// //////////////////////////
Dllentry proc hinstance: DWORD, dwfunction: DWORD, lpreserve: DWORD

XOR eax, eax
INC eax
RET
 
Dllentry endp
; //////////////////////////////////////// //////////////////////////
Movebitl proc ndec: DWORD, dwmovenum: DWORD

MoV eax, dword ptr [ndec]
MoV ECx, dword ptr [dwmovenum]
SHL eax, Cl
RET

Movebitl endp
; //////////////////////////////////////// ///////////////////////////
Movebitr proc ndec: DWORD, dwmovenum: DWORD

MoV eax, dword ptr [ndec]
MoV ECx, dword ptr [dwmovenum]
SHR eax, Cl
RET

Movebitr endp
; //////////////////////////////////////// ///////////////////////////
Bin2dec proc pbin: DWORD

; Argument check
MoV eax, dword ptr [pbin]
Test eax, eax
JZ @ exit

Push ESI
MoV ESI, eax
XOR eax, eax
XOR edX, EDX
ClD

@@:
Lodsb
Test Al, Al
JZ finalflag; Final

CMP Al, 30 h; 0
JZ isbin
CMP Al, 31 H; 1
JZ isbin
Jnz @ B

Isbin:
Add edX, EDX
Lea edX, [edX + eax-30 h]
JMP @ B

Finalflag:
MoV eax, EDX
Pop ESI

@ Exit:
RET

Bin2dec endp
; //////////////////////////////////////// ///////////////////////////
Dec2bin proc pbin: DWORD, ndec: DWORD

; Argument check
MoV eax, dword ptr [pbin]
Test eax, eax
JZ @ exit

Push EDI
MoV EDI, eax
MoV edX, dword ptr [ndec]
MoV ECx, 21 h; 33
MoV Al, 30 h; '0'
ClD

@ Locate:
Dec ECx
JZ finalflag
SHL edX, 1
JNC @ locate

RCR edX, 1; Set CF to left most, restore bit
MoV ah, Cl; Total BITs
INC ECx

@@:
Dec ECx
JZ finalflag
SHL edX, 1
JNC iszero
INC al; is 1
Stosb; CAT in pbin
Dec al; reset as '0'
JMP @ B

Iszero:; is 0
Stosb;
JMP @ B

Finalflag:
Movzx eax, ah
Pop EDI

@ Exit:
RET

Dec2bin endp
; //////////////////////////////////////// ///////////////////////////
Dec2hex proc Phex: DWORD, ndec: DWORD

; Argument check
MoV eax, dword ptr [Phex]
Test eax, eax
JZ @ exit

MoV edX, eax
Push EBX
MoV EBX, dword ptr [ndec]
XOR ECx, ECx

MoV [edX + 8], Al; put terminator at correct length
MoV Cl, 7; length of hexstring-1

@@:
MoV eax, EBX; we're going to work on Al
And Al, 0fh; mask out high nibble

CMP Al, 10; cf
SBB Al, 69 H
Das

MoV [edX + ECx], Al; store the asciihex (Al) in the string
Shr ebx, 4; next nibble
Dec ECx; decrease counter (one byte less than Dec Cl
JNS @ B; eat them if there's any more

Pop EBX

@ Exit:
RET

Dec2hex endp
; //////////////////////////////////////// ///////////////////////////
Hex2dec proc Phex: DWORD

; Argument check
MoV eax, dword ptr [Phex]
Test eax, eax
JZ @ exit

Push ESI
MoV ESI, eax
XOR edX, EDX
XOR eax, eax

@ Next:
Lodsb
Test Al, Al
JZ @ final

CMP Al, '0'; '0 '~ '9': 48 ~ 57
JB @ next; less
CMP Al, '9'
Ja @ range_af; 'A '~ 'F': 97 ~ 122
Sub Al, '0'
JMP @ convert

@ Range_af:
Or Al, 20 h; 'A' 2 'A' offset value is 32, viz 10, 0000
CMP Al, 'a'; 'A '~ 'F'
JB @ next
CMP Al, 'F'
Ja @ next
Sub Al, 'A'
Add Al, 10

@ Convert:
SHL edX, 4
Add edX, eax
JMP @ next

@ Final:
MoV eax, EDX
Pop ESI

@ Exit:
RET

Hex2dec endp
; //////////////////////////////////////// ///////////////////////////
; Borland uses the method
Mstrlen proc psrc: DWORD

; Argument check
MoV eax, dword ptr [psrc]
Test eax, eax
JZ @ exit

MoV edX, EDI
MoV EDI, eax

MoV ECx, 0 ffffffffh
XOR Al, Al
Repne scasb
MoV eax, ECx
Not eax
Dec eax

MoV EDI, EDX

@ Exit: Ret

Mstrlen endp
; //////////////////////////////////////// ///////////////////////////
; Borland uses the method
Mstrend proc psrc: DWORD

; Argument check
MoV eax, dword ptr [psrc]
Test eax, eax
JZ @ exit

MoV edX, EDI
MoV EDI, eax

MoV ECx, 0 ffffffffh
XOR Al, Al
Repne scasb
Lea eax, [edi-1]

MoV EDI, EDX

@ Exit: Ret

Mstrend endp
; //////////////////////////////////////// ///////////////////////////
; Borland uses the method
Mstrcopy proc uses esi edi pdest: DWORD, psrc: DWORD

; Count length of psrc
MoV ESI, dword ptr [pdest]
MoV EDI, dword ptr [psrc]

; Argument check
Test ESI, ESI
JZ @ exit
Test EDI, EDI
JZ @ exit

MoV edX, EDI
MoV ECx, 0 ffffffffh
XOR Al, Al
Repne scasb
Not ECx

; Moving data
MoV EDI, ESI
MoV ESI, EDX
MoV edX, ECx
SHR ECx, 2
Rep movsd

MoV ECx, EDX
And ECx, 3
Rep movsb

@ Exit:
RET

Mstrcopy endp
; //////////////////////////////////////// ///////////////////////////
Mstrcat proc uses esi edi pdest: DWORD, psrc: DWORD

; Goto end of pdest
MoV EDI, dword ptr [pdest]
MoV ESI, dword ptr [psrc]

; Argument check
Test ESI, ESI
JZ @ exit
Test EDI, EDI
JZ @ exit

ClD
MoV ECx, 0 ffffffffh
XOR Al, Al
Repne scasb
Dec EDI
MoV edX, EDI; backup the pdest

; Count length of psrc
MoV EDI, ESI
MoV ECx, 0 ffffffffh
XOR Al, Al
Repne scasb
Not ECx

; CAT
MoV EDI, EDX
MoV edX, ECx
SHR ECx, 2
Rep movsd

MoV ECx, EDX
And ECx, 3
Rep movsb

@ Exit:
RET

Mstrcat endp
; //////////////////////////////////////// ///////////////////////////
Mstrpos proc uses esi edi ebx pszdest: DWORD, pszsrc: DWORD

; Check the destination string whether empty
MoV eax, dword ptr [pszdest]
Or eax, eax
JZ @ Error

; Check the source string whether empty
MoV edX, dword ptr [pszsrc]
Or edX, EDX
JZ @ Error

; Search length of pszsrc
MoV EBX, eax
MoV EDI, EDX; Search pszsrc
XOR Al, Al
MoV ECx, 0 ffffffffh
Repne scasb
Not ECx
Dec ECx; length of pszsrc
JZ @ error; If has not any chars then go to final

; Check the length of destination string whether less than length of source string
MoV ESI, ECx; length of pszsrc
MoV EDI, EBX; pszdest
MoV ECx, 0 ffffffffh
Repne scasb
Not ECx
Sub ECx, ESI
Jbe @ Error

MoV EDI, EBX; pszdest
Lea EBX, [esi-1]; length of pszsrc-1

; Locate the first char of pszsrc in prcdest
@ Next:
MoV ESI, EDX; pszsrc
Lodsb
Repne scasb
JNE @ error; not found

; Compare the two strings
MoV eax, ECx; length of pszsrc
Push EDI
MoV ECx, EBX; length of pszsrc-1
Repe cmpsb

Pop EDI
MoV ECx, eax; length of pszsrc
JNE @ next; not found then to find next first string

; Return address of pszsrc in pszdest
Lea eax, [edi-1]; found, luck! :)

; Return position of pszsrc in pszdest
; MoV eax, EDI
; Sub eax, dword ptr [pszdest]

JMP @ exit

@ Error:
XOR eax, eax; not found any chars

@ Exit:
RET

Mstrpos endp
; //////////////////////////////////////// ///////////////////////////
Mstrupper proc pszsrc: DWORD

; Argument check
MoV eax, dword ptr [pszsrc]
Test eax, eax
JZ @ exit

Push ESI
MoV ESI, eax
MoV edX, ESI
XOR eax, eax

@ Next:
Lodsb
Or Al, Al
Je @ final

CMP Al, 'A'
JB @ next

CMP Al, 'Z'
Ja @ next

Sub Al, 20 h; set the six bits 10, 0000 as zero

MoV [esi-1], Al
JMP @ next

@ Final:
Xchg eax, EDX
Pop ESI

@ Exit:
RET

Mstrupper endp
; //////////////////////////////////////// ///////////////////////////
Mstrlower proc pszsrc: DWORD

; Argument check
MoV eax, dword ptr [pszsrc]
Test eax, eax
JZ @ exit

Push ESI
MoV ESI, eax
MoV edX, ESI
XOR eax, eax

@ Next:
Lodsb
Or Al, Al
Je @ final

CMP Al, 'A'
JB @ next

CMP Al, 'Z'
Ja @ next

Or Al, 20 h; set the six bits as 1
MoV [esi-1], Al
JMP @ next

@ Final:
Xchg eax, EDX
Pop ESI

@ Exit:
RET

Mstrlower endp
; //////////////////////////////////////// ///////////////////////////
Mmemzero proc pdest: DWORD, ucount: DWORD

; Argument check
MoV eax, dword ptr [pdest]
Test eax, eax
JZ @ exit

Push EDI
MoV EDI, eax
ClD
MoV ECx, dword ptr [ucount]
MoV edX, ECx

SHR ECx, 2; Div 4
XOR eax, eax
Rep stosd

MoV ECx, EDX
And ECx, 3; mod of 4
Rep stosb

Pop EDI

@ Exit:
RET

Mmemzero endp
; //////////////////////////////////////// ///////////////////////////
Mmemcopy proc uses esi edi pdst: DWORD, psrc: DWORD, nlength: DWORD

MoV ESI, dword ptr [psrc]
MoV EDI, dword ptr [pdst]

; Argument check
Test ESI, ESI
JZ @ exit
Test EDI, EDI
JZ @ exit

ClD
MoV ECx, dword ptr [nlength]
MoV edX, ECx

SHR ECx, 2
Rep movsd

MoV ECx, EDX
And ECx, 3
Rep movsb

@ Exit:
RET

Mmemcopy endp
; //////////////////////////////////////// ///////////////////////////
Mmemfill proc pdst: DWORD, cfill: DWORD, nlength: DWORD

; Argument check
MoV eax, dword ptr [pdst]; buffer address
Test eax, eax
JZ @ exit

MoV edX, eax
MoV eax, dword ptr [cfill]; fill chars
MoV ECx, dword ptr [nlength]; Byte Length
SHR ECx, 5; divide by 32
CMP ECx, 0
JZ rmndr

;------------
; Unroll by 8
;------------
@@:
MoV [edX], eax; put fill chars at address in edX
MoV [edX + 4], eax
MoV [edX + 8], eax
MoV [edX + 12], eax
MoV [edX + 16], eax
MoV [edX + 20], eax
MoV [edX + 24], eax
MoV [edX + 28], eax
Add edX, 32
Dec ECx
Jnz @ B

Rmndr:
And dword ptr [nlength], 31; get Remainder
Cmp dword ptr [nlength], 0
Je @ exit
MoV ECx, dword ptr [nlength]
SHR ECx, 2; divide by 4

@@:
MoV [edX], eax
Add edX, 4
Dec ECx
Jnz @ B

@ Exit:
RET

Mmemfill endp
; //////////////////////////////////////// ///////////////////////////
; Start
End dllentry

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.