Floattohex code de brunews retrenscris en asm par moi

Description

convertis nombre reel en simple et double hexadecimal
petit utilitaire pour dévelopeur trés utile avec masm32
parceque l'on ne peut pas mettre un float literalement
sur la pile.
ex: push 1.5f ;erreur
push 3FC00000h ;ok
fld dword ptr [esp]
add esp,4
ainsi il n'est pas obligatoire de definir une variable
! ATTENTION Ce programme utilise des instructions SSE2
1 - il faut savoir si votre proc comporte le jeux d'instruction SSE2
2 - pour compiler il faut remplacer le ml.exe d'origine avec celui
qui ce trouve dans le ZIP,ou dans SP5 qui ce trouve à l'addresse
http://mirror.href.com/thestarman/asm/masm.htm.
Sinon il ne reconnaitra pas la syntaxe xmm.
Ce code peut être un BON didactel pour SSE2

Source / Exemple :


.686p
.model flat,stdcall
option casemap:none
.xmm

include windows.inc
include kernel32.inc
include user32.inc
includelib kernel32
includelib user32.lib

AppDlgProc			PROTO :HWND,:UINT,:WPARAM,:LPARAM 
ProcEdFloat    			PROTO :HWND,:UINT,:WPARAM,:LPARAM 
FloatsToHex                     PROTO

.CONST

IDD_APP                         EQU	101
IDI_APP                         EQU	102
ID_TXT                          EQU	1001
ID_DBL                          EQU	1002
ID_FLT 				EQU     1003

.DATA
;ALIGN 16
BNDIXPZERO  			dq      4024000000000000h
BNUNDIXIEM 			dq	3FB999999999999Ah
BNMINUSDBL 			dq	8000000000000000h

 	 hdbl 			dd 0
         hflt			dd 0
         htxt			dd 0
	 oldFloatProc		dd 0

.CODE
START:	INVOKE			DialogBoxParam,400000h,101,0,addr AppDlgProc,400000h
        INVOKE			ExitProcess,0

;################################################## AppDlgProc ####################################################################

AppDlgProc			PROC   hdlg:HWND,msg:UINT,wParam:WPARAM,lParam:LPARAM 
	mov    			eax,msg
        cmp    			eax,WM_INITDIALOG
        je     			WMINIT
        cmp    			eax,WM_COMMAND
        je     			WMCMD
        jmp			DLG
WMINIT:	INVOKE    		LoadIcon,400000h,IDI_APP
    	INVOKE			SetClassLong,hdlg,GCL_HICON,eax
        INVOKE			GetDlgItem,hdlg,ID_TXT
        mov			htxt,eax
        INVOKE			SendMessage,eax, EM_LIMITTEXT, 15, 0
        INVOKE			SetWindowLong,htxt, GWL_WNDPROC,ADDR ProcEdFloat
        mov			oldFloatProc,eax
	INVOKE			GetDlgItem,hdlg,ID_DBL
      	mov			hdbl,eax		
      	INVOKE			GetDlgItem,hdlg,ID_FLT
      	mov			hflt,eax
        jmp                     DLG
WMCMD:  mov			eax,wParam
        and                     eax,0FFFFh
        cmp                     eax,IDOK
        jnz                     @F
        INVOKE			FloatsToHex
        jmp                     DLG
@@:     cmp			eax,IDCANCEL
        jnz                     DLG
        INVOKE			EndDialog,hdlg,0        
DLG:  	sub 			eax,eax
      	ret
AppDlgProc			ENDP

;##################################################### ProcEdFloat #############################################################

ProcEdFloat    			PROC hwnd:HWND,msg:UINT,wParam:WPARAM,lParam:LPARAM 
	LOCAL 			buf[16]:BYTE
        mov			eax,msg
        cmp			eax,WM_CHAR
        je			WMCHAR
        jmp			DEFRET
WMCHAR:	mov                     eax,wParam
        cmp			eax,VK_BACK
        je                      DEFRET
        cmp                     eax,57
        jg                      NOGOOD
        cmp                     eax,48
        jge                     DEFRET
        cmp                     eax,44
        jl                      NOGOOD
        cmp                     eax,46
        jg                      NOGOOD
        cmp                     eax,45
        jge                     GVERIF
        mov                     wParam,46
GVERIF:	INVOKE			GetWindowText,hwnd,addr buf, 16
        lea			edi,buf
        mov			ecx,eax
        mov                     eax,wParam
        repne                   scasb
        jne                     DEFRET
NOGOOD: sub			eax,eax
        ret
DEFRET: INVOKE			CallWindowProc,oldFloatProc, hwnd, msg, wParam, lParam
        ret
ProcEdFloat			ENDP

;########################################################### FloatsToHex #########################################################

FloatsToHex 			PROC
	sub       		esp, 32
    	mov       		ecx, 16
    	mov       		eax, esp
    	INVOKE 			GetWindowText,htxt,eax,16
    	mov       		ecx, esp
    	call      		bnatod
	DB      0F2h,0Fh,11h,44h,24h,14h  ;movsd     qword ptr[esp+20],XMM0
        cvtsd2ss		XMM2,XMM0
    	mov       		edx, esp
    	mov       		ecx, dword ptr[esp+24]
    	call      		bndwordtox
    	mov       		edx, eax
    	mov       		ecx, dword ptr[esp+20]
    	call      		bndwordtox
    	push      		esp
    	push      		hdbl
    	call      		SetWindowText
    	movss     		dword ptr[esp+20], XMM2
    	mov       		edx, esp
    	mov       		ecx, dword ptr[esp+20]
    	call      		bndwordtox
    	push      		esp
    	push      		hflt
    	call      	 	SetWindowText
    	add       		esp, 32
        sub                     eax,eax
        ret
FloatsToHex		ENDP

;############################################################ bndwordtox ####################################################
;ECX = dwnum, EDX = szdst
bndwordtox		PROC
	mov       		al, cl
    	add       		edx, 8
    	and       		cl, 15
    	shr       		al, 4
    	add       		cl, 48
    	add       		al, 48
    	cmp       		cl, 57
    	jbe       		short L1
    	add       		cl, 7
L1:	cmp       		al, 57
    	jbe       		short L2
    	add       		al, 7
L2:	mov       		byte ptr[edx-1], cl
    	mov       		byte ptr[edx-2], al
    	shr       		ecx, 8
    	mov       		byte ptr[edx], 0
    	mov       		al, cl
    	and       		cl, 15
    	shr       		al, 4
    	add       		cl, 48
    	add       		al, 48
    	cmp       		cl, 57
    	jbe       		short L3
    	add       		cl, 7
L3:	cmp       		al, 57
    	jbe       		short L4
    	add       		al, 7
L4:	mov       		byte ptr[edx-3], cl
    	mov       		byte ptr[edx-4], al
    	shr       		ecx, 8
    	mov       		al, cl
    	and       		cl, 15
    	shr       		al, 4
    	add       		cl, 48
    	add       		al, 48
    	cmp       		cl, 57
    	jbe       		short L5
    	add       		cl, 7
L5:	cmp       		al, 57
    	jbe       		short L6
    	add       		al, 7
L6:	mov       		byte ptr[edx-5], cl
    	mov       		byte ptr[edx-6], al
    	shr       		ecx, 8
    	mov       		byte ptr[edx], 0
    	mov       		al, cl
    	and       		cl, 15
    	shr       		al, 4
    	add       		cl, 48
    	add       		al, 48
    	cmp       		cl, 57
    	jbe       		short L7
    	add       		cl, 7
L7:	cmp       		al, 57
    	jbe       		short L8
    	add       		al, 7
L8:	mov       		byte ptr[edx-7], cl
    	mov       		byte ptr[edx-8], al
    	mov       		eax, edx
    	ret       
bndwordtox			ENDP

;####################################################### bnatod ###########################################################
;ECX = psz
bnatod				PROC
    	push        		ebx
    	xorps       		XMM0, XMM0
   	DB 0F2h,0Fh,10h,0Dh 	;movsd   XMM1,
        DD OFFSET BNDIXPZERO    ;qword ptr BNDIXPZERO
	xor         		ebx, ebx
    	xor         		edx, edx
    	xor         		eax, eax
    	movapd      		XMM3, XMM0
    	cmp         		byte ptr[ecx], 45
    	jne         		short outZERO
    	add         		ecx, 1
    	add         		ebx, 1
outZERO:mov         		al, [ecx]
    	cmp         		al, 48
    	jne         		short goINT
    	add         		ecx, 1
    	jmp         		short outZERO
goINT:	cmp         		al, 46
    	je          		short goDCML
    	sub         		al, 48
    	js          		short goSIGN
    	cmp         		al, 9
    	ja          		short goSIGN
    	cvtsi2sd    		XMM2, eax
    	mulsd       		XMM0, XMM1
    	add         		ecx, 1
    	addsd       		XMM0, XMM2
    	mov         		al, [ecx]
    	jmp         		short goINT
goDCML:	add         		ecx, 1
    	mov         		al, [ecx]
inDCML:	sub         		al, 48
    	js          		short dcmlIF
    	cmp         		al, 9
    	ja          		short dcmlIF
    	cvtsi2sd    		XMM2, eax
    	mulsd       		XMM0, XMM1
    	add         		ecx, 1
    	add         		edx, 1
    	addsd       		XMM0, XMM2
    	mov         		al, [ecx]
    	jmp         		short inDCML
dcmlIF:	test        		edx, edx
    	je          		short goSIGN
   	DB 0F2h,0Fh,10h,15h     ;MOVSD   XMM2,
        DD OFFSET BNUNDIXIEM 	;qword ptr BNUNDIXIEM
dcmlADD:sub         		edx, 1
	mulsd       		XMM0, XMM2
    	jne         		short dcmlADD
goSIGN:	comisd      		XMM0, XMM3
    	je          		short atodEXIT
    	test        		ebx, ebx
    	je          		short atodEXIT
	DB 0F2h,0Fh,10h,0Dh	;movsd  XMM1,
	DD OFFSET BNMINUSDBL 	;qword ptr BNMINUSDBL
	subsd       		XMM1, XMM0
    	movapd      		XMM0, XMM1
atodEXIT:
    	pop         		ebx
        sub			eax,eax
    	ret         
bnatod				ENDP

END   				START

Conclusion :


Ce code est de Brunews moi je ne comprend rien
que ce soit pour la routine de convertion
ou que ce soit des instructions SSE2.
J'ai fait que retrenscrire un code pour VC en code pour MASM32.

Codes Sources

A voir également

Vous n'êtes pas encore membre ?

inscrivez-vous, c'est gratuit et ça prend moins d'une minute !

Les membres obtiennent plus de réponses que les utilisateurs anonymes.

Le fait d'être membre vous permet d'avoir un suivi détaillé de vos demandes et codes sources.

Le fait d'être membre vous permet d'avoir des options supplémentaires.