Skip to main content
Engineering LibreTexts

12.13: Example, Statistical Function 1 (leaf)

  • Page ID
    58053
  • \( \newcommand{\vecs}[1]{\overset { \scriptstyle \rightharpoonup} {\mathbf{#1}} } \) \( \newcommand{\vecd}[1]{\overset{-\!-\!\rightharpoonup}{\vphantom{a}\smash {#1}}} \)\(\newcommand{\id}{\mathrm{id}}\) \( \newcommand{\Span}{\mathrm{span}}\) \( \newcommand{\kernel}{\mathrm{null}\,}\) \( \newcommand{\range}{\mathrm{range}\,}\) \( \newcommand{\RealPart}{\mathrm{Re}}\) \( \newcommand{\ImaginaryPart}{\mathrm{Im}}\) \( \newcommand{\Argument}{\mathrm{Arg}}\) \( \newcommand{\norm}[1]{\| #1 \|}\) \( \newcommand{\inner}[2]{\langle #1, #2 \rangle}\) \( \newcommand{\Span}{\mathrm{span}}\) \(\newcommand{\id}{\mathrm{id}}\) \( \newcommand{\Span}{\mathrm{span}}\) \( \newcommand{\kernel}{\mathrm{null}\,}\) \( \newcommand{\range}{\mathrm{range}\,}\) \( \newcommand{\RealPart}{\mathrm{Re}}\) \( \newcommand{\ImaginaryPart}{\mathrm{Im}}\) \( \newcommand{\Argument}{\mathrm{Arg}}\) \( \newcommand{\norm}[1]{\| #1 \|}\) \( \newcommand{\inner}[2]{\langle #1, #2 \rangle}\) \( \newcommand{\Span}{\mathrm{span}}\)\(\newcommand{\AA}{\unicode[.8,0]{x212B}}\)

    This simple example will demonstrate calling a simple void function to find the sum and average of an array of numbers. The High-Level Language (HLL) call for C/C++ is as follows:

        stats1(arr, len, sum, ave);
    

    As per the C/C++ convention, the array, arr, is call-by-reference and the length, len, is call-by-value. The arguments for sum and ave are both call-by-reference (since there are no values as yet). For this example, the array arr, sum, and ave variables are all signed double-word integers. Of course, in context, the len must be unsigned.

    12.9.1 Caller

    In this case, there are 4 arguments, and all arguments are passed in registers in accordance with the standard calling convention. The assembly language code in the calling routine for the call to the stats function would be as follows:

        ; stats1(arr, len, sum, ave); 
        mov     rcx, ave                        ; 4th arg, addr of ave
        mov     rdx, sum                        ; 3rd arg, addr of sum
        mov     esi, dword [len]                ; 2nd arg, value of len
        mov     rdi, arr                        ; 1st arg, addr of arr
        call    stats1
    

    There is no specific required order for setting the argument registers. This example sets them in reverse order in preparation for the next, extended example.

    Note, the setting of the esi register also sets the upper-order double-word to zero, thus ensuring the rsi register is set appropriately for this specific usage since length is unsigned.

    No return value is provided by this void routine. If the function was a value returning function, the value returned would be in the A register (of appropriate size).

    Callee

    The function being called, the callee, must perform the prologue and epilogue operations (as specified by the standard calling convention) before and after the code to perform the function goal. For this example, the function must perform the summation of values in the array, compute the integer average, return the sum and average values.

    The following code implements the stats1 example.

        ; Simple example function to find and return
        ; the sum and average of an array.
        
        ; HLL call:
        ; stats1(arr, len, sum, ave); 
        ; -----
        ; Arguments:
        ;  arr, address – rdi
        ;  len, dword value – esi 
        ;  sum, address – rdx
        ;  ave, address - rcx
    
        global stats1
        stats1:
            push     r12                        ; prologue
            
            mov      r12, 0                     ; counter/index
            mov      rax, 0                     ; running sum
        sumLoop:
            add      eax, dword [rdi+r12*4]     ; sum += arr[i]
            inc      r12
            cmp      r12, rsi
            jl       sumLoop
            
            mov  dword [rdx], eax               ; return sum
            
            cdq
            idiv    esi                         ; compute average
            mov     dword [rcx], eax            ; return ave
            
            pop     r12                         ; epilogue
            ret
    

    The choice of the r12 register is arbitrary, however a 'saved register' was selected. The call frame for this function would be as follows:

    截屏2021-07-29 下午9.49.53.png

    The minimal use of the stack helps reduce the function call run-time overhead.


    12.13: Example, Statistical Function 1 (leaf) is shared under a not declared license and was authored, remixed, and/or curated by LibreTexts.

    • Was this article helpful?