SCREAM!

Reading time ~1 minute

Aufgabenstellung: Schreiben Sie ein Programm in x86_64, dass einen C-String derart bearbeitet, dass alle Buchstaben gross und alle Sonderzeichen Ausrufezeichen sind.

C

#include <stdio.h>
#include <string.h>

extern int scream(char* text);

int main(int argc, char const * argv[])
{
    if (argc != 2)
    {
        return 1;
    }

    char text[strlen(argv[1])];
    strcpy(text, argv[1]);

    int result = scream(text);
    printf("%s\n", text);

    return result;
}

NASM

global scream
section .text
scream:
    ; use rcx as loop counter
    xor rcx, rcx
while:
    cmp byte[rdi+rcx], 0
    je  end
    mov r8b  , [rdi+rcx]

    ; Catch special and uppercase Characters
    cmp r8b  , 'A'
    jl  spec_to_excl
    cmp r8b  , 'z'
    ja  spec_to_excl
    cmp r8b  , 'Z'
    jle end_while
    cmp r8b  , 'a'
    jl  spec_to_excl

    ; lower- to uppercase
    sub r8b  , 0x20
    jmp end_while

    ; special charaacter to exclamation mark
spec_to_excl:
    mov r8b  , '!'

end_while:
    mov [rdi+rcx], r8b
    inc rcx
    jmp while

end:
    mov rax, 0
    ret