my getline is better than yours

R

Rosario

do you like the "GetLine_m" function i have written today?
How many errors do you see?
i have try only the option for to load the all [text] file in a string
not the option to load a line "\n" ended so it should be full of
errors
[but because i have the "len" i can load with that function the binary
files too]


#define P printf


int main(int c, char** a)
{ char *s;
uns len;
int i, n;
FILE_m *f;
/* ....open file */
if(c<=1) return 0;
f=fopen_m( a[1], "rb");
if(f==0) {P("Errore apertura\n"); R 0;}
len=0; s=0;
n=GetLine_m(&s, &len, f); // per avere in s tutto il file
if(n==1) P("Tutto ok il file è = %s\n", s);
else if(n==2) P("Errore di parametro\n");
P("len=%u n=%i\n ", len, n);
free_m(s); // alla fine s deve essere liberato
fclose_m(f);
return 0;
}

/* int __stdcall GetLine_m( char** s, uns* len, FILE* f)
/* trash ecx, edx
/* Assegna a "s" un indirizzo a una stringa contenente una
/* linea di "f", (o tutto il file puntato da f se len==0)
/* ritorna un codice di errore e assegna (se len!=0) ad len
/* la lunghezza di tale stringa
/* usare come in
/* char *s;
/* uns len;
/* int i;
/* FILE *f;
/* ....open file
/* ----------------------
/* len=0; s=0;
/* n=GetLine_m(&s, &len, f); // per avere in s tutto il file
/* if(n==1) // tutto ok il file è in s e nessun errore
/* else if(n==2) // errore di parametro etc
/* free(s); // alla fine s deve essere liberato
/* ----------------------
/* len=12345678; s=0; n=GetLine_m(&s, &len, f); ... free(s);
/* ----------------------
/* s=0; n=GetLine_m(&s, &len, f); ... free(s);
/* ----------------------
/* ritorna 0 se tutto ok e EOF non raggiunto CF==0
/* 1 Eof raggiunto e nessun errore trovato CF==0
/* 2 errore parametro CF==1
/* 3 errore memoria CF==1
/* 4 errore lettura file CF==1
/* 5 errore file troppo grande per la memoria CF==1
/*
/* 0k, 4j, 8i, 12b, 16Ra, 20P_s, 24P_len, 28P_f
GetLine_m:
< b, i, j, k
a=^20; k=^28; a==0#.e2; k==0#.e2;
i=0; b=*a; j=2; // era j=64
b#.1; a=j; a+=8; _malloc_m<(a);
a==0#.e3; b=a; a=^20; *a=b; #.1;
..e2: a=2; #.e; / errori di parametro
..e3: a=3; #.e; / errori di memoria
..e4: a=4; #.e; / errori di overflow indice
..e5: a=5; / errori di lettura file
..e: stc; ##.f / below the loop
..1: j&i!#.2 | j+=i; jc .e4; a=j; a+=8; jc .e4;
| _realloc_m<(b, a);
| a#.2b | r=^24; r==0#.2a; *r=i;
..2a: | #.e3;
..2b: | b=a; a= ^20; *a=b;
..2: _fgetc_m<(k); a==-1!#.3| r=^24; B[b+i]=0; r==0#.2d| *r=i;
..2d: | D [k+12]& 16 #.e5 // ferror(k)?
| k=1; #.8; // EOF
..3: [b+i]=al; ++i; jz .e4; r=^24; r==0#.4; D*r==0#.1; i>=*r#.5;
..4: al!=10#.1; #.6;
..5: r=^24; B[b+i]=0; r==0#.e4; *r=i; ##.e4;
..6: r=^24; B[b+i]=0; r==0#.7; *r=i;
..7: k=0;
..8: a=i; a+=4; _realloc_m<(b, a); a==0#.e4;
b=a; a=^20; *a=b;
a=k; clc;
..f:
b, i, j, k
ret 12

GetLine_m:
push ebx
push esi
push edi
push ebp
mov eax, [ esp + 20 ]
mov ebp, [ esp + 28 ]
cmp eax, 0
je .e2
cmp ebp, 0
je .e2
mov esi, 0 ;/ era j=64
mov ebx, [eax]
mov edi, 2
cmp ebx, 0
jne .1
mov eax, edi
add eax, 8
push eax
call _malloc_m
add esp, 4
cmp eax, 0
je .e3
mov ebx, eax
mov eax, [ esp + 20 ]
mov [eax], ebx
jmp short .1
..e2: ; errori di parametro
mov eax, 2
jmp short .e
..e3: ; errori di memoria
mov eax, 3
jmp short .e
..e4: ; errori di overflow indice
mov eax, 4
jmp short .e
..e5: ; errori di lettura file
mov eax, 5
..e:
stc
jmp .f
..1:
test edi, esi
jz .2
add edi, esi
jc .e4
mov eax, edi
add eax, 8
jc .e4

push eax
push ebx
call _realloc_m
add esp, 8

cmp eax, 0
jne .2b
mov edx, [ esp + 24 ]
cmp edx, 0
je .2a
mov [edx], esi
..2a:

jmp short .e3
..2b:

mov ebx, eax
mov eax, [ esp + 20 ]
mov [eax], ebx
..2:
push ebp
call _fgetc_m
add esp, 4
cmp eax, -1
jne .3
mov edx, [ esp + 24 ]
mov byte[ebx+esi], 0
cmp edx, 0
je .2d
mov [edx], esi
..2d: ;/ ferror(k)?

test dword [ebp+12], 16
jnz .e5
;/ EOF
mov ebp, 1
jmp short .8
..3:
mov [ebx+esi], al
inc esi
jz .e4
mov edx, [ esp + 24 ]
cmp edx, 0
je .4
cmp dword[edx], 0
je .1
cmp esi, [edx]
jae .5
..4:
cmp al, 10
jne .1
jmp short .6
..5:
mov edx, [ esp + 24 ]
mov byte[ebx+esi], 0
cmp edx, 0
je .e4
mov [edx], esi
jmp .e4
..6:
mov edx, [ esp + 24 ]
mov byte[ebx+esi], 0
cmp edx, 0
je .7
mov [edx], esi
..7:
mov ebp, 0
..8:
mov eax, esi
add eax, 4
push eax
push ebx
call _realloc_m
add esp, 8
cmp eax, 0
je .e4
mov ebx, eax
mov eax, [ esp + 20 ]
mov [eax], ebx
mov eax, ebp
clc
..f:
pop ebp
pop edi
pop esi
pop ebx
ret 12
 
M

Martin Ambuhl

Rosario said:
do you like the "GetLine_m" function i have written today?
How many errors do you see?
i have try only the option for to load the all [text] file in a string
not the option to load a line "\n" ended so it should be full of
errors
[but because i have the "len" i can load with that function the binary
files too]


#define P printf
^^^^^^^^^^^^^^^^
Oh shit, he's back.
int main(int c, char** a)
{ char *s;
uns len;
^^^
undefined type
int i, n;
FILE_m *f;
^^^^^^
undefined type

[remaineder of unindented C-like-but-not-quite-C code and large amounts
of nothing-like-C code snipped]
 
R

Rod Pemberton

Rosario said:
do you like the "GetLine_m" function i have written today?
How many errors do you see?
i have try only the option for to load the all [text] file in a string
not the option to load a line "\n" ended so it should be full of
errors
[but because i have the "len" i can load with that function the binary
files too]

Follow ups to this post set to a.l.a.
#define P printf


int main(int c, char** a)
{ char *s;
uns len;
int i, n;
FILE_m *f;
/* ....open file */
if(c<=1) return 0;
f=fopen_m( a[1], "rb");
if(f==0) {P("Errore apertura\n"); R 0;}
len=0; s=0;
n=GetLine_m(&s, &len, f); // per avere in s tutto il file
if(n==1) P("Tutto ok il file è = %s\n", s);
else if(n==2) P("Errore di parametro\n");
P("len=%u n=%i\n ", len, n);
free_m(s); // alla fine s deve essere liberato
fclose_m(f);
return 0;
}

