next up previous contents

[ENGN3213 Home]

Text-based I/O via dBug Monitor

Text-based i/o is an important means for communication. We have seen several examples, where we used the C function printf. Here we will look more closely at text i/o by studying some elementary subroutines which make use of dBug functions via the trap mechanism.

Download the assembly program chario1s.s:

/*    chario1s.s
      basic character i/o
      putac - prints a char, using dBug trap function
      getac - gets a char, using dBug trap function

*/

      .text
      .even
      .global     main
main:
      move.l      #'a',%d1                /* print char 'a' */
      jsr         putac

      jsr         getac                   /* get a char */
      jsr         putac                   /* echo it */

      move.l      #LINEFEED,%d1           /* print char LINEFEED = \n */
      jsr         putac

      move.l      #CARR_RTN,%d1           /* print char CARR_RTN */
      jsr         putac

      pea    word                         /* print a string using C printf */
      jsr printf
      addq.l #4,%a7                       /* clean stack */

      rts

putac:                              /* prints a char, expects char in d1 */
      move.l      %d0,-(%a7)        /* save d0 on stack */
      move.l      #0x0013,%d0       /* dbug out_char trap code */
      trap        #15
      move.l      (%a7)+,%d0        /* restore registers */
      rts

getac:                              /* gets a char, returns in d1 */
      move.l      %d0,-(%a7)        /* save d0 on stack */
get2: move.l      #0x0014,%d0       /* dbug char_present trap code */
      trap        #15
      btst        #0,%d0            /* d0 not equal to 0 means char present */
      bne         get2              /* keep polling until char present */
      move.l      #0x0010,%d0       /* dbug in_char trap code */
      trap        #15
      move.l      (%a7)+,%d0        /* restore d0 */
      rts

LINEFEED        = 10
CARR_RTN = 13                    

word:    .ascii  "Done!\n\0"

This program has two subroutines for putting and getting characters (they do not use the C functions putchar, getchar), and also printf is called. Notice that getac uses the technique of polling to determine when a character is available.

Exercise.

1.
Assemble and link the program.

2.
Load the program chario1s.x into the SBC.

3.
Run the code and check for correct operation:
go 10000

4.
What does the program do?

5.
How are parameters passed to each subroutine?


next up previous contents

[ENGN3213 Home]

ANU Engineering - ENGN3213