Academic Integrity: tutoring, explanations, and feedback — we don’t complete graded work or submit on a student’s behalf.

Assmbly Language The objectives of this program is to understand dynamic arrays,

ID: 3862766 • Letter: A

Question

Assmbly Language

The objectives of this program is to understand dynamic arrays, stacks and subprogram calling another subprogram (function calls that are more than one level deep).

create_array will have no arguments IN and two arguments OUT:

$sp+0 - array base address (OUT)
$sp+4 - array length (OUT)

create_array will call subprogram allocate_array to dynamically allocate array. Then it will call read_array to fill the array. Lastly, create_array will return base address and array size back to the main.

allocate_array will have no arguments IN and two arguments OUT:

$sp+0 - array base address (OUT)

$sp+4 - array length (OUT)

allocate_array will ask the user for the size of the array and will validate that it is between 5 (exclusive) and 10 (inclusive) elements or (5, 10]. If an invalid length is entered, allocate_array will print an error message and reprompt the user for the length. When a valid length is entered, allocate_array will dynamically allocate exactly enough memory to hold the array, and then return base address and array size back to create_array subprogram.

read_array will have two arguments IN and no arguments OUT:

$sp+0 - array base address (IN)

$sp+4 - array length (IN)

read_array will read values from the user and store them in the array until the array is full. A prompt must be printed before each entry is read (e.g. “Enter an integer: “).

print_array will have two arguments IN and no arguments OUT:

$sp+0 - array length (IN)

$sp+4 - array base address (IN)

print_array will print all values from the array with each value separated by a space.

print_every_nth will have three arguments IN and no arguments OUT:

$sp+0 - array base address (IN)

$sp+4 - array length (IN)

$sp+8 - element 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 must print an error and return without printing anything.

sum_odd_values will have two arguments IN and one argument OUT:

$sp+0 - array base address (IN)
$sp+4 - array length (IN)
$sp+8 - sum of odd values (OUT)


sum_odd_values will calculate the sum of all odd values in the array and return that sum. For example: given the array [8 4 3 6 5 5 2 8 7 2], the sum would be 30. sum_odd_values MAY NOT print anything (main must print the sum).

reverse_array will have two arguments IN and one argument OUT:

$sp+0 - array base address (IN)
$sp+4 - array length (IN)

$sp+8 - array base address of reversed array (OUT)

reverse_array will dynamically allocate an array with the same size as original array. Then copies values from original array into reversed array. Lastly, it will return array base address of reversed array back to main. Remember the size of reverse array is the same as size of original array so there is no need to return it back to main.

pseudocode:

revered array [0] = original array [size -1]

revered array [1] = original array [size -2]

revered array [size - 1] = original array [0]

Array: [1, 2, 3, 4, 5, 6, 7, 8, 9, 0]

Reversed array: [0, 9, 8, 7, 6, 5, 4, 3, 2, 1]

Explanation / Answer

Answer:

Assembly Language Code:

stack::stack():
        push    rbp
        mov     rbp, rsp
        mov     QWORD PTR [rbp-8], rdi
        mov     rax, QWORD PTR [rbp-8]
        mov     DWORD PTR [rax+20], -1
        nop
        pop     rbp
        ret
.LC0:
        .string "stack over flow"
.LC1:
        .string "inserted"
stack::push(int):
        push    rbp
        mov     rbp, rsp
        sub     rsp, 16
        mov     QWORD PTR [rbp-8], rdi
        mov     DWORD PTR [rbp-12], esi
        mov     rax, QWORD PTR [rbp-8]
        mov     eax, DWORD PTR [rax+20]
        cmp     eax, 4
        jle     .L3
        mov     esi, OFFSET FLAT:.LC0
        mov     edi, OFFSET FLAT:std::cout
        call    std::basic_ostream<char, std::char_traits<char> >& std::operator<< <std::char_traits<char> >(std::basic_ostream<char, std::char_traits<char> >&, char const*)
        jmp     .L2
.L3:
        mov     rax, QWORD PTR [rbp-8]
        mov     eax, DWORD PTR [rax+20]
        lea     edx, [rax+1]
        mov     rax, QWORD PTR [rbp-8]
        mov     DWORD PTR [rax+20], edx
        mov     rax, QWORD PTR [rbp-8]
        mov     edx, DWORD PTR [rax+20]
        mov     rax, QWORD PTR [rbp-8]
        movsx   rdx, edx
        mov     ecx, DWORD PTR [rbp-12]
        mov     DWORD PTR [rax+rdx*4], ecx
        mov     esi, OFFSET FLAT:.LC1
        mov     edi, OFFSET FLAT:std::cout
        call    std::basic_ostream<char, std::char_traits<char> >& std::operator<< <std::char_traits<char> >(std::basic_ostream<char, std::char_traits<char> >&, char const*)
        mov     rdx, rax
        mov     eax, DWORD PTR [rbp-12]
        mov     esi, eax
        mov     rdi, rdx
        call    std::basic_ostream<char, std::char_traits<char> >::operator<<(int)
.L2:
        leave
        ret
.LC2:
        .string "stack under flow"
.LC3:
        .string "deleted"
stack::pop():
        push    rbp
        mov     rbp, rsp
        push    rbx
        sub     rsp, 24
        mov     QWORD PTR [rbp-24], rdi
        mov     rax, QWORD PTR [rbp-24]
        mov     eax, DWORD PTR [rax+20]
        test    eax, eax
        jns     .L6
        mov     esi, OFFSET FLAT:.LC2
        mov     edi, OFFSET FLAT:std::cout
        call    std::basic_ostream<char, std::char_traits<char> >& std::operator<< <std::char_traits<char> >(std::basic_ostream<char, std::char_traits<char> >&, char const*)
        jmp     .L5
.L6:
        mov     rax, QWORD PTR [rbp-24]
        mov     eax, DWORD PTR [rax+20]
        lea     ecx, [rax-1]
        mov     rdx, QWORD PTR [rbp-24]
        mov     DWORD PTR [rdx+20], ecx
        mov     rdx, QWORD PTR [rbp-24]
        cdqe
        mov     ebx, DWORD PTR [rdx+rax*4]
        mov     esi, OFFSET FLAT:.LC3
        mov     edi, OFFSET FLAT:std::cout
        call    std::basic_ostream<char, std::char_traits<char> >& std::operator<< <std::char_traits<char> >(std::basic_ostream<char, std::char_traits<char> >&, char const*)
        mov     esi, ebx
        mov     rdi, rax
        call    std::basic_ostream<char, std::char_traits<char> >::operator<<(int)
.L5:
        add     rsp, 24
        pop     rbx
        pop     rbp
        ret
.LC4:
        .string " stack empty"
.LC5:
        .string " "
stack::display():
        push    rbp
        mov     rbp, rsp
        sub     rsp, 32
        mov     QWORD PTR [rbp-24], rdi
        mov     rax, QWORD PTR [rbp-24]
        mov     eax, DWORD PTR [rax+20]
        test    eax, eax
        jns     .L9
        mov     esi, OFFSET FLAT:.LC4
        mov     edi, OFFSET FLAT:std::cout
        call    std::basic_ostream<char, std::char_traits<char> >& std::operator<< <std::char_traits<char> >(std::basic_ostream<char, std::char_traits<char> >&, char const*)
        jmp     .L8
.L9:
        mov     rax, QWORD PTR [rbp-24]
        mov     eax, DWORD PTR [rax+20]
        mov     DWORD PTR [rbp-4], eax
.L11:
        cmp     DWORD PTR [rbp-4], 0
        js      .L8
        mov     rax, QWORD PTR [rbp-24]
        mov     edx, DWORD PTR [rbp-4]
        movsx   rdx, edx
        mov     eax, DWORD PTR [rax+rdx*4]
        mov     esi, eax
        mov     edi, OFFSET FLAT:std::cout
        call    std::basic_ostream<char, std::char_traits<char> >::operator<<(int)
        mov     esi, OFFSET FLAT:.LC5
        mov     rdi, rax
        call    std::basic_ostream<char, std::char_traits<char> >& std::operator<< <std::char_traits<char> >(std::basic_ostream<char, std::char_traits<char> >&, char const*)
        sub     DWORD PTR [rbp-4], 1
        jmp     .L11
.L8:
        leave
        ret
.LC6:
        .string " 1.push 2.pop 3.display 4.exit Enter ur choice"
.LC7:
        .string "enter the element"
main:
        push    rbp
        mov     rbp, rsp
        sub     rsp, 32
        lea     rax, [rbp-32]
        mov     rdi, rax
        call    stack::stack()
.L19:
        mov     esi, OFFSET FLAT:.LC6
        mov     edi, OFFSET FLAT:std::cout
        call    std::basic_ostream<char, std::char_traits<char> >& std::operator<< <std::char_traits<char> >(std::basic_ostream<char, std::char_traits<char> >&, char const*)
        lea     rax, [rbp-4]
        mov     rsi, rax
        mov     edi, OFFSET FLAT:std::cin
        call    std::basic_istream<char, std::char_traits<char> >::operator>>(int&)
        mov     eax, DWORD PTR [rbp-4]
        cmp     eax, 2
        je      .L14
        cmp     eax, 2
        jg      .L15
        cmp     eax, 1
        je      .L16
        jmp     .L13
.L15:
        cmp     eax, 3
        je      .L17
        cmp     eax, 4
        je      .L18
        jmp     .L13
.L16:
        mov     esi, OFFSET FLAT:.LC7
        mov     edi, OFFSET FLAT:std::cout
        call    std::basic_ostream<char, std::char_traits<char> >& std::operator<< <std::char_traits<char> >(std::basic_ostream<char, std::char_traits<char> >&, char const*)
        lea     rax, [rbp-4]
        mov     rsi, rax
        mov     edi, OFFSET FLAT:std::cin
        call    std::basic_istream<char, std::char_traits<char> >::operator>>(int&)
        mov     edx, DWORD PTR [rbp-4]
        lea     rax, [rbp-32]
        mov     esi, edx
        mov     rdi, rax
        call    stack::push(int)
        jmp     .L13
.L14:
        lea     rax, [rbp-32]
        mov     rdi, rax
        call    stack::pop()
        jmp     .L13
.L17:
        lea     rax, [rbp-32]
        mov     rdi, rax
        call    stack::display()
        jmp     .L13
.L18:
        mov     edi, 0
        call    exit
.L13:
        jmp     .L19
__static_initialization_and_destruction_0(int, int):
        push    rbp
        mov     rbp, rsp
        sub     rsp, 16
        mov     DWORD PTR [rbp-4], edi
        mov     DWORD PTR [rbp-8], esi
        cmp     DWORD PTR [rbp-4], 1
        jne     .L22
        cmp     DWORD PTR [rbp-8], 65535
        jne     .L22
        mov     edi, OFFSET FLAT:std::__ioinit
        call    std::ios_base::Init::Init()
        mov     edx, OFFSET FLAT:__dso_handle
        mov     esi, OFFSET FLAT:std::__ioinit
        mov     edi, OFFSET FLAT:std::ios_base::Init::~Init()
        call    __cxa_atexit
.L22:
        nop
        leave
        ret
_GLOBAL__sub_I_main:
        push    rbp
        mov     rbp, rsp
        mov     esi, 65535
        mov     edi, 1
        call    __static_initialization_and_destruction_0(int, int)
        pop     rbp
        ret

Hire Me For All Your Tutoring Needs
Integrity-first tutoring: clear explanations, guidance, and feedback.
Drop an Email at
drjack9650@gmail.com
Chat Now And Get Quote