I don't like your C coding style.

#define P printf

int main(int c,char **a)
{
char *s;
uns len;
int i, n;
FILE_m *f;
/* ....open file */
if(c<=1)
return(0);
f=fopen_m(a[1],"rb");
if(f==0)
{
P("Errore apertura\n");
R(0);
}
len=0;
s=0;
n=GetLine_m(&s,&len,f); // per avere in s tutto il file
if(n==1)
{
P("Tutto ok il file è = %s\n",s);
}
else
{
if(n==2)
P("Errore di parametro\n");
}
P("len=%u n=%i\n ",len,n);
free_m(s); // alla fine s deve essere liberato
fclose_m(f);
return(0);
}

Obviously, there is much missing, like:

#define R return
#define uns unsigned int
#define FILE_m ?????
#define fopen_m ?????
#define GetLine_m ?????
#define free_m ?????
#define fclose_m ?????
various includes

Should one assume that the _m functions are custom or just redefined ANSI C
functions?

I'd rather see main defined as:
int main(int argc,char **argv)

So, that I know what a[1] is without having to track down the location where
a is defined.

s is a pointer to char, but you didn't allocate space in the C source.
Based on "a+=8; _malloc_m<(a);" in the cryptic code section below, I suspect
that you may have allocated space for s. However, you still set s to zero,
instead of NULL, before the only place where it might be allocated.

My personal preference is to see arguments written as such, "return(0);",
not "return 0;".
/* int __stdcall GetLine_m( char** s, uns* len, FILE* f)
/* trash ecx, edx
/* Assegna a "s" un indirizzo a una stringa contenente una
/* linea di "f", (o tutto il file puntato da f se len==0)
/* ritorna un codice di errore e assegna (se len!=0) ad len
/* la lunghezza di tale stringa
/* usare come in
/* char *s;
/* uns len;
/* int i;
/* FILE *f;
/* ....open file
/* ----------------------
/* len=0; s=0;
/* n=GetLine_m(&s, &len, f); // per avere in s tutto il file
/* if(n==1) // tutto ok il file è in s e nessun errore
/* else if(n==2) // errore di parametro etc
/* free(s); // alla fine s deve essere liberato
/* ----------------------
/* len=12345678; s=0; n=GetLine_m(&s, &len, f); ... free(s);
/* ----------------------
/* s=0; n=GetLine_m(&s, &len, f); ... free(s);
/* ----------------------
/* ritorna 0 se tutto ok e EOF non raggiunto CF==0
/* 1 Eof raggiunto e nessun errore trovato CF==0
/* 2 errore parametro CF==1
/* 3 errore memoria CF==1
/* 4 errore lettura file CF==1
/* 5 errore file troppo grande per la memoria CF==1
/*
/* 0k, 4j, 8i, 12b, 16Ra, 20P_s, 24P_len, 28P_f
GetLine_m:
< b, i, j, k
a=^20; k=^28; a==0#.e2; k==0#.e2;
i=0; b=*a; j=2; // era j=64
b#.1; a=j; a+=8; _malloc_m<(a);
a==0#.e3; b=a; a=^20; *a=b; #.1;
.e2: a=2; #.e; / errori di parametro
.e3: a=3; #.e; / errori di memoria
.e4: a=4; #.e; / errori di overflow indice
.e5: a=5; / errori di lettura file
.e: stc; ##.f / below the loop
.1: j&i!#.2 | j+=i; jc .e4; a=j; a+=8; jc .e4;
| _realloc_m<(b, a);
| a#.2b | r=^24; r==0#.2a; *r=i;
.2a: | #.e3;
.2b: | b=a; a= ^20; *a=b;
.2: _fgetc_m<(k); a==-1!#.3| r=^24; B[b+i]=0; r==0#.2d| *r=i;
.2d: | D [k+12]& 16 #.e5 // ferror(k)?
| k=1; #.8; // EOF
.3: [b+i]=al; ++i; jz .e4; r=^24; r==0#.4; D*r==0#.1; i>=*r#.5;
.4: al!=10#.1; #.6;
.5: r=^24; B[b+i]=0; r==0#.e4; *r=i; ##.e4;
.6: r=^24; B[b+i]=0; r==0#.7; *r=i;
.7: k=0;
.8: a=i; a+=4; _realloc_m<(b, a); a==0#.e4;
b=a; a=^20; *a=b;
a=k; clc;
.f:
b, i, j, k
ret 12

I don't understand this cryptic code section. You got assembly "clc",
assembly labels ".4", C operators "a+=8", C-ish stuff "_fgetc_m<(k)",
comments with both a single and double forward slash, and a bunch of junk
with # and ##. About the only existing language that could that all to work
together is FORTH or perhaps extensive C pre-processor abuse...
GetLine_m:
push ebx
push esi
push edi
push ebp
mov eax, [ esp + 20 ]
mov ebp, [ esp + 28 ]
cmp eax, 0
je .e2
cmp ebp, 0
je .e2
mov esi, 0 ;/ era j=64
mov ebx, [eax]
mov edi, 2
cmp ebx, 0
jne .1
mov eax, edi
add eax, 8
push eax
call _malloc_m
add esp, 4
cmp eax, 0
je .e3
mov ebx, eax
mov eax, [ esp + 20 ]
mov [eax], ebx
jmp short .1
.e2: ; errori di parametro
mov eax, 2
jmp short .e
.e3: ; errori di memoria
mov eax, 3
jmp short .e
.e4: ; errori di overflow indice
mov eax, 4
jmp short .e
.e5: ; errori di lettura file
mov eax, 5
.e:
stc
jmp .f
.1:
test edi, esi
jz .2
add edi, esi
jc .e4
mov eax, edi
add eax, 8
jc .e4

push eax
push ebx
call _realloc_m
add esp, 8

cmp eax, 0
jne .2b
mov edx, [ esp + 24 ]
cmp edx, 0
je .2a
mov [edx], esi
.2a:

jmp short .e3
.2b:

mov ebx, eax
mov eax, [ esp + 20 ]
mov [eax], ebx
.2:
push ebp
call _fgetc_m
add esp, 4
cmp eax, -1
jne .3
mov edx, [ esp + 24 ]
mov byte[ebx+esi], 0
cmp edx, 0
je .2d
mov [edx], esi
.2d: ;/ ferror(k)?

test dword [ebp+12], 16
jnz .e5
;/ EOF
mov ebp, 1
jmp short .8
.3:
mov [ebx+esi], al
inc esi
jz .e4
mov edx, [ esp + 24 ]
cmp edx, 0
je .4
cmp dword[edx], 0
je .1
cmp esi, [edx]
jae .5
.4:
cmp al, 10
jne .1
jmp short .6
.5:
mov edx, [ esp + 24 ]
mov byte[ebx+esi], 0
cmp edx, 0
je .e4
mov [edx], esi
jmp .e4
.6:
mov edx, [ esp + 24 ]
mov byte[ebx+esi], 0
cmp edx, 0
je .7
mov [edx], esi
.7:
mov ebp, 0
.8:
mov eax, esi
add eax, 4
push eax
push ebx
call _realloc_m
add esp, 8
cmp eax, 0
je .e4
mov ebx, eax
mov eax, [ esp + 20 ]
mov [eax], ebx
mov eax, ebp
clc
.f:
pop ebp
pop edi
pop esi
pop ebx
ret 12

This I understand as x86 assembly. It might actually compile with FASM.
do you like the "GetLine_m" function i have written today?

1) C code: No.
2) cryptic pseudo-FORTH-FASM-C-mixup: No.
3) x86 assembly: Yes.

Why do you post code nobody can use? Neither a.l.a. or c.l.c. is likely to
use this ever...


Rod Pemberton
 
R

Rosario

In data Mon, 12 Nov 2007 22:04:02 +0100, Rosario scrisse:
#define P printf


int main(int c, char** a)
{ char *s;
uns len;
int i, n;
FILE_m *f;
/* ....open file */
if(c<=1) return 0;
f=fopen_m( a[1], "rb");
if(f==0) {P("Errore apertura\n"); R 0;}
len=0; s=0;
n=GetLine_m(&s, &len, f); // per avere in s tutto il file
if(n==1) P("Tutto ok il file è = %s\n", s);
else if(n==2) P("Errore di parametro\n");
P("len=%u n=%i\n ", len, n);
free_m(s); // alla fine s deve essere liberato
fclose_m(f);
return 0;
}

