SHARE
TWEET

Untitled

a guest Oct 16th, 2019 93 Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
  1. # First section:  user data,
  2. # each process has its own "private" area.
  3.  
  4.         .data        
  5. glob1:  .byte   'A'
  6.         .space  999
  7. glob2:  .byte   '0'
  8.         .space  999
  9. glob3:  .byte   'a'
  10.         .space  999
  11.  
  12. # Second section: user code.
  13. #
  14. # The first instructions initializes IO and Timer.
  15. # The last instruction in main are only used for start
  16. # up, they initialize the first process.
  17.  
  18.         .text
  19.         .set noreorder
  20. main:
  21.         li    $t0, 0xFFFF0010   # address to Timer registers:
  22.                                 # +0: Timer control register
  23.                                 # +4: Timer count register
  24.                                 # +8: Timer compare register
  25.  
  26.         li  $t1, 0
  27.         sw  $t1, 4($t0)       # reset counter register
  28.  
  29.         li  $t1, 63           # count from 0 to 63
  30.         sw  $t1, 8($t0)       # compare register := 63
  31.  
  32.         li  $t1, 0b101001     # "101001": compare interrupt enable,
  33.                                 #           compare reset enable,
  34.                                 #           timer start
  35.         sw  $t1, 0($t0)       # control register := "101001"
  36.  
  37.         li  $t0, 0xFFFF0000   # address to I/O registers:
  38.                                 # +0: Input control register
  39.  
  40.         li  $t1, 0b10         # "10": interrupt enable
  41.         sw  $t1, 0($t0)       # control register := "10"
  42.  
  43.         li  $t0, 0x0C03       # enable HW Interrupt 1 (input): bit 11
  44.                                 # enable HW Interrupt 0 (timer): bit 10
  45.                                 # set user mode:     bit 1
  46.                                 # enable interrupts: bit 0
  47.  
  48.         mtc0  $t0, $12          # CP0 status := 0x0Cma03
  49.  
  50.         la      $gp, glob1        # dirty setup for process 1
  51.  
  52.    
  53.  
  54. proc1:  # ++++++++++ first process +++++++++
  55.  
  56.         # Description: proc1 reads the byte stored at 0($gp),
  57.         # prints it, increments to the next character,
  58.         # saving that back into 0($gp).  After printing 'Z',
  59.         # this process should then start over again with 'A', in
  60.         # an endless loop.  The symbol "glob1" may not be used.
  61.  
  62.  
  63.     lb  $t0, 0($gp) #Loads a byte
  64.     nop
  65.     ori $t1, $zero, 90  #Loads 90 into t0
  66.     move    $a0, $t0    #
  67.    
  68.     lui     $v0, 0x102
  69.     nop
  70.     syscall
  71.     nop
  72.     beq $t0, $t1, reset
  73.     nop
  74.  
  75. add:   
  76.     addiu   $t0, $t0, 1
  77.     sb  $t0, 0($gp)
  78.  
  79.         j proc1
  80.         nop
  81.  
  82. reset:
  83.     addiu   $t0, $t0, -25
  84.     sb  $t0, 0($gp)
  85.     j   proc1
  86.     nop
  87.  
  88.  
  89. proc2: # ++++++++++ second process +++++++++
  90.  
  91.         # Description: almost identical to the code of proc1 above,
  92.         # only 2-3 lines should differ.  Prints '0' through '9' in
  93.         # an endless loop. The symbol "glob2" may not be used.
  94.  
  95.  
  96.     lb  $t0, 0($gp) #Loads a byte
  97.     nop
  98.     ori $t1, $zero, 57  #Loads 90 into t0
  99.     move    $a0, $t0    #
  100.    
  101.     lui     $v0, 0x102
  102.     nop
  103.     syscall
  104.     nop
  105.     beq $t0, $t1, reset2   
  106.     nop
  107.  
  108. add2:  
  109.     addiu   $t0, $t0, 1
  110.     sb  $t0, 0($gp)
  111.  
  112.         j proc2
  113.         nop
  114.  
  115. reset2:
  116.     addiu   $t0, $t0, -9
  117.     sb  $t0, 0($gp)
  118.     j   proc2
  119.     nop
  120.  
  121.  
  122.  
  123. proc3:  # +++++++++ third process ++++++++++
  124.  
  125.         # Description: almost identical to the code of proc1 above,
  126.         # only 2-3 lines should differ.  Prints 'a' through 'z' in
  127.         # an endless loop. The symbol "glob3" may not be used.
  128.  
  129.  
  130.     lb  $t0, 0($gp) #Loads a byte
  131.     nop
  132.     ori $t1, $zero, 122 #Loads 90 into t0
  133.     move    $a0, $t0    #
  134.    
  135.     lui     $v0, 0x102
  136.     nop
  137.     syscall
  138.     nop
  139.     beq $t0, $t1, reset3   
  140.     nop
  141.  
  142. add3:  
  143.     addiu   $t0, $t0, 1
  144.     sb  $t0, 0($gp)
  145.  
  146.         j proc3
  147.         nop
  148.  
  149. reset3:
  150.     addiu   $t0, $t0, -25
  151.     sb  $t0, 0($gp)
  152.     j   proc3
  153.     nop
  154.  
  155.  
  156. # Third section: data structures for the kernel:
  157. # Process Control Block (PCB) consists of three words:
  158. # pcb: .word  (next Program Counter for this process)
  159. #      .word  (contents of $gp for this process)
  160. #      .word  (contents of $sp for this process)
  161. # All other context is saved on the process' own stack
  162. # during exception handling and SYSCALL.
  163.  
  164.         .section .kdata
  165. curpcb: .word  pcb1
  166. pcb1:   .word  0, 0, 0
  167. pcb2:   .word  proc2, glob2, 0x7fffbf94
  168. pcb3:   .word  proc3, glob3, 0x7fff7f94
  169.  
  170. # Fourth section: kernel code.
  171.  
  172.         .section .ktext , "xa"
  173.         .set noreorder
  174.  
  175. kernel_loop:
  176.  
  177.     mfc0    $t2, $13
  178.     nop
  179.     ori     $t5, $zero, 8
  180.     sll $t2, $t2, 26
  181.     srl $t2, $t2, 28
  182.    
  183.  
  184.     beq $t2, $t5, check
  185.     nop
  186.     beq $zero, $t2, exc
  187.     nop
  188.     b kernel_loop
  189.     nop
  190. check:
  191.     lui $t3, 0x102
  192.     nop
  193.     beq $v0, $t3, back
  194.     nop
  195.     rfe
  196.     nop
  197.  
  198. back:
  199.     lui $t4, 0xFFFF
  200.     sb  $a0, 8($t4)
  201.     nop
  202.     nop
  203.     mfc0    $k0, $14
  204.     nop
  205.  
  206.     addiu   $k0, $k0, 4
  207.     jr  $k0
  208.     rfe
  209.  
  210. exc:
  211.     ori $t3, $zero, 1
  212.     mfc0    $t2, $13
  213.     sll $t2, $t2, 20
  214.     srl $t2, $t2, 31
  215.    
  216.     beq $t2, $t3, timerint
  217.     nop
  218.     lw $s5, curpcb
  219.     nop
  220.     nop
  221.     b kernel_loop
  222.     nop
  223.  
  224.  
  225. timerint:
  226.     #loads in the current stack pointer
  227.     lw $s5, curpcb
  228.     nop
  229.     la  $s0, curpcb
  230.     nop
  231.     lw  $s1, ($s0)
  232.     nop
  233.    
  234. ignore:
  235.     mfc0    $k1, $14
  236.     nop
  237.     move    $ra, $k1
  238.     sw  $k1, 0($s1)
  239.     sw  $gp, 4($s1)
  240.     sw  $sp, 8($s1) #FUCK IT ALL
  241.     addiu $sp, $sp, -4
  242.     sw  $ra, 0($sp)
  243.     addiu $sp, $sp, -4
  244.     sw  $fp, 0($sp)
  245.     move $fp, $sp
  246.     #addiu  $sp, $sp, -104
  247.     sw  $gp, -4($fp)
  248.     sw  $s0, -8($fp)
  249.  
  250.     sw  $s1, -12($fp)
  251.     sw  $s2, -16($fp)
  252.     sw  $s3, -20($fp)
  253.     sw  $s4, -24($fp)
  254.     sw  $s5, -28($fp)
  255.     sw  $s6, -32($fp)
  256.     sw  $s7, -36($fp)
  257.  
  258.     sw  $t0, -40($fp)
  259.     sw  $t1, -44($fp)
  260.     sw  $t2, -48($fp)
  261.     sw  $t3, -52($fp)
  262.     sw  $t4, -56($fp)
  263.     sw  $t5, -60($fp)
  264.     sw  $t6, -64($fp)
  265.     sw  $t7, -68($fp)
  266.     sw  $t8, -72($fp)
  267.     sw  $t9, -76($fp)
  268.  
  269.     sw  $a0, -80($fp)
  270.     sw  $a1, -84($fp)
  271.     sw  $a2, -88($fp)
  272.     sw  $a3, -92($fp)
  273.  
  274.     sw  $v0, -96($fp)
  275.     sw  $v1, -100($fp)
  276.  
  277.     .set noat
  278.         sw  $at, -104($fp) # FIX
  279.     .set at
  280.     nop
  281.     la $s2, pcb2
  282.     lw $s7, ($s2)
  283.     la $s4, pcb3
  284.     nop
  285.     beq $s1, $s2, pcb2_3
  286.     nop
  287.     beq $s1, $s4, pcb3_1
  288.     nop
  289.     b pcb1_2
  290.     nop
  291.  
  292. pcb3_1:
  293.     la  $s3, pcb1
  294.     #move $s0, $s3
  295.     sw  $s3, ($s0)
  296.     lw  $gp, 4($s3)
  297.     nop
  298.     b   load
  299.     nop
  300.  
  301. pcb1_2:
  302.     la  $s3, pcb2
  303.     #move $s0, $s3
  304.     nop
  305.     sw  $s3, ($s0)
  306.     nop
  307.     la $s6, curpcb
  308.     nop
  309.     lw $s5, curpcb
  310.     nop
  311.     lw $gp, 4($s3)
  312.     nop
  313.     b load
  314.     nop
  315.  
  316. pcb2_3:
  317.     la  $s3, pcb3
  318.     #move $s0, $s3
  319.     sw  $s3, ($s0)
  320.     lw $gp, 4($s3)
  321.     nop
  322. load:
  323.    
  324.     #Clear Timer Interrupt
  325.     li      $k0, 0xFFFF0010
  326.     li      $k1, 0b101001
  327.     sw      $k1, 0($k0)
  328.  
  329.     #---------------------
  330.     lw  $sp, 8($s3)
  331.     lw  $ra, 0($s3)
  332.     #addiu  $sp, $sp, 104
  333.     move $fp, $sp
  334.     addiu $fp, $fp, -8
  335.  
  336.     sw $gp, -4($fp)
  337.     sw $s0, -8($fp)
  338.  
  339.     lw  $gp, -4($fp)
  340.     lw  $s0, -8($fp)
  341.  
  342.     lw  $s1, -12($fp)
  343.     lw  $s2, -16($fp)
  344.     lw  $s3, -20($fp)
  345.     lw  $s4, -24($fp)
  346.     lw  $s5, -28($fp)
  347.     lw  $s6, -32($fp)
  348.     lw  $s7, -36($fp)
  349.  
  350.     lw  $t0, -40($fp)
  351.     lw  $t1, -44($fp)
  352.     lw  $t2, -48($fp)
  353.     lw  $t3, -52($fp)
  354.     lw  $t4, -56($fp)
  355.     lw  $t5, -60($fp)
  356.     lw  $t6, -64($fp)
  357.     lw  $t7, -68($fp)
  358.     lw  $t8, -72($fp)
  359.     lw  $t9, -76($fp)
  360.  
  361.     lw  $a0, -80($fp)
  362.     lw  $a1, -84($fp)
  363.     lw  $a2, -88($fp)
  364.     lw  $a3, -92($fp)
  365.     lw  $v0, -96($fp)
  366.     lw  $v1, -100($fp)
  367.  
  368.     .set noat
  369.         lw  $at, -104($fp) # FIX
  370.         nop
  371.     .set at
  372.    
  373.     lw $s5, curpcb
  374.     nop
  375.     jr $ra
  376.     rfe
RAW Paste Data
We use cookies for various purposes including analytics. By continuing to use Pastebin, you agree to our use of cookies as described in the Cookies Policy. OK, I Understand
 
Top