(b) Write a NEAR subroutine using 8086 assembly Language (with proper comments) that returns the average value of the values stored in a byte array of length 3. All three values of the byte array are passed to the subroutine in the stack. You should write both the calling program and subroutine.

, , No Comments

Certainly, below is an example of a calling program and a NEAR subroutine written in 8086 assembly language that calculates the average value of a byte array of length 3. The values of the byte array are passed to the subroutine through the stack.


**Calling Program:**


```assembly

section .data

    array_values db 10, 20, 30


section .text

    global _start


_start:

    ; Load array values onto the stack

    mov al, [array_values]

    push ax

    mov al, [array_values + 1]

    push ax

    mov al, [array_values + 2]

    push ax


    ; Call the subroutine

    call calculate_average


    ; Display the result

    mov ah, 4Ch

    int 21h


calculate_average:

    ; NEAR subroutine to calculate average

    pop bx  ; Pop the first value

    pop ax  ; Pop the second value

    add ax, bx  ; Sum of the first two values

    pop bx  ; Pop the third value

    add ax, bx  ; Sum of all three values

    mov bx, 3  ; Number of elements

    div bx  ; Divide the sum by the number of elements

    ; AX now contains the average value


    ; Display the result (for demonstration purposes)

    mov ah, 2

    int 21h


    ret

```


This program defines an array with three values (10, 20, 30) and calls the `calculate_average` subroutine to calculate and display the average value.


Note: The subroutine `calculate_average` assumes that the array values are integers, and the average is an integer. If you want to handle floating-point averages, you would need to adjust the subroutine accordingly. 

0 टिप्पणियाँ:

Post a Comment