Lab 9 - Arrays

Arrays are common in programs. They store a series of values instead of a single (scalar) value.

The code below defines an array in the data section, then the code prints each value out.


    extern  printf      ; We will use this external function

    section .data       ; Data section, initialized variables

mystr: db "%d", 10, 0   ; String format to use (decimal), followed by NL

myarr: dd 10, 20, 30, 40, 50, 60
temp:  dd 0

    section .text
    global main
main:

   xor eax, eax                ; A = 0
   mov ecx, myarr              ; C points to myarr

myloop:
   mov   ebx, DWORD [ecx+4*eax]  ; Get the value  B = myarr[A]
   mov   [temp], ebx
   push  rax
   push  rcx
                               ; Now print the result out
   mov   rdi, mystr            ; Format of the string to print
   mov   rsi, [temp]           ; Value to print
   mov   rax, 0
   call  printf

   pop   rcx
   pop   rax
   add   eax, 1                ; A++
   cmp   eax, 6                ; Does A == 6?
   jl    myloop                ; if less, jump to myloop

   mov  rax, 0
   ret

Questions:

The objective for this lab is to add to the code, to determine the minimum value, the maximum value, and the average value. You will need to define storage for each of these in the .data section (or the .bss section).

Keep the part that prints each value in the array, only alter it to print the values on a single line. Also, have the program print something before the values, like "The array has the following values:".

In the program, the first thing to do is to initialize the minimum and maximum values. Some programmers try to use values that are clearly wrong for initialization, such as 100,000 for the minimum and 0 for the maximum. Then, looking at each value, replace the minimum and maximum with anything that is smaller or larger, respectively.

Questions:

The idea of looking at each value in turn and updating the minimum and/or maximum is good. However, the initialization should be with a value in the array. To do this, we can copy the first array value to the minimum, and the maximum.

Have the program print some text, such as "The minimum is:" then the minimum value. Likewise, it should print text followed by the maximum value. And it should print some appropriate text followed by the average.

Questions:

Now code this algorithm into the assembly language program. It should initialize the minimum and maximum with the first value of the array, then examine each value in the array (starting with the second value), and update the minimum and maximum as needed.

Questions:

To find the average, we can make a sum of the values. Then divide the sum by the number of values in the array. Store this average in another memory location.

Questions:

Include comments, and meaningful identifiers. Make sure to show the final version of the .asm program, and the assembly, link, and test run(s).