DEV Community

Discussion on: Daily Challenge #47 - Alphabets

Collapse
 
serbuvlad profile image
Șerbu Vlad Gabriel • Edited

x86_64 assembly (System V ABI, GNU assembler), as usual. Not really correct, since there will be an extra ' ' at the end which I was too lazy to remove, but it'll do.

alphabetic_position.S

    .global alphabetic_position

    .text
alphabetic_position:
    #using these registers to avoid sprintf breaking them
    push %rbx
    push %rbp

    mov %rdi, %rbx
    mov %rdi, %r12
    mov %rsi, %rbp

    xor %eax, %eax
    xor %edx, %edx
loop:
    mov (%rbp), %dl

    cmp $65, %dl # 'A'
    jl skip

    cmp $91, %dl # 'Z' + 1
    jl print

    cmp $97, %dl # 'a'
    jl skip

    cmp $123, %dl # 'z' + 1
    ja skip

printl:
    sub $32, %dl # 'a' -> 'A'
print:
    sub $64, %dl # 'A' -> 1

    push %rdx

    mov %rbx, %rdi
    mov $format, %rsi
    call sprintf

    add %rax, %rbx

    pop %rdx
skip:
    inc %rbp
    cmp $0, %dl
    jne loop

    mov %r12, %rax

    pop %rbp
    pop %rbx
    ret


    .section .rodata
format:
    .asciz "%d "

alphabetic_position.h:

char *alphabetic_position(char *dst, const char *src);

Edit: the function name now conforms to the specification, as well as with the "returns the string" requirement (by returning a copy of dst).

Collapse
 
hectorpascual profile image
Héctor Pascual

that's crazy