Advertisement
Guest User

Untitled

a guest
Aug 16th, 2017
101
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 3.61 KB | None | 0 0
  1. Source name : CHARSIN.ASM
  2. ; Executable name : CHARSIN
  3. ; Version : 2.0
  4. ; Created date : 11/21/1999
  5. ; Last update : 5/28/2009
  6. ; Author : Jeff Duntemann
  7. ; Description : A character input demo for Linux, using NASM 2.05,
  8. ; incorporating calls to both fgets() and scanf().
  9. ;
  10. ; Build using these commands:
  11. ; nasm -f elf -g -F stabs charsin.asm
  12. ; gcc charsin.o -o charsin
  13. ;
  14.  
  15. [SECTION .data] ; Section containing initialised data
  16.  
  17. SPrompt db 'Enter string data, followed by Enter: ',0
  18. IPrompt db 'Enter an integer value, followed by Enter: ',0
  19. IFormat db '%d',0
  20. SShow db 'The string you entered was: %s',10,0
  21. IShow db 'The integer value you entered was: %5d',10,0
  22.  
  23. [SECTION .bss] ; Section containing uninitialized data
  24.  
  25. IntVal resd 1 ; Reserve an uninitialized double word
  26. InString resb 128m ; Reserve 128 bytes for string entry buffer
  27.  
  28. [SECTION .text] ; Section containing code
  29.  
  30. extern stdin ; Standard file variable for input
  31. extern fgets
  32. extern printf
  33. extern scanf
  34. global main ; Required so linker can find entry point
  35.  
  36. main:
  37.  
  38. push ebp ; Set up stack frame for debugger
  39. mov ebp,esp
  40. push ebx ; Program must preserve ebp, ebx, esi, & edi
  41. push esi
  42. push edi
  43. ;;; Everything before this is boilerplate; use it for all ordinary apps!
  44.  
  45. ; First, an example of safely limited string input using fgets:
  46. push Sprompt ; Push address of the prompt string
  47. call printf ; Display it
  48. add esp,4 ; Stack cleanup for 1 parm
  49.  
  50. push dword [stdin] ; Push file handle for standard input
  51. push 72 ; Accept no more than 72 chars from keybd
  52. push InString ; Push address of buffer for entered chars
  53. call fgets ; Call fgets
  54. add esp,12 ; Stack cleanup: 3 parms X 4 bytes = 12
  55.  
  56. push InString ; Push address of entered string data buffer
  57. push Sshow ; Push address of the string display prompt
  58. call printf ; Display it
  59. add esp,8 ; Stack cleanup: 2 parms X 4 bytes = 8
  60.  
  61. ; Next, use scanf() to enter numeric data:
  62. push Iprompt ; Push address of the integer input prompt
  63. call printf ; Display it
  64. add esp,4 ; Stack cleanup for 1 parm
  65.  
  66. push IntVal ; Push the address of the integer buffer
  67. push Iformat ; Push the address of the integer format string
  68. call scanf ; Call scanf to enter numeric data
  69. add esp,8 ; Stack cleanup: 2 parms X 4 bytes = 8
  70.  
  71. push dword [IntVal] ; Push integer value to display
  72. push Ishow ; Push base string
  73. call printf ; Call printf to convert & display the integer
  74. add esp,8 ; Stack cleanup: 2 parms X 4 bytes = 8
  75.  
  76. ;;; Everything after this is boilerplate; use it for all ordinary apps!
  77. pop edi ; Restore saved registers
  78. pop esi
  79. pop ebx
  80. mov esp,ebp ; Destroy stack frame before returning
  81. pop ebp
  82. ret ; Return control to Linux
  83.  
  84. [SECTION .data] ; Section containing initialised data
  85.  
  86. sprompt db 'Enter string data, followed by Enter: ',0
  87. iprompt db 'Enter an integer value, followed by Enter: ',0
  88. iformat db '%d',0
  89. sshow db 'The string you entered was: %s',10,0
  90. ishow db 'The integer value you entered was: %5d',10,0
  91.  
  92. [SECTION .bss] ; Section containing uninitialized data
  93.  
  94. intval resd 1 ; Reserve an uninitialized double word
  95. instring resb 128 ; Reserve 128 bytes for string entry buffer
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement