fork download
  1. #include <stdio.h>
  2.  
  3. #define SYS_READ 3
  4. #define STDIN_FILENO 0
  5. char buff[100]; /* Declare a buff to hold the returned string. */
  6. int main()
  7. {
  8.  
  9. ssize_t charsread; /* The number of characters returned. */
  10.  
  11. /* Use constraints to move buffer size into edx, stdin handle number
  12.   into ebx, address of buff into ecx. Also, "0" means this value
  13.   goes into the same place as parameter 0 (charsread). So eax will
  14.   hold SYS_READ (3) on input, and charsread on output. Lastly, you
  15.   MUST use the "memory" clobber since you are changing the contents
  16.   of buff without any of the constraints saying that you are.
  17.  
  18.   This is a much better approach than doing the "mov" statements
  19.   inside the asm. For one thing, since gcc will be moving the
  20.   values into the registers, it can RE-USE them if you make a
  21.   second call to read more chars. */
  22.  
  23. asm volatile("int $0x80" /* Call the syscall interrupt. */
  24. :
  25. : "a" (SYS_READ), "b" (STDIN_FILENO), "c" (buff), "d" (sizeof(buff))
  26. : "memory", "cc");
  27.  
  28. printf("%d: %s", (int)charsread, buff);
  29.  
  30. return 0;
  31. }
Success #stdin #stdout 0s 2296KB
stdin
hello world
stdout
0: hello world