The MASM Forum Archive 2004 to 2012

Project Support Forums => GoAsm Assembler and Tools => Topic started by: donkey on November 12, 2008, 04:56:13 AM

Title: Snippets
Post by: donkey on November 12, 2008, 04:56:13 AM
I was looking through some of the volumes of code I have written and was thinking that it might be useful to create a thread where we can post snippets of code. This thread will be dedicated to small (<20 lines) peices of code that can be used to perform a task. Rather that pollute the thread with comments on a particular peice of code I would ask that you limit posting to actual code snippets.

Here's the first snippet...

Purpose: To convert a binary value (0-99) to a 2 digit ascii decimal number

Uses: I use this for counting through entries in INI files mostly (i.e. entry01,entry02...entry99)

Prereqs: Value to convert must be in the EAX register

aam
add eax,3030h
bswap eax
shr eax,16


So let's see your little peices of magic !!!
Title: Re: Snippets
Post by: beatrix on November 12, 2008, 07:50:57 PM
Nice idea  :bg I have a small recursive routine (11 lines) that I usually use.

Purpose : To increment a counter . example : I define MyCounter == 20h,20h,20h. If I increment it, MyCounter == 21h,20h,20h. Ones more, MyCounter == 22h,20h,20h etc... When MyCounter == 7Fh, 20h,20h, if we increment it ones more time, then MyCounter == 20h,21h,20h.

Uses : Very useful to make bruteforce. The counter is the key we look for. Each time we need to change the key, we call this small routine.

Prereqs : This routine is used in a fastcall convention. edx points at the origin of the counter we have to increment. In my example, edx == offset MyCounter.


RecursiveIncremente:
    mov bl, b [edx]
    cmp bl, 7Fh
    jge >
    inc byte ptr [edx]
    ret
    :
    mov b [edx],20h
    inc edx
    call RecursiveIncremente
    dec edx
    ret
Title: Re: Snippets
Post by: herge on November 13, 2008, 08:42:53 AM
 Hi donkey:

Here's a small refinement of your code.
DecEAx will set the carry if greater than
99. It makes the output more pure.


DecEAx proc
    push ebx
    mov ebx, 99   
    cmp ebx, eax
    pop ebx
    pushf ; Save Flags
    aam
    add eax, "00"
    bswap eax
    shr eax,16
    popf ; Get Flags
    ret
DecEAx endp



Regards herge.
Title: Re: Snippets
Post by: Kernel on January 21, 2009, 08:23:50 PM
Maybe somebody finds use for this one.
It's for converting a memory pattern into ascii bytes.
Or maybe somebody can get it even smaller ?
I thought the nibble-to-ascii stuff with the 'das' might be interesting..

Mem2Hex proc pData:PTR, lpOutput:PTR, nCount:DWORD

pushad
mov esi,pData
mov edi,lpOutput
mov ecx,nCount
byte2hex_:
lodsb
push eax
shr eax,4
call nibble2hex_
pop eax
call nibble2hex_
loop byte2hex_
xchg eax,ecx
stosb
popad
ret

nibble2hex_:
and al,0Fh
cmp al,0Ah
sbb al,69h
das
stosb
db 0C3h

Mem2Hex endp
Title: Re: Snippets
Post by: ASMexpert77 on February 06, 2009, 06:54:17 PM
Quote from: beatrix on November 12, 2008, 07:50:57 PM
Nice idea  :bg I have a small recursive routine (11 lines) that I usually use.

Purpose : To increment a counter . example : I define MyCounter == 20h,20h,20h. If I increment it, MyCounter == 21h,20h,20h. Ones more, MyCounter == 22h,20h,20h etc... When MyCounter == 7Fh, 20h,20h, if we increment it ones more time, then MyCounter == 20h,21h,20h.

Uses : Very useful to make bruteforce. The counter is the key we look for. Each time we need to change the key, we call this small routine.

Prereqs : This routine is used in a fastcall convention. edx points at the origin of the counter we have to increment. In my example, edx == offset MyCounter.


RecursiveIncremente:
    mov bl, b [edx]
    cmp bl, 7Fh
    jge >
    inc byte ptr [edx]
    ret
    :
    mov b [edx],20h
    inc edx
    call RecursiveIncremente
    dec edx
    ret


Small? That's not a small routine, looks like C++ to me :D

Here's a small one and FAST one:


R1: INC B[EDX]
    IF S {MOV B[EDX],20h
          INC EDX
          JMP R1}   

I guess this is the form for MASM:

R1: INC Byte ptr[EDX]
    JNS N1
    MOV Byte ptr[EDX],20h
    INC EDX
    JMP R1
N1:
Title: Re: Snippets
Post by: beatrix on February 07, 2009, 11:20:06 AM
Hi ASMExpert,

When you say your routine is smaller than mine, your are right :) but when you say it is faster than mine, your are wrong. I just tested the 2 routines on a counter of 4 bytes. Here are the results :

QuoteMy Routine -> Elapse time  = 375 (ms)
Your Routine -> Elapse time  = 453 (ms)

But, your code is very interesting and I am nearly sure we can do something faster than that :) Thanks ASMExpert.
Title: Re: Snippets
Post by: donkey on February 07, 2009, 06:22:55 PM
QuoteThis thread will be dedicated to small (<20 lines) peices of code that can be used to perform a task. Rather that pollute the thread with comments on a particular peice of code I would ask that you limit posting to actual code snippets.

Please try to stick with the spirit of the thread....