_write.c 1.9 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061
  1. /* -*-comment-start: "//";comment-end:""-*-
  2. * GNU Mes --- Maxwell Equations of Software
  3. * Copyright © 2016,2017,2019,2020 Jan (janneke) Nieuwenhuizen <janneke@gnu.org>
  4. * Copyright © 2019,2020 Danny Milosavljevic <dannym@scratchpost.org>
  5. *
  6. * This file is part of GNU Mes.
  7. *
  8. * GNU Mes is free software; you can redistribute it and/or modify it
  9. * under the terms of the GNU General Public License as published by
  10. * the Free Software Foundation; either version 3 of the License, or (at
  11. * your option) any later version.
  12. *
  13. * GNU Mes is distributed in the hope that it will be useful, but
  14. * WITHOUT ANY WARRANTY; without even the implied warranty of
  15. * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  16. * GNU General Public License for more details.
  17. *
  18. * You should have received a copy of the GNU General Public License
  19. * along with GNU Mes. If not, see <http://www.gnu.org/licenses/>.
  20. */
  21. #include "mes/lib-mini.h"
  22. // *INDENT-OFF*
  23. #if !__TINYC__
  24. #define SYS_write "0x04"
  25. ssize_t
  26. _write (int filedes, void const *buffer, size_t size)
  27. {
  28. long r;
  29. asm (
  30. "mov r7, $"SYS_write"\n\t"
  31. "mov r0, %1\n\t"
  32. "mov r1, %2\n\t"
  33. "mov r3, %3\n\t"
  34. "swi $0\n\t"
  35. "mov %0, r0\n\t"
  36. : "=r" (r)
  37. : "r" (filedes), "r" (buffer), "r" (size)
  38. : "r0", "r1", "r2", "r7"
  39. );
  40. return r;
  41. }
  42. #else //__TINYC__
  43. #define SYS_write 0x04
  44. ssize_t
  45. _write (int filedes, void const *buffer, size_t size)
  46. {
  47. long r;
  48. int c = SYS_write;
  49. __asm__ (".int 0xe51b7008\n"); //ldr r7, [fp, #-8] ; <c>
  50. __asm__ (".int 0xe59b000c\n"); //ldr r0, [fp, #12] ; filedes
  51. __asm__ (".int 0xe59b1010\n"); //ldr r1, [fp, #16] ; buffer
  52. __asm__ (".int 0xe59b2014\n"); //ldr r2, [fp, #20] ; size
  53. __asm__ (".int 0xef000000\n"); //svc 0x00000000
  54. __asm__ (".int 0xe50b0004\n"); //str r0, [fp, #-4]
  55. return r;
  56. }
  57. #endif //__TINYC__
  58. // *INDENT-ON*