EASy68K  
It is currently Fri Dec 15, 2017 8:17 am

All times are UTC




Post new topic Reply to topic  [ 12 posts ] 
Author Message
 Post subject: read/ write to file
PostPosted: Mon Dec 14, 2015 2:26 am 
Offline

Joined: Fri Aug 14, 2015 2:12 pm
Posts: 15
Any quick code for storing a character string or number to a file from a memory address or register, and then being able to read the value from the file and store in a register or address.? I have been looking at the I/O tasks in the 50s but it seems incomplete. I see how to identify the file, and how to specify how many bytes to read/ write, but not how to specify the actual contents of the write or the storage destination of the read.

Think storage of a high score / initials in a game that can be accessed the next time the program is run.


Top
 Profile  
 
 Post subject: Re: read/ write to file
PostPosted: Mon Dec 14, 2015 10:54 am 
Offline
User avatar

Joined: Thu Dec 16, 2004 6:42 pm
Posts: 1056
fileio.X68 in the examples folder of EASy68K demonstrates reading and writing.

For both reading (task 53) and writing (task 54) register A1 contains the address of the buffer that holds the data.

_________________
Prof. Kelly


Top
 Profile  
 
 Post subject: Re: read/ write to file
PostPosted: Mon Dec 14, 2015 2:57 pm 
Offline

Joined: Fri Aug 14, 2015 2:12 pm
Posts: 15
thanks


Top
 Profile  
 
 Post subject: Re: read/ write to file
PostPosted: Tue Dec 15, 2015 1:14 am 
Offline

Joined: Fri Aug 14, 2015 2:12 pm
Posts: 15
As usual , I am probably missing something obvious. I searched through the forum posts and found some example code.
So:
this code prints the number 300 into the output window

ORG $1000
start
move #50,d0
trap #15 close all files
move #300,d3
lea filename,a1
move #52,d0 open new file
trap #15

lea buffer,a1 buffer
move.w d3,(a1) store d3 to buffer
move #2,d2 # bytes to write
move #54,d0 write buffer to file
trap #15

move #0,d2 file position
move #55,d0
trap #15

lea buffer,a1
move #1,d2 # bytes to read
move #53,d0 read from file
trap #15
move.w (a1),d4

move.l d4,d1
move #3,d0
trap #15

move.b #9,D0
trap #15 Halt Simulator

filename dc.b 'test1.txt',0
buffer ds.b 80

END start

But when I open the txt file it is empty. Somehow it seems like 300 must just still be in the buffer....

Also, it seems to me that after that file has been created, written to, then this entirely new program below should open the existing saved file, read the data, and print it:

ORG $1000

start


move #51,d0
lea filename,a1
trap #15

move #0,d2 file position
move #55,d0
trap #15

lea buffer,a1
move #1,d2 # bytes to read
move #53,d0 read from file
trap #15
move.w (a1),d4

move.l d4,d1
move #3,d0
trap #15

move.b #9,D0
trap #15

filename dc.b 'test1.txt',0
buffer ds.b 80

END start


However, this outputs 0 into the output window. What am I missing ? I have run the "diagnostic s" , such as trap task 59, and looked at the post conditions and all seems to be checking out.


Top
 Profile  
 
 Post subject: Re: read/ write to file
PostPosted: Tue Dec 15, 2015 10:37 am 
Offline
User avatar

Joined: Thu Dec 16, 2004 6:42 pm
Posts: 1056
The file must be closed to complete the write operation. This is SOP for writing to files in computer programs but when looking at this question I realized it is not explained in the EASy68K help. I'll try to remember to update the help.

Code:
*-----------------------------------------------------------
* Title      :
* Written by :
* Date       :
* Description: Save the number 300 to test1.txt file
*-----------------------------------------------------------
    ORG $1000
start
    move #50,d0
    trap #15 close all files
    move #300,d3
    lea filename,a1
    move #52,d0 open new file
    trap #15

    lea buffer,a1 buffer
    move.w d3,(a1) store d3 to buffer
    move #2,d2 # bytes to write
    move #54,d0 write buffer to file
    trap #15

    move    #56,d0
    trap    #15             close the file to complete write

    SIMHALT

