:add lab7 spoc exercise related codes
This commit is contained in:
27
related_info/lab7/software-hardware-locks/flag.s
Normal file
27
related_info/lab7/software-hardware-locks/flag.s
Normal file
@@ -0,0 +1,27 @@
|
||||
.var flag
|
||||
.var count
|
||||
|
||||
.main
|
||||
.top
|
||||
|
||||
.acquire
|
||||
mov flag, %ax # get flag
|
||||
test $0, %ax # if we get 0 back: lock is free!
|
||||
jne .acquire # if not, try again
|
||||
mov $1, flag # store 1 into flag
|
||||
|
||||
# critical section
|
||||
mov count, %ax # get the value at the address
|
||||
add $1, %ax # increment it
|
||||
mov %ax, count # store it back
|
||||
|
||||
# release lock
|
||||
mov $0, flag # clear the flag now
|
||||
|
||||
# see if we're still looping
|
||||
sub $1, %bx
|
||||
test $0, %bx
|
||||
jgt .top
|
||||
|
||||
halt
|
||||
|
||||
53
related_info/lab7/software-hardware-locks/peterson.s
Normal file
53
related_info/lab7/software-hardware-locks/peterson.s
Normal file
@@ -0,0 +1,53 @@
|
||||
# array of 2 integers (each size 4 bytes)
|
||||
# load address of flag into fx register
|
||||
# access flag[] with 0(%fx,%index,4)
|
||||
# where %index is a register holding 0 or 1
|
||||
# index reg contains 0 -> flag[0], if 1->flag[1]
|
||||
.var flag 2
|
||||
|
||||
# global turn variable
|
||||
.var turn
|
||||
|
||||
# global count
|
||||
.var count
|
||||
|
||||
.main
|
||||
|
||||
# put address of flag into fx
|
||||
lea flag, %fx
|
||||
|
||||
# assume thread ID is in bx (0 or 1, scale by 4 to get proper flag address)
|
||||
mov %bx, %cx # bx: self, now copies to cx
|
||||
neg %cx # cx: - self
|
||||
add $1, %cx # cx: 1 - self
|
||||
|
||||
.acquire
|
||||
mov $1, 0(%fx,%bx,4) # flag[self] = 1
|
||||
mov %cx, turn # turn = 1 - self
|
||||
|
||||
.spin1
|
||||
mov 0(%fx,%cx,4), %ax # flag[1-self]
|
||||
test $1, %ax
|
||||
jne .fini # if flag[1-self] != 1, skip past loop to .fini
|
||||
|
||||
.spin2 # just labeled for fun, not needed
|
||||
mov turn, %ax
|
||||
test %cx, %ax # compare 'turn' and '1 - self'
|
||||
je .spin1 # if turn==1-self, go back and start spin again
|
||||
|
||||
# fall out of spin
|
||||
.fini
|
||||
|
||||
# do critical section now
|
||||
mov count, %ax
|
||||
add $1, %ax
|
||||
mov %ax, count
|
||||
|
||||
.release
|
||||
mov $0, 0(%fx,%bx,4) # flag[self] = 0
|
||||
|
||||
|
||||
# end case: make sure it's other's turn
|
||||
mov %cx, turn # turn = 1 - self
|
||||
halt
|
||||
|
||||
26
related_info/lab7/software-hardware-locks/test-and-set.s
Normal file
26
related_info/lab7/software-hardware-locks/test-and-set.s
Normal file
@@ -0,0 +1,26 @@
|
||||
.var mutex
|
||||
.var count
|
||||
|
||||
.main
|
||||
.top
|
||||
|
||||
.acquire
|
||||
mov $1, %ax
|
||||
xchg %ax, mutex # atomic swap of 1 and mutex
|
||||
test $0, %ax # if we get 0 back: lock is free!
|
||||
jne .acquire # if not, try again
|
||||
|
||||
# critical section
|
||||
mov count, %ax # get the value at the address
|
||||
add $1, %ax # increment it
|
||||
mov %ax, count # store it back
|
||||
|
||||
# release lock
|
||||
mov $0, mutex
|
||||
|
||||
# see if we're still looping
|
||||
sub $1, %bx
|
||||
test $0, %bx
|
||||
jgt .top
|
||||
|
||||
halt
|
||||
@@ -0,0 +1,29 @@
|
||||
.var mutex
|
||||
.var count
|
||||
|
||||
.main
|
||||
.top
|
||||
|
||||
.acquire
|
||||
mov mutex, %ax
|
||||
test $0, %ax
|
||||
jne .acquire
|
||||
mov $1, %ax
|
||||
xchg %ax, mutex # atomic swap of 1 and mutex
|
||||
test $0, %ax # if we get 0 back: lock is free!
|
||||
jne .acquire # if not, try again
|
||||
|
||||
# critical section
|
||||
mov count, %ax # get the value at the address
|
||||
add $1, %ax # increment it
|
||||
mov %ax, count # store it back
|
||||
|
||||
# release lock
|
||||
mov $0, mutex
|
||||
|
||||
# see if we're still looping
|
||||
sub $1, %bx
|
||||
test $0, %bx
|
||||
jgt .top
|
||||
|
||||
halt
|
||||
30
related_info/lab7/software-hardware-locks/ticket.s
Normal file
30
related_info/lab7/software-hardware-locks/ticket.s
Normal file
@@ -0,0 +1,30 @@
|
||||
.var ticket
|
||||
.var turn
|
||||
.var count
|
||||
|
||||
.main
|
||||
.top
|
||||
|
||||
.acquire
|
||||
mov $1, %ax
|
||||
fetchadd %ax, ticket # grab a ticket (keep it in dx)
|
||||
.tryagain
|
||||
mov turn, %cx # check if it's your turn
|
||||
test %cx, %ax
|
||||
jne .tryagain
|
||||
|
||||
# critical section
|
||||
mov count, %ax # get the value at the address
|
||||
add $1, %ax # increment it
|
||||
mov %ax, count # store it back
|
||||
|
||||
# release lock
|
||||
mov $1, %ax
|
||||
fetchadd %ax, turn
|
||||
|
||||
# see if we're still looping
|
||||
sub $1, %bx
|
||||
test $0, %bx
|
||||
jgt .top
|
||||
|
||||
halt
|
||||
1186
related_info/lab7/software-hardware-locks/x86.py
Executable file
1186
related_info/lab7/software-hardware-locks/x86.py
Executable file
File diff suppressed because it is too large
Load Diff
29
related_info/lab7/software-hardware-locks/yield.s
Normal file
29
related_info/lab7/software-hardware-locks/yield.s
Normal file
@@ -0,0 +1,29 @@
|
||||
.var mutex
|
||||
.var count
|
||||
|
||||
.main
|
||||
.top
|
||||
|
||||
.acquire
|
||||
mov $1, %ax
|
||||
xchg %ax, mutex # atomic swap of 1 and mutex
|
||||
test $0, %ax # if we get 0 back: lock is free!
|
||||
je .acquire_done
|
||||
yield # if not, yield and try again
|
||||
j .acquire
|
||||
.acquire_done
|
||||
|
||||
# critical section
|
||||
mov count, %ax # get the value at the address
|
||||
add $1, %ax # increment it
|
||||
mov %ax, count # store it back
|
||||
|
||||
# release lock
|
||||
mov $0, mutex
|
||||
|
||||
# see if we're still looping
|
||||
sub $1, %bx
|
||||
test $0, %bx
|
||||
jgt .top
|
||||
|
||||
halt
|
||||
Reference in New Issue
Block a user