flat assembler
Message board for the users of flat assembler.

Index > Windows > HexEncode & HexDecode

Author
Thread Post new topic Reply to topic
jochenvnltn



Joined: 15 Jul 2011
Posts: 83
jochenvnltn
Hi everyone

I had only programed in Delphi before.
Now im learning FASM.

I needed HexEncode & HexDecode to use in FASM.
In Delphi it's just HexToString and StringToHex..
I found a proc in asm that does this, but it's not working in FASM.

CODE
-------
Code:
; Author: Jake Commander
; Copyright The GeneSys Development System

HexEncode proc uses edi esi ebx pBuff:dword,dwLen:dword,pOutBuff:dword
;---------------------------------------
   mov    ebx, dwLen
   mov    edi, pOutBuff
   test    ebx, ebx
   mov    esi, pBuff
   jz      @F
   .repeat
     movzx  eax, byte ptr [esi]
     mov    ecx, eax
     add    edi, 2
     shr    ecx, 4
     and    eax, 1111b
     and    ecx, 1111b
     cmp    eax, 10
     sbb    edx, edx
     adc    eax, 0
     lea    eax, [eax+edx*8+'7']
     cmp    ecx, 10
     sbb    edx, edx
     adc    ecx, 0
     shl    eax, 8
     lea    ecx, [ecx+edx*8+'7']
     or      eax, ecx
     inc    esi
     mov    [edi-2], ax
     dec    ebx
   .until ZERO?
@@: mov    eax, edi
   mov    byte ptr [edi], 0
   sub    eax, pOutBuff
   ret
;---------------------------------------
HexEncode endp

; Author: Jake Commander
; Copyright The GeneSys Development System

HexDecode proc uses esi edi ebx pHexStr:dword,pOutBuffer:dword
;---------------------------------------
   mov    esi, pHexStr
   mov    edi, pOutBuffer
   jmp    @1
@@: and    ebx, 0Fh
   add    eax, ebx
   mov    [edi], al
   inc    edi
@1: movzx  edx, byte ptr[esi]
   cmp    edx, 40h
   sbb    ebx, ebx
   sub    edx, 37h
   and    ebx, 7
   inc    esi
   add    ebx, edx
   js      @F
   mov    eax, ebx
   shl    eax, 4
   mov    [edi], al
   movzx  edx, byte ptr [esi]
   cmp    edx, 40h
   sbb    ebx, ebx
   sub    edx, 37h
   and    ebx, 7
   inc    esi
   add    ebx, edx
   jns    @B
@@: ret
;---------------------------------------
HexDecode endp
    


Can someone explain to me how to do this in FASM please ?
I realy want to learn FASM

thanks a bunch

edit by revolution: added code tags
Post 07 Jul 2012, 09:14
View user's profile Send private message MSN Messenger Reply with quote
bzdashek



Joined: 15 Feb 2012
Posts: 147
Location: Tolstokvashino, Russia
bzdashek
Converted to FASM syntax, now compiles, don't know if works.

Code:
proc HexEncode  uses edi esi ebx,pBuff:dword,dwLen:dword,pOutBuff:dword 
;--------------------------------------- 
   mov    ebx, [dwLen] 
   mov    edi, [pOutBuff] 
   test    ebx, ebx 
   mov    esi, [pBuff] 
   jz      @F 
   .repeat: 
     movzx  eax, byte[esi] 
     mov    ecx, eax 
     add    edi, 2 
     shr    ecx, 4 
     and    eax, 1111b 
     and    ecx, 1111b 
     cmp    eax, 10 
     sbb    edx, edx 
     adc    eax, 0 
     lea    eax, [eax+edx*8+'7'] 
     cmp    ecx, 10 
     sbb    edx, edx 
     adc    ecx, 0 
     shl    eax, 8 
     lea    ecx, [ecx+edx*8+'7'] 
     or      eax, ecx 
     inc    esi 
     mov    [edi-2], ax 
     dec    ebx 
     jnz    .repeat 
@@: mov    eax, edi 
   mov    byte[edi], 0 
   sub    eax, [pOutBuff] 
   ret 
;--------------------------------------- 
endp 

; Author: Jake Commander 
; Copyright The GeneSys Development System 

proc HexDecode uses esi edi ebx, pHexStr:dword,pOutBuffer:dword 
;--------------------------------------- 
   mov    esi, [pHexStr] 
   mov    edi, [pOutBuffer] 
   jmp    @1 
@@: and    ebx, 0Fh 
   add    eax, ebx 
   mov    [edi], al 
   inc    edi 
@1: movzx  edx, byte[esi] 
   cmp    edx, 40h 
   sbb    ebx, ebx 
   sub    edx, 37h 
   and    ebx, 7 
   inc    esi 
   add    ebx, edx 
   js      @F 
   mov    eax, ebx 
   shl    eax, 4 
   mov    [edi], al 
   movzx  edx, byte[esi] 
   cmp    edx, 40h 
   sbb    ebx, ebx 
   sub    edx, 37h 
   and    ebx, 7 
   inc    esi 
   add    ebx, edx 
   jns    @B 
@@: ret 
;--------------------------------------- 
endp
    
Post 07 Jul 2012, 09:44
View user's profile Send private message Reply with quote
jochenvnltn



Joined: 15 Jul 2011
Posts: 83
jochenvnltn
bzdashek wrote:
Converted to FASM syntax, now compiles, don't know if works.

