Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- ; Name: toupper.asm
- ; Author: s3my0n
- ; Function: Get text input from stdin and convert any lowercase letters
- ; to uppercase, writing to stdout.
- ;
- ; Compile and link: nasm -f elf toupper.asm
- ; ld -o toupper toupper.o
- ;
- SECTION .bss
- LEN equ 1024 ; Length of buffer
- Buff: resb LEN ; Buffer for characters
- SECTION .data
- SECTION .text
- global _start
- _start:
- nop ; For gdb
- ; Read LEN characters from stdin to Buff
- Read:
- mov eax, 3 ; Place syscall for sys_read
- mov ebx, 0 ; Place stdin number
- mov ecx, Buff ; Place start of Buff
- mov edx, LEN ; Place length of Buff
- int 80h ; Execute sys_read
- cmp eax, 0 ; Check if EOF occured
- je Exit ; Jump to Exit if it did
- mov esi, eax ; Save the count of read chars
- mov ecx, esi ; Make ECX the counter register
- mov ebp, Buff ; Place the start of Buff in EBX
- dec ebp ; Decrement EBP so [EBP+ECX] points at the last char
- ; Convert from lowercase to uppercase
- Scan:
- cmp byte [ebp+ecx], 61h ; Compare current char with 'a'
- jb Next ; Jump to Next if the char is below 'a'
- cmp byte [ebp+ecx], 7Ah ; Compare current char with 'z'
- ja Next ; Jump to Next if the char is above 'z'
- sub byte [ebp+ecx], 20h ; Convert current char to uppercase
- Next:
- dec ecx ; Decrement the counter to point at the next char
- jnz Scan ; If the counter not zero convert next char
- ; Write the converted buffer to stdout
- Write:
- mov eax, 4 ; Place syscall for sys_write
- mov ebx, 1 ; Place stdout number
- mov ecx, Buff ; Place start of buffer to write from
- mov edx, esi ; Place the length of chars to write
- int 80h ; Execute sys_write call
- jmp Read ; Read next set of chars
- ; Print out I/O error message
- ; Exit procedure
- Exit:
- mov eax, 1 ; Place syscall number for sys_exit
- mov ebx, 0 ; Place 0 for successful exit
- int 80h ; Execute sys_exit call
Advertisement
Add Comment
Please, Sign In to add comment