filename dc.b 'test1.txt',0
buffer ds.b 80

    END start



This code will read and display the number save in the file.
Code:
*-----------------------------------------------------------
* Title      :
* Written by :
* Date       :
* Description: Read number from file
*-----------------------------------------------------------
    ORG $1000

start
    move.b #50,d0
    trap #15            ; close all files
   
    move #51,d0         ; open existing file
    lea filename,a1
    trap #15

    lea buffer,a1
    move.L #2,d2        # bytes to read
    move #53,d0         read from file
    trap #15
    move.w (a1),d4
    move.w d4,d1
    move #3,d0          ; display the number in D1
    trap #15

    move.b #9,D0
    trap #15

filename dc.b 'test1.txt',0
buffer ds.b 80

    END    START        ; last line of source

_________________
Prof. Kelly


Top
 Profile  
 
 Post subject: Re: read/ write to file
PostPosted: Wed Dec 16, 2015 11:49 am 
Offline

Joined: Fri Aug 14, 2015 2:12 pm
Posts: 15
Ok, I got that to work. Hopefully this is my last question for a while......

Trying to write keyed in text to file and then call it up and write to screen.

This program works when I do them together.

Code:
    ORG $1000
start
    move #50,d0
    trap #15 close all files

    lea filename,a1
    move #51,d0
    trap #15
   
    move.l (a1),d4
    move.l d4,buffer
   
    lea buffer,a1     buffer
    move #4,d2 # bytes to write
    move #54,d0 write buffer to file
    trap #15

    move    #56,d0
    trap    #15             close the file to complete write
   
    move #51,d0         ; open existing file
    lea filename,a1
    trap #15

    lea buffer,a1
    move.l #4,d2        # bytes to read
   
    move #53,d0         read from file
    trap #15
   
    move.w #3,d1
    move #0,d0
    trap #15

    simhalt
       
filename dc.b 'Protinit.txt',0
buffer ds.l 20
    END    START 


Now if I just try to open the existing , written to file, nothing appears on the screen. See code below. Also, after the read step , the d2 indicator goes to 2, which indicates that only 2 bytes are being read when I am asking for 4.


Code:
    ORG    $1000
START:                  ; first instruction of program

    move #50,d0
    trap #15

    move #51,d0         ; open existing file
    lea filename,a1
    trap #15
    move #55,d0
    move.l #0,d2
    trap #15
   
    lea buffer,a1
    move.l #4,d2        # bytes to read
    move #53,d0         read from file
    trap #15
   
    move #56,d0
    trap #15
 
    move.w #3,d1
    move #0,d0
    trap #15

    simhalt
       
filename dc.b 'Protinit.txt',0
buffer ds.l 20
    END    START

{admin: code tags added}


Top
 Profile  
 
 Post subject: Re: read/ write to file
PostPosted: Thu Dec 17, 2015 11:22 am 
Offline
User avatar

Joined: Thu Dec 16, 2004 6:42 pm
Posts: 1056
Your first example is not creating a file. It must use trap task #52 to open a new file.

_________________
Prof. Kelly


Top
 Profile  
 
 Post subject: Re: read/ write to file
PostPosted: Thu Dec 17, 2015 12:53 pm 
Offline

Joined: Fri Aug 14, 2015 2:12 pm
Posts: 15
The file I am writing to already exists. It is to be over written.

The output from the second program is giving ' yyy' with umlauts. It is now reading all bytes requested but it is giving an EOF indicator in D0


Top
 Profile  
 
 Post subject: Re: read/ write to file
PostPosted: Thu Dec 17, 2015 11:28 pm 
Offline
User avatar

Joined: Thu Dec 16, 2004 6:42 pm
Posts: 1056
If the file exists then the code works.
The last section of code
Code:
   move.w #3,d1
    move #0,d0
    trap #15
is only displaying 3 characters. Change the #3 to #4 to display 4.

_________________
Prof. Kelly


Top
 Profile  
 
 Post subject: Re: read/ write to file
PostPosted: Fri Dec 18, 2015 12:27 am 
Offline