Code:
proc HexEncode  uses edi esi ebx,pBuff:dword,dwLen:dword,pOutBuff:dword 
;--------------------------------------- 
   mov    ebx, [dwLen] 
   mov    edi, [pOutBuff] 
   test    ebx, ebx 
   mov    esi, [pBuff] 
   jz      @F 
   .repeat: 
     movzx  eax, byte[esi] 
     mov    ecx, eax 
     add    edi, 2 
     shr    ecx, 4 
     and    eax, 1111b 
     and    ecx, 1111b 
     cmp    eax, 10 
     sbb    edx, edx 
     adc    eax, 0 
     lea    eax, [eax+edx*8+'7'] 
     cmp    ecx, 10 
     sbb    edx, edx 
     adc    ecx, 0 
     shl    eax, 8 
     lea    ecx, [ecx+edx*8+'7'] 
     or      eax, ecx 
     inc    esi 
     mov    [edi-2], ax 
     dec    ebx 
     jnz    .repeat 
@@: mov    eax, edi 
   mov    byte[edi], 0 
   sub    eax, [pOutBuff] 
   ret 
;--------------------------------------- 
endp 

; Author: Jake Commander 
; Copyright The GeneSys Development System 

proc HexDecode uses esi edi ebx, pHexStr:dword,pOutBuffer:dword 
;--------------------------------------- 
   mov    esi, [pHexStr] 
   mov    edi, [pOutBuffer] 
   jmp    @1 
@@: and    ebx, 0Fh 
   add    eax, ebx 
   mov    [edi], al 
   inc    edi 
@1: movzx  edx, byte[esi] 
   cmp    edx, 40h 
   sbb    ebx, ebx 
   sub    edx, 37h 
   and    ebx, 7 
   inc    esi 
   add    ebx, edx 
   js      @F 
   mov    eax, ebx 
   shl    eax, 4 
   mov    [edi], al 
   movzx  edx, byte[esi] 
   cmp    edx, 40h 
   sbb    ebx, ebx 
   sub    edx, 37h 
   and    ebx, 7 
   inc    esi 
   add    ebx, edx 
   jns    @B 
@@: ret 
;--------------------------------------- 
endp
    


Dear friend bzdashek

Thank you a 1000000 times !!

Not only it compiles, but it also works like a charm !
I like this forum alot !
If im gona be helped like this all the time, im gona have a great journey learning the 'Fasm'

format PE GUI 4.0

include 'C:\Users\X-Ray Cat\Downloads\fasmw17002\INCLUDE\win32ax.inc'

section '.data' data readable writeable

HexBuff db '4A6F6368656E',0 ; This HexString is my name --> Jochen
szBuff db ?


section '.code' data readable writeable

main:
push szBuff
push HexBuff
call HexDecode
invoke MessageBox,0,szBuff, 'Fasm',MB_OK

invoke ExitProcess, 0

.end main

; Author: Jake Commander
; Copyright The GeneSys Development System

proc HexDecode uses esi edi ebx, pHexStr:dword,pOutBuffer:dword
;---------------------------------------
mov esi, [pHexStr]
mov edi, [pOutBuffer]
jmp @1
@@: and ebx, 0Fh
add eax, ebx
mov [edi], al
inc edi
@1: movzx edx, byte[esi]
cmp edx, 40h
sbb ebx, ebx
sub edx, 37h
and ebx, 7
inc esi
add ebx, edx
js @F
mov eax, ebx
shl eax, 4
mov [edi], al
movzx edx, byte[esi]
cmp edx, 40h
sbb ebx, ebx
sub edx, 37h
and ebx, 7
inc esi
add ebx, edx
jns @B
@@: ret
;---------------------------------------
endp
Post 07 Jul 2012, 15:37
View user's profile Send private message MSN Messenger Reply with quote
Enko



Joined: 03 Apr 2007
Posts: 678
Location: Mar del Plata
Enko
You can use sprintf from the c runtime.
http://www.cplusplus.com/reference/clibrary/cstdio/sprintf/

You will need to import it from mvcrt and use cinvoke insted of inovke.
Post 07 Jul 2012, 15:54
View user's profile Send private message Reply with quote
bzdashek



Joined: 15 Feb 2012
Posts: 147
Location: Tolstokvashino, Russia
bzdashek
You're welcome, comrade!

Also, instead of
Code:
push szBuff 
push HexBuff 
call HexDecode 
    


you can just write
Code:
stdcall HexDecode,HexBuff,szBuff
    


There's one think in your programme that needs to be corrected.
If "szBuff db ? " is buffer, then it would be better if you specify the size of it, for example, to statically define a buffer for 80 chars, you write:
Code:
szBuff db 80 dup (?)
    

or FASM way:
Code:
szBuff rb 80
    

where "rb" means that it's an array of bytes.

Take a look in FASM\EXAMPLES, there are lot's of interesting and helpful examples. Also, it is advised to take a look at FASM\FASM.PDF, many useful things can be found there.

This thread is great when you start learning asm:
http://board.flatassembler.net/topic.php?t=2527
Post 07 Jul 2012, 20:33
View user's profile Send private message Reply with quote
jochenvnltn



Joined: 15 Jul 2011
Posts: 83
jochenvnltn
Comrade Bzdashek,

Thank you alot for all the new information. It's stored in my memory.
Im sure gona read up on all the usefull links you gave me !

до свидания !

Jochen Very Happy
Post 07 Jul 2012, 21:54
View user's profile Send private message MSN Messenger Reply with quote
Display posts from previous:
Post new topic Reply to topic

Jump to:  


< Last Thread | Next Thread >
Forum Rules:
You cannot post new topics in this forum
You cannot reply to topics in this forum
You cannot edit your posts in this forum
You cannot delete your posts in this forum
You cannot vote in polls in this forum
You cannot attach files in this forum
You can download files in this forum


Copyright © 1999-2020, Tomasz Grysztar. Also on YouTube, Twitter.

Website powered by rwasa.