Advertisement
obernardovieira

Get input length (max 8 letters)

Feb 11th, 2015
495
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
  1. ;Well, I have some problems when I try to input more than 9 letters, not because the length of input,
  2. ;but because it is a little complicated to save number bigger than 9, so, for now,
  3. ;it is what it is, and soon I will share more codes
  4. ;Have a nice day ;)
  5.  
  6.  
  7. %assign SYS_READ    3
  8. %assign SYS_WRITE   4
  9. %assign STDIN       0
  10. %assign STDOUT      1
  11.  
  12. section .data
  13.     msg1        db "The length is:",0xA
  14.     len_msg1    equ $-msg1
  15.  
  16. segment .bss
  17.     text        resb 8      ;reserve 8bits for letter (max 8 letters)
  18.     textLength  resb 1      ;reserve 1bit to save the length
  19.  
  20. section .text
  21.     global _start
  22.  
  23. _getLength:
  24.     pushad          ;save the registers
  25.  
  26.     xor esi, esi    ;esi = 0
  27.     mov ecx, 0      ;ecx = 0
  28.     myloop:
  29.  
  30.         mov eax, [text + esi]   ;read letter in specific position
  31.         inc esi         ;increment the position
  32.  
  33.         add ecx, 1          ;add 1
  34.  
  35.     cmp eax, 0      ;verify if is the end of string
  36.     jne myloop      ;jne (jump if not equal) if is not the end of string, return to loop
  37.  
  38.     sub ecx, 2      ;it count's the end of string and stop in the next loop, so, it need to subtract 2
  39.     add ecx, '0'    ;convert from decimal to ASCII
  40.     mov [textLength], ecx   ;save length in variable
  41.  
  42.     popad           ;return the saved values to registers
  43.     ret
  44.  
  45. _start:
  46.  
  47.     mov eax, SYS_READ   ;sys_read
  48.     mov ebx, STDIN  ;stdit
  49.     mov ecx, text   ;variable to save the input
  50.     mov edx, 8      ;max length of input
  51.     int 0x80
  52.  
  53.     call    _getLength
  54.  
  55.     mov eax, SYS_WRITE  ;sys_write
  56.     mov ebx, STDOUT ;stdout
  57.     mov ecx, msg1   ;variable to output
  58.     mov edx, len_msg1   ;max length of output
  59.     int 0x80
  60.  
  61.     mov eax, SYS_WRITE
  62.     mov ebx, STDOUT
  63.     mov ecx, textLength
  64.     mov edx, 1
  65.     int 0x80
  66.  
  67.     mov eax, 1      ;system call number (sys_exit)
  68.     xor ebx, ebx    ;ebx = 0 (success)
  69.     int     0x80        ;call kernel
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement