fork download
  1. ;Assignment No. : 2
  2. ;Assignment Name: Write X86/64 ALP to accept a string and to display its length.
  3. ;---------------------------------------------------------------------
  4. section .data
  5.  
  6. msg db 10,10,"Enter the string: "
  7. msg_len equ $-msg
  8.  
  9. smsg db 10,10,"The length of string is: "
  10. smsg_len equ $-smsg
  11.  
  12. ;---------------------------------------------------------------------
  13. Section .bss
  14.  
  15. string resb 50
  16. stringl equ $-string
  17. count resb 1
  18. char_ans resb 2
  19. ;---------------------------------------------------------------------
  20. %macro Print 2
  21. mov rax, 1
  22. mov rdi, 1
  23. mov rsi, %1
  24. mov rdx, %2
  25. syscall
  26. %endmacro
  27.  
  28. %macro Read 2
  29. mov rax, 0
  30. mov rdi, 0
  31. mov rsi, %1
  32. mov rdx, %2
  33. syscall
  34. %endmacro
  35.  
  36. %macro Exit 0
  37. mov rax, 60
  38. mov rdi, 0
  39. syscall
  40. %endmacro
  41.  
  42. ;---------------------------------------------------------------------
  43. section .text
  44. global _start
  45. _start:
  46. Print msg, msg_len
  47. Read string, stringl
  48. mov [count], rax
  49.  
  50. Print smsg, smsg_len
  51. mov rax, [count]
  52. call Display
  53. Exit
  54. ;--------------------------------------------------------------------
  55. Display:
  56. mov rbx,16 ; divisor=16 for hex
  57. mov rcx,2 ; number of digits
  58. mov rsi,char_ans+1 ; load last byte address of char_ans buffer in rsi
  59.  
  60. cnt: mov rdx,0 ; make rdx=0 (as in div instruction rdx:rax/rbx)
  61. div rbx
  62.  
  63. cmp dl, 09h ; check for remainder in rdx
  64. jbe add30
  65. add dl, 07h
  66. add30:
  67. add dl,30h ; calculate ASCII code
  68. mov [rsi],dl ; store it in buffer
  69. dec rsi ; point to one byte back
  70.  
  71. dec rcx ; decrement count
  72. jnz cnt ; if not zero repeat
  73.  
  74. Print char_ans,2 ; display result on screen
  75. ret
  76. ;----------------------------------------------------------------
Success #stdin #stdout 0s 5288KB
stdin
hello
stdout

Enter the string:	

The length of string is:	06