hello.c 934 B

1234567891011121314151617181920212223242526272829303132333435363738394041424344
  1. /* hello: a real ring-3 user process.
  2. *
  3. * Prints "Hello user this is a real process!" once per second (10 times),
  4. * then "Goodbye", and exits — all through int 0x80 syscalls. */
  5. #define SYS_PUTSTR 1
  6. #define SYS_GET_TICKS_MS 2
  7. #define SYS_SLEEP_MS 3
  8. #define SYS_EXIT 4
  9. static inline int syscall1(int num, int a1)
  10. {
  11. int ret;
  12. __asm__ volatile ("int $0x80"
  13. : "=a"(ret)
  14. : "a"(num), "b"(a1)
  15. : "memory");
  16. return ret;
  17. }
  18. static inline int syscall0(int num)
  19. {
  20. int ret;
  21. __asm__ volatile ("int $0x80"
  22. : "=a"(ret)
  23. : "a"(num)
  24. : "memory");
  25. return ret;
  26. }
  27. static void putstr(const char *s) { syscall1(SYS_PUTSTR, (int)s); }
  28. static void sleep_ms(unsigned int ms) { syscall1(SYS_SLEEP_MS, (int)ms); }
  29. void _start(void)
  30. {
  31. int i;
  32. for (i = 0; i < 10; i++) {
  33. sleep_ms(1000);
  34. putstr("Hello user this is a real process!\n");
  35. }
  36. putstr("Goodbye\n");
  37. syscall1(SYS_EXIT, 0);
  38. for (;;)
  39. ;
  40. }