Groups | Search | Server Info | Keyboard shortcuts | Login | Register [http] [https] [nntp] [nntps]


Groups > comp.programming > #641

Re: example4-25 of c++cookbook

From "io_x" <a@b.c.invalid>
Newsgroups comp.lang.c++, comp.lang.c, comp.programming, alt.lang.asm
References <5b15b612-b003-4b37-8859-38af722dd4f3@v7g2000prl.googlegroups.com> <4e1a92f9$0$44207$4fafbaef@reader1.news.tin.it> <4e1a9f1a$0$15668$4fafbaef@reader2.news.tin.it> <4e1bf1c5$0$15667$4fafbaef@reader2.news.tin.it>
Subject Re: example4-25 of c++cookbook
Date 2011-07-12 09:42 +0200
Message-ID <4e1bfa6d$0$15664$4fafbaef@reader2.news.tin.it> (permalink)
Organization TIN.IT (http://www.tin.it)

Cross-posted to 4 groups.

Show all headers | View raw


"io_x" <a@b.c.invalid> ha scritto nel messaggio
news:4e1bf1c5$0$15667$4fafbaef@reader2.news.tin.it...
>>> [comp.lang.c++,comp.lang.c,comp.programming,alt.lang.asm]
>>> "eric" <cneric12lin0@gmail.com> ha scritto nel messaggio
>>> news:5b15b612-b003-4b37-8859-38af722dd4f3@v7g2000prl.googlegroups.com...
>>> Dear advanced programers:
>>>
>>>>  In that book's chapter 4 Section 16: Wrapping Lines in a Text File
>>>>author desribe:
>>>>Problem
>>>>You want to "wrap" text at a spicific number of characters in a file.
>>>>For example, if
>>>>you want to wrap text at 72 characters, you would insert a new-line
>>>>character after
>>>>every 72 characters in the file.  If the file contains human-readable
>>>>text, you probably
>>>>want to avoid splitting words.
>>>>Solution
>>>>Write a function that uses input and output streams to read in
>>>>characters with
>>>>istream::get(char), do some bookkeeping, and wirte our characters with
>>>>ostream::
>>>>put(char).  Example 4-25 shows how to do this for text files that
>>>>contain human-
>>>>readable text without splitting words.
>>>>c++ cookbook 's example source code is here
>>>>http://examples.oreilly.com/9780596007614/
>>>
>>> very interesting problem...
>>>
>>> this is my solution in .asm [that splipt if long words> maxSizeLine];
>>> i tried some time one solution more difficult but
>>> afther many debugging time no good result;
>>>
>>> when i thought to this solution, using sub functions
>>> all goes well the first time of the run
>>> don't know why...
>>> so it could be full of errors and can make seg fault
>>> your pc if not worse.
>>>
>>> I wait for your C or C++ or whatherver other language
>>> solution: i think if the language not have one function
>>> like the RompiLinee() below; it is difficult for you
>>> to write such program.
>>>
>>
>> first bug:  i found one char(13) more in the end of lines...
>
> second bug: it seem that even space can generate one \n

  III bug:  i forget to push pop eax
Buona Gironata grazie

> some adjustament:
> use int as ^0 type for not to think on too much big numbers;
> use ferror for the file that can be stdout too, for find error.
>
> In the world out here, there is someone that can run this?

;Nasmw  -fobj  thisFile.asm
;bcc32  -v     ThisFile.obj

section _DATA use32 public class=DATA

global _main
extern _printf
extern _fputc
extern _fgetc
extern _fclose
extern _fopen
extern _atoi
extern _ferror
extern _isspace
extern __streams

          IIIthisProgIinFileIIoutFileIINIdilineeIn
db "> thisProg inFile [outFile] N.dilinee" , 13, 10, 0, 0

          IFindIoneIorImoreIerrorIn
db "Find one or more error" , 13, 10, 0, 0

nl db 13, 10, 0, 0

section _BSS use32 public class=BSS
buffer resd 41792

section _TEXT use32 public class=CODE

;[32+20]52ra, 56P1, 60P2
          align   4
_main:
          pushad
          sub     esp,  20
          mov     ecx,  dword[esp+  56]
          mov     eax,  dword[esp+  60]
          mov     dword[esp+  12],  0 ; se c=1  r=a[0] il nome del file
          cmp     eax,  0
          jne     .1
.usage:   push    IIIthisProgIinFileIIoutFileIINIdilineeIn
          call    _printf
          add     esp,  4
          jmp     .z
.e2:      push    esi
          call    _fclose
          add     esp,  4
.e1:      cmp     dword[esp+  12],  0
          je      .error
          push    ebp
          call    _fclose
          add     esp,  4
.error:   push    IFindIoneIorImoreIerrorIn
          call    _printf
          add     esp,  4
          jmp     .z
.1:       cmp     ecx,  3
          jb      .usage
          cmp     ecx,  4
          ja      .usage        ; only 3 or 4 argument
          mov     esi,  [eax+4]
          mov     edi,  [eax+8]
; __streams point to stdin, 24=sizeof(FILE)
          mov     ebp,  __streams
          add     ebp,  24      ; k point to stdout
          cmp     ecx,  4
          jne     .2
          mov     edi,  [eax+12]
          mov     ebp,  [eax+8]
          mov     eax,  esp
          mov     dword[esp],  "w"
          push    eax
          push    ebp
          call    _fopen
          add     esp,  8
          cmp     eax,  0
          je      .error
          mov     ebp,  eax
          mov     dword[esp+  12],  7 ; k point to a oper file handle
.2:       push    edi
          call    _atoi
          add     esp,  4
          cmp     edi,  10
          jle     .e1
          mov     edi,  eax
          mov     eax,  esp
          mov     dword[esp],  "r"
          push    eax
          push    esi
          call    _fopen
          add     esp,  8
          cmp     eax,  0
          je      .e1
          mov     esi,  eax
          push    edi
          push    esi
          push    ebp
          call    RompiLinee
          jc      .e2
          push    esi
          call    _fclose
          add     esp,  4
          cmp     eax,  0
          jne     .e1
          push    ebp
          call    _ferror
          add     esp,  4
          cmp     eax,  0
          jne     .e1
          cmp     dword[esp+  12],  0
          je      .z
          push    ebp
          call    _fclose
          add     esp,  4
          cmp     eax,  0
          jne     .error
.z:
          add     esp,  20
          popad
          xor     eax,  eax
          ret

;32Ra,  36P_fileOut, 40P_fileIn, 44P_linesz + 2048
;     2084         2088        2092
          align   4
RompiLinee:
          pushad
          sub     esp,  2048
          mov     edi,  dword[esp+  2084]
          mov     esi,  dword[esp+  2088]
          mov     ebp,  dword[esp+  2092]
          mov     dword[esp+  0],  ebp
          mov     dword[esp+  4],  10 ; ^0  sizeMax
          cmp     edi,  0
          je      .e
          cmp     esi,  0
          je      .e
          cmp     ebp,  10
          jle     .e
          cmp     ebp,  1010
          ja      .e
          lea     ebx,  [esp+40]
          jmp     short  .1
.e:       mov     eax,  -1
          stc
          jmp     .z
.f:       jmp     .3
.1:       mov     ecx,  ebp
          sub     ecx,  2
          call    LeggiParola_ibc
          mov     dword[esp+  8],  eax
          mov     dword[esp+  12],  edx
          mov     dword[esp+  16],  ecx
          cmp     ecx,  dword[esp+  0]
          jl      .2
          cmp     dword[esp+  4],  10
          je      .10
          push    edi
          push    10
          call    _fputc
          add     esp,  8
          cmp     eax,  -1
          je      .e
          mov     dword[esp+  4],  0
.10:      mov     dword[esp+  0],  ebp
.2:       mov     ecx,  dword[esp+  16]
          call    InserisciFile_jbc
          jc      .e
          mov     ecx,  dword[esp+  16]
          sub     dword[esp+  0],  ecx
          mov     eax,  dword[esp+  8]
          mov     edx,  dword[esp+  12]
          cmp     edx,  0
          je      .f
          cmp     edx,  2
          jne     .23
          cmp     eax,  10
          jne     .21
          mov     dword[esp+  0],  ebp
          jmp     short  .22
.21:      sub     dword[esp+  0],  1
          jg      .22
          push    eax
          push    edi
          push    10
          call    _fputc
          add     esp,  8
          mov     edx,  eax
          pop     eax
          cmp     edx,  -1
          je      .e
          mov     dword[esp+  0],  ebp
          dec     dword[esp+  0]
.22:      mov     dword[esp+  4],  eax
          push    edi
          push    eax
          call    _fputc
          add     esp,  8
          cmp     eax,  -1
          je      .e
          jmp     .1
.23:      ; caso parola troppo lunga
          push    edi
          push    10
          call    _fputc
          add     esp,  8
          cmp     eax,  -1
          je      .e            ; rompe la parola
          mov     dword[esp+  0],  ebp
          mov     eax,  dword[esp+  8]
          jmp     short  .21    ; stampa il carattere letto
.3:       xor     eax,  eax
.z:
          lea     esp,  [esp+2048]
          popad
          ret     12

;i:FileInHandle  b:buffer   c:maxleninbuffer
;Ritorna
;in c la lunghezza, in a il carattere spazio che ha fermato
;la lettura [caso r=2]
;oppure nessun carattere EOF o errore [r=0]
;oppure il prossimo carattere se la parola ha + caratteri
;del massimo consentito [r=1]
          align   4
LeggiParola_ibc:
          push    ebx
          push    edi
          push    ebp
          mov     ebp,  ecx
.1:       push    esi
          call    _fgetc
          add     esp,  4
          mov     edi,  eax
          cmp     eax,  -1
          je      .2
          push    eax
          call    _isspace
          add     esp,  4
          cmp     eax,  0
          jne     .4
          dec     ebp
          jz      .3
          mov     eax,  edi
          mov     [ebx],  al
          inc     ebx
          jmp     short  .1
.2:       mov     edx,  0
          jmp     short  .5
.3:       mov     edx,  1
          jmp     short  .5
.4:       mov     edx,  2
.5:       mov     eax,  edi
          mov     ecx,  ebx
          sub     ecx,  dword[esp+  8]
          clc
          pop     ebp
          pop     edi
          pop     ebx
          ret

;j:FileOutHandle  b:buffer  c:lenBuffer
          align   4
InserisciFile_jbc:
          push    ebx
          push    ebp
          cmp     ecx,  0
          jl      .e
          jz      .2
          mov     ebp,  ecx
          jmp     short  .1
.e:       mov     eax,  -1
          stc
          jmp     short  .z
.1:       xor     eax,  eax
          mov     al,  [ebx]
          push    edi
          push    eax
          call    _fputc
          add     esp,  8
          cmp     eax,  -1
          je      .e
          inc     ebx
          dec     ebp
          jnz     .1
.2:       mov     eax,  ebx
          sub     eax,  dword[esp+  4]
          clc
.z:
          pop     ebp
          pop     ebx
          ret


> ;Nasmw  -fobj  thisFile.asm
> ;bcc32  -v     ThisFile.obj
>
> section _DATA use32 public class=DATA
>
> global    _main
> extern    _printf
> extern    _fputc
> extern    _fgetc
> extern    _fclose
> extern    _fopen
> extern    _atoi
> extern    _ferror
> extern    _isspace
> extern    __streams
>
> "> thisProg inFile [outFile] N.dilinee\n"
> db "> thisProg inFile [outFile] N.dilinee",13,10,0,0
>
> "Find one or more error\n"
> db "Find one or more error",13,10,0,0
>
> nl        db   13, 10, 0, 0
>
> section _BSS use32 public class=BSS
> buffer   resd    41792
>
> section _TEXT use32 public class=CODE
>
> ;[32+20]52ra, 56P1, 60P2
> align 4
> _main:
> pushad
> s-=20
>     c=^56|a=^60|^12=0   ; se c=1  r=a[0] il nome del file
>     a==0!#.1
> .usage:  _printf<("> thisProg inFile [outFile] N.dilinee\n")|##.z
> .e2:     _fclose<(i)
> .e1:     ^12==0#.error|_fclose<(k)
> .error:  _printf<("Find one or more error\n")|##.z
> .1:  c<3#.usage|c>4#.usage    ; only 3 or 4 argument
>     i=*a+4  |j=*a+8
>   ; __streams point to stdin, 24=sizeof(FILE)
>     k=__streams|k+=24    ; k point to stdout
>     c==4!#.2|j=*a+12 |k=*a+8
>              a=s|D*s="w"|_fopen<(k,a)|a==0#.error
>              k=a|^12=7 ; k point to a oper file handle
> .2:  _atoi<(j)|j<=10?#.e1|j=a
>     a=s|D*s="r"|_fopen<(i,a)|a==0#.e1|i=a
>     RompiLinee(k,i,j)|jc  .e2
>     _fclose<(i)|a#.e1
>     _ferror<(k)|a#.e1
>     ^12==0#.z|_fclose<(k)|a#.error
> .z:
> s+=20
> popad
> a^=a
> ret
>
> ;32Ra,  36P_fileOut, 40P_fileIn, 44P_linesz + 2048
> ;     2084         2088        2092
> align  4
> RompiLinee:
> pushad
> s-=    2048
>     j=^2084|i=^2088|k=^2092|^0=k|^4=10   ; ^0  sizeMax
>     j==0#.e|i==0#.e|k<=10?#.e|k>1010#.e|b=&*s+40|#.1
> .e:  a=-1|stc|##.z
> .f:  ##.3
> .1:  c=k |c-=2|LeggiParola_ibc()|^8=a|^12=r|^16=c
>     c>=^0!?#.2|^4!=10!#.10|_fputc<(10, j)|a==-1#.e|^4=0
> .10: ^0=k
> .2:  c=^16|InserisciFile_jbc()|jc  .e
>     c=^16|^0-=c|a=^8|r=^12
>     r==0#.f
>     r==2!#.23|a==10!#.21|^0=k|#.22
> .21:           ^0-=1|>?#.22|_fputc<(10, j)|a==-1#.e|^0=k|--^0

^0-=1|>?#.22|<a|_fputc<(10, j)|r=a|>a|r==-1#.e|^0=k|--^0
forget to push pop eax

> .22:           ^4=a |_fputc<(a, j)|a==-1#.e
>               ##.1
> .23:    ; caso parola troppo lunga
>    _fputc<(10, j)|a==-1#.e  ; rompe la parola
>    ^0=k|a=^8|#.21           ; stampa il carattere letto
> .3: a^=a
> .z:
> s =&*s+2048
> popad
> ret  12
>
> ;i:FileInHandle  b:buffer   c:maxleninbuffer
> ;Ritorna
> ;in c la lunghezza, in a il carattere spazio che ha fermato
> ;la lettura [caso r=2]
> ;oppure nessun carattere EOF o errore [r=0]
> ;oppure il prossimo carattere se la parola ha + caratteri
> ;del massimo consentito [r=1]
> align  4
> LeggiParola_ibc:
> <b,j,k
>    k=c
> .1: _fgetc<(i)|j=a|a==-1#.2|_isspace<(a)|a#.4
>    --k!#.3|a=j|*b=al|++b|#.1
> .2: r=0|#.5
> .3: r=1|#.5
> .4: r=2
> .5: a=j|c=b|c-=^8|clc
>>b,j,k
> ret
>
> ;j:FileOutHandle  b:buffer  c:lenBuffer
> align  4
> InserisciFile_jbc:
> <b,k
>    c<>0|<?#.e|=#.2|k=c|#.1
> .e: a=-1|stc|#.z
> .1: a^=a|al=*b|_fputc<(a, j)|a==-1#.e|++b|--k#.1
> .2: a=b |a-=^4|clc
> .z:
>>b,k
> ret
>
>
>
>


Back to comp.programming | Previous | Next — Previous in thread | Find similar | Unroll thread


Thread

Re: example4-25 of c++cookbook "io_x" <a@b.c.invalid> - 2011-07-11 08:08 +0200
  Re: example4-25 of c++cookbook "io_x" <a@b.c.invalid> - 2011-07-11 09:00 +0200
    Re: example4-25 of c++cookbook "Kleuskes & Moos" <kleuske@xs4all.nl> - 2011-07-11 01:02 -0700
      Re: example4-25 of c++cookbook "io_x" <a@b.c.invalid> - 2011-07-11 10:14 +0200
      Re: example4-25 of c++cookbook James Kuyper <jameskuyper@verizon.net> - 2011-07-11 06:23 -0400
        Re: example4-25 of c++cookbook "io_x" <a@b.c.invalid> - 2011-07-11 12:51 +0200
    Re: example4-25 of c++cookbook "io_x" <a@b.c.invalid> - 2011-07-12 09:05 +0200
      Re: example4-25 of c++cookbook "io_x" <a@b.c.invalid> - 2011-07-12 09:42 +0200

csiph-web