EASy68K  
It is currently Thu Apr 02, 2020 12:38 pm

All times are UTC




Post new topic Reply to topic  [ 6 posts ] 
Author Message
PostPosted: Sun Mar 13, 2011 7:21 pm 
Offline

Joined: Sat Mar 12, 2011 2:53 am
Posts: 9
Hello everyone,

Can anyone explain how i could go about writing to a text file from a storage defined in memory? (e.g. mystorage ds.w 20)
The value to be placed in that storage is coming from a data register using 'MOVEM' (e.g. MOVEM.w d2,mystorage)

My reason for doin this is that i am accepting alot values from the user and i need the data resgister space. So i assumed it would be easier to store
them to memory for later use, which involves writing these values to a text file

Attempts i've made before:
i found two threads on this forum

1) How to generally write to a text file from a defined constant(http://www.easy68k.com/EASy68Kforum/viewtopic.php?t=614)
2) How to write to a text file from a data register(http://www.easy68k.com/EASy68Kforum/vie ... light=file)

I've tried to modify the first one to accomplish the task with no luck

For the second one, it helped me to understand that in order to write to a file a 'buffer' is needed, however when i tried to test this code by
placing a value in a data register to see the outcome, the "test1.txt" file contained giberish

Any help would be appreciated, Thanks in advance

Regards


Top
 Profile  
 
 Post subject:
PostPosted: Sun Mar 13, 2011 11:18 pm 
Offline

Joined: Mon Jul 26, 2010 11:43 pm
Posts: 198
Location: Aurora, IL
Do you have any familiarity with file access in a high level language like C?

You do understand that if you write a register to memory, and then write that memory directly to a file that the data in the file will be in a binary form, not ASCII, right?

Is there a real need to write the memory to file? Your initial description seems to suggest you want to free up registers, writing to memory is quite sufficient to meet that need. You would write that to a file if you needed to load that exact data back at some later time. Perhaps you could take the user input from a file.

The first example you cite should be a pretty clear starting point for reading/writing memory. This would be the equivalent to a "game save", where you write a large block of state memory to a file, which can be reloaded later with very little effort, and used immediately.

If you want user readable ASCII, you'll have to convert your data into that format to save it, and write code the re-parse that data when you want to read it in again.


Last edited by clive on Mon Mar 14, 2011 1:00 pm, edited 1 time in total.

Top
 Profile  
 
 Post subject:
PostPosted: Sun Mar 13, 2011 11:56 pm 
Offline

Joined: Sat Mar 12, 2011 2:53 am
Posts: 9
Hi clive,

Thank you for your reply first of all.

Yes I’m somewhat familiar with file access in c and thanks to you i now understand that writing from memory will be in binary form.

About getting the user input from a file, this is an option provided to the user to choose between keyboard and file input

Once I’ve processed the values how i need to, all results are written to a file. For the purposes of my program, i do not wish to read in this data that
i have written to this file(it’s pretty much a 'report')

What i initial opted to do was to take each value back out of memory, when i needed it, and place it in a register then use the
code from my second citation to write to the file using the 'buffer' mentioned before. I assume this still would not write ASCII to file.(Please correct
me if I’m wrong).

how could i go about this conversion to ascii?


Top
 Profile  
 
 Post subject:
PostPosted: Mon Mar 14, 2011 4:22 pm 
Offline

Joined: Mon Jul 26, 2010 11:43 pm
Posts: 198
Location: Aurora, IL
If you are just generating a report, you would replace whatever code you use now for the screen "putchar" function, but instead use one more like "fputc" which sends the output to the file handle you have opened for the text data.

You'd need to add carriage return ($0D <CR>) and line feed ($0A <LF>) at the end of each line. Perhaps you'd want spaces, or commas to separate numeric output.

For HEX output I have the following routines, where _outchr is my character output subroutine/abstraction. These use a recursive/drop-through mechanism to reduce the code footprint. ie the LONG output is broken into two WORD outputs, etc. decomposing them to a single digit.

Code:
;------------------------------------------------------------------------------

_outcrlf
        move.l  d0,-(a7) ; Save D0
        move.b  #13,d0 ; Carriage Return
        bsr     _outchr
        move.b  #10,d0 ; Line Feed
        bsr     _outchr
        move.l  (a7)+,d0 ; Restore D0
        rts

;------------------------------------------------------------------------------

_outlng move.l  d0,-(a7)        ; Output LONG in D0 to UART2, Kills D0
        swap    d0
        bsr     _outwrd
        move.l  (a7)+,d0
_outwrd move.l  d0,-(a7)        ; Output WORD in D0 to UART2, Kills D0
        lsr.l   #8,d0
        bsr     _outbyt
        move.l  (a7)+,d0
_outbyt move.l  d0,-(a7)        ; Output BYTE in D0 to UART2, Kills D0
        lsr.l   #4,d0
        bsr     _outnbl
        move.l  (a7)+,d0
_outnbl andi.b  #$0F,d0         ; Output NIBBLE in D0 to UART2, Kills D0
        add.b   #$30,d0 ; '0'
        cmp.b   #$39,d0 ; '9'
        ble     _outchr
        add.b   #$7,d0 ; '0' + 10 + 7 = 'A'
        bra     _outchr

;------------------------------------------------------------------------------


Top
 Profile  
 
 Post subject:
PostPosted: Mon Mar 14, 2011 5:40 pm 
Offline

Joined: Mon Jul 26, 2010 11:43 pm
Posts: 198
Location: Aurora, IL
To convert a 16-bit binary number to a decimal string in a buffer.
Code:
           org     $400

* Program starts here

start
        move.l  #12345,d0
        lea     buffer,a0
        bsr     dec16

        bsr     outstr

   MOVE.B   #9,D0
   TRAP   #15      ; halt simulator

dec16: ; Convert 16-bit binary to decimal

;D0=binary number (0-65535)
;A0=string output address

    movem.l d0-d1/a0,-(a7) ;Save D0,D1,A0
    move.w  #0,-(a7)    ;Push NUL End-Of-Stack marker
loop1
    divu    #10,d0      ;D0:LOW = D0/10, D0:HIGH = D0%10
    move.l  d0,d1       ;Copy to split quotient and remainder
    swap    d1          ;Get remainder in D1:LOW
    add.w   #$30,d1     ;Add ASCII '0' to 0-9 digit
    move.w  d1,-(a7)    ;Push Digit, number built backward
    and.l   #$FFFF,d0   ;D0:HIGH = 0
    bne     loop1       ;until D0 = 0

; Now unstack reverse decimal built on stack

loop2
    move.w  (a7)+,d1    ;unstack next digit
    move.b  d1,(a0)+    ;copy into output string buffer
    bne     loop2       ;Until NUL unstacked

    movem.l (a7)+,d0-d1/a0 ;Restore D0,D1,A0
    rts

*------------------------------------
* Subroutine to display NUL terminated string

outstr   movem.l   d0/a0-a1,-(a7)
   move   #14,d0      task number into D0
   move   a0,a1
   trap   #15      display string
   movem.l   (a7)+,d0/a0-a1
   rts

*------------------------------------

buffer  ds.b    10

        end start


Now technically, the way the division works, this routine would support 32-bit numbers in the range 0 .. 655359


Top
 Profile  
 
 Post subject:
PostPosted: Mon Mar 14, 2011 8:56 pm 
Offline

Joined: Mon Jul 26, 2010 11:43 pm
Posts: 198
Location: Aurora, IL
Code:
*------------------------------------
* Convert 32-bit binary to decimal

;D0=binary number (0-4,294,967,295)
;A0=string output address

dec32   cmp.l     #655360,d0   ; Use 16-bit routine if in range
        bcs     dec16      ; unsigned less than 655360

   ; At least 6 significant digits
   
        movem.l d0-d3/a0,-(a7)    ; Save D0,D1,D2,D3,A0

        move.w  #0,-(a7)       ; Push NUL End-Of-Stack marker

        move.l  d0,d2      ; Save original D0 for remainder computation

   ; Improve 68K efficiency by using 16-bit divides, beware
   ;  the student that blindly copies this code. It was written
   ;  by someone who knows what they are doing.
   
   lsr.l   #1,d0      ; / 2
   move.w   #50000,d1   ;   50000
        divu    d1,d0      ; / 100000, ie (50000 * 2)   
   and.l   #$FFFF,d0   ; Mask quotient
   mulu   d0,d1      ; D1 = D0 * 50000
   sub.l   d1,d2      ; back out quotient twice for remainder
   sub.l   d1,d2      ; D2 -= D0 * 100000

   ; The preceeding sequence effectively performs a divide
   ;  beyond the scope of a single 16-bit hardware divide.
   
   ; D0 = D0 / 100000, D2 = D0 % 100000
      
   move.l   #5,d3          ; D3 = 5 Least significant digits
   
loop3   divu    #10,d2         ; D2:LOW = D2/10, D2:HIGH = D2%10
        move.l  d2,d1          ; Copy to split quotient and remainder
        swap    d1             ; Get remainder in D1:LOW
        add.w   #$30,d1        ; Add ASCII '0' to 0-9 digit
        move.w  d1,-(a7)       ; Push Digit, number built backward
        and.l   #$FFFF,d2      ; D2:HIGH = 0
        subq.w   #1,d3          ; D3--
        bne     loop3          ; until 5 digits

   ; Most significant 1-5 digits
            
loop4   divu    #10,d0         ; D0:LOW = D0/10, D0:HIGH = D0%10
        move.l  d0,d1          ; Copy to split quotient and remainder
        swap    d1             ; Get remainder in D1:LOW
        add.w   #$30,d1        ; Add ASCII '0' to 0-9 digit
        move.w  d1,-(a7)       ; Push Digit, number built backward
        and.l   #$FFFF,d0      ; D0:HIGH = 0
        bne     loop4          ; until D0 = 0

loop5   move.w  (a7)+,d1       ; unstack next digit
        move.b  d1,(a0)+       ; copy into output string buffer
        bne     loop5          ; Until NUL unstacked

        movem.l (a7)+,d0-d3/a0   ; Restore D0,D1,D2,D3,A0
        rts


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

All times are UTC


Who is online

Users browsing this forum: No registered users and 7 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