read.s 2.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293
  1. ## Copyright (C) 2016 Jeremiah Orians
  2. ## This file is part of stage0.
  3. ##
  4. ## stage0 is free software: you an redistribute it and/or modify
  5. ## it under the terms of the GNU General Public License as published by
  6. ## the Free Software Foundation, either version 3 of the License, or
  7. ## (at your option) any later version.
  8. ##
  9. ## stage0 is distributed in the hope that it will be useful,
  10. ## but WITHOUT ANY WARRANTY; without even the implied warranty of
  11. ## MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  12. ## GNU General Public License for more details.
  13. ##
  14. ## You should have received a copy of the GNU General Public License
  15. ## along with stage0. If not, see <http://www.gnu.org/licenses/>.
  16. .data
  17. # we must export the entry point to the ELF linker or loader.
  18. # They convientionally recognize _start as their entry point.
  19. # Use ld -e main to override the default if you wish
  20. .global _start
  21. _start:
  22. # first check that we got the correct number of inputs
  23. pop %rax # Get the number of arguments
  24. pop %rdi # Get the program name
  25. pop %rdi # Get the actual argument
  26. # Check if we have the correct number of inputs
  27. cmp $2, %rax
  28. # Jump to Bail if the number is not correct
  29. jne Bail
  30. # attempt to open the file for reading
  31. mov $0, %rsi # prepare read_only
  32. # we already have what we need in ebx
  33. mov $2, %rax # the syscall number for open()
  34. syscall # call the Kernel
  35. # Check if we have a valid file
  36. test %rax, %rax
  37. # Jump to Bail_file if not actual file
  38. js Bail
  39. mov %rax, %rdi # move the pointer to the right location
  40. Circle: #print contents of file
  41. mov $read_size, %rdx # set the size of chars we want
  42. mov $buffer, %rsi # Where to put it
  43. # We already have what we need in ebx
  44. mov $0, %rax # the syscall number for read
  45. syscall # call the Kernel
  46. test %rax, %rax # check what we got
  47. jz Done # Got EOF call it done
  48. # Make sure we don't write a bunch of NULLs
  49. mov %rax, %rdx
  50. # get file pointer out of the way
  51. movq %rdi, %rsp
  52. # edx was already setup
  53. mov $1, %rdi # setup stdout write
  54. mov $1, %rax # setup the write
  55. syscall # call the Kernel
  56. #now to prepare for next loop
  57. movq %rsp, %rdi
  58. jmp Circle
  59. Done:
  60. # program completed Successfully
  61. mov $0, %rdi # All is well
  62. mov $60, %rax # put the exit syscall number in eax
  63. syscall # Call it a good day
  64. Bail:
  65. # terminate with an error
  66. mov $1, %rdi # there was an error
  67. mov $60, %rax # put the exit syscall number in eax
  68. syscall # bail out
  69. # Our writable space
  70. # 2^ 30 Should be enough per read
  71. read_size = 1073741824
  72. buffer:
  73. .space 1