irenicus09

Touper

Jan 27th, 2015
374
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
  1. ; Name:     toupper.asm
  2. ; Author:   s3my0n
  3. ; Function: Get text input from stdin and convert any lowercase letters
  4. ;           to uppercase, writing to stdout.
  5. ;
  6. ; Compile and link: nasm -f elf toupper.asm
  7. ;                   ld -o toupper toupper.o
  8. ;
  9.  
  10. SECTION .bss
  11.     LEN equ 1024    ; Length of buffer
  12.     Buff: resb LEN  ; Buffer for characters
  13.  
  14. SECTION .data
  15.  
  16. SECTION .text
  17.  
  18. global _start
  19.  
  20. _start:
  21.         nop             ; For gdb
  22.  
  23. ; Read LEN characters from stdin to Buff
  24. Read:
  25.         mov eax, 3      ; Place syscall for sys_read
  26.         mov ebx, 0      ; Place stdin number
  27.         mov ecx, Buff   ; Place start of Buff
  28.         mov edx, LEN    ; Place length of Buff
  29.         int 80h         ; Execute sys_read
  30.         cmp eax, 0      ; Check if EOF occured
  31.         je Exit         ; Jump to Exit if it did
  32.  
  33.         mov esi, eax    ; Save the count of read chars
  34.         mov ecx, esi    ; Make ECX the counter register
  35.         mov ebp, Buff   ; Place the start of Buff in EBX
  36.         dec ebp         ; Decrement EBP so [EBP+ECX] points at the last char
  37.  
  38. ; Convert from lowercase to uppercase
  39. Scan:
  40.         cmp byte [ebp+ecx], 61h ; Compare current char with 'a'
  41.         jb Next                 ; Jump to Next if the char is below 'a'
  42.         cmp byte [ebp+ecx], 7Ah ; Compare current char with 'z'
  43.         ja Next                 ; Jump to Next if the char is above 'z'
  44.         sub byte [ebp+ecx], 20h ; Convert current char to uppercase
  45. Next:
  46.         dec ecx                 ; Decrement the counter to point at the next char
  47.         jnz Scan                ; If the counter not zero convert next char
  48.  
  49. ; Write the converted buffer to stdout  
  50. Write:
  51.         mov eax, 4      ; Place syscall for sys_write
  52.         mov ebx, 1      ; Place stdout number
  53.         mov ecx, Buff   ; Place start of buffer to write from
  54.         mov edx, esi    ; Place the length of chars to write
  55.         int 80h         ; Execute sys_write call
  56.         jmp Read        ; Read next set of chars
  57.  
  58. ; Print out I/O error message
  59.  
  60. ; Exit procedure
  61. Exit:
  62.         mov eax, 1      ; Place syscall number for sys_exit
  63.         mov ebx, 0      ; Place 0 for successful exit
  64.         int 80h         ; Execute sys_exit call
Advertisement
Add Comment
Please, Sign In to add comment