DEV Community

Discussion on: Daily Challenge #45 - Change Machine

Collapse
 
serbuvlad profile image
Șerbu Vlad Gabriel • Edited

x86_64 assembly (System V ABI, GNU assembler), for those concerned about speed.

change.S:

    .global change

    .text
change:
    push %rbx
    xor %edx, %edx
    xor %ecx, %ecx
    mov %edi, %eax
    mov $table, %r9
loop:
    mov (%r9, %rcx, 4), %ebx
    div %ebx

    mov %eax, (%rsi, %rcx, 4)
    mov %edx, %eax
    xor %edx, %edx

    inc %ecx
    cmp $4, %ecx
    jne loop

    pop %rbx
    ret


    .section .rodata
table:
    .int 25
    .int 10
    .int 5
    .int 1

change.h:

/*
 * coins[0] = number of 25 cent coins
 * coins[1] = number of 10 cent coins
 * coins[2] = number of 5 cent coins
 * coins[3] = number of 1 cent coins
 */
unsigned int change(unsigned int n, unsigned int coins[4]);
Collapse
 
thepeoplesbourgeois profile image
Josh • Edited

I am concerned about speed

we need to get that stuff off the streets smh

causin' good folk to think they need to write stuff in assembly, fcol >.>