/******************************************************* * File: AddXY_Fns_Ref.m * Date: Oct. 11, 2021 * Author: DSS * Computer: acad * Assemble: asm AddXY_Fns_Ref * Execute: AddXY_Fns_Ref * Purpose: to demonstrate function calls * * This program will declare ints x and y by allocating space for them in * main()'s stack frame. x and y will be read inside a function where each is passed by reference * They will be stored in the addresses passed in as parameters (in registers) * Their sum will be computed and output * Several registers will be aliased * * It uses minimal code, optimizing several things *******************************************************/ define(varMem,16) define(xOffset,0) define(yOffset,4) define(xReg,%r12) define(yReg,%r13) define(zReg,%r14) .global main main: push %rbp # keep stack aligned # Make room for x and y on the stack subq $varMem, %rsp # must also remain divisible by 16 # Prompt for X leaq promptX(%rip), %rdi # load format string to proper register call printf # print it # Read X xor %rax,%rax movq %rsp, %rdi addq $xOffset, %rdi call readInt # Put value in xReg movq %rsp,xReg # Stack pointer into xReg addq $xOffset, xReg # xReg now has address of X movq (xReg),xReg # Load the value of x into xReg # Prompt for Y leaq promptY(%rip), %rdi # load format string to proper register call printf # print it # Read Y xor %rax,%rax movq %rsp, %rdi addq $yOffset, %rdi call readInt # Put value in yReg movq %rsp,yReg # Stack pointer into xReg addq $yOffset, yReg # xReg now has address of X movq (yReg),yReg # Load the value of x into xReg # Sum xor zReg, zReg # Clear the sum register zReg addq xReg, zReg # Add x to it addq yReg, zReg # Add y to it # Print leaq frmto(%rip),%rdi # Output format mov xReg,%rsi mov yReg,%rdx mov zReg,%rcx xor %rax,%rax call printf done: addq $varMem, %rsp # Deallocate the variables pop %rbp ret readInt: # Address to read to is in %rdi movq %rdi, %rsi # Copy address to 2nd parameter leaq fmt_in(%rip), %rdi # load format string # Read call scanf ret .data promptX: .asciz "enter x : " promptY: .asciz "enter y : " fmt_in: .asciz "%d" frmto: .asciz "%d + %d = %d\n"