NGs comp.lang.c,alt.lang.asm
"88888 Dihedral" <
dihedr...@googlemail.com> ha scritto nel messaggio
news:16923526.318.1324470963080.JavaMail.geo-discussion-forums@prez15...
>I wrote a binary search in assembly in 386 20 years ago.
>
> This is trivial. There were EAX, EBX, ECX, EDX and ESI, EDI which are 6
> 32 bit general registers and a stack that can be pushed and popped.
>
> I suggest you store the array starting address in ESI and use EDI as a
> general
> pointer in the heap.
>
> A program in C code within 20 lines should be coded in assembly easily for
> professionals in any 32 bit cpu easily.
>
> I'll warn any programmer that dare to use any lousy name of any variable
> to be a pointer witout ptr* or p* in the name under my supervision.
>
>
> Then I'll ask the programmer to convert the program into a specified assembly
> to prove the programmer is not stealing codes form others and faking to be
> his own works.
this is the implemention to the one in k&r2 so it is not my work 100%
i had one try on that in .asm. I use RosAsm with one my home
made asm macro language; i don't know if it is right...
i like all that, these simple little instruction that goes togheter...
fuck all hlls languages
; return -10 error, 0 not find, address of the found element in array
; would be like the C library one, with the only difference return -10 for error
; and use type without UB, and not admit index with (i32) indx<0
;u8* __stdcall
;binSearchR(u8* InOutPKey, u8* InBase,
; u32 nElmBase, u32 SizeElmBase, i32 cmp(u8* elm1, u8* elm2) );
; 0k, 4k, 8j, 12i, 16b, 20ra, 24InOutKey, 28InBase, 32P_nElmBase,
36P_SizeElmBase, 40P_cmp
binSearchR:
push ebx
push esi
push edi
push ebp
push ebp
;int3
cmp dword[esp+ 24], 0
je .e
cmp dword[esp+ 28], 0
je .e
cmp dword[esp+ 36], 0
jle .e
cmp dword[esp+ 40], 0
je .e
cmp dword[esp+ 32], 0
je .nf
jl .e
jmp short .1
.nf: mov eax, 0
clc
jmp short .z
.e: mov eax, -10
stc
jmp short .z
.1: mov esi, 0
mov edi, dword[esp+ 32]
dec edi
mov ebx, dword[esp+ 40]
.2: cmp esi, edi
ja .nf
mov ebp, esi
add ebp, edi
jc .e
shr ebp, 1 ; k=(i+j)/2
mov eax, dword[esp+ 36]
mul ebp
cmp edx, 0
jne .e
mov edx, dword[esp+ 24]
add eax, dword[esp+ 28]
mov dword[esp+ 0], eax
push edx
push eax
call ebx
add esp, 8
cmp eax, 0
jge .3
mov edi, ebp
sub edi, 1
jmp short .2
.3: cmp eax, 0
jle .4
mov esi, ebp
add esi, 1
jmp short .2
.4: mov eax, dword[esp+ 0]
clc
.z:
pop ebp
pop ebp
pop edi
pop esi
pop ebx
ret 20
this is the C program to experiment the above
#include <stdio.h>
#include <stdlib.h>
#define u32 unsigned
#define i32 int
#define u8 unsigned char
#define R return
#define P printf
u8* __stdcall
binSearchR(u8* InOutPKey, u8* InBase,
u32 nElmBase, u32 SizeElmBase, i32 cmp(u8* elm1, u8* elm2) );
i32 cmpu32(u8* elm1, u8* elm2)
{ if(*elm1>*elm2) R -1;
else if(*elm1<*elm2) R 1;
else R 0;
}
int main(void)
{u32 v[]={0, 2, 5, 7, 8, 8, 9, 13, 20}, val=13;
u8 *r;
r=binSearchR((u8*)&val, (u8*) v, 9, sizeof(u32), cmpu32);
if(r!=0) P("r=%p find=%u\n", (void*)r, *r);
R 0;
}
this is the origin like RosAsm .dll
----------------------------
PREPARSE MACRO2D
section DATA
hInstance dd 0
; 9
arrayInt dd 0,2,4,5,8,8,9,10, 11
export binSearchR
section TEXT
; return -10 error, 0 not find, address of the found element in array
; would be like the C one, with the only difference return -10 for error
;u8* __stdcall
;binSearchR(u8* InOutPKey, u8* InBase,
; u32 nElmBase, u32 SizeElmBase, i32 cmp(u8* elm1, u8* elm2) );
; 0k, 4k, 8j, 12i, 16b, 20ra, 24InOutKey, 28InBase, 32P_nElmBase,
36P_SizeElmBase, 40P_cmp
binSearchR:
<b,i,j,k,k
;int3
^24==0#.e|^28==0#.e |^36<=0?#.e
^40==0#.e|^32==0#.nf|<?#.e|#.1
.nf: a=0 |clc|#.z
.e: a=-10|stc|#.z
.1: i=0 |j=^32|--j|b=^40
.2: i>j#.nf
k=i |k+=j |jc .e |k>>=1 ; k=(i+j)/2
a=^36|mul k|r!=0#.e|r=^24|a+=^28|^0=a
b<(a,r)|a<0!?#.3|j=k|j-=1|#.2
.3: a>0!?#.4|i=k|i+=1|#.2
.4: a=^0|clc
.z:
>b,i,j,k,k
ret 20
Main:
mov eax D$esp+8
mov D$hInstance eax
mov eax 1
ret 12