fork download
  1. ; ------------------------------------------------------------------
  2. ; userinput.asm
  3. ;
  4. ; This is a Win32 console program that reads user input from the keyboard
  5. ; and displays it on the screen then exits.
  6. ;
  7. ; To assemble to .obj: nasm -f win32 userinput.asm
  8. ; To compile to .exe: gcc userinput.obj -o userinput.exe
  9. ; ------------------------------------------------------------------
  10.  
  11. global _main ; _start
  12.  
  13. ; The data section is used for declaring initialized data or constants.
  14. ; This data does not change at runtime.
  15. ; You can declare various constant values, file names or buffer size etc. in this section.
  16. section .data
  17. inputMsg db 'Please enter a number: ', ; Ask the user to enter a number.
  18. inputMsglen equ $-inputMsg ; length of the input message.
  19.  
  20. outputMsg db 'The entered number is: '
  21. outputMsgLenm equ $-outputMsg
  22.  
  23.  
  24. ; The bss section is used for declaring variables.
  25. section .bss
  26. num resb 5
  27.  
  28. ; The text section is used for keeping the actual code.
  29. ; This section must begin with the declarationglobal main,
  30. ; which tells the kernel where the program execution begins.
  31. section .text
  32.  
  33. ; _start:
  34. _main:
  35. ; User prompt "Please enter a number: "
  36. mov edx, inputMsglen ; message length
  37. mov ecx, inputMsg ; message to write
  38. mov ebx, 01h ; file descriptor (stdout)
  39. mov eax, 04h ; system call number (sys_write)
  40. int 0x80 ; call kernel
  41.  
  42. ; Read & store user input.
  43. mov edx, 5 ; 5 bytes length (numeric, 1 for sign) of that information
  44. mov ecx, num ; variable to store user input
  45. mov ebx, 02h ; file descriptor (stdin)
  46. mov eax, 03h ; system call number (sys_read)
  47. int 0x80 ; call kernel
  48.  
  49. ; Output the message 'The entered number is: '
  50. mov edx, outputMsgLenm ; message length
  51. mov ecx, outputMsg ; message to write
  52. mov ebx, 01h ; file descriptor (stdout)
  53. mov eax, 04h ; system call number (sys_write)
  54. int 0x80 ; call kernel
  55.  
  56. ; Output the number entered
  57. mov edx, 5 ;
  58. mov ecx, num ;
  59. mov ebx, 01h ;
  60. mov eax, 04h ;
  61. int 80h ;
  62.  
  63. jmp exit
  64.  
  65. exit:
  66. mov eax, 01h ; exit()
  67. mov ebx, 0 ; errno
  68. int 80h
  69.  
Success #stdin #stdout 0s 5296KB
stdin
Standard input is empty
stdout
Please enter a number: The entered number is: