Hi can i get help writing this program in assembly language The purpose of this
ID: 3604867 • Letter: H
Question
Hi can i get help writing this program in assembly language
The purpose of this program is to help you understand dynamic array, system stack and multi-level subprogram calls.
This program will first prompt user to input a valid array size (between 4 inclusive to 11 exclusive) and then dynamically allocate an array in memory based on the size. It will then ask user to input integers one by one to fill out the allocated array until it’s full. After input, the program will print out the original array and then do three operations on the array:
calculate the summation of all even numbers in the array and print out the result;
dynamically allocate a new array with the same size, reversely copy all elements from the original array in to the new array, and then print the new array to the console;
Prompt user for a stride n and print out every nth element in the new reversed array.
To achieve this goal, you will need 1 main program associate with 7 subprograms:
create_array
This subprogram will have no argument IN and two arguments OUT:
$sp+0 - array base address (OUT)
$sp+4 - array size (OUT)
create_array will prompt user for a size of the array and will validate the input (between 4 inclusive to 11 exclusive). If an invalid size is entered, it will print an error message and re-prompt the user for a valid one. When a valid size is entered, create_array will call allocate_array to dynamically allocate an array. And then it will call read_array to fill it out. It will return base address and array size to the calling location at the end of this subprogram.
allocate_array
This subprogram will have one argument IN and one arguments OUT:
$sp+0 - user input size (IN)
$sp+4 - array base address (OUT)
allocate_array will dynamically allocate enough memory to hold the array based on the input size, and then return base address of the array back to the calling location.
read_array
This subprogram will have two arguments IN and no argument OUT:
$sp+0 - array base address (IN)
$sp+4 - array size (IN)
read_array will read integer values from user and store them into the array until the array is full. A prompt must be printed before each entry is read (e.g. “Enter an integer: “).
print_array
This subprogram will have two arguments IN and no arguments OUT:
$sp+0 - array base address (IN)
$sp+4 - array size (IN)
print_array will print all values from the array with each value separated by a space.
print_every_nth
This subprogram will have three arguments IN and no argument OUT:
$sp+0 - array base address (IN)
$sp+4 - array size (IN)
$sp+8 - stride N (IN)
print_every_nth will print every nth value from the array, starting from the first value (index 0). For example, consider the array [1 2 3 4 5 6 7 8 9 0].
For N = 3, the output would be [1 4 7 0].
For N = 4, the output would be [1 5 9].
If N 0, the subprogram will print an error message instead.
sum_even_values
This subprogram will have two arguments IN and one argument OUT:
$sp+0 - array base address (IN)
$sp+4 - array size (IN)
$sp+8 - sum of even values (OUT)
sum_even_values will calculate the sum of all even values in the array and return that sum to calling location. For example: given the array [8 4 3 6 5 5 2 8 7 2], the sum would be (8+4+6+2+8+2 = 30). sum_even_values will NOT print anything (main should print the sum).
reverse_array
This subprogram will have two arguments IN and one argument OUT:
$sp+0 - array base address (IN)
$sp+4 - array size (IN)
$sp+8 - array base address of reversed array (OUT)
reverse_array will call allocate_array to dynamically allocate a new array with the same size as the original one, and then reversely copy allvalues from the original array into the new array. This subprogram will return the base address of reversed array back to the calling location. Remember the size of reverse array is the same as size of original array so there is no need to return it again.
pseudocode:
revered array [0] = original array [size -1]
revered array [1] = original array [size -2]
…
revered array [size - 1] = original array [0]
Original Array: [1, 2, 3, 4, 5, 6, 7, 8, 9, 0]
Reversed array: [0, 9, 8, 7, 6, 5, 4, 3, 2, 1]
The structure of the program should as follow:
Main:
Create_array
Allocate_array
Read-array
Print_array
Sum_even_values
Reverse_array
Allocate_array
Print_every_nth
Remember that main should also print the returned sum of even values after sum_even_values subprogram. You should store array base address, reversed array base address, array length into static variables.
Explanation / Answer
section .text
global _start ;must be declared for linker (ld)
_start:
mov eax,3 ;number bytes to be summed
mov ebx,0 ;EBX will store the sum
mov ecx, x ;ECX will point to the current element to be summed
top: add ebx, [ecx]
add ecx,1 ;move pointer to next element
dec eax ;decrement counter
jnz top ;if counter not 0, then loop again
done:
add ebx, '0'
mov [sum], ebx ;done, store result in "sum"
display:
mov edx,1 ;message length
mov ecx, sum ;message to write
mov ebx, 1 ;file descriptor (stdout)
mov eax, 4 ;system call number (sys_write)
int 0x80 ;call kernel
mov eax, 1 ;system call number (sys_exit)
int 0x80 ;call kernel
section .data
global x
x:
db 2
db 4
db 3
sum:
db 0
Related Questions
drjack9650@gmail.com
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.