home bbs files messages ]

Forums before death by AOL, social media and spammers... "We can't have nice things"

   comp.lang.asm.x86      Ahh, the lost art of x86 assembly      4,675 messages   

[   << oldest   |   < older   |   list   |   newer >   |   newest >>   ]

   Message 4,370 of 4,675   
   luser.droog@nospicedham.gmail.com to luser...@nospicedham.gmail.com   
   Re: reading a file from disk (1/2)   
   03 Jul 21 21:17:24   
   
   From: luser...@nospicedham.gmail.com   
      
   On Thursday, June 17, 2021 at 8:43:57 PM UTC-5, luser...@nospicedham.gmail.com   
   wrote:   
   > On Monday, May 24, 2021 at 1:22:49 AM UTC-5, luser...@nospicedham.gmail.com   
   wrote:   
   > > On Sunday, May 16, 2021 at 3:48:21 PM UTC-5, luser...@nospic   
   dham.gmail.com wrote:   
   > > > Hello all,   
   > > >   
   > > > I'm planning to resume work on my partly written 8086 emulator after a   
   > > > long hiatus. I want to add the ability to read a file, but I'm having   
   some   
   > > > difficulty   
   > > [...]   
   > > > Is there a good resource to understand how this all should work?   
   > > I suppose this isn't really related to assembly language. I've ordered   
   Peter Norton's   
   > > Guide to the IBM PC. I guess I'll post in comp.os.msdos.programmer if I   
   run into   
   > > trouble.   
   > For posterity, Norton's Guide really seems to be the perfect book to learn   
   all about   
   > this stuff. It looks like I want to bypass DOS 1.0 stuff, too, and go   
   straight for the DOS 2.0   
   > additions to have a file handle and less fiddly business.   
      
   So I read the whole book except some of the video and keyboard stuff that I   
   may not need   
   (or at least don't need yet).   
      
   Here's my rough draft of dos support functions. The vv array is the payload of   
   the ESC   
   bytes from the ESC instruction. My interrupt handlers fill it in with the   
   interrupt number.   
   U is a uintptr_t.   
      
   It need to do more error checking and reporting, but this should be enough to   
   access   
   files from my Forth CODEs. Maybe exiting a dos program ought not to exit() the   
   whole   
   emulator.   
      
      
   static int keyboard_input_with_echo(){   
     bput(al, fgetc(stdin));   
   }   
      
   static int display_output(){   
     fputs( cp437tounicode( bget(dl) ), stdout );   
     bput(al,bget(dl)); if(bget(al)=='\t')bput(al,' ');   
   }   
      
   static int display_string(){   
     f=wget(dx);   
     while(mem[f]!='$')fputs( cp437tounicode( mem[f++] ), stdout );   
     bput(al,'$');   
   }   
      
   static int get_date(){   
     time_t t=time(NULL);struct tm*tm=localtime(&t);   
     wput(cx,tm->tm_year);   
     wput(dh,tm->tm_mon);   
     wput(dl,tm->tm_mday);   
     wput(al,tm->tm_wday);   
   }   
      
   static int get_time(){   
     struct timeval tv;gettimeofday(&tv,0);   
     time_t t=time(NULL);struct tm*tm=localtime(&t);   
     bput(ch,tm->tm_hour);   
     bput(cl,tm->tm_min);   
     bput(dh,tm->tm_sec);   
     bput(dl,tv.tv_usec/10);   
   }   
      
   static int open_file(){   
     U mode = bget(al);   
     FILE *f = fopen(mem + ds_(dx), (mode & 7) == 0? "r":   
   				 (mode & 7) == 1? "w": "rw");   
     if(  f  ){   
       U handle = next_handle ++;   
       handles[ handle ] = f;   
       wput(ax, handle);   
       clc();   
       return 0;   
     }   
     wput(ax, 0);   
     stc();   
   }   
      
   static int close_file_handle(){   
     U handle = wget(bx);   
     fclose( handles[ handle ] );   
     handles[ handle ] = 0;   
     -- next_handle;   
   }   
      
   static int read_file(){   
     U handle = wget(bx);   
     U count = fread(mem + ds_(dx), 1, wget(cx), handles[ handle ]);   
     if(  count  ){   
       wput(ax, count);   
       clc();   
       return 0;   
     }   
     wput(ax, 5); //access denied   
     stc();   
   }   
      
   static int write_file(){   
     U handle = wget(bx);   
     U count = fwrite(mem + ds_(dx), 1, wget(cx), handles[ handle ]);   
     if(  count == wget(cx)  ){   
       clc();   
       return 0;   
     }   
     wput(ax, 5); //access denied   
     stc();   
   }   
      
   static int move_file_pointer(){   
     U handle = wget(bx);   
     U whence = bget(al);   
     fseek( handles[ handle ], qget(cx,dx), whence == 0? SEEK_SET:   
   					 whence == 1? SEEK_CUR:   
   					 whence == 2? SEEK_END: 0);   
     U pos = ftell( handles[ handle ] );   
     qput(dx, ax, pos);   
   }   
      
   static int dos( UC vv[7] ){   
     switch(bget(ah)){   
     CASE 0x01: return keyboard_input_with_echo();   
     CASE 0x02: return display_output();   
     CASE 0x09: return display_string();   
      
     CASE 0x2A: return get_date();   
     CASE 0x2C: return get_time();   
      
     CASE 0x3C: // create file   
     CASE 0x3D: return open_file();   
     CASE 0x3E: return close_file_handle();   
     CASE 0x3F: return read_file();   
     CASE 0x40: return write_file();   
     CASE 0x42: return move_file_pointer();   
     CASE 0x44: // ioctl   
     CASE 0x4B: // load/execute program   
     CASE 0x4C: exit(bget(al));   
     CASE 0x5B: // create new file   
       ;   
     }   
   }   
      
      
   static int keyboard_input_with_echo(){   
     bput(al, fgetc(stdin));   
   }   
      
   static int display_output(){   
     fputs( cp437tounicode( bget(dl) ), stdout );   
     bput(al,bget(dl)); if(bget(al)=='\t')bput(al,' ');   
   }   
      
   static int display_string(){   
     f=wget(dx);   
     while(mem[f]!='$')fputs( cp437tounicode( mem[f++] ), stdout );   
     bput(al,'$');   
   }   
      
   static int get_date(){   
     time_t t=time(NULL);struct tm*tm=localtime(&t);   
     wput(cx,tm->tm_year);   
     wput(dh,tm->tm_mon);   
     wput(dl,tm->tm_mday);   
     wput(al,tm->tm_wday);   
   }   
      
   static int get_time(){   
     struct timeval tv;gettimeofday(&tv,0);   
     time_t t=time(NULL);struct tm*tm=localtime(&t);   
     bput(ch,tm->tm_hour);   
     bput(cl,tm->tm_min);   
     bput(dh,tm->tm_sec);   
     bput(dl,tv.tv_usec/10);   
   }   
      
   static int open_file(){   
     U mode = bget(al);   
     FILE *f = fopen(mem + ds_(dx), (mode & 7) == 0? "r":   
   				 (mode & 7) == 1? "w": "rw");   
     if(  f  ){   
       U handle = next_handle ++;   
       handles[ handle ] = f;   
       wput(ax, handle);   
       clc();   
       return 0;   
     }   
     wput(ax, 0);   
     stc();   
   }   
      
   static int close_file_handle(){   
     U handle = wget(bx);   
     fclose( handles[ handle ] );   
     handles[ handle ] = 0;   
     -- next_handle;   
   }   
      
   static int read_file(){   
     U handle = wget(bx);   
     U count = fread(mem + ds_(dx), 1, wget(cx), handles[ handle ]);   
     if(  count  ){   
       wput(ax, count);   
       clc();   
       return 0;   
     }   
     wput(ax, 5); //access denied   
     stc();   
   }   
      
   static int write_file(){   
     U handle = wget(bx);   
     U count = fwrite(mem + ds_(dx), 1, wget(cx), handles[ handle ]);   
     if(  count == wget(cx)  ){   
       clc();   
       return 0;   
     }   
     wput(ax, 5); //access denied   
     stc();   
   }   
      
   static int move_file_pointer(){   
     U handle = wget(bx);   
     U whence = bget(al);   
     fseek( handles[ handle ], qget(cx,dx), whence == 0? SEEK_SET:   
   					 whence == 1? SEEK_CUR:   
   					 whence == 2? SEEK_END: 0);   
     U pos = ftell( handles[ handle ] );   
     qput(dx, ax, pos);   
   }   
      
   static int dos( UC vv[7] ){   
     switch(bget(ah)){   
     CASE 0x01: return keyboard_input_with_echo();   
     CASE 0x02: return display_output();   
     CASE 0x09: return display_string();   
      
     CASE 0x2A: return get_date();   
     CASE 0x2C: return get_time();   
      
     CASE 0x3C: // create file   
     CASE 0x3D: return open_file();   
     CASE 0x3E: return close_file_handle();   
     CASE 0x3F: return read_file();   
     CASE 0x40: return write_file();   
     CASE 0x42: return move_file_pointer();   
     CASE 0x44: // ioctl   
     CASE 0x4B: // load/execute program   
     CASE 0x4C: exit(bget(al));   
     CASE 0x5B: // create new file   
       ;   
     }   
   }   
      
      
   And this bit is silly but fun:   
      
   unsigned cp437table[256] = {   
   ' ', 0x263A,0x263B,0x2665,0x2666,0x2663,0x2660,0x2022,   
     0x25D8,0x25CB,0x2509,0x2642,0x2640,0x266A,0x266B,0x263C,   
   0x25BA,0x25C4,0x2195,0x203C,0x00B6,0x00A7,0x25AC,0x21A8,   
     0x2191,0x2193,0x2192,0x2190,0x221F,0x2194,0x25B2,0x25BC,   
      
   [continued in next message]   
      
   --- SoupGate-Win32 v1.05   
    * Origin: you cannot sedate... all the things you hate (1:229/2)   

[   << oldest   |   < older   |   list   |   newer >   |   newest >>   ]


(c) 1994,  bbs@darkrealms.ca