Example of File I/O

The sample program below will open a new file for writing, write text to it from a memory buffer, then close it. The file will be created in the directory in which RARS was run.

# Sample program that writes to a new file.
#   by Kenneth Vollmar and Pete Sanderson

        .data
fout:   .asciz "testout.txt"      # filename for output
buffer: .asciz "The quick brown fox jumps over the lazy dog."
        .text
  ###############################################################
  # Open (for writing) a file that does not exist
  li   a7, 1024     # system call for open file
  la   a0, fout     # output file name
  li   a1, 1        # Open for writing (flags are 0: read, 1: write)
  ecall             # open a file (file descriptor returned in a0)
  mv   s6, a0       # save the file descriptor
  ###############################################################
  # Write to file just opened
  li   a7, 64       # system call for write to file
  mv   a0, s6       # file descriptor
  la   a1, buffer   # address of buffer from which to write
  li   a2, 44       # hardcoded buffer length
  ecall             # write to file
  ###############################################################
  # Close the file
  li   a7, 57       # system call for close file
  mv   a0, s6       # file descriptor to close
  ecall             # close file
  ###############################################################


Using MIDI output

These system services are unique to RARS, and provide a means of producing sound. MIDI output is simulated by your system sound card, and the simulation is provided by the javax.sound.midi package.

This service requires four parameters as follows:

pitch (a0)

  • Accepts a positive byte value (0-127) that denotes a pitch as it would be represented in MIDI
  • Each number is one semitone / half-step in the chromatic scale.
  • 0 represents a very low C and 127 represents a very high G (a standard 88 key piano begins at 9-A and ends at 108-C).
  • If the parameter value is outside this range, it applies a default value 60 which is the same as middle C on a piano.
  • From middle C, all other pitches in the octave are as follows:
  • 61 = C# or Db
  • 62 = D
  • 63 = D# or Eb
  • 64 = E or Fb
  • 65 = E# or F
  • 66 = F# or Gb
  • 67 = G
  • 68 = G# or Ab
  • 69 = A
  • 70 = A# or Bb
  • 71 = B or Cb
  • 72 = B# or C
  • To produce these pitches in other octaves, add or subtract multiples of 12.

  • duration in milliseconds (a1)

  • Accepts a positive integer value that is the length of the tone in milliseconds.
  • If the parameter value is negative, it applies a default value of one second (1000 milliseconds).

  • instrument (a2)

  • Accepts a positive byte value (0-127) that denotes the General MIDI "patch" used to play the tone.
  • If the parameter is outside this range, it applies a default value 0 which is an Acoustic Grand Piano.
  • General MIDI standardizes the number associated with each possible instrument (often referred to as program change numbers), however it does not determine how the tone will sound. This is determined by the synthesizer that is producing the sound. Thus a Tuba (patch 58) on one computer may sound different than that same patch on another computer.
  • The 128 available patches are divided into instrument families of 8:
  • 0-7 Piano 64-71 Reed
    8-15 Chromatic Percussion 72-79 Pipe
    16-23 Organ 80-87 Synth Lead
    24-31 Guitar 88-95 Synth Pad
    32-39 Bass 96-103 Synth Effects
    40-47 Strings 104-111 Ethnic
    48-55 Ensemble 112-119 Percussion
    56-63 Brass 120-127 Sound Effects
  • Note that outside of Java, General MIDI usually refers to patches 1-128. When referring to a list of General MIDI patches, 1 must be subtracted to play the correct patch. For a full list of General MIDI instruments, see www.midi.org/about-midi/gm/gm1sound.shtml. The General MIDI channel 10 percussion key map is not relevant to the toneGenerator method because it always defaults to MIDI channel 1.

  • volume (a3)

  • Accepts a positive byte value (0-127) where 127 is the loudest and 0 is silent. This value denotes MIDI velocity which refers to the initial attack of the tone.
  • If the parameter value is outside this range, it applies a default value 100.
  • MIDI velocity measures how hard a note on (or note off) message is played, perhaps on a MIDI controller like a keyboard. Most MIDI synthesizers will translate this into volume on a logarithmic scale in which the difference in amplitude decreases as the velocity value increases.
  • Note that velocity value on more sophisticated synthesizers can also affect the timbre of the tone (as most instruments sound different when they are played louder or softer).
  • MIDI Output was developed and documented by Otterbein student Tony Brock in July 2007.