Skip to content

Commit 5cfbe87

Browse files
committed
TJ version
1 parent c1c393b commit 5cfbe87

File tree

2 files changed

+48
-55
lines changed

2 files changed

+48
-55
lines changed

concurrency.asm

+32-41
Original file line numberDiff line numberDiff line change
@@ -3,7 +3,7 @@
33
; Initialize constants.
44
mov r12, 65537 ; Exponent to modular-exponentiate with
55
mov rbx, 235 ; Modulus to modular-exponentiate with
6-
mov r15, NPROCS ; Number of processes to fork.
6+
mov r15, NPROCS ; Number of worker processes to fork.
77
mov r14, (SIZE+1)*8 ; Size of shared memory; reserving first
88
; 64 bits for bookkeeping
99

@@ -13,50 +13,47 @@
1313

1414
open_file:
1515
; We have a file specified on the command line, so open() it.
16-
mov rax, SYSCALL_OPEN
17-
mov rdi, [rsp+2*8]
18-
mov rsi, O_RDWR|O_CREAT ; read/write mode; create if necessary
19-
mov rdx, 0o660 ; `chmod`-mode of file to create (octal)
20-
syscall
16+
mov rax, SYSCALL_OPEN ; set up open()
17+
mov rdi, [rsp+2*8] ; filename from command line
18+
mov rsi, O_RDWR|O_CREAT ; read/write mode; create if necessary
19+
mov rdx, 660o ; `chmod`-mode of file to create (octal)
20+
syscall ; do open() system call
2121
mov r13, rax ; preserve file descriptor in r13
22-
mov rax, SYSCALL_PWRITE ; adjust file size by writing to "end"
23-
mov rdi, r13
24-
mov rsi, null_byte ; write a null byte
25-
mov rdx, 1 ; just one byte
26-
mov r10, r14 ; at offset of our desired file size
27-
dec r10 ; minus one
28-
syscall
22+
mov rax, SYSCALL_FTRUNCATE ; set up ftruncate() to adjust file size
23+
mov rdi, r13 ; file descriptor
24+
mov rsi, r14 ; desired file size
25+
syscall ; do ftruncate() system call
2926
mov r8, r13
3027
mov r10, MAP_SHARED
3128
jmp mmap
3229

30+
; Ask the kernel for a shared memory mapping.
3331
map_anon:
34-
mov r10, MAP_SHARED|MAP_ANON ; MAP_ANON means not backed by a file
35-
mov r8, -1 ; thus our file descriptor is -1
32+
mov r10, MAP_SHARED|MAP_ANON ; MAP_ANON means not backed by a file
33+
mov r8, -1 ; thus our file descriptor is -1
3634
mmap:
37-
mov r9, 0 ; ...and there's no file offset in either case.
38-
; Ask the kernel for a shared memory mapping.
39-
mov eax, SYSCALL_MMAP
40-
mov rdx, PROT_READ|PROT_WRITE ; We'd like a read/write mapping
41-
mov rdi, 0 ; at no pre-specified memory location.
42-
mov rsi, r14 ; Length of the mapping in bytes.
43-
syscall ; Do system call.
35+
mov r9, 0 ; and there's no file offset in either case.
36+
mov rax, SYSCALL_MMAP ; set up mmap()
37+
mov rdx, PROT_READ|PROT_WRITE ; We'd like a read/write mapping
38+
mov rdi, 0 ; at no pre-specified memory location.
39+
mov rsi, r14 ; Length of the mapping in bytes.
40+
syscall ; do mmap() system call.
4441
test rax, rax ; Return value will be in rax.
4542
js error ; If it's negative, that's trouble.
4643
mov rbp, rax ; Otherwise, we have our memory region [rbp].
4744

48-
lock add [rbp], r15 ; Initialize the first machine word to NPROCS.
45+
lock add [rbp], r15 ; Add NPROCS to the file's first machine word.
4946
; We'll use it to track the # of still-running
50-
; processes.
47+
; worker processes.
5148

5249
; Next, fork NPROCS processes.
5350
fork:
5451
mov eax, SYSCALL_FORK
5552
syscall
56-
%ifidn __OUTPUT_FORMAT__,elf64
53+
%ifidn __OUTPUT_FORMAT__,elf64 ; (This means we're running on Linux)
5754
test rax, rax ; We're a child iff return value of fork()==0.
5855
jz child
59-
%elifidn __OUTPUT_FORMAT__,macho64
56+
%elifidn __OUTPUT_FORMAT__,macho64 ; (This means we're running on OSX)
6057
test rdx, rdx ; Apple...you're not supposed to touch rdx here
6158
jnz child ; Apple, what
6259
%endif
@@ -66,7 +63,7 @@ fork:
6663
parent:
6764
pause
6865
cmp qword [rbp], 0
69-
jnz parent ; Wait for [rbp] to be zero
66+
jnz parent ; Wait for [rbp], the worker count, to be zero
7067
%ifndef NOPRINT
7168
mov rbx, rbp
7269
add rbx, r14 ; rbx marks the end of the [rbp] region
@@ -84,7 +81,7 @@ print_loop:
8481
jne print_loop
8582
%endif
8683

87-
success:
84+
exit_success:
8885
mov eax, SYSCALL_EXIT ; Normal exit
8986
mov edi, 0
9087
syscall
@@ -102,17 +99,17 @@ find_work: ; and try to find a piece of work to claim
10299
jz found_work ; If successful, zero flag is set
103100
.moveon:
104101
add rdi, 8 ; Otherwise, try a different piece.
105-
.next:
102+
find_work.next:
106103
cmp rdi, rsi ; Make sure we haven't hit the end.
107104
jne find_work
108105

109106
child_exit: ; If we have hit the end, we're done.
110107
lock dec qword [rbp] ; Atomic-decrement the # of active processes.
111-
jmp success
108+
jmp exit_success
112109

113110
found_work:
114-
mov r8, 8 ; There are 8 tasks per piece.
115-
do_task: ; This part does the actual work of mod-exp.
111+
mov r8, 8 ; There are 8 pieces per task.
112+
do_piece: ; This part does the actual work of mod-exp.
116113
mov r13, r12 ; Copy exponent to r13.
117114
mov rax, rdi ; The actual value to mod-exp should start
118115
sub eax, 0x7 ; at 1 for the first byte after the bookkeeping
@@ -137,9 +134,9 @@ do_task: ; This part does the actual work of mod-exp.
137134
jnz .modexploop ; If the exponent isn't zero, keep working
138135
mov byte [rbp+rdi], al ; Else, store result byte.
139136
inc rdi ; Move forward
140-
dec r8 ; Decrement task counter
141-
jnz do_task ; Do the next task if there is one.
142-
jmp find_work.next ; Else, find the next piece of work.
137+
dec r8 ; Decrement piece counter
138+
jnz do_piece ; Do the next piece if there is one.
139+
jmp find_work.next ; Else, find the next task.
143140

144141
error:
145142
mov rdi, rax ; In case of error, return code is -errno...
@@ -152,10 +149,4 @@ error:
152149
section .data
153150
unsigned_int:
154151
db `%u\n`
155-
null_byte:
156-
db 0
157-
%else
158-
section .data
159-
null_byte:
160-
db 0
161152
%endif

os_dependent_stuff.asm

+16-14
Original file line numberDiff line numberDiff line change
@@ -1,22 +1,24 @@
11
; syscalls
22
%ifidn __OUTPUT_FORMAT__,elf64
33
; http://lxr.linux.no/linux+v3.13.5/arch/x86/syscalls/syscall_64.tbl
4-
%define SYSCALL_OPEN 2
5-
%define SYSCALL_WRITE 1
6-
%define SYSCALL_MMAP 9
7-
%define SYSCALL_PWRITE 18
8-
%define SYSCALL_FORK 57
9-
%define SYSCALL_WAITID 247
10-
%define SYSCALL_EXIT 60
4+
%define SYSCALL_OPEN 2
5+
%define SYSCALL_WRITE 1
6+
%define SYSCALL_MMAP 9
7+
%define SYSCALL_FTRUNCATE 77
8+
%define SYSCALL_PWRITE 18
9+
%define SYSCALL_FORK 57
10+
%define SYSCALL_WAITID 247
11+
%define SYSCALL_EXIT 60
1112
%elifidn __OUTPUT_FORMAT__,macho64
1213
; http://www.opensource.apple.com/source/xnu/xnu-1456.1.26/bsd/kern/syscalls.master
13-
%define SYSCALL_OPEN 0x2000005
14-
%define SYSCALL_WRITE 0x2000004
15-
%define SYSCALL_MMAP 0x20000C5
16-
%define SYSCALL_PWRITE 0x200009A
17-
%define SYSCALL_FORK 0x2000002
18-
%define SYSCALL_WAITID 0x20000AD
19-
%define SYSCALL_EXIT 0x2000001
14+
%define SYSCALL_OPEN 0x2000005
15+
%define SYSCALL_WRITE 0x2000004
16+
%define SYSCALL_MMAP 0x20000C5
17+
%define SYSCALL_FTRUNCATE 0x20000C9
18+
%define SYSCALL_PWRITE 0x200009A
19+
%define SYSCALL_FORK 0x2000002
20+
%define SYSCALL_WAITID 0x20000AD
21+
%define SYSCALL_EXIT 0x2000001
2022
%endif
2123

2224
; fcntls

0 commit comments

Comments
 (0)