《操作系统》的实验代码。
選択できるのは25トピックまでです。 トピックは、先頭が英数字で、英数字とダッシュ('-')を使用した35文字以内のものにしてください。

53 行
1.2 KiB

  1. # array of 2 integers (each size 4 bytes)
  2. # load address of flag into fx register
  3. # access flag[] with 0(%fx,%index,4)
  4. # where %index is a register holding 0 or 1
  5. # index reg contains 0 -> flag[0], if 1->flag[1]
  6. .var flag 2
  7. # global turn variable
  8. .var turn
  9. # global count
  10. .var count
  11. .main
  12. # put address of flag into fx
  13. lea flag, %fx
  14. # assume thread ID is in bx (0 or 1, scale by 4 to get proper flag address)
  15. mov %bx, %cx # bx: self, now copies to cx
  16. neg %cx # cx: - self
  17. add $1, %cx # cx: 1 - self
  18. .acquire
  19. mov $1, 0(%fx,%bx,4) # flag[self] = 1
  20. mov %cx, turn # turn = 1 - self
  21. .spin1
  22. mov 0(%fx,%cx,4), %ax # flag[1-self]
  23. test $1, %ax
  24. jne .fini # if flag[1-self] != 1, skip past loop to .fini
  25. .spin2 # just labeled for fun, not needed
  26. mov turn, %ax
  27. test %cx, %ax # compare 'turn' and '1 - self'
  28. je .spin1 # if turn==1-self, go back and start spin again
  29. # fall out of spin
  30. .fini
  31. # do critical section now
  32. mov count, %ax
  33. add $1, %ax
  34. mov %ax, count
  35. .release
  36. mov $0, 0(%fx,%bx,4) # flag[self] = 0
  37. # end case: make sure it's other's turn
  38. mov %cx, turn # turn = 1 - self
  39. halt