Re: newbie: I/O with nasm



Herbert Kleebauer wrote:

....
Now, if Windows is what TK is looking for, we've got something to go on.

But for a start I think it is better to use a simple getc (return the
read character (or -1 if EOF) in eax) and putc (write character in al)
for a simple IO to stdin/stdout (based on ReadFile and WriteFile, but hiding the internals):

004010f7: 50 putc: move.l r0,-(sp)
004010f8: a2 00401157 move.b r0,_buf
004010fd: 31 c0 eor.l r0,r0
004010ff: 03 05 00401164 add.l _handle,r0
00401105: 75 0d bne.b _10
00401107: 6a f5 moveq.l #-11,-(sp)
00401109: ff 15 0040100c jsr.l (GetStdHandle)
0040110f: a3 00401164 move.l r0,_handle
00401114: 6a 00 _10: moveq.l #0,-(sp)
00401116: 68 00401168 move.l #_count,-(sp)
0040111b: 6a 01 moveq.l #1,-(sp)
0040111d: 68 00401157 move.l #_buf,-(sp)
00401122: 50 move.l r0,-(sp)
00401123: ff 15 00401014 jsr.l (WriteFile)
00401129: 09 c0 or.l r0,r0
0040112b: 75 1c bne.b _20
0040112d: 6a 00 _30: moveq.l #0,-(sp) 0040112f: 68 00401158 move.l #_text,-(sp)
00401134: 68 00401158 move.l #_text,-(sp)
00401139: 6a 00 moveq.l #0,-(sp)
0040113b: ff 15 00401000 jsr.l (MessageBoxA)

Okay... I suppose we can't report the error with Writefile :)

00401141: 6a 00 moveq.l #0,-(sp)
00401143: ff 15 00401008 jsr.l (ExitProcess)

Should we be returning an errornumber?

00401149: 81 3d 00401168 0040114f: 00000001 _20: cmp.l #1,_count
00401153: 75 d8 bne.b _30
00401155: 58 move.l (sp)+,r0
00401156: c3 rts.l
00401157: 00 _buf: dc.b 0
00401158: 77 72 69 74 65 20 0040115e: 65 72 72 6f 72 00 _text: dc.b 'write error',0
even4
00401164: 00000000 _handle:dc.l 0
00401168: 00000000 _count: dc.l 0
0040116c: 31 c0 getc: eor.l r0,r0
0040116e: 03 05 004011e0 add.l _handle,r0
00401174: 75 0d bne.b _10
00401176: 6a f6 moveq.l #-10,-(sp)
00401178: ff 15 0040100c jsr.l (GetStdHandle)
0040117e: a3 004011e0 move.l r0,_handle
00401183: 6a 00 _10: moveq.l #0,-(sp)
00401185: 68 004011e4 move.l #_count,-(sp)
0040118a: 6a 01 moveq.l #1,-(sp)
0040118c: 68 004011d1 move.l #_buf,-(sp)
00401191: 50 move.l r0,-(sp)
00401192: ff 15 00401010 jsr.l (ReadFile)
00401198: 09 c0 or.l r0,r0
0040119a: 75 1c bne.b _20
0040119c: 6a 00 moveq.l #0,-(sp) 0040119e: 68 004011d2 move.l #_text,-(sp)
004011a3: 68 004011d2 move.l #_text,-(sp)
004011a8: 6a 00 moveq.l #0,-(sp)
004011aa: ff 15 00401000 jsr.l (MessageBoxA)
004011b0: 6a 00 moveq.l #0,-(sp)
004011b2: ff 15 00401008 jsr.l (ExitProcess)
004011b8: 0f b6 05 004011d1 _20: movu.bl _buf,r0
004011bf: 81 3d 004011e4 004011c5: 00000001 cmp.l #1,_count
004011c9: 74 05 beq.b _30
004011cb: b8 ffffffff move.l #-1,r0
004011d0: c3 _30: rts.l
004011d1: 00 _buf: dc.b 0
004011d2: 72 65 61 64 20 65 004011d8: 72 72 6f 72 00 _text: dc.b 'read error',0
004011dd: 00 00 00 even4
004011e0: 00000000 _handle:dc.l 0
004011e4: 00000000 _count: dc.l 0

Okay... These don't match the "with Nasm" part of the spec, but I agree that they're nice wrappers for the APIs. In fact, I think I cribbed the "getc" from you some time ago :) It was slightly different, though - involved GetConsoleMode and SetConsoleMode, IIRC... I thought this was so ReadFile would return without the victim having to hit "enter"... In any case, we can translate to Nasm-speak, if that's what TK wants...

I don't know what "overlapped buffer" means.

That wouldn't be necessary to know if people would DOS to learn
assembly programming!

=========================================================================
= ReadFile =
=========================================================================

[snip]

Yup! Dos is easier! We can do "getc" with several dos ints, or bios - we can do "putc" straight to screen memory without any ints at all, if we don't want it "redirectable"... There may not be much point to "learning dos" in itself, but it makes an easy platform to learn the basics of asm. Linux ain't bad - perhaps easier, in some ways. We *do* need to do tricks on stdin to get a decent getc that returns without waiting for "enter". At least, like dos, we know what "stdin" and "stdout" are, without having to jump through hoops. But with some "wrappers" like you show, a beginner could use Windows, too. And as the extensive documentation shows, it *is* a "powerful" API...

Best,
Frank
.



Relevant Pages

  • Re: EV_RXCHAR event received, but ReadFile sometimes failed to read a byte from serial port
    ... With non-overlapped ReadFile, if a ReadFile is pending, a WriteFile issued after the ... Why do you declare BOOL variables (prefix 'b') with some meaningless prefix ('f', ... Why read only one character? ... to use the events in serial ports at all. ...
    (microsoft.public.vc.mfc)
  • Re: Reading a file.
    ... If a file contains the the character "9" then the file will contain 0x39 or 0x39 0x00 depending on if it's stored as Unicode or not. ... ReadFile will return 0x39 regardless if you read a single byte. ... >> Bruce.Eitman AT EuroTech DOT com ... >>> int WINAPI WinMain(HINSTANCE hInstance, ...
    (microsoft.public.windowsce.app.development)
  • Re: Reading a file.
    ... Problem is when i am reading a file through "readfile" function,it is being ... Bruce.Eitman AT EuroTech DOT com ... buffer is character values, then in memory ASCII values are displayed. ... DWORD d = GetLastError; ...
    (microsoft.public.windowsce.app.development)
  • Re: Readfile 0 byte from serial byte
    ... The way it's going to work is that ReadFile will, on each loop, return one ... I/O code because reading one character at a time is *very* slow. ... > some times the ReadFile hang even the data is available, ...
    (microsoft.public.windowsce.embedded.vc)
  • Re: Whats wrong with the string?
    ... Your option just claims memory for a pointer, ... putc is used to put a character from a stream to the screen (see man ... > putcis a function to put a char on the serial console. ...
    (comp.os.linux.embedded)