/* int __stdcall GetLine_m( char** s, uns* len, FILE* f)
/* trash ecx, edx
/* Assegna a "s" un indirizzo a una stringa contenente una
/* linea di "f", (o tutto il file puntato da f se len==0)
/* ritorna un codice di errore e assegna (se len!=0) ad len
/* la lunghezza di tale stringa
/* usare come in
/* char *s;
/* uns len;
/* int i;
/* FILE *f;
/* ....open file
/* ----------------------
/* len=0; s=0;
/* n=GetLine_m(&s, &len, f); // per avere in s tutto il file
/* if(n==1) // tutto ok il file è in s e nessun errore
/* else if(n==2) // errore di parametro etc
/* free(s); // alla fine s deve essere liberato
/* ----------------------
/* len=12345678; s=0; n=GetLine_m(&s, &len, f); ... free(s);
/* ----------------------
/* s=0; n=GetLine_m(&s, &len, f); ... free(s);
/* ----------------------
/* ritorna 0 se tutto ok e EOF non raggiunto CF==0
/* 1 Eof raggiunto e nessun errore trovato CF==0
/* 2 errore parametro CF==1
/* 3 errore memoria CF==1
/* 4 errore lettura file CF==1
/* 5 errore file troppo grande per la memoria CF==1
/*
/* 0k, 4j, 8i, 12b, 16Ra, 20P_s, 24P_len, 28P_f
GetLine_m:
< b, i, j, k
a=^20; k=^28; a==0#.e2; k==0#.e2;
i=0; b=*a; j=2; // era j=64
b#.1; a=j; a+=8; _malloc_m<(a);
a==0#.e3; b=a; a=^20; *a=b; #.1;
.e2: a=2; #.e; / errori di parametro
.e3: a=3; #.e; / errori di memoria
.e4: a=4; #.e; / errori di overflow indice
.e5: a=5; / errori di lettura file
.e: stc; ##.f / below the loop
.1: j&i!#.2 | j+=i; jc .e4; a=j; a+=8; jc .e4;
| _realloc_m<(b, a);
| a#.2b | r=^24; r==0#.2a; *r=i;
.2a: | #.e3;
.2b: | b=a; a= ^20; *a=b;
.2: _fgetc_m<(k); a==-1!#.3| r=^24; B[b+i]=0; r==0#.2d| *r=i;
.2d: | D [k+12]& 16 #.e5 // ferror(k)?
| k=1; #.8; // EOF
.3: [b+i]=al; ++i; jz .e4; r=^24; r==0#.4; D*r==0#.1; i>=*r#.5;
.4: al!=10#.1; #.6;
.5: r=^24; B[b+i]=0; r==0#.e4; *r=i; ##.e4;
.6: r=^24; B[b+i]=0; r==0#.7; *r=i;
.7: k=0;
.8: a=i; a+=4; _realloc_m<(b, a); a==0#.e4;
b=a; a=^20; *a=b;
a=k; clc;
.f:
b, i, j, k
ret 12

an error: here should be something like:

..8: a=i; a+=4; _realloc_m<(b, a); a==0#.9;
b=a; a=^20; *a=b;
..9: a=k; clc;
..f:
b, i, j, k
ret 12
 
R

Rosario

In data Mon, 12 Nov 2007 20:04:07 -0500, Rod Pemberton scrisse:
Rosario said:
do you like the "GetLine_m" function i have written today?
How many errors do you see?
i have try only the option for to load the all [text] file in a string
not the option to load a line "\n" ended so it should be full of
errors
[but because i have the "len" i can load with that function the binary
files too]

Follow ups to this post set to a.l.a.
#define P printf


int main(int c, char** a)
{ char *s;
uns len;
int i, n;
FILE_m *f;
/* ....open file */
if(c<=1) return 0;
f=fopen_m( a[1], "rb");
if(f==0) {P("Errore apertura\n"); R 0;}
len=0; s=0;
n=GetLine_m(&s, &len, f); // per avere in s tutto il file
if(n==1) P("Tutto ok il file è = %s\n", s);
else if(n==2) P("Errore di parametro\n");
P("len=%u n=%i\n ", len, n);
free_m(s); // alla fine s deve essere liberato
fclose_m(f);
return 0;
}

I don't like your C coding style.

#define P printf

