Advertisement
Guest User

Untitled

a guest
Oct 15th, 2019
84
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 6.62 KB | None | 0 0
  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.  
  218. nop
  219. b kernel_loop
  220. nop
  221.  
  222.  
  223. timerint:
  224. #loads in the current stack pointer
  225. la $s0, curpcb
  226. nop
  227. #lw $s0, ($s0)
  228. nop
  229.  
  230. ignore:
  231. mfc0 $k1, $14
  232. nop
  233. move $ra, $k1
  234. sw $k1, 0($s0)
  235. sw $gp, 4($s0)
  236.  
  237. addiu $sp, $sp, -4
  238. sw $ra, 0($sp)
  239. addiu $sp, $sp, -4
  240. sw $fp, 0($sp)
  241. move $fp, $sp
  242. addiu $sp, $sp, -104
  243. sw $gp, -4($fp)
  244. sw $s0, -8($fp)
  245.  
  246. sw $s1, -12($fp)
  247. sw $s2, -16($fp)
  248. sw $s3, -20($fp)
  249. sw $s4, -24($fp)
  250. sw $s5, -28($fp)
  251. sw $s6, -32($fp)
  252. sw $s7, -36($fp)
  253.  
  254. sw $t0, -40($fp)
  255. sw $t1, -44($fp)
  256. sw $t2, -48($fp)
  257. sw $t3, -52($fp)
  258. sw $t4, -56($fp)
  259. sw $t5, -60($fp)
  260. sw $t6, -64($fp)
  261. sw $t7, -68($fp)
  262. sw $t8, -72($fp)
  263. sw $t9, -76($fp)
  264.  
  265. sw $a0, -80($fp)
  266. sw $a1, -84($fp)
  267. sw $a2, -88($fp)
  268. sw $a3, -92($fp)
  269.  
  270. sw $v0, -96($fp)
  271. sw $v1, -100($fp)
  272.  
  273. .set noat
  274. sw $at, -104($fp) # FIX
  275. .set at
  276.  
  277. la $s2, pcb2
  278. la $s4, pcb3
  279. lw $s5, ($s0)
  280. nop
  281. beq $s5, $s2, pcb2_3
  282. nop
  283. beq $s5, $s4, pcb3_1
  284. nop
  285. b pcb1_2
  286. nop
  287.  
  288. pcb3_1:
  289. la $s3, pcb1
  290. #move $s0, $s3
  291. sw $s3, ($s0)
  292. lw $gp, 4($s3)
  293. nop
  294. b load
  295. nop
  296.  
  297. pcb1_2:
  298. la $s3, pcb2
  299. #move $s0, $s3
  300. sw $s3, ($s0)
  301. lw $gp, 4($s3)
  302. nop
  303. b load
  304. nop
  305.  
  306. pcb2_3:
  307. la $s3, pcb3
  308. #move $s0, $s3
  309. sw $s3, ($s0)
  310. lw $gp, 4($s3)
  311. nop
  312. load:
  313.  
  314. #Clear Timer Interrupt
  315. li $k0, 0xFFFF0010
  316. li $k1, 0b101001
  317. sw $k1, 0($k0)
  318.  
  319. #---------------------
  320. lw $sp, 8($s3)
  321. lw $ra, 0($s3)
  322.  
  323. move $fp, $sp
  324. addiu $fp, $fp, -8
  325.  
  326. sw $gp, 4($fp)
  327. sw $s0, -4($fp)
  328.  
  329. lw $gp, 4($fp)
  330. lw $s0, -4($fp)
  331.  
  332. lw $s1, -8($fp)
  333. lw $s2, -12($fp)
  334. lw $s3, -16($fp)
  335. lw $s4, -20($fp)
  336. lw $s5, -24($fp)
  337. lw $s6, -28($fp)
  338. lw $s7, -32($fp)
  339.  
  340. lw $t0, -36($fp)
  341. lw $t1, -40($fp)
  342. lw $t2, -44($fp)
  343. lw $t3, -48($fp)
  344. lw $t4, -52($fp)
  345. lw $t5, -56($fp)
  346. lw $t6, -60($fp)
  347. lw $t7, -64($fp)
  348. lw $t8, -68($fp)
  349. lw $t9, -72($fp)
  350.  
  351. lw $a0, -76($fp)
  352. lw $a1, -80($fp)
  353. lw $a2, -84($fp)
  354. lw $a3, -88($fp)
  355. lw $v0, -92($fp)
  356. lw $v1, -96($fp)
  357.  
  358. .set noat
  359. lw $at, -104($fp) # FIX
  360. nop
  361. .set at
  362.  
  363. jr $ra
  364. rfe
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement