Re: Excercices - 1 Exercise completed

From: Kain (kahlinor_at_yahoo.com)
Date: 10/07/04


Date: 6 Oct 2004 23:15:44 -0700

Hi Andy,

stdin.getc probably will not work since the code reads in chunks of
input to process. Try communicating directly with WinAPI.

This program works as desired:

//====================================================================
program ConvertUnixDos;

//Excercise provided by Herbert Kleebauer <klee@unibwm.de>
//newsgroup: news://alt.lang.asm

#include( "stdlib.hhf" )
#include( "w.hhf" )

static

        ReadFile:procedure
        ( overlapped:dword; var bytesRead:dword; len:dword;
                         var buffer:byte; Handle:dword );
                @external( "__imp__ReadFile@20" );

        GetStdHandle:procedure ( WhichHandle:int32);
                @external( "__imp__GetStdHandle@4" );

        stdhandle :dword;
        bread :dword;
        buffer :byte;

begin ConvertUnixDos;

        GetStdHandle(-10); // get console input handle
        mov(eax,stdhandle);
        
_loop:
        ReadFile(0, bread, 1, buffer, stdhandle); // read a single byte
        cmp(bread,0); // eof?
        je _endloop;
        
        mov(buffer,al);
        cmp(al,$a);
        if(@e) then
                stdout.put(nl);
        else
                stdout.putc(al);
        endif;
        jmp _loop;
_endloop:

end ConvertUnixDos;
//==============================================================

-kain
http://www.geocities.com/kahlinor/index.html

"Andy" <x_terminat_or_3@yahoo.fr> wrote in message news:<41645351$0$287$a3f2974a@nnrp1.numericable.fr>...
> Correctum:
>
> Read >> You're right about the stdin.eof() this time all of the file is
> converted!
> as >>I think you're right about the stdin.eof() this time all of the file
> should be converted
>
> I was not able to reassemble the program with stdin.eof , the instruction is
> not implemented...
>
> As for your remarks:
>
> $10 means hex(10)
> nl in hla means CRLF for Dos/Windows, LF for unix
>
>
>
> --
> With kind regards
>
>
> Andy
> "Herbert Kleebauer" <klee@unibwm.de> a écrit dans le message de news:
> 41644BC1.678759D7@unibwm.de...
> > Andy wrote:
> >
> >> You're right about the stdin.eof() this time all of the file is
> >> converted!
> >
> > Are you sure, that the result is ok?
> >
> > repeat
> > stdin.getc();
> > if( al==$A) then
> > stdout.put(nl);
> > else
> > stdout.putc(al);
> > endif;
> > until (stdin.eoln());
> >
> > As I already said, I don't know whether in the HLA library
> > stdin and stdout is opened as a text file with an implicit
> > eol conversion or as a binary file. I hope that it is opened
> > as binary file so you can write filter for binary files too,
> > but then you replace a <LF> = $0a (does HLA use $ for hex
> > values?) by a nl (I suppose nl is also defined as $0a=<LF>)
> > which means your code doesn't modify the file. If stdin/stdout
> > is opened as a text file (a very bad choice), then you don't
> > need any "if .. else" at all, just write the read character
> > to stdout, the conversion is done internally to stdout.getc
> > and stdout.putc.
> >
> >
> >
> >> As to the code you posted, I find it impossible to comprehend.
> >> Did you do this at hand?
> >
> > Yes, assembly programming means to write processor instructions
> > by hand, using mnemonics. Don't forget HLA has nothing to do
> > with assembly programming, it is just an other HL language with
> > an inline assembler which you can use if really insist.
> >
> > loop: bsr.l getc ; get char from stdin
> > cmpq.l #-1,r0 ; EOF
> > bne.b _10 ; branch if not
> > moveq.l #0,-(sp)
> > jsr.l (ExitProcess) ; exit program
> > _10: cmp.b #$0a,r0 ; <LF>
> > bne.b _20
> > move.b #$0d,r0 ; <CR>
> > bsr.l putc
> > move.b #$0a,r0 ; <LF>
> > _20: bsr.l putc ; write char to stdout
> > br.b loop ; go on
> >
> > Each line in the assembler source code is a processor instruction.
> > The assembler converts independently each line to the binary code
> > which is executed by the CPU:
> >
> > 000002d2: 004010d2: e8 00000099
> > 000002d7: 004010d7: 83 f8 ff
> > 000002da: 004010da: 75 08
> > 000002dc: 004010dc: 6a 00
> > 000002de: 004010de: ff 15 00401008
> > 000002e4: 004010e4: 3c 0a
> > 000002e6: 004010e6: 75 09
> > 000002e8: 004010e8: b0 0d
> > 000002ea: 004010ea: e8 00000009
> > 000002ef: 004010ef: b0 0a
> > 000002f1: 004010f1: e8 00000002
> > 000002f6: 004010f6: eb da
> >
> > The first number is the position in the exe file, the second
> > number is the virtual address at which the code is executed
> > at run time and the remaining numbers ar the hex code of
> > the processor instruction.
> >
> > If you want to learn assembly programming, forget about HLA
> > and download an assembler (there are plenty free versions).