News:

MASM32 SDK Description, downloads and other helpful links
MASM32.com New Forum Link
masmforum WebSite

Snippets

Started by donkey, November 12, 2008, 04:56:13 AM

Previous topic - Next topic

donkey

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 !!!
"Ahhh, what an awful dream. Ones and zeroes everywhere...[shudder] and I thought I saw a two." -- Bender
"It was just a dream, Bender. There's no such thing as two". -- Fry
-- Futurama

Donkey's Stable

beatrix

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

herge

 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.
// Herge born  Brussels, Belgium May 22, 1907
// Died March 3, 1983
// Cartoonist of Tintin and Snowy

Kernel

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

ASMexpert77

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:

beatrix

#5
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.

donkey

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....
"Ahhh, what an awful dream. Ones and zeroes everywhere...[shudder] and I thought I saw a two." -- Bender
"It was just a dream, Bender. There's no such thing as two". -- Fry
-- Futurama

Donkey's Stable