_forktest: file format elf32-i386 Disassembly of section .text: 00000000 : #define N 1000 void printf(int fd, char *s, ...) { 0: 55 push %ebp 1: 89 e5 mov %esp,%ebp 3: 83 ec 18 sub $0x18,%esp write(fd, s, strlen(s)); 6: 8b 45 0c mov 0xc(%ebp),%eax 9: 89 04 24 mov %eax,(%esp) c: e8 98 01 00 00 call 1a9 11: 89 44 24 08 mov %eax,0x8(%esp) 15: 8b 45 0c mov 0xc(%ebp),%eax 18: 89 44 24 04 mov %eax,0x4(%esp) 1c: 8b 45 08 mov 0x8(%ebp),%eax 1f: 89 04 24 mov %eax,(%esp) 22: e8 76 03 00 00 call 39d } 27: c9 leave 28: c3 ret 00000029 : void forktest(void) { 29: 55 push %ebp 2a: 89 e5 mov %esp,%ebp 2c: 83 ec 28 sub $0x28,%esp int n, pid; printf(1, "fork test\n"); 2f: c7 44 24 04 20 04 00 movl $0x420,0x4(%esp) 36: 00 37: c7 04 24 01 00 00 00 movl $0x1,(%esp) 3e: e8 bd ff ff ff call 0 for(n=0; n pid = fork(); 4c: e8 24 03 00 00 call 375 51: 89 45 f0 mov %eax,-0x10(%ebp) if(pid < 0) 54: 83 7d f0 00 cmpl $0x0,-0x10(%ebp) 58: 79 02 jns 5c break; 5a: eb 18 jmp 74 if(pid == 0) 5c: 83 7d f0 00 cmpl $0x0,-0x10(%ebp) 60: 75 05 jne 67 exit(); 62: e8 16 03 00 00 call 37d { int n, pid; printf(1, "fork test\n"); for(n=0; n break; if(pid == 0) exit(); } if(n == N){ 74: 81 7d f4 e8 03 00 00 cmpl $0x3e8,-0xc(%ebp) 7b: 75 21 jne 9e printf(1, "fork claimed to work N times!\n", N); 7d: c7 44 24 08 e8 03 00 movl $0x3e8,0x8(%esp) 84: 00 85: c7 44 24 04 2c 04 00 movl $0x42c,0x4(%esp) 8c: 00 8d: c7 04 24 01 00 00 00 movl $0x1,(%esp) 94: e8 67 ff ff ff call 0 exit(); 99: e8 df 02 00 00 call 37d } for(; n > 0; n--){ 9e: eb 26 jmp c6 if(wait() < 0){ a0: e8 e0 02 00 00 call 385 a5: 85 c0 test %eax,%eax a7: 79 19 jns c2 printf(1, "wait stopped early\n"); a9: c7 44 24 04 4b 04 00 movl $0x44b,0x4(%esp) b0: 00 b1: c7 04 24 01 00 00 00 movl $0x1,(%esp) b8: e8 43 ff ff ff call 0 exit(); bd: e8 bb 02 00 00 call 37d if(n == N){ printf(1, "fork claimed to work N times!\n", N); exit(); } for(; n > 0; n--){ c2: 83 6d f4 01 subl $0x1,-0xc(%ebp) c6: 83 7d f4 00 cmpl $0x0,-0xc(%ebp) ca: 7f d4 jg a0 printf(1, "wait stopped early\n"); exit(); } } if(wait() != -1){ cc: e8 b4 02 00 00 call 385 d1: 83 f8 ff cmp $0xffffffff,%eax d4: 74 19 je ef printf(1, "wait got too many\n"); d6: c7 44 24 04 5f 04 00 movl $0x45f,0x4(%esp) dd: 00 de: c7 04 24 01 00 00 00 movl $0x1,(%esp) e5: e8 16 ff ff ff call 0 exit(); ea: e8 8e 02 00 00 call 37d } printf(1, "fork test OK\n"); ef: c7 44 24 04 72 04 00 movl $0x472,0x4(%esp) f6: 00 f7: c7 04 24 01 00 00 00 movl $0x1,(%esp) fe: e8 fd fe ff ff call 0 } 103: c9 leave 104: c3 ret 00000105
: int main(void) { 105: 55 push %ebp 106: 89 e5 mov %esp,%ebp 108: 83 e4 f0 and $0xfffffff0,%esp forktest(); 10b: e8 19 ff ff ff call 29 exit(); 110: e8 68 02 00 00 call 37d 00000115 : "cc"); } static inline void stosb(void *addr, int data, int cnt) { 115: 55 push %ebp 116: 89 e5 mov %esp,%ebp 118: 57 push %edi 119: 53 push %ebx asm volatile("cld; rep stosb" : 11a: 8b 4d 08 mov 0x8(%ebp),%ecx 11d: 8b 55 10 mov 0x10(%ebp),%edx 120: 8b 45 0c mov 0xc(%ebp),%eax 123: 89 cb mov %ecx,%ebx 125: 89 df mov %ebx,%edi 127: 89 d1 mov %edx,%ecx 129: fc cld 12a: f3 aa rep stos %al,%es:(%edi) 12c: 89 ca mov %ecx,%edx 12e: 89 fb mov %edi,%ebx 130: 89 5d 08 mov %ebx,0x8(%ebp) 133: 89 55 10 mov %edx,0x10(%ebp) "=D" (addr), "=c" (cnt) : "0" (addr), "1" (cnt), "a" (data) : "memory", "cc"); } 136: 5b pop %ebx 137: 5f pop %edi 138: 5d pop %ebp 139: c3 ret 0000013a : #include "user.h" #include "x86.h" char* strcpy(char *s, char *t) { 13a: 55 push %ebp 13b: 89 e5 mov %esp,%ebp 13d: 83 ec 10 sub $0x10,%esp char *os; os = s; 140: 8b 45 08 mov 0x8(%ebp),%eax 143: 89 45 fc mov %eax,-0x4(%ebp) while((*s++ = *t++) != 0) 146: 90 nop 147: 8b 45 08 mov 0x8(%ebp),%eax 14a: 8d 50 01 lea 0x1(%eax),%edx 14d: 89 55 08 mov %edx,0x8(%ebp) 150: 8b 55 0c mov 0xc(%ebp),%edx 153: 8d 4a 01 lea 0x1(%edx),%ecx 156: 89 4d 0c mov %ecx,0xc(%ebp) 159: 0f b6 12 movzbl (%edx),%edx 15c: 88 10 mov %dl,(%eax) 15e: 0f b6 00 movzbl (%eax),%eax 161: 84 c0 test %al,%al 163: 75 e2 jne 147 ; return os; 165: 8b 45 fc mov -0x4(%ebp),%eax } 168: c9 leave 169: c3 ret 0000016a : int strcmp(const char *p, const char *q) { 16a: 55 push %ebp 16b: 89 e5 mov %esp,%ebp while(*p && *p == *q) 16d: eb 08 jmp 177 p++, q++; 16f: 83 45 08 01 addl $0x1,0x8(%ebp) 173: 83 45 0c 01 addl $0x1,0xc(%ebp) } int strcmp(const char *p, const char *q) { while(*p && *p == *q) 177: 8b 45 08 mov 0x8(%ebp),%eax 17a: 0f b6 00 movzbl (%eax),%eax 17d: 84 c0 test %al,%al 17f: 74 10 je 191 181: 8b 45 08 mov 0x8(%ebp),%eax 184: 0f b6 10 movzbl (%eax),%edx 187: 8b 45 0c mov 0xc(%ebp),%eax 18a: 0f b6 00 movzbl (%eax),%eax 18d: 38 c2 cmp %al,%dl 18f: 74 de je 16f p++, q++; return (uchar)*p - (uchar)*q; 191: 8b 45 08 mov 0x8(%ebp),%eax 194: 0f b6 00 movzbl (%eax),%eax 197: 0f b6 d0 movzbl %al,%edx 19a: 8b 45 0c mov 0xc(%ebp),%eax 19d: 0f b6 00 movzbl (%eax),%eax 1a0: 0f b6 c0 movzbl %al,%eax 1a3: 29 c2 sub %eax,%edx 1a5: 89 d0 mov %edx,%eax } 1a7: 5d pop %ebp 1a8: c3 ret 000001a9 : uint strlen(char *s) { 1a9: 55 push %ebp 1aa: 89 e5 mov %esp,%ebp 1ac: 83 ec 10 sub $0x10,%esp int n; for(n = 0; s[n]; n++) 1af: c7 45 fc 00 00 00 00 movl $0x0,-0x4(%ebp) 1b6: eb 04 jmp 1bc 1b8: 83 45 fc 01 addl $0x1,-0x4(%ebp) 1bc: 8b 55 fc mov -0x4(%ebp),%edx 1bf: 8b 45 08 mov 0x8(%ebp),%eax 1c2: 01 d0 add %edx,%eax 1c4: 0f b6 00 movzbl (%eax),%eax 1c7: 84 c0 test %al,%al 1c9: 75 ed jne 1b8 ; return n; 1cb: 8b 45 fc mov -0x4(%ebp),%eax } 1ce: c9 leave 1cf: c3 ret 000001d0 : void* memset(void *dst, int c, uint n) { 1d0: 55 push %ebp 1d1: 89 e5 mov %esp,%ebp 1d3: 83 ec 0c sub $0xc,%esp stosb(dst, c, n); 1d6: 8b 45 10 mov 0x10(%ebp),%eax 1d9: 89 44 24 08 mov %eax,0x8(%esp) 1dd: 8b 45 0c mov 0xc(%ebp),%eax 1e0: 89 44 24 04 mov %eax,0x4(%esp) 1e4: 8b 45 08 mov 0x8(%ebp),%eax 1e7: 89 04 24 mov %eax,(%esp) 1ea: e8 26 ff ff ff call 115 return dst; 1ef: 8b 45 08 mov 0x8(%ebp),%eax } 1f2: c9 leave 1f3: c3 ret 000001f4 : char* strchr(const char *s, char c) { 1f4: 55 push %ebp 1f5: 89 e5 mov %esp,%ebp 1f7: 83 ec 04 sub $0x4,%esp 1fa: 8b 45 0c mov 0xc(%ebp),%eax 1fd: 88 45 fc mov %al,-0x4(%ebp) for(; *s; s++) 200: eb 14 jmp 216 if(*s == c) 202: 8b 45 08 mov 0x8(%ebp),%eax 205: 0f b6 00 movzbl (%eax),%eax 208: 3a 45 fc cmp -0x4(%ebp),%al 20b: 75 05 jne 212 return (char*)s; 20d: 8b 45 08 mov 0x8(%ebp),%eax 210: eb 13 jmp 225 } char* strchr(const char *s, char c) { for(; *s; s++) 212: 83 45 08 01 addl $0x1,0x8(%ebp) 216: 8b 45 08 mov 0x8(%ebp),%eax 219: 0f b6 00 movzbl (%eax),%eax 21c: 84 c0 test %al,%al 21e: 75 e2 jne 202 if(*s == c) return (char*)s; return 0; 220: b8 00 00 00 00 mov $0x0,%eax } 225: c9 leave 226: c3 ret 00000227 : char* gets(char *buf, int max) { 227: 55 push %ebp 228: 89 e5 mov %esp,%ebp 22a: 83 ec 28 sub $0x28,%esp int i, cc; char c; for(i=0; i+1 < max; ){ 22d: c7 45 f4 00 00 00 00 movl $0x0,-0xc(%ebp) 234: eb 4c jmp 282 cc = read(0, &c, 1); 236: c7 44 24 08 01 00 00 movl $0x1,0x8(%esp) 23d: 00 23e: 8d 45 ef lea -0x11(%ebp),%eax 241: 89 44 24 04 mov %eax,0x4(%esp) 245: c7 04 24 00 00 00 00 movl $0x0,(%esp) 24c: e8 44 01 00 00 call 395 251: 89 45 f0 mov %eax,-0x10(%ebp) if(cc < 1) 254: 83 7d f0 00 cmpl $0x0,-0x10(%ebp) 258: 7f 02 jg 25c break; 25a: eb 31 jmp 28d buf[i++] = c; 25c: 8b 45 f4 mov -0xc(%ebp),%eax 25f: 8d 50 01 lea 0x1(%eax),%edx 262: 89 55 f4 mov %edx,-0xc(%ebp) 265: 89 c2 mov %eax,%edx 267: 8b 45 08 mov 0x8(%ebp),%eax 26a: 01 c2 add %eax,%edx 26c: 0f b6 45 ef movzbl -0x11(%ebp),%eax 270: 88 02 mov %al,(%edx) if(c == '\n' || c == '\r') 272: 0f b6 45 ef movzbl -0x11(%ebp),%eax 276: 3c 0a cmp $0xa,%al 278: 74 13 je 28d 27a: 0f b6 45 ef movzbl -0x11(%ebp),%eax 27e: 3c 0d cmp $0xd,%al 280: 74 0b je 28d gets(char *buf, int max) { int i, cc; char c; for(i=0; i+1 < max; ){ 282: 8b 45 f4 mov -0xc(%ebp),%eax 285: 83 c0 01 add $0x1,%eax 288: 3b 45 0c cmp 0xc(%ebp),%eax 28b: 7c a9 jl 236 break; buf[i++] = c; if(c == '\n' || c == '\r') break; } buf[i] = '\0'; 28d: 8b 55 f4 mov -0xc(%ebp),%edx 290: 8b 45 08 mov 0x8(%ebp),%eax 293: 01 d0 add %edx,%eax 295: c6 00 00 movb $0x0,(%eax) return buf; 298: 8b 45 08 mov 0x8(%ebp),%eax } 29b: c9 leave 29c: c3 ret 0000029d : int stat(char *n, struct stat *st) { 29d: 55 push %ebp 29e: 89 e5 mov %esp,%ebp 2a0: 83 ec 28 sub $0x28,%esp int fd; int r; fd = open(n, O_RDONLY); 2a3: c7 44 24 04 00 00 00 movl $0x0,0x4(%esp) 2aa: 00 2ab: 8b 45 08 mov 0x8(%ebp),%eax 2ae: 89 04 24 mov %eax,(%esp) 2b1: e8 07 01 00 00 call 3bd 2b6: 89 45 f4 mov %eax,-0xc(%ebp) if(fd < 0) 2b9: 83 7d f4 00 cmpl $0x0,-0xc(%ebp) 2bd: 79 07 jns 2c6 return -1; 2bf: b8 ff ff ff ff mov $0xffffffff,%eax 2c4: eb 23 jmp 2e9 r = fstat(fd, st); 2c6: 8b 45 0c mov 0xc(%ebp),%eax 2c9: 89 44 24 04 mov %eax,0x4(%esp) 2cd: 8b 45 f4 mov -0xc(%ebp),%eax 2d0: 89 04 24 mov %eax,(%esp) 2d3: e8 fd 00 00 00 call 3d5 2d8: 89 45 f0 mov %eax,-0x10(%ebp) close(fd); 2db: 8b 45 f4 mov -0xc(%ebp),%eax 2de: 89 04 24 mov %eax,(%esp) 2e1: e8 bf 00 00 00 call 3a5 return r; 2e6: 8b 45 f0 mov -0x10(%ebp),%eax } 2e9: c9 leave 2ea: c3 ret 000002eb : int atoi(const char *s) { 2eb: 55 push %ebp 2ec: 89 e5 mov %esp,%ebp 2ee: 83 ec 10 sub $0x10,%esp int n; n = 0; 2f1: c7 45 fc 00 00 00 00 movl $0x0,-0x4(%ebp) while('0' <= *s && *s <= '9') 2f8: eb 25 jmp 31f n = n*10 + *s++ - '0'; 2fa: 8b 55 fc mov -0x4(%ebp),%edx 2fd: 89 d0 mov %edx,%eax 2ff: c1 e0 02 shl $0x2,%eax 302: 01 d0 add %edx,%eax 304: 01 c0 add %eax,%eax 306: 89 c1 mov %eax,%ecx 308: 8b 45 08 mov 0x8(%ebp),%eax 30b: 8d 50 01 lea 0x1(%eax),%edx 30e: 89 55 08 mov %edx,0x8(%ebp) 311: 0f b6 00 movzbl (%eax),%eax 314: 0f be c0 movsbl %al,%eax 317: 01 c8 add %ecx,%eax 319: 83 e8 30 sub $0x30,%eax 31c: 89 45 fc mov %eax,-0x4(%ebp) atoi(const char *s) { int n; n = 0; while('0' <= *s && *s <= '9') 31f: 8b 45 08 mov 0x8(%ebp),%eax 322: 0f b6 00 movzbl (%eax),%eax 325: 3c 2f cmp $0x2f,%al 327: 7e 0a jle 333 329: 8b 45 08 mov 0x8(%ebp),%eax 32c: 0f b6 00 movzbl (%eax),%eax 32f: 3c 39 cmp $0x39,%al 331: 7e c7 jle 2fa n = n*10 + *s++ - '0'; return n; 333: 8b 45 fc mov -0x4(%ebp),%eax } 336: c9 leave 337: c3 ret 00000338 : void* memmove(void *vdst, void *vsrc, int n) { 338: 55 push %ebp 339: 89 e5 mov %esp,%ebp 33b: 83 ec 10 sub $0x10,%esp char *dst, *src; dst = vdst; 33e: 8b 45 08 mov 0x8(%ebp),%eax 341: 89 45 fc mov %eax,-0x4(%ebp) src = vsrc; 344: 8b 45 0c mov 0xc(%ebp),%eax 347: 89 45 f8 mov %eax,-0x8(%ebp) while(n-- > 0) 34a: eb 17 jmp 363 *dst++ = *src++; 34c: 8b 45 fc mov -0x4(%ebp),%eax 34f: 8d 50 01 lea 0x1(%eax),%edx 352: 89 55 fc mov %edx,-0x4(%ebp) 355: 8b 55 f8 mov -0x8(%ebp),%edx 358: 8d 4a 01 lea 0x1(%edx),%ecx 35b: 89 4d f8 mov %ecx,-0x8(%ebp) 35e: 0f b6 12 movzbl (%edx),%edx 361: 88 10 mov %dl,(%eax) { char *dst, *src; dst = vdst; src = vsrc; while(n-- > 0) 363: 8b 45 10 mov 0x10(%ebp),%eax 366: 8d 50 ff lea -0x1(%eax),%edx 369: 89 55 10 mov %edx,0x10(%ebp) 36c: 85 c0 test %eax,%eax 36e: 7f dc jg 34c *dst++ = *src++; return vdst; 370: 8b 45 08 mov 0x8(%ebp),%eax } 373: c9 leave 374: c3 ret 00000375 : name: \ movl $SYS_ ## name, %eax; \ int $T_SYSCALL; \ ret SYSCALL(fork) 375: b8 01 00 00 00 mov $0x1,%eax 37a: cd 40 int $0x40 37c: c3 ret 0000037d : SYSCALL(exit) 37d: b8 02 00 00 00 mov $0x2,%eax 382: cd 40 int $0x40 384: c3 ret 00000385 : SYSCALL(wait) 385: b8 03 00 00 00 mov $0x3,%eax 38a: cd 40 int $0x40 38c: c3 ret 0000038d : SYSCALL(pipe) 38d: b8 04 00 00 00 mov $0x4,%eax 392: cd 40 int $0x40 394: c3 ret 00000395 : SYSCALL(read) 395: b8 05 00 00 00 mov $0x5,%eax 39a: cd 40 int $0x40 39c: c3 ret 0000039d : SYSCALL(write) 39d: b8 10 00 00 00 mov $0x10,%eax 3a2: cd 40 int $0x40 3a4: c3 ret 000003a5 : SYSCALL(close) 3a5: b8 15 00 00 00 mov $0x15,%eax 3aa: cd 40 int $0x40 3ac: c3 ret 000003ad : SYSCALL(kill) 3ad: b8 06 00 00 00 mov $0x6,%eax 3b2: cd 40 int $0x40 3b4: c3 ret 000003b5 : SYSCALL(exec) 3b5: b8 07 00 00 00 mov $0x7,%eax 3ba: cd 40 int $0x40 3bc: c3 ret 000003bd : SYSCALL(open) 3bd: b8 0f 00 00 00 mov $0xf,%eax 3c2: cd 40 int $0x40 3c4: c3 ret 000003c5 : SYSCALL(mknod) 3c5: b8 11 00 00 00 mov $0x11,%eax 3ca: cd 40 int $0x40 3cc: c3 ret 000003cd : SYSCALL(unlink) 3cd: b8 12 00 00 00 mov $0x12,%eax 3d2: cd 40 int $0x40 3d4: c3 ret 000003d5 : SYSCALL(fstat) 3d5: b8 08 00 00 00 mov $0x8,%eax 3da: cd 40 int $0x40 3dc: c3 ret 000003dd : SYSCALL(link) 3dd: b8 13 00 00 00 mov $0x13,%eax 3e2: cd 40 int $0x40 3e4: c3 ret 000003e5 : SYSCALL(mkdir) 3e5: b8 14 00 00 00 mov $0x14,%eax 3ea: cd 40 int $0x40 3ec: c3 ret 000003ed : SYSCALL(chdir) 3ed: b8 09 00 00 00 mov $0x9,%eax 3f2: cd 40 int $0x40 3f4: c3 ret 000003f5 : SYSCALL(dup) 3f5: b8 0a 00 00 00 mov $0xa,%eax 3fa: cd 40 int $0x40 3fc: c3 ret 000003fd : SYSCALL(getpid) 3fd: b8 0b 00 00 00 mov $0xb,%eax 402: cd 40 int $0x40 404: c3 ret 00000405 : SYSCALL(sbrk) 405: b8 0c 00 00 00 mov $0xc,%eax 40a: cd 40 int $0x40 40c: c3 ret 0000040d : SYSCALL(sleep) 40d: b8 0d 00 00 00 mov $0xd,%eax 412: cd 40 int $0x40 414: c3 ret 00000415 : SYSCALL(uptime) 415: b8 0e 00 00 00 mov $0xe,%eax 41a: cd 40 int $0x40 41c: c3 ret