Public paste
STACKS
By: microhaxo | Date: Jul 4 2009 03:15 | Format: None | Expires: never | Size: 3.25 KB | Hits: 1094

  1. ! Not So Simple Typing Program
  2. ! Blake Haas
  3.  
  4.         .begin
  5.  
  6. ! Constants
  7. BASE                    .equ 0x3fffc0   ! Starting point of the memory mapped region
  8. CONSOLE                 .equ 0x0                ! 0xffff0000 Console Data Port
  9. CONSOLE_STATUS  .equ 0x4                ! 0xffff0004 Console Status Port (-0x80 = Not Ready)
  10. KEYBOARD                .equ 0x8                ! 0xffff0008 Keyboard in port
  11. KEYBOARD_STATUS .equ 0xc                ! 0xffff000c Keyboard status port
  12.  
  13. ! Push the contents of a register onto the stack.
  14. .macro PUSH arg1
  15.         st arg1, %r14                           ! Store the value
  16.         sub %r14, 4, %r14                       ! Decrement the stack pointer
  17. .endmacro
  18.  
  19. ! Pop whatever is on the top of the stack and put it in arg1
  20. .macro POP arg1
  21.         add %r14, 4, %r14                       ! Incremement the stack
  22.         ld %r14, arg1                           ! Get the value
  23. .endmacro
  24. .macro POPESC arg1
  25.         add %r17, 4, %r17                       ! Incremement the stack
  26.         ld %r17, arg1                           ! Get the value
  27. .endmacro
  28.  
  29. .macro PUSHESC arg1
  30.         st arg1, %r17                           ! Store the value
  31.         sub %r14, 4, %r17                       ! Decrement the stack pointer
  32. .endmacro
  33.  
  34. .macro RETURN
  35.         jmpl %r15 + 4, %r0
  36. .endmacro
  37.  
  38.  
  39. !-------------------------------------------------------
  40. ! Start
  41. !-------------------------------------------------------
  42.         .org 2048
  43.                 mov %r0,%r14
  44.                 ld [SP], %r14           ! Load the address of our string onto the stack
  45.  
  46. MAIN_LOOP:
  47.                 call READ_CHAR
  48.                 POP %r1
  49.                 subcc %r1, 0x1B, %r0    ! Compare with ESC
  50.                 be QUIT
  51.  
  52.                 PUSH %r1
  53.                 call PRINT_CHAR
  54.                 ba MAIN_LOOP
  55. QUIT:
  56.                 addcc %r0, %r10, %r6
  57.         loop:  
  58.                                                                                 ! Grab the char to print
  59.                 call PRINT_CHAR_WAIT
  60.                 subcc %r6, 1, %r6
  61.                 bne loop
  62.                 halt
  63. !-------------------------------------------------------
  64.  
  65.  
  66.  
  67. !-------------------------------------------------------
  68. ! READ_CHAR FUNCTION
  69. ! Reads a single char from the input console
  70. ! RETURN = Char Found
  71. !-------------------------------------------------------
  72. READ_CHAR:
  73.                                                                                         ! We never call out, so %r15 is safe- don't need to store it
  74.  
  75.         add %r0, %r0, %r4                                               ! %r4 Holds the base address
  76.         sethi BASE, %r4                                                 !
  77.  
  78. READ_CHAR_WAIT:        
  79.                 ldub [%r4 + KEYBOARD_STATUS], %r1       ! Get the status
  80.                 andcc %r1, 0x80, %r1                            ! Add 0x80 - if we're ready, this is not zero
  81.                 be READ_CHAR_WAIT                                       ! Loop
  82.  
  83.                 ldub [%r4 + KEYBOARD], %r3                      ! Get the char
  84.  
  85.         PUSH %r3
  86.  
  87.        
  88.                                                 ! Push the result on the stack and...
  89.         RETURN                                                                  ! Return
  90. !-------------------------------------------------------
  91.  
  92. !-------------------------------------------------------
  93. ! PRINT_CHAR FUNCTION
  94. ! Prints a single char to the output console
  95. ! ARG1 = Char to print
  96. ! RETURN = Nothing
  97. !-------------------------------------------------------
  98. PRINT_CHAR:
  99.         POP %r1                                                         ! Grab the char to print
  100.                                                                         ! We never call out, so we can assume %r15 stays put
  101.  
  102.          add %r0, %r0, %r4                                      ! %r4 Holds the base address
  103.          sethi BASE, %r4                                                !
  104.        
  105.  
  106. PRINT_CHAR_WAIT:                                                ! We have to wait for the console to be ready!
  107.         ldub [%r4+ CONSOLE_STATUS], %r5
  108.                                                                                 ! Load the console status (as an unsigned byte) into %r5
  109.         andcc %r5, 0x80, %r5                            ! -0x80 = no console not ready, so add 0x80 to the value and compare against zero
  110.         be PRINT_CHAR_WAIT                                      ! Loop while we're not ready
  111.  
  112.         stb %r1, [%r4 + CONSOLE]                                                ! We're ready - put the char to print into the console memory location
  113.         add %r10, 1, %r10
  114.        
  115.         RETURN
  116.  
  117. !-------------------------------------------------------
  118.  
  119. SP: 0x7FFFFFFC         
  120.  
  121.         .end