/******************************************************* * File: DivDemo.s * Date: Oct 12, 2021 * Author: DSS * Assembler: as under the gcc compiler * Purpose: Demonstrate quotient and remainder of two ints *******************************************************/ .globl main main: push %rbp # keep stack aligned #Get operands mov $0, %rax # clear AL (zero FP args in XMM registers) leaq DividendPrompt(%rip), %rdi # load format string to proper register call printf # print it # Read dividend leaq fmt_in(%rip), %rdi # load format string leaq dividend(%rip), %rsi # set storage to address of operand call scanf mov $0, %rax # clear AL (zero FP args in XMM registers) leaq DivisorPrompt(%rip), %rdi # load format string to proper register call printf # print it # Read divisor leaq fmt_in(%rip), %rdi # load format string leaq divisor(%rip), %rsi # set storage to address of operand call scanf # Load values from memory movq dividend(%rip), %r13 # Holders to minimize RAM access movq divisor(%rip), %r14 # Divide mov $0,%edx # Division is a double sized dividend. High 32 -> 0 mov dividend(%rip),%eax # When I loaded to %rax it had wrong value; use eax divq %r14 # Divide by value in %r14 mov %rax, %r11 # Store quotient mov %rdx, %r12 # Store remainder # Print result mov $Output, %rdi # Output string to 1st parameter # Two ways to load values from memory into a register # 1. Load directly from address movq dividend(%rip),%rsi # load dividend direct from RAM to %rsi # 2. Copy address to register and load from there mov $divisor,%r15 # put adrs of divisor in r15 mov (%r15),%rdx # load divisor into %rdx for output # quotient is already in register; just copy it mov %r11,%rcx # into next argument register mov %r12,%r8 # Same for remainder xor %rax, %rax #must 0 out %rax call printf #print it done: pop %rbp ret .data Output: .asciz "%d divided by %d: Quotient: %d Remainder:%d\n" fmt_in: .string "%d" DivisorPrompt: .asciz "Divisor >" DividendPrompt: .asciz "Dividend >" dividend: .long 0 divisor: .long 0