/******************************************************* * File: AddXY_NoDef.s * Date: Oct. 24, 2021 * Author: DSS * Computer: acad * Assemble: asm AddXY_NoDef * Execute: AddXY * Purpose: to demonstrate allocation of automatic (local) variables on the stack. * * This program will declare ints x and y by allocating space for them in * main()'s stack frame. x and y will be read and stored directly * Their sum will be computed and output * Several registers will be aliased * * It demonstrates a downfall of using defines *******************************************************/ .global main main: push %rbp # keep stack aligned # Make room for x and y on the stack subq $16, %rsp # must also remain divisible by 16 # Read values leaq prompt(%rip), %rdi # load format string to proper register call printf # print it # Read x and y xor %rax,%rax leaq fmt_in(%rip), %rdi # load format string movq %rsp, %rsi addq $0, %rsi movq %rsp, %rdx addq $4, %rdx call scanf # Load and sum # Put x location in xReg movq (%rsp),%r12 # Value of x into xReg (offset is 0) # Put y location in xReg movq 4(%rsp),%r13 # Value of y into yReg (offset is 4) # Sum xor %r14, %r14 # Clear the sum register zReg addq %r12, %r14 # Add x to it addq %r13, %r14 # Add y to it # Print leaq frmto(%rip),%rdi # Output format mov %r12,%rsi mov %r13,%rdx mov %r14,%rcx xor %rax,%rax call printf done: addq $16, %rsp # Deallocate the variables pop %rbp ret .data prompt: .asciz "enter x and y: " fmt_in: .asciz "%d %d" frmto: .asciz "%d + %d = %d\n"