home *** CD-ROM | disk | FTP | other *** search
/ Crawly Crypt Collection 1 / crawlyvol1.bin / program / books / 68k_book / arp_src / prg_6kc.s < prev    next >
Text File  |  1985-11-20  |  4KB  |  112 lines

  1.  ; PROGRAM NAME: PRG_6KC.S
  2.  ;      VERSION: 1.001
  3.  
  4.  ; The function of this program is identical to that of PRG_6JC.S.  I am
  5.  ; including it because, in PRG_6JC's trap handler, I used an algorithm to
  6.  ; access stack data which you are most likely to see in references.  In this
  7.  ; program I show you another method, the method that I prefer. 
  8.  
  9. program_start:                  ; Compute program size and retain result.
  10.  lea        program_end(pc), a3 ; Fetch program end address.
  11.  movea.l    4(a7), a4           ; Fetch basepage address.
  12.  suba.l     a4, a3              ; Yields size of memory that must remain
  13.                                 ; resident.
  14. load_stack_address:
  15.  lea        stack(pc), a7
  16.  
  17. install_new_trap_13_vector:
  18.  pea       custom_trap_handler(pc)
  19.  move.w    #$2D, -(sp)          ; Trap 13 vector number.
  20.  move.w    #5, -(sp)            ; Function = setexec = BIOS $5.
  21.  trap      #13                  ; Current trap handler vector returned in D0.
  22.  addq.l    #8, sp
  23.  move.l    d0, preempted_handler_address
  24.  
  25. relinquish_processor_control:   ; Maintain memory residency.
  26.  move.w    #0, -(sp)            ; See page 121 of Internals book.
  27.  move.l    a3, -(sp)            ; Size of memory to remain resident.
  28.  move.w    #$31, -(sp)          ; Function = ptermres = GEMDOS $31.
  29.  trap      #1
  30.  
  31.  ; Here I illustrate another way to adjust one of the stack reference pointers
  32.  ; so that common offset values may be used to access stack data.  In this
  33.  ; program the value six is added to the register that is used to access the
  34.  ; data if the processor was in supervisor mode before the invocation.
  35.  
  36. custom_trap_handler:
  37.  tst.b     initialization_flag
  38.  bne       skip_initialization
  39.  move.l    usp, a0              ; Load address of current top of user stack.
  40.  
  41. get_processor_status:
  42.  btst      #5, (sp)             ; User mode test.
  43.  beq.s     was_user_mode        ; No adjustment is necessary if the
  44.                                 ; processor was in user mode.
  45.  movea.l   sp, a0               ; Load current top of supervisor stack.
  46.  addq.l    #6, a0               ; Adjust SSP for user mode type data access.
  47.  
  48. was_supervisor_mode:
  49. was_user_mode:                  ; Processing for either mode follows.
  50.  cmpi.w    #3, (a0)             ; Writing a character to a device?
  51.  bne.s     not_bconout_call
  52.  cmpi.w    #2, 2(a0)            ; Is device the screen?
  53.  bne.s     not_screen
  54.  
  55. esc_sequence_test:
  56.  tst.b     esc_sequence_flag
  57.  bne.s     reset_esc_sequence_flag
  58.  cmpi.w    #$1B, 4(a0)           
  59.  bne.s     not_esc_sequence
  60.  move.b    #1, esc_sequence_flag
  61.  bra.s     use_preempted_handler
  62. reset_esc_sequence_flag:
  63.  move.b    #0, esc_sequence_flag
  64. use_preempted_handler:
  65.  movea.l   preempted_handler_address(pc), a0
  66.  jmp       (a0)                 ; JUMP TO PREEMPTED TRAP #13 HANDLER.
  67.  
  68. not_esc_sequence:
  69.  move.b    #1, initialization_flag
  70.  move.w    4(a0), character     ; Store character for printer.
  71. write_character_to_screen:
  72.  move.w    4(a0), -(sp)         ; Push character onto stack.
  73.  move.w    #2, -(sp)            ; Device = screen.
  74.  move.w    #3, -(sp)            ; Function = bconout = BIOS $3.
  75.  trap      #13
  76.  addq.l    #6, sp
  77.  
  78. ascii_code_test:                ; Filter out undesirable codes.
  79.  move.w    character(pc), d0
  80.  cmpi.w    #$1B, d0
  81.  bgt.s     write_character_to_printer
  82.  cmpi.w    #$A, d0
  83.  beq.s     write_character_to_printer
  84.  cmpi.w    #$D, d0
  85.  bne.s     undesirable_ascii
  86. write_character_to_printer:
  87.  move.w    d0, -(sp)            ; Push character onto stack.
  88.  move.w    #0, -(sp)            ; Device = printer.
  89.  move.w    #3, -(sp)
  90.  trap      #13
  91.  addq.l    #6, sp
  92. undesirable_ascii:
  93.  move.b    #0, initialization_flag
  94.  rte
  95.  
  96. not_screen:
  97. not_bconout_call:
  98. skip_initialization:
  99.  movea.l   preempted_handler_address(pc), a0
  100.  jmp       (a0)                 ; JUMP TO PREEMPTED TRAP #13 HANDLER
  101.  
  102.  bss
  103. character:                  ds.w    1
  104. preempted_handler_address:  ds.l    1
  105. esc_sequence_flag:          ds.b    1
  106. initialization_flag:        ds.b    1
  107.  align
  108.                             ds.l   48    ; Stack
  109. stack:                      ds.l    1    ; Address of stack.
  110. program_end:                ds.l    0
  111.  end
  112.