DEV Community

Discussion on: Daily Challenge #56 - Coffee Shop

Collapse
 
serbuvlad profile image
Șerbu Vlad Gabriel • Edited

x86_64 assembly (System V ABI, GNU assembler), using the C standard library for printf.

coffee.S:

    .global coffee

    .text
coffee:
    push %rbp
    mov %rsp, %rbp

    mov $prices, %rsi
    xor %eax, %eax #offset

    mov $4, %ecx
loop:
    movlpd (%rsi, %rax, 8), %xmm1

    cmpeqsd %xmm0, %xmm1

    sub $8, %rsp
    movlpd %xmm1, (%rsp)
    pop %rdx

    cmp $0, %rdx
    jne goodjob

    inc %rax
    loop loop

    mov $badfmt, %rdi
    xor %eax, %eax
end:
    call printf

    pop %rbp
    ret

goodjob:
    mov $goodfmt, %rdi
    mov $names, %rsi
    mov (%rsi, %rax, 8), %rsi
    mov $1, %eax

    jmp end

    .section .rodata
prices:
    .double 2.20
    .double 2.30
    .double 2.40
    .double 3.50

names:
    .quad first
    .quad second
    .quad third
    .quad fourth

first:
    .asciz "Americano"
second:
    .asciz "Latte"
third:
    .asciz "Flat white"
fourth:
    .asciz "Filter"

goodfmt:
    .asciz "Here is your %s, have a nice day!\n"
badfmt:
    .asciz "Sorry, exact change only, try again tomorrow!\n"

coffee.h:

void coffee(double d);