int main(int c,char **a)
{
char *s;
uns len;
int i, n;
FILE_m *f;
/* ....open file */
if(c<=1)
return(0);
f=fopen_m(a[1],"rb");
if(f==0)
{
P("Errore apertura\n");
R(0);
}
len=0;
s=0;
n=GetLine_m(&s,&len,f); // per avere in s tutto il file
if(n==1)
{
P("Tutto ok il file è = %s\n",s);
}
else
{
if(n==2)
P("Errore di parametro\n");
}
P("len=%u n=%i\n ",len,n);
free_m(s); // alla fine s deve essere liberato
fclose_m(f);
return(0);
}

Obviously, there is much missing, like:

#define R return
#define uns unsigned int
#define FILE_m ?????
#define fopen_m ?????
#define GetLine_m ?????
#define free_m ?????
#define fclose_m ?????

#define FILE_m FILE
#define fopen_m fopen
#define free_m free
#define fclose_m fclose

int __stdcall GetLine_m( char** s, uns* len, FILE_m* f);
various includes

Should one assume that the _m functions are custom or just redefined ANSI C
functions?

yes they would be an implementation of C standard functions
I'd rather see main defined as:
int main(int argc,char **argv)

So, that I know what a[1] is without having to track down the location where
a is defined.

all variables and arguments are in the start of routine: so all we
know where we can find them
s is a pointer to char, but you didn't allocate space in the C source.
Based on "a+=8; _malloc_m<(a);" in the cryptic code section below, I suspect
that you may have allocated space for s. However, you still set s to zero,
instead of NULL, before the only place where it might be allocated.

there is some different if i write "s=NULL;" or "s=0;"?
in GetLine_m(&s, &len, f) the first time "s" has to be "0"
(or has to point memory returned from malloc)
My personal preference is to see arguments written as such, "return(0);",
not "return 0;".

this can be good
/* int __stdcall GetLine_m( char** s, uns* len, FILE* f)
/* trash ecx, edx
/* Assegna a "s" un indirizzo a una stringa contenente una
/* linea di "f", (o tutto il file puntato da f se len==0)
/* ritorna un codice di errore e assegna (se len!=0) ad len
/* la lunghezza di tale stringa
/* usare come in
/* char *s;
/* uns len;
/* int i;
/* FILE *f;
/* ....open file
/* ----------------------
/* len=0; s=0;
/* n=GetLine_m(&s, &len, f); // per avere in s tutto il file
/* if(n==1) // tutto ok il file è in s e nessun errore
/* else if(n==2) // errore di parametro etc
/* free(s); // alla fine s deve essere liberato
/* ----------------------
/* len=12345678; s=0; n=GetLine_m(&s, &len, f); ... free(s);
/* ----------------------
/* s=0; n=GetLine_m(&s, &len, f); ... free(s);
/* ----------------------
/* ritorna 0 se tutto ok e EOF non raggiunto CF==0
/* 1 Eof raggiunto e nessun errore trovato CF==0
/* 2 errore parametro CF==1
/* 3 errore memoria CF==1
/* 4 errore lettura file CF==1
/* 5 errore file troppo grande per la memoria CF==1
/*
/* 0k, 4j, 8i, 12b, 16Ra, 20P_s, 24P_len, 28P_f
GetLine_m:
< b, i, j, k
a=^20; k=^28; a==0#.e2; k==0#.e2;
i=0; b=*a; j=2; // era j=64
b#.1; a=j; a+=8; _malloc_m<(a);
a==0#.e3; b=a; a=^20; *a=b; #.1;
.e2: a=2; #.e; / errori di parametro
.e3: a=3; #.e; / errori di memoria
.e4: a=4; #.e; / errori di overflow indice
.e5: a=5; / errori di lettura file
.e: stc; ##.f / below the loop
.1: j&i!#.2 | j+=i; jc .e4; a=j; a+=8; jc .e4;
| _realloc_m<(b, a);
| a#.2b | r=^24; r==0#.2a; *r=i;
.2a: | #.e3;
.2b: | b=a; a= ^20; *a=b;
.2: _fgetc_m<(k); a==-1!#.3| r=^24; B[b+i]=0; r==0#.2d| *r=i;
.2d: | D [k+12]& 16 #.e5 // ferror(k)?
| k=1; #.8; // EOF
.3: [b+i]=al; ++i; jz .e4; r=^24; r==0#.4; D*r==0#.1; i>=*r#.5;
.4: al!=10#.1; #.6;
.5: r=^24; B[b+i]=0; r==0#.e4; *r=i; ##.e4;
.6: r=^24; B[b+i]=0; r==0#.7; *r=i;
.7: k=0;
.8: a=i; a+=4; _realloc_m<(b, a); a==0#.e4;
b=a; a=^20; *a=b;
a=k; clc;
.f:
b, i, j, k
ret 12

I don't understand this cryptic code section. You got assembly "clc",
assembly labels ".4", C operators "a+=8", C-ish stuff "_fgetc_m<(k)",
comments with both a single and double forward slash, and a bunch of junk
with # and ##. About the only existing language that could that all to work
together is FORTH or perhaps extensive C pre-processor abuse...
GetLine_m:
push ebx
push esi
push edi
push ebp
mov eax, [ esp + 20 ]
mov ebp, [ esp + 28 ]
cmp eax, 0
je .e2
cmp ebp, 0
je .e2
mov esi, 0 ;/ era j=64
mov ebx, [eax]
mov edi, 2
cmp ebx, 0
jne .1
mov eax, edi
add eax, 8
push eax
call _malloc_m
add esp, 4
cmp eax, 0
je .e3
mov ebx, eax
mov eax, [ esp + 20 ]
mov [eax], ebx
jmp short .1
.e2: ; errori di parametro
mov eax, 2
jmp short .e
.e3: ; errori di memoria
mov eax, 3
jmp short .e
.e4: ; errori di overflow indice
mov eax, 4
jmp short .e
.e5: ; errori di lettura file
mov eax, 5
.e:
stc
jmp .f
.1:
test edi, esi
jz .2
add edi, esi
jc .e4
mov eax, edi
add eax, 8
jc .e4

push eax
push ebx
call _realloc_m
add esp, 8

cmp eax, 0
jne .2b
mov edx, [ esp + 24 ]
cmp edx, 0
je .2a
mov [edx], esi
.2a:

jmp short .e3
.2b:

mov ebx, eax
mov eax, [ esp + 20 ]
mov [eax], ebx
.2:
push ebp
call _fgetc_m
add esp, 4
cmp eax, -1
jne .3
mov edx, [ esp + 24 ]
mov byte[ebx+esi], 0
cmp edx, 0
je .2d
mov [edx], esi
.2d: ;/ ferror(k)?

test dword [ebp+12], 16
jnz .e5
;/ EOF
mov ebp, 1
jmp short .8
.3:
mov [ebx+esi], al
inc esi
jz .e4
mov edx, [ esp + 24 ]
cmp edx, 0
je .4
cmp dword[edx], 0
je .1
cmp esi, [edx]
jae .5
.4:
cmp al, 10
jne .1
jmp short .6
.5:
mov edx, [ esp + 24 ]
mov byte[ebx+esi], 0
cmp edx, 0
je .e4
mov [edx], esi
jmp .e4
.6:
mov edx, [ esp + 24 ]
mov byte[ebx+esi], 0
cmp edx, 0
je .7
mov [edx], esi
.7:
mov ebp, 0
.8:
mov eax, esi
add eax, 4
push eax
push ebx
call _realloc_m
add esp, 8
cmp eax, 0
je .e4
mov ebx, eax
mov eax, [ esp + 20 ]
mov [eax], ebx
mov eax, ebp
clc
.f:
pop ebp
pop edi
pop esi
pop ebx
ret 12

This I understand as x86 assembly. It might actually compile with FASM.
do you like the "GetLine_m" function i have written today?

1) C code: No.
2) cryptic pseudo-FORTH-FASM-C-mixup: No.
3) x86 assembly: Yes.

so you can understand well the assembly paths of that routine?
(i read the code thru the "2"nd point above)
you are very good in assembly programming if yes (i can't read easily
the assembly version)
Why do you post code nobody can use? Neither a.l.a. or c.l.c. is likely to
use this ever...

1) because i think it is a good thing to see what a cpu does
2) and because i think 'my' language is better
than your assembly and C
3) Because this show that all theories that say goto and jumps are
not for structured programming are all wrong
 
P

Philip Potter

Rosario said:
3) Because this show that all theories that say goto and jumps are
not for structured programming are all wrong

That clears that one up then. I'm glad you came along.

(sarcasm-o-meter asplodes)

Incedentally, I have read a paper by Knuth called, I think, "Structured
programming with gotos" which was interesting reading; he gave examples
of constructs which are more efficiently implemented using gotos than
"traditional" structured constructs. This was in the mid-70s, though,
and I don't think such efficiency savings are worthwhile today given how
few programmers are at all familiar with gotos.
 
H

H. Peter Anvin

Philip said:
That clears that one up then. I'm glad you came along.

(sarcasm-o-meter asplodes)

Incedentally, I have read a paper by Knuth called, I think, "Structured
programming with gotos" which was interesting reading; he gave examples
of constructs which are more efficiently implemented using gotos than
"traditional" structured constructs. This was in the mid-70s, though,
and I don't think such efficiency savings are worthwhile today given how
few programmers are at all familiar with gotos.

The official style of the Linux kernel encourages the judicious use of
gotos for a programming construct sometimes known as a posit/admit
structure. Languages which have explicit support for it often implement
it with a construct called try/catch, but there is no such construct in C.

-hpa
 
R

Rosario

In data Tue, 13 Nov 2007 07:28:10 +0100, Rosario scrisse:

this is my last version but i have not debug well it...
(today i were out all the day)

/* 0k, 4j, 8i, 12b, 16Ra, 20P_s, 24P_len, 28P_f
GetLine_m:
< b, i, j, k
a=^20; k=^28; i=0; a==0#.e2; k==0#.e2;
b=*a; j=4; / era j=64
a=^24; D*a==0#.0; D*a==1#.e2; --D*a; / 0..(len-1),
..0: b#.1; a=j; a+=4; _malloc_m<(a);
/ thre are problems if i change one arg?
a==0#.e0; b=a; a=^20; *a=b; #.1;
..e0: a=3; stc; ##.c;
/ errori iniziale di memoria [non settati "s" e "len"]
..e2: a=2; stc; ##.c;
/ errori di parametro [non settati "s" e "len"]
..e3: k=3; ##.8; / errori di memoria
..e4: k=4; ##.8; / errori di overflow indice
..e5: k=5; ##.8; / errori di lettura file
..1: j&i!#.2 | j+=i; jc .e4; a=j; a+=4; jc .e4;
| _realloc_m<(b, a); a==0#.e3;
| b=a; a= ^20; *a=b;
..2: _fgetc_m<(k); a==-1!#.3| D [k+12]& 16 #.e5 // ferror(k)? (ext)
| k=1; #.8; // EOF (ext)
..3: i==-1#.e4; [b+i]=al; r=^24; ++i;
r==0#.4; D*r==0#.1; i>=*r#.e4; // ceck if i>max_index
..4: al!=10#.1;
..7: k=0; / here "\n" is found
..8: a=i; a+=4; _realloc_m<(b, a); a==0#.9;
b=a; a=^20; *a=b;
..9: a=k;
B[b+i]=0; r=^24; r==0#.a; *r=i; // save len
..a: a<=1#.b| stc; #.c; / error
..b: | clc; / ok
..c:
b, i, j, k
ret 12

; int __stdcall GetLine_m( char** s, uns* len, FILE* f)
; trash ecx, edx
; Assegna a "s" un indirizzo a una stringa contenente una
; linea di "f", (o tutto il file puntato da f se len==0)
; ritorna un codice di errore e assegna (se len!=0) ad len
; la lunghezza di tale stringa
; usare come in
; char *s;
; uns len;
; int i;
; FILE *f;
; ....open file
; ----------------------
; len=0; s=0;
; n=GetLine_m(&s, &len, f); // per avere in s tutto il file
; if(n==1) // tutto ok il file è in s e nessun errore
; else if(n==2) // errore di parametro etc
; free(s); // alla fine s deve essere liberato
; ----------------------
; len=12345678; s=0; n=GetLine_m(&s, &len, f); ... free(s);
; ----------------------
; s=0; n=GetLine_m(&s, &len, f); ... free(s);
; ----------------------
; ritorna 0 se tutto ok e EOF non raggiunto CF==0
; 1 Eof raggiunto e nessun errore trovato CF==0
; 2 errore parametro CF==1
; 3 errore memoria CF==1
; 4 errori di overflow indice CF==1
; 5 errori di lettura file CF==1
;
; 0k, 4j, 8i, 12b, 16Ra, 20P_s, 24P_len, 28P_f
GetLine_m:
push ebx
push esi
push edi
push ebp
mov eax, [ esp + 20 ]
mov ebp, [ esp + 28 ]
mov esi, 0
cmp eax, 0
je .e2
cmp ebp, 0
je .e2
mov ebx, [eax] ; era j=64
mov edi, 4
mov eax, [ esp + 24 ] ; 0..(len-1),
cmp dword[eax], 0
je .0
cmp dword[eax], 1
je .e2
dec dword[eax]
..0: ; thre are problems if i change one arg?
cmp ebx, 0
jne .1
mov eax, edi
add eax, 4
push eax
call _malloc_m
add esp, 4
cmp eax, 0
je .e0
mov ebx, eax
mov eax, [ esp + 20 ]
mov [eax], ebx
jmp short .1
..e0: ; errori iniziale di memoria [non settati "s" e "len"]
mov eax, 3
stc
jmp .c
..e2: ; errori di parametro [non settati "s" e "len"]
mov eax, 2
stc
jmp .c
..e3: ; errori di memoria
mov ebp, 3
jmp .8
..e4: ; errori di overflow indice
mov ebp, 4
jmp .8
..e5: ; errori di lettura file
mov ebp, 5
jmp .8
..1:
test edi, esi
jz .2
add edi, esi
jc .e4
mov eax, edi
add eax, 4
jc .e4

push eax
push ebx
call _realloc_m
add esp, 8
cmp eax, 0
je .e3

mov ebx, eax
mov eax, [ esp + 20 ]
mov [eax], ebx
..2: ;/ ferror(k)? (ext)
push ebp
call _fgetc_m
add esp, 4
cmp eax, -1
jne .3
test dword [ebp+12], 16
jnz .e5
;/ EOF (ext)
mov ebp, 1
jmp short .8
..3:
cmp esi, -1
je .e4
mov [ebx+esi], al
mov edx, [ esp + 24 ]
inc esi
cmp edx, 0
je .4 ;/ ceck if i>max_index
cmp dword[edx], 0
je .1
cmp esi, [edx]
jae .e4
..4:
cmp al, 10
jne .1
..7: ; here "\n" is found
mov ebp, 0
..8:
mov eax, esi
add eax, 4
push eax
push ebx
call _realloc_m
add esp, 8
cmp eax, 0
je .9
mov ebx, eax
mov eax, [ esp + 20 ]
mov [eax], ebx
..9:
mov eax, ebp
mov byte[ebx+esi], 0
mov edx, [ esp + 24 ]
cmp edx, 0
je .a
mov [edx], esi
..a: ; error
cmp eax, 1
jbe .b
stc
jmp short .c
..b: ; ok

clc
..c:
pop ebp
pop edi
pop esi
pop ebx
ret 12
 
R

Rod Pemberton

Rosario said:
3) Because this show that all theories that say goto and jumps are
not for structured programming are all wrong

Structured programming techniques can be applied to unstructured languages
such as Basic or assembly. That's what you've done. This isn't new. It's
many decades old.

Some languages, assembly or BASIC, only have unstructured programming flow
control like goto and jump. This doesn't mean that the gotos and jumps are
the _cause_ of the problem or can't be used in a structured manner. They
were the _effect_ of the language design and programmer choice. The purpose
of structured programming was to _force_ programmers to use structured
methods via language design instead of allowing them to _choose_
unstructured methods.

The use of unstructured programming techniques, where allowed, frequently
results in coding errors. One of the main problems was multiple entry and
exit points to code blocks or loops. This was the plague of line oriented
BASIC. It was difficult to reorganize code once it was on a specific line
or set of lines. So, the solution was to goto somewhere else and goto back
later. But, as the a number of these jump out, jump back patches were
applied, the code became unreadable and unmaintainable, i.e., "spaghetti"
code. And, as a result, difficult to locate and correct programming errors,
such as part of the loop or block not being executed, would arise.

C allows unstructured coding using a) goto's, b) the unstructured form of
the switch statement, c) multiple exit points via returns from procedures or
functions, and d) coroutines (multiple entry points) (See Duff's Device or
Simon Tatham's coroutines) e) multiple exit points from loops using break,
etc., but is usually considered to be a structured programming language like
Pascal because it has many semi-structured forms of control flow that can be
used as structured forms. Those who use multiple returns from procedures or
functions or use goto's to setup error routines within a procedure usually
haven't learned a programming technique called "fall through." The code
falls through to the next section or the programmer sets a flag to skip some
code before falling into another section or return. With structured
programming techniques, one never has to use a goto but may do so for
clarity, speed, size reduction etc. Multiple entry and exit points aren't
needed in new code, but can be very useful when patching two or more pieces
of existing code together.

Unfortunately, structured programming languages have problems too. One of
the most notable is the "cut 'n' paste" problem. It is much easier in a
structured programming language to copy a small section of code than write a
procedure to reuse it. Therefore, the code becomes bloated with large
amounts of small pieces of repeated code like identical if statements. This
tends to affect larger applications.


Rod Pemberton
 
T

Terence

Incidentally, I have read a paper by Knuth called, I think, "Structured
programming with gotos" which was interesting reading; he gave examples
of constructs which are more efficiently implemented using gotos than
"traditional" structured constructs.

Yes, as part of my M.Sc Thesis I was requested to comment on
"Structured Programming with Come-froms", a real paper (no, not mine)
showing an alternative to Knuth, which is not the joke it first seems.
It has been implemented.
This was in the mid-70s, though, and I don't think such efficiency savings are worthwhile today given how
few programmers are at all familiar with gotos.

Oh come ON Phillip!
I run a programming enterprise. Everybody I know knows and uses GOTOs.

One of the best structure tools for following code later (apart from
using another program on a tested production program, to rewrite and
resequence all labels from 1 up and all FORMAT statements from 900 up,
each by order of occurence), was to use
GOTO ( long, long series of cases ), index
wherever possible (we parse text a lot) and have nice self-explanatory
blocks of code for each case, each GOING somewhere onward afterwards.
The CASE structure cannot do this well without using internal GOTOs;
so if they have to have them there, why not use the GOTO to make all
the code more readable because it really can be structured with that
facility?.

The only decent addition to older Fortran, in my opinion, was the IF-
THEN- ELSE- ENDIF structure, which I have come to like for saving
labels, (but I wish it had some labelling system to avoid getting the
nesting wrong)..
 
R

Rosario

In data Tue, 13 Nov 2007 18:43:52 +0100, Rosario scrisse:
In data Tue, 13 Nov 2007 07:28:10 +0100, Rosario scrisse:

this is my last version but i have not debug well it...
(today i were out all the day)

you have to change only the name realloc_m -> realloc,
fgetc_m -> fgetc,
D [k+12]& 16 #.e5 ->
"push ebp; call _ferror; pop edx; cmp eax, 0; jnz .e5"
How many errors do you see?
but it will be full of errors :)

/* int __stdcall
/* GetLine_m( char** s, uns* len, uns max_len, FILE* f)
/* trash ecx, edx
/* NB: *deve essere* "s"=0 oppure "s" allocato
/* tramite malloc() o realloc().
/* Assegna a "s" un indirizzo a una stringa contenente una
/* linea di "f", o tutto il file puntato da "f" se "max_len"==0;
/* se "max_len!=0" la lunghezza massima della stringa è "max_len"
/* ritorna 0 se tutto ok e EOF non raggiunto CF==0
/* 1 EOF raggiunto e nessun errore trovato CF==0
/* 2 errore parametro CF==1
/* 3 errore memoria CF==1
/* 4 errori di overflow indice CF==1
/* 5 errori di lettura file CF==1
/* usare come in: char *s; uns len; int i; FILE *f;
/* ----------------------
/* s=0; n=GetLine_m(&s, &len, 0, f); ... free(s); // tutto il file
/* ----------------------
/* s=0; n=GetLine_m(&s, &len, -1, f); ... free(s); // la linea
/* ----------------------
/* 0k, 4j, 8i, 12b, 16Ra, 20P_s, 24P_len, 28P_max_len, 32P_f
GetLine_m:
< b, i, j, k
a=^20; k=^32; i=0; a==0#.e2; k==0#.e2;
b=*a; j=4; D[s+28]==1#.e2
..0: a=j; a+=4; _realloc_m<(b, a); a==0#.e0;
b=a; a=^20; *a=b; #.1;
..e0: b#.e3;
a=3; stc; ##.c; / errori iniziale di memoria
..e2: a=2; stc; ##.c; / errori di parametro
..e3: k=3; ##.8; / errori di memoria
..e4: k=4; ##.8; / errori di overflow indice
..e5: k=5; ##.8; / errori di lettura file
..1: j&i!#.2 | j+=i; jc .e4; a=j; a+=4; jc .e4;
| _realloc_m<(b, a); a==0#.e3;
| b=a; a=^20; *a=b;
..2: _fgetc_m<(k); a==-1!#.3| D [k+12]& 16 #.e5 / ferror(k)? (ext)
| k=1; #.8; / EOF (ext)
..3: i==-1#.e4; [b+i]=al; r=^28; ++i; r==0#.1; i>=r#.e4;
..4: al!=10#.1;
..7: k=0; / here "\n" is found
..8: a=i; a+=4; _realloc_m<(b, a); a==0#.9;
b=a; a=^20; *a=b;
..9: a=k;
B[b+i]=0; r=^24; r==0#.a; *r=i;
..a: a<=1#.b| stc; #.c; / error
..b: | clc; / ok
..c:
b, i, j, k
ret 16

GetLine_m:
push ebx
push esi
push edi
push ebp
mov eax, [ esp + 20 ]
mov ebp, [ esp + 32 ]
mov esi, 0
cmp eax, 0
je .e2
cmp ebp, 0
je .e2
mov ebx, [eax]
mov edi, 4
cmp dword[esp+28], 1
je .e2
..0:
mov eax, edi
add eax, 4
push eax
push ebx
call _realloc_m
add esp, 8
cmp eax, 0
je .e0
mov ebx, eax
mov eax, [ esp + 20 ]
mov [eax], ebx
jmp short .1
..e0:
cmp ebx, 0
jne .e3
mov eax, 3 ; errori iniziale di memoria
stc
jmp .c
..e2: ; errori di parametro
mov eax, 2
stc
jmp .c
..e3: ; errori di memoria
mov ebp, 3
jmp .8
..e4: ; errori di overflow indice
mov ebp, 4
jmp .8
..e5: ; errori di lettura file
mov ebp, 5
jmp .8
..1:
test edi, esi
jz .2
add edi, esi
jc .e4
mov eax, edi
add eax, 4
jc .e4

push eax
push ebx
call _realloc_m
add esp, 8
cmp eax, 0
je .e3

mov ebx, eax
mov eax, [ esp + 20 ]
mov [eax], ebx
..2: ; ferror(k)? (ext)
push ebp
call _fgetc_m
add esp, 4
cmp eax, -1
jne .3
test dword [ebp+12], 16
jnz .e5
; EOF (ext)
mov ebp, 1
jmp short .8
..3:
cmp esi, -1
je .e4
mov [ebx+esi], al
mov edx, [ esp + 28 ]
inc esi
cmp edx, 0
je .1
cmp esi, edx
jae .e4
..4:
cmp al, 10
jne .1
..7: ; here "\n" is found
mov ebp, 0
..8:
mov eax, esi
add eax, 4
push eax
push ebx
call _realloc_m
add esp, 8
cmp eax, 0
je .9
mov ebx, eax
mov eax, [ esp + 20 ]
mov [eax], ebx
..9:
mov eax, ebp
mov byte[ebx+esi], 0
mov edx, [ esp + 24 ]
cmp edx, 0
je .a
mov [edx], esi
..a: ; error
cmp eax, 1
jbe .b
stc
jmp short .c
..b: ; ok

clc
..c:
pop ebp
pop edi
pop esi
pop ebx
ret 16
 
P

Philip Potter

Terence said:
Oh come ON Phillip!
I run a programming enterprise. Everybody I know knows and uses GOTOs.

Oh come ON Terence! Your company does not seem representative, some have
explicit coding policies against gotos. (And please spell my name
correctly.)
One of the best structure tools for following code later (apart from
using another program on a tested production program, to rewrite and
resequence all labels from 1 up and all FORMAT statements from 900 up,
each by order of occurence), was to use
GOTO ( long, long series of cases ), index
wherever possible (we parse text a lot) and have nice self-explanatory
blocks of code for each case, each GOING somewhere onward afterwards.
The CASE structure cannot do this well without using internal GOTOs;
so if they have to have them there, why not use the GOTO to make all
the code more readable because it really can be structured with that
facility?.

I don't understand why you need a goto /after/ the end of each case. And
without that goto, I don't understand why you don't just use a switch.
 
R

Rosario

In data Wed, 14 Nov 2007 06:30:32 +0100, Rosario scrisse:

for this there is the need to know how the malloc and realloc have to
save the size of the buffer, and to do some calculation that dipend of
that

/* int __stdcall
/* GetLine_m( char** s, uns* len, uns size_max, FILE* f)
/* trash ecx, edx
/* NB: *deve essere* "s"=0 oppure "s" allocato
/* tramite malloc() o realloc().
/* Assegna a "s" un indirizzo a una stringa contenente una
/* linea di "f", o tutto il file puntato da "f" se "size_max"==0;
/* se "size_max"!=0 la lunghezza massima della stringa è "size_max-1"
/* ritorna 0 se tutto ok e EOF non raggiunto CF==0
/* 1 EOF raggiunto e nessun errore trovato CF==0
/* 2 errore parametro CF==1
/* 3 errore memoria CF==1
/* 4 errori di overflow indice CF==1
/* 5 errori di lettura file CF==1
/* usare come in: char *s; uns len; int i; FILE *f;
/* ----------------------
/* s=0; n=GetLine_m(&s, &len, 0, f); ... free(s); // tutto il file
/* ----------------------
/* s=0; n=GetLine_m(&s, &len, -1, f); ... free(s); // la linea
/* ----------------------
/* s=0; while(!(n=GetLine_m(&s, &len, -1, f))){...}
/* if( n==0|| n==1 ) { tutto ok}
/* free(s)
/* ----------------------
/* 0k, 4j, 8i, 12b, 16Ra, 20P_s, 24P_len, 28P_max_len, 32P_f
GetLine_m:
< b, i, j, k
a=^20; k=^32; i=0; a==0#.e2; k==0#.e2;
b=*a; j=4; D[s+28]==1#.e2; D[s+24]==0#.e2;
/ e' dword[ebx-4] la dimensione
/ in unità sizeof(header)==8bytes della memoria
/ nunits = (nbytes+8)/8 + (nbytes%8?1:0) + 1
/ 8*nunits = nbytes + 8 + 8*(nbytes%8?1:0) + 8
/ nbytes = 8*(nunits-1) - 8*(nbytes%8?1:0) - 8
/ nbytes <= 8*(nunits-1) -8
b==0#.0; a=[b-4]; a<=1#.ee; --a; a<<=3; jc .e0; a>8#.1;
..0: a=j; a+=4; _realloc_m<(b, a); a==0#.e0;
b=a; a=^20; *a=b; #.1;
..e0: b#.e3;
..ee: a=3; stc; ##.c; / errori iniziale di memoria
..e2: a=2; stc; ##.c; / errori di parametro
..e3: k=3; ##.8; / errori di memoria
..e4: k=4; ##.8; / errori di overflow indice
..e5: k=5; ##.8; / errori di lettura file
..1: j&i!#.2 | j+=i; jc .e4; a=j; a+=8; jc .e4;
| r=[b-4]; r<=1#.ee; --r; r<<=3; jc .e3; r>a#.2;
| _realloc_m<(b, a); a==0#.e3;
| b=a; a=^20; *a=b;
..2: _fgetc_m<(k); a==-1!#.3| D [k+12]& 16 #.e5 / ferror(k)? (ext)
| k=1; #.8; / EOF (ext)
/ i non va in overflow (prima di j)
..3: [b+i]=al; r=^28; ++i; r==0#.1; --r; i>=r#.e4; / 0..size-1
..4: al!=10#.1;
..7: k=0; / here "\n" is found
..8: a=i; r=[b-4]; r<=1#.ee; --r; r<<=3; jc .9; r-=i;
r<16#.9; / in teoria mai in realloc
a+=4; _realloc_m<(b, a); a==0#.9;
b=a; a=^20; *a=b;
..9: a=k;
B[b+i]=0; r=^24; r==0#.a; *r=i;
..a: a<=1#.b| stc; #.c; / error
..b: | clc; / ok
..c:
b, i, j, k
ret 16



; int __stdcall
; GetLine_m( char** s, uns* len, uns size_max, FILE* f)
; trash ecx, edx
; NB: *deve essere* "s"=0 oppure "s"
; allocato tramite malloc() o realloc().
; Assegna a "s" un indirizzo a una stringa contenente una
; linea di "f", o tutto il file puntato da "f" se "size_max"==0;
; se "size_max"!=0 la lunghezza massima della stringa è "size_max-1"
; ritorna 0 se tutto ok e EOF non raggiunto CF==0
; 1 EOF raggiunto e nessun errore trovato CF==0
; 2 errore parametro CF==1
; 3 errore memoria CF==1
; 4 errori di overflow indice CF==1
; 5 errori di lettura file CF==1
; usare come in: char *s; uns len; int i; FILE *f;
; ----------------------
; s=0; n=GetLine_m(&s, &len, 0, f); ... free(s); // tutto il file
; ----------------------
; s=0; n=GetLine_m(&s, &len, -1, f); ... free(s); // la linea
; ----------------------
; s=0; while(!(n=GetLine_m(&s, &len, -1, f))){...}
; if( n==0|| n==-1 ) { tutto ok}
; free(s)
; ----------------------
; 0k, 4j, 8i, 12b, 16Ra, 20P_s, 24P_len, 28P_max_len, 32P_f
GetLine_m:
push ebx
push esi
push edi
push ebp
mov eax, [ esp + 20 ]
mov ebp, [ esp + 32 ]
mov esi, 0
cmp eax, 0
je .e2
cmp ebp, 0
je .e2
mov ebx, [eax]
mov edi, 4
cmp dword[esp+28], 1
je .e2
cmp dword[esp+24], 0
je .e2
; e' D b[-4]la dimensione in unità
; sizeof(header)==8bytes della memoria
; nunits = (nbytes+8)/8 + (nbytes%8?1:0) + 1
; 8*nunits = nbytes + 8 + 8*(nbytes%8?1:0) + 8
; nbytes = 8*(nunits-1) - 8*(nbytes%8?1:0) - 8
; nbytes <= 8*(nunits-1) -8
cmp ebx, 0
je .0
mov eax, [ebx-4]
cmp eax, 1
jbe .ee
dec eax
shl eax, 3
jc .e0
cmp eax, 8
ja .1
..0:
mov eax, edi
add eax, 4
push eax
push ebx
call _realloc_m
add esp, 8
cmp eax, 0
je .e0
mov ebx, eax
mov eax, [ esp + 20 ]
mov [eax], ebx
jmp short .1
..e0:
cmp ebx, 0
jne .e3
..ee: ; errori iniziale di memoria
mov eax, 3
stc
jmp .c
..e2: ; errori di parametro
mov eax, 2
stc
jmp .c
..e3: ; errori di memoria
mov ebp, 3
jmp .8
..e4: ; errori di overflow indice
mov ebp, 4
jmp .8
..e5: ; errori di lettura file
mov ebp, 5
jmp .8
..1:
test edi, esi
jz .2
add edi, esi
jc .e4
mov eax, edi
add eax, 8
jc .e4

mov edx, [ebx-4]
cmp edx, 1
jbe .ee
dec edx
shl edx, 3
jc .e3
cmp edx, eax
ja .2

push eax
push ebx
call _realloc_m
add esp, 8
cmp eax, 0
je .e3

mov ebx, eax
mov eax, [ esp + 20 ]
mov [eax], ebx
..2: ; ferror(k)? (ext)
push ebp
call _fgetc_m
add esp, 4
cmp eax, -1
jne .3
test dword [ebp+12], 16
jnz .e5
; EOF (ext)
mov ebp, 1
jmp short .8
; i non va in overflow (prima di j)
..3: ; 0..size-1
mov [ebx+esi], al
mov edx, [ esp + 28 ]
inc esi
cmp edx, 0
je .1
dec edx
cmp esi, edx
jae .e4
..4:
cmp al, 10
jne .1
..7: ; here "\n" is found
mov ebp, 0
..8:
mov eax, esi
mov edx, [ebx-4]
cmp edx, 1
jbe .ee
dec edx
shl edx, 3
jc .9
sub edx, esi
cmp edx, 16
jb .9 ; in teoria mai in realloc
add eax, 4
push eax
push ebx
call _realloc_m
add esp, 8
cmp eax, 0
je .9
mov ebx, eax
mov eax, [ esp + 20 ]
mov [eax], ebx
..9:
mov eax, ebp
mov byte[ebx+esi], 0
mov edx, [ esp + 24 ]
cmp edx, 0
je .a
mov [edx], esi
..a: ; error
cmp eax, 1
jbe .b
stc
jmp short .c
..b: ; ok

clc
..c:
pop ebp
pop edi
pop esi
pop ebx
ret 16
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

No members online now.

Forum statistics

Threads
473,836
Messages
2,569,748
Members
45,545
Latest member
rapter____0

Latest Threads

Top