Search code examples
assemblyuppercasei386toupper

Assembly: lowercase to UPPERCASE


I need to transform "h4ppy c0d1ng" into "H4PPY C0D1NG". I am a beginner in this language, but here is my attempt (ubuntu i386 VirtualBox Mac).I think the int 21h is wrong, other than that the program won't finish nor print the string when executed:

section .text
GLOBAL _start

_start:
        mov ecx, string
        mov edx, length
        call toUpper
        call print

        mov eax, 1
        mov ebx, 0 
        int 80h

;String in ecx and length in edx?
;-------------------------
toUpper:
        mov eax,ecx
        cmp al,0x0 ;check it's not the null terminating character?
        je done
        cmp al,'a'
        jb next_please
        cmp al,'z'
        ja next_please
        sub cl,0x20
        ret
next_please:
        inc al
        jmp toUpper
done:   int 21h ; just leave toUpper (not working)
print:
        mov ebx, 1
        mov eax, 4
        int 80h
        ret
section .data
string db "h4ppy c0d1ng", 10
length equ $-string

Solution

  • some minor changes and it should run:

    section .text
    GLOBAL _start
    
    _start: mov ecx, string
            call toUpper
            call print
            mov eax,1
            mov ebx,0
            int 80h
    
    toUpper:
            mov al,[ecx]      ; ecx is the pointer, so [ecx] the current char
            cmp al,0x0 
            je done
            cmp al,'a'
            jb next_please
            cmp al,'z'
            ja next_please
            sub al,0x20       ; move AL upper case and
            mov [ecx],al      ; write it back to string
    
    next_please:
            inc ecx           ; not al, that's the character. ecx has to
                              ; be increased, to point to next char
            jmp toUpper
    done:   ret
    
    print:  mov ecx, string    ; what to print
            mov edx, len       ; length of string to be printed
            mov ebx, 1
            mov eax, 4
            int 80h
            ret
    
    section .data
    string: db "h4ppy c0d1ng",10,0
    len:    equ $-string
    

    edit:
    updated "print" to work,
    bugfix for making uppercase: al holds the char, not cl
    added a symbol to determine the length of the string

    tested on my linux box, not it works