Joined: Fri Aug 14, 2015 2:12 pm
Posts: 15
I'll probably just give up now, but I accidentally left out an important part of my code that I posted in the last message that made it unclear what I was doing.

The first step is a read text from keyboard using trap task #2, then I want to write the text to file for saving and later recall. I can get it to work when trying to read/write a number, but it is not working with text.

So I am doing :

Code:
lea filename, a1
move #51 , d0
trap #15

 move #2,d0   
   trap #15                          read in key sequence ( 3 letters-    my initials  EAS) I left this out above.
     
  move.l (a1),d4                 
  move.l d4,buffer
 
lea buffer , a1                                     I think this should move the hex ascii codes for the initials to buffer
move #3,  d2 # bytes to write
    move #54, d0 write buffer to file
    trap #15

    move    #56,d0
    trap    #15   

I think that should write the initials to the file, but when I try to read the 3 characters from the file using a second program and opens the file and reads, when I print using trap #0 it shows y's with umlauts. I am getting an error code in D0 after
Code:
   lea filename ,a1
  move #51,d0
trap #15 
 
  lea buffer,a1
    move.l #4,d2        # bytes to read
   
    move #53,d0         read from file
    trap #15                                                    error code appears in D0 after this step , EOF
whether I am reading 3 or 4 bytes


Top
 Profile  
 
 Post subject: Re: read/ write to file
PostPosted: Fri Dec 18, 2015 3:58 pm 
Offline
User avatar

Joined: Thu Dec 16, 2004 6:42 pm
Posts: 1056
Here is some example code:

StringToFile.X68
Code:
*-----------------------------------------------------------
* Title      : StringToFile
* Written by : Charles Kelly
* Date       : Dec. 18th 2015
* Description: Get a string from the keyboard and save to file.
*-----------------------------------------------------------
        ORG     $1000
START:                  ; first instruction of program
; Display description
        lea     description,A1          ; location of text to display
        move.b  #14,D0                  ; trap task #14, displays null terminated string at (A1)
        trap    #15                     ; perform task
; Prompt for input string
        lea     prompt,A1               ; location of text to display
        move.b  #14,D0                  ; trap task #14, displays null terminated string at (A1)
        trap    #15                     ; perform task
; Get input string from keyboard and store in buffer
        lea     string,A1               ; location of buffer
        move.b  #2,D0                   ; trap task #2, reads string from keyboard to buffer at (A1)
        trap    #15                     ; perform task
        move.w  D1,strSize              ; save size of string
; Attempt to open file for output (erases existing file)
        lea     fileName,A1             ; name of file to create
        move.b  #52,D0                  ; trap task #52, open new file
        trap    #15                     ; perform task
        move.l  D1,fileID               ; save file ID
; Check for file errors
        if.w D0 <NE> #0 then.s          ; if file error
            lea     errorFile,A1        ; location of text to display
            move.b  #14,D0              ; trap task #14, displays null terminated string at (A1)
            trap    #15                 ; perform task
            bra     EXIT                ; halt program
        endi
; Write buffer to file
        lea     string,A1               ; location of text to write to file
        move.l  fileID,D1               ; file ID of file to write to
        clr.l   D2
        move.w  strSize,D2              ; number of bytes to write
        add.w   #1,D2                   ; add 1 to include NULL character
        move.b  #54,D0                  ; trap task #54, write to file
        trap    #15                     ; perform task
; Close file to complete write
        move.b  #56,D0                  ; trap task #56, close file, D1 contains fileID
        trap    #15                     ; perform task
; Display success message
        lea     success,A1              ; location of text to display
        move.b  #14,D0                  ; trap task #14, displays null terminated string at (A1)
        trap    #15                     ; perform task

EXIT    SIMHALT         ; halt simulator

* Variables and constants
CR              EQU     $D              ; ASCII code for carriage return
LF              EQU     $A              ; ASCII code for line feed
SIZE            EQU     80              ; Max string size
description     DC.B    'Demonstrates reading a string from the keyboard and saving to a text file. ',CR,LF,CR,LF,0
prompt          DC.B    'Enter text: ',0
fileName        DC.B    'text1.txt',0   ; name of text file
errorFile       DC.B    'A file error occurred.',CR,LF,0
success         DC.B    'File write complete.',CR,LF,0
string          DS.B    80              ; I/O buffer
strSize         DS.W    1               ; Size if input string
fileID          DS.L    1               ; File ID
        END     START                   ; last line of source




FileToString.X68
Code:
*-----------------------------------------------------------
* Title      : FileToString
* Written by : Charles Kelly
* Date       : Dec. 18th 2015
* Description: Read a string from a file.
*-----------------------------------------------------------
        ORG     $1000
START:                  ; first instruction of program
; Display description
        lea     description,A1          ; location of text to display
        move.b  #14,D0                  ; trap task #14, displays null terminated string at (A1)
        trap    #15                     ; perform task
; Attempt to open file for input
        lea     fileName,A1             ; name of file to open
        move.b  #51,D0                  ; trap task #51, open existing file
        trap    #15                     ; perform task
        move.l  D1,fileID               ; save file ID
; Check for file errors
        if.w D0 <NE> #0 then.s          ; if file error
            lea     errorFile,A1        ; location of text to display
            move.b  #14,D0              ; trap task #14, displays null terminated string at (A1)
            trap    #15                 ; perform task
            bra     EXIT                ; halt program
        endi
; Read string from file
        lea     string,A1               ; location of string buffer
        move.l  fileID,D1               ; file ID of file to read from
        clr.l   D2
        move.w  #SIZE,D2                ; number of bytes to read
        move.b  #53,D0                  ; trap task #53, read from file
        trap    #15                     ; perform task
; Close file when finished
        move.b  #56,D0                  ; trap task #56, close file, D1 contains fileID
        trap    #15                     ; perform task
; Display result label
        lea     result,A1               ; location of text to display
        move.b  #14,D0                  ; trap task #14, displays null terminated string at (A1)
        trap    #15                     ; perform task
; Display string
        lea     string,A1               ; location of text to display
        move.w  #SIZE,D1                ; max characters to display
        move.b  #0,D0                   ; trap task #0, displays string
        trap    #15                     ; perform task
; Display success message
        lea     success,A1              ; location of text to display
        move.b  #14,D0                  ; trap task #14, displays null terminated string at (A1)
        trap    #15                     ; perform task

EXIT    SIMHALT         ; halt simulator

* Variables and constants
CR              EQU     $D              ; ASCII code for carriage return
LF              EQU     $A              ; ASCII code for line feed
SIZE            EQU     80              ; Max string size
description     DC.B    'Demonstrates reading a string from a file. ',CR,LF,CR,LF,0
result          DC.B    'The string read from the file is:',CR,LF,0
fileName        DC.B    'text1.txt',0   ; name of text file
errorFile       DC.B    'A file error occurred.',CR,LF,0
success         DC.B    'File read complete.',CR,LF,0
string          DS.B    SIZE            ; I/O buffer
strSize         DS.W    1               ; Size if input string
fileID          DS.L    1               ; File ID
        END     START                   ; last line of source

_________________
Prof. Kelly


Top
 Profile  
 
 Post subject: Re: read/ write to file
PostPosted: Fri Dec 18, 2015 4:39 pm 
Offline

Joined: Fri Aug 14, 2015 2:12 pm
Posts: 15
Thanks! I caught my error after looking at your code.

I was opening the file to write to and then entering the character string. This was overwriting the file ID in d1 with the length of the string.
So when I went to write to the file the ID was not valid.

If I just swap the order , read string , then open file, my code works. sometimes it is obvious and it takes someone else to help you see it.

ES


Top
 Profile  
 
Display posts from previous:  Sort by  
Post new topic Reply to topic  [ 12 posts ] 

All times are UTC


Who is online

Users browsing this forum: No registered users and 2 guests


You cannot post new topics in this forum
You cannot reply to topics in this forum
You cannot edit your posts in this forum
You cannot delete your posts in this forum
You cannot post attachments in this forum

Search for:
Jump to:  
cron
Powered by phpBB® Forum Software © phpBB Group