.comm x,4,4
.comm y,4,4

.section    .rodata

format1:    .string "Div : %d / %d = %.2f\n"
format2:    .string "Mod : %d %% %d = %d\n"
format3:    .string "Multiply : %u * %u = %llu\n"
format4:    .string "%d %d"
const100:   .int 100

.text
.globl  main
.type   main, @function
main:
    subl $32, %esp # allocate space, preserve alignment

    movl $format4, (%esp)
    movl $x, 4(%esp)
    movl $y, 8(%esp)
    call scanf

# operation divide
    fildl x
    fimul const100
    fidivl y
# truncate to integer
# use this if current FPU rounding mode
# is known to be truncate
#   frndint
# otherwise use this
    fnclex
    fnstcw (%esp)       # save a copy to modify
    fnstcw 2(%esp)      # and a copy to preserve
    orw $0x0c00, (%esp) # rounding mode = truncate
    fldcw (%esp)        # activate
    frndint             # do the truncate
    fldcw 2(%esp)       # restore original
# end of truncate code
    fidiv const100
    fstpl 12(%esp) # x / y

    movl $format1, (%esp)
    movl x, %eax
    movl %eax, 4(%esp)
    movl y, %eax
    movl %eax, 8(%esp)
    call printf

# operation modulo
    movl x, %eax
    cltd
    idivl y
    movl $format2, (%esp)
    movl x, %eax
    movl %eax, 4(%esp)
    movl y, %eax
    movl %eax, 8(%esp)
    movl %edx, 12(%esp)
    call printf

# operation multiply
    movl x, %eax
    mull y
    movl $format3, (%esp)
    movl x, %ecx
    movl %ecx, 4(%esp)
    movl y, %ecx
    movl %ecx, 8(%esp)
    movl %eax, 12(%esp)
    movl %edx, 16(%esp)
    call printf

    addl $32, %esp
    xor %eax, %eax
    ret
