05-userinput.asm 1.8 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182
  1. ;;; Required packages: nasm, ld
  2. ;;; compile commands:
  3. ;;; nasm -f elf 05-userinput.asm -o bin/05-userinput.o
  4. ;;; ld -m elf_i386 -s -o bin/05-userinput bin/05-userinput.o
  5. %define SYSCALL_EXIT 1 ; system call sys_exit
  6. %define SYSCALL_WRITE 4 ; system call sys_write
  7. %define SYSCALL_READ 3
  8. %define STDIN 0 ; file descriptor stdin
  9. %define STDOUT 1 ; file descriptor stdout
  10. %define NAME_BUFFER_LEN 256
  11. section .data
  12. question db "What is your name? ", 0xa
  13. questionLen equ $ - question
  14. prompt db "> "
  15. promptLen equ $ - prompt
  16. greetings db "Hello, "
  17. greetingsLen equ $ - greetings
  18. section .bss
  19. name resb NAME_BUFFER_LEN ; resb -> Reserve 16 bytes
  20. section .text
  21. global _start
  22. _start:
  23. call printQuestionAndPrompt
  24. call scanName
  25. call printGreetings
  26. ; exit code
  27. mov eax, SYSCALL_EXIT ;
  28. mov ebx, 0 ; 0 - error code
  29. int 0x80 ; processor interruption to run syscall
  30. ret
  31. scanName:
  32. mov eax, SYSCALL_READ
  33. mov ebx, STDIN
  34. mov ecx, name
  35. mov edx, NAME_BUFFER_LEN
  36. int 0x80
  37. ret
  38. printGreetings:
  39. mov eax, SYSCALL_WRITE
  40. mov ebx, STDOUT
  41. mov ecx, greetings ; text
  42. mov edx, greetingsLen ; text size
  43. int 0x80 ; processor interruption to run syscall
  44. mov eax, SYSCALL_WRITE
  45. mov ebx, STDOUT
  46. mov ecx, name ; text
  47. mov edx, NAME_BUFFER_LEN ; text size (16 bytes reserved)
  48. int 0x80 ; processor interruption to run syscall
  49. ; name ends with \n
  50. ret
  51. printQuestionAndPrompt:
  52. mov eax, SYSCALL_WRITE
  53. mov ebx, STDOUT
  54. mov ecx, question ; text
  55. mov edx, questionLen ; text size
  56. int 0x80 ; processor interruption to run syscall
  57. mov eax, SYSCALL_WRITE
  58. mov ebx, STDOUT
  59. mov ecx, prompt ; text
  60. mov edx, promptLen ; text size
  61. int 0x80 ; processor interruption to run syscall
  62. ret