Prev: popen
Next: cpu type idea
From: rio on

Getline() function is born today, what do you say? not have debug it
GetLine_m(b, i, pf)
in b there is a pointer of a pointer of one array
i there is its len
pf is a pointer to file
return 1 error of parameter CF=1
2 error of memory
3 error memory is not of malloc
-1 EOF
;
; b i j
; u32 GetLine_m(u8** s, u32* len, u32* pf)
; (0k,4j,8i,12b,16ra,20P_s,24P_len,28P_pf)
GetLine_m:
push ebx
push esi
push edi
push ebp
mov eax, 1
mov ebx, [esp+20]
mov esi, [esp+24]
mov edi, [esp+28]
cmp ebx, 0
je .e1
cmp esi, 0
je .e1
cmp edi, 0
jne .1
..e1: ; err di parametro
stc
jmp .f
..e2: ; err di memoria
mov eax, 2
jmp short .e1
..e3: ; err il vettore non di malloc
mov eax, 3
jmp short .e1
..e4: ; incontrato EOF
clc
jmp .f
..1:
mov eax, dword[ebx]
cmp eax, 0
jne .2
cmp dword[esi], 0
jne .e1
push 128
call Malloc_m
cmp eax, 0
je .e2
mov dword[ebx], eax
mov ebp, 128
jmp short .3
..2:
push eax
call GetSizeFast
jc .e3
mov ebp, eax
..3:
xor edx, edx
mov ebx, [ebx]
mov byte[ebx], 0
jmp short .6
..4:
mov ecx, edx
shl ecx, 1
mov ebp, ecx
mov esi, eax
push edx
push ecx
push ebx
call Realloc_m
pop edx
cmp eax, 0
jne .5
dec edx
mov byte[ebx+edx], 0
mov esi, [esp+24]
mov [esi], edx
jmp short .e2
..5:
mov ebx, eax
mov ecx, [esp+20]
mov eax, esi
mov [ecx], ebx
..6: ; Fgetc preserve all but eax and CF
push edi
call Fgetc_m
cmp eax, -1
je .e4
mov [ebx+edx], al
inc edx
dec ebp
jz .4
cmp al, 10
jne .4
mov byte[ebx+edx], 0
mov esi, [esp+24]
mov [esi], edx
xor eax, eax
clc
..f:
pop ebp
pop edi
pop esi
pop ebx
ret 12


; ritorna il numero di char riservati al programma
; puntati dall'argomento ap che deve essere un
; puntatore ritornato da malloc-realloc
; 0ra, 4P_ap
GetSizeFast:
mov eax, [esp+4]
cmp eax, 0
je .e
cmp eax, [eax-4]
je .1
..e:
xor eax, eax
stc
jmp short .f
..ee:
xor eax, eax
clc
jmp short .f
..1:
mov eax, [eax-8]
test eax, 0xF0000000
jnz .e
shl eax, 4 ; 12+15=27
sub eax, 27
cmp eax, -11
jl .e ; 16-12=4 4-15=-11=16-27
cmp eax, 0
jl .ee
clc
..f:
ret 4


---------------------------------

/*
/* b i j
/* u32 GetLine_m(u8** s, u32* len, u32* pf)
/* (0k,4j,8i,12b,16ra,20P_s,24P_len,28P_pf)
GetLine_m:
<b,i,j,k
a=1|b=[s+20]|i=[s+24]|j=[s+28]
b==0#.e1|i==0#.e1|j#.1
..e1: stc| ##.f /* err di parametro
..e2: a=2| #.e1 /* err di memoria
..e3: a=3| #.e1 /* err il vettore non di malloc
..e4: clc| ##.f /* incontrato EOF
..1: a=D[b]
a#.2|D[i]#.e1|Malloc_m(128)|a!#.e2
D[b]=a|k=128| #.3
..2: GetSizeFast(a) | jc .e3 | k=a
..3: r^=r | b=[b] | B[b]=0 | #.6
..4: c=r|c<<=1|k=c|i=a
<r |Realloc_m(b, c)| >r
a==0!#.5|--r|B[b+r]=0|i=[s+24]| *i=r| #.e2
..5: b=a|c=[s+20]|a=i|[c]=b
..6: Fgetc_m(j)| a==-1#.e4 /* Fgetc preserve all but eax and CF
[b+r]=al| ++r| --k
jz .4| al!=10#.4
B[b+r]=0
i=[s+24]| *i=r|a^=a|clc
..f:
>b,i,j,k
ret 12

/* ritorna il numero di char riservati al programma
/* puntati dall'argomento ap che deve essere un
/* puntatore ritornato da malloc-realloc
/* 0ra, 4P_ap
GetSizeFast:
a=[s+4]|a==0#.e|a==[a-4]#.1
..e: a^=a |stc| #.f
..ee: a^=a |clc| #.f
..1: a=[a-8]|a&0xF0000000#.e
a<<=4 |a-=27 /* 12+15=27
a< -11?#.e|a<0?#.ee /* 16-12=4 4-15=-11=16-27
clc
..f:
ret 4






From: Wolfgang Kern on

"rio" posted:

> Getline() function is born today, what do you say? not have debug it

Haven't checked in detail if it may work at all, and beside that
I think you could save on many branches by reordering your code,
I marked a few lines (*).

Not sure for it if your array contains variable sized items.
__
wolfgang
_____________________
> GetLine_m(b, i, pf)
> in b there is a pointer of a pointer of one array
> i there is its len
> pf is a pointer to file
> return 1 error of parameter CF=1
> 2 error of memory
> 3 error memory is not of malloc
> -1 EOF
> ;
> ; b i j
> ; u32 GetLine_m(u8** s, u32* len, u32* pf)
> ; (0k,4j,8i,12b,16ra,20P_s,24P_len,28P_pf)
* you sure got a reason for using C-styled 'procedures'
* I'd had this three arguments in registers.
> GetLine_m:
> push ebx
> push esi
> push edi
> push ebp
> mov eax, 1
> mov ebx, [esp+20]
> mov esi, [esp+24]
> mov edi, [esp+28]
> cmp ebx, 0
* why not OR ebx,ebx ?
> je .e1
> cmp esi, 0
*
> je .e1
> cmp edi, 0
*
> jne .1
> .e1: ; err di parametro
> stc
> jmp .f
> .e2: ; err di memoria
> mov eax, 2
> jmp short .e1
> .e3: ; err il vettore non di malloc
> mov eax, 3
> jmp short .e1
> .e4: ; incontrato EOF
> clc
> jmp .f
> .1:
> mov eax, dword[ebx]
> cmp eax, 0
*
> jne .2
> cmp dword[esi], 0
> jne .e1
> push 128
> call Malloc_m
> cmp eax, 0
*
> je .e2
> mov dword[ebx], eax
> mov ebp, 128
> jmp short .3
> .2:
> push eax
> call GetSizeFast
> jc .e3
> mov ebp, eax
> .3:
> xor edx, edx
> mov ebx, [ebx]
> mov byte[ebx], 0
* MOV [ebx],DL ?
> jmp short .6
> .4:
> mov ecx, edx
> shl ecx, 1
> mov ebp, ecx
> mov esi, eax
> push edx
> push ecx
> push ebx
> call Realloc_m
> pop edx
> cmp eax, 0
> jne .5
> dec edx
> mov byte[ebx+edx], 0
* MOV [ebx+edx],AL ?
> mov esi, [esp+24]
> mov [esi], edx
> jmp short .e2
> .5:
> mov ebx, eax
> mov ecx, [esp+20]
> mov eax, esi
> mov [ecx], ebx
> .6: ; Fgetc preserve all but eax and CF
> push edi
> call Fgetc_m
> cmp eax, -1
> je .e4
> mov [ebx+edx], al
> inc edx
> dec ebp
> jz .4
> cmp al, 10
> jne .4
> mov byte[ebx+edx], 0
> mov esi, [esp+24]
> mov [esi], edx
> xor eax, eax
> clc
* ;XOR clears the Carry anyway
> .f:
> pop ebp
> pop edi
> pop esi
> pop ebx
> ret 12
>
>
> ; ritorna il numero di char riservati al programma
> ; puntati dall'argomento ap che deve essere un
> ; puntatore ritornato da malloc-realloc
> ; 0ra, 4P_ap
> GetSizeFast:
> mov eax, [esp+4]
> cmp eax, 0
*
> je .e
> cmp eax, [eax-4]
> je .1
> .e:
> xor eax, eax
> stc
> jmp short .f
> .ee:
> xor eax, eax
> clc
* ;XOR ...
> jmp short .f
> .1:
> mov eax, [eax-8]
> test eax, 0xF0000000
> jnz .e
> shl eax, 4 ; 12+15=27
> sub eax, 27
> cmp eax, -11
> jl .e ; 16-12=4 4-15=-11=16-27
> cmp eax, 0
> jl .ee
> clc
> .f:
> ret 4
>
>
> ---------------------------------
* [snipped the almost unreadable 'to A or not to bee' source] :)


From: rio on
"Wolfgang Kern" <nowhere(a)never.at> ha scritto nel messaggio
news:g5ai74$g3s$1(a)newsreader2.utanet.at...
>
> "rio" posted:
>
>> Getline() function is born today, what do you say? not have debug it
>
> Haven't checked in detail if it may work at all, and beside that
> I think you could save on many branches by reordering your code,
> I marked a few lines (*).

the branches (==jumps) have to be, because there are many cases.
i don't understand, how this can save from a branch:
cmp ebx, 0
* why not OR ebx,ebx ?
je .e1

why is it better "OR ebx,ebx|jz .e1" on
"cmp ebx, 0|je .e1"?



From: rio on
"Wolfgang Kern" <nowhere(a)never.at> ha scritto nel messaggio
news:g5ai74$g3s$1(a)newsreader2.utanet.at...
>
>> xor eax, eax
>> clc
> * ;XOR ...

Would you say that "xor eax, eax" clear the carry flag?
Saluti



From: Wolfgang Kern on

"rio" asked:

>>> Getline() function is born today, what do you say? not have debug it

>> Haven't checked in detail if it may work at all, and beside that
>> I think you could save on many branches by reordering your code,
>> I marked a few lines (*).

> the branches (==jumps) have to be, because there are many cases.
> i don't understand, how this can save from a branch:
> cmp ebx, 0
> * why not OR ebx,ebx ?
> je .e1

Sure, not all jumps can be avoided, but by using CMOVcc, SETcc,
ORing bits, ADC,0 and shift you can get rid of many spaghetti-
code and gain speed and often also size.

an not optimised example:

or ebx,ebx
setz ah
or al,ah
add al,al ;shift left 1
or esi,esi
setz ah
or al,ah
add al,al ;shif left 1
or edi,edi
setz ah
or al,ah
movzx eax,al ;eax = 0 if none of the three is zero 000 *
= 1 if edi=0 001 *
= 2 if esi=0 010 *
= 3 if edi=esi=0 011
= 4 if ebx=0 100 *
= 5 if ebx=edi=0 101
= 6 if ebx=esi=0 110
= 7 if all three are zero 111

If you only need the cases 0,1,2,4 then just look at the bits*.

and if you have (like me) error text 2**n fixsized and aligned
you can use this eax to direct point to an error message string:

;shl eax,... ;if more than 8 bytes
lea edx,[err_txt_array+eax*8] ;edx points to the error text yet


but if you just want to detect if any of the three is zero:

MOV eax,ebx
OR eax,esi
OR eax,edi
CMOVz eax,[err]
CMOVnz eax,[ok]

> why is it better "OR ebx,ebx|jz .e1" on
> "cmp ebx, 0|je .e1"?

It is shorter, two bytes 09 DB OR ebx,ebx
instead of three 83 FB 00 CMP ebx,+0
or six bytes 81 FB 00 00 00 00 CMP ebx,0
and it works identical if just exact zero is of interest.

__
wolfgang



 |  Next  |  Last
Pages: 1 2
Prev: popen
Next: cpu type idea