Write a program in x86-64 assembly that sums all the elements of an array of 10…
- User
Write a program in x86-64 assembly that sums all the elements of an array of 10 integers, using a stack to store temporary values.
- Declare and define an integer array with a predefined size.
- Initialize a sum variable to zero.
- Set up the stack and establish the initial stack pointer.
- Use a loop to iterate over the elements of the array:
- Load the current array element into a register.
- Store the register value on the stack.
- Update the stack pointer.
- Add the register value to the sum variable.
- Restore the registers and the stack pointer to their original state.
- Display the sum result on the screen and/or make it available for later reading.
Wrap the resulting program in C for ease of use.
Assistant
The following program, written in x86-64 assembly and wrapped in a C main function, calculates the sum of elements in a predefined integer array. It specifically uses the stack to temporarily store each array element before adding it to the sum.
sum_array.asm(x86-64 Assembly Code); Filename: sum_array.asm ; ; This assembly program sums all elements of a predefined integer array. ; It demonstrates using the stack to store temporary values during the summation process. ; The sum is returned in the rax register, following x86-64 System V ABI. section .data ; 1. Declare and define an integer array with a predefined size. ; array_dw: An array of 10 32-bit (dword) integers. array_dw dd 10, 20, 30, 40, 50, 60, 70, 80, 90, 100 ARRAY_SIZE equ 10 ; Number of elements in the array ELEMENT_SIZE equ 4 ; Size of each element in bytes (dword = 4 bytes) section .text ; Make the 'sum_array_asm' function globally visible for the linker. global sum_array_asm ; sum_array_asm function: Sums all elements of 'array_dw'. ; This function adheres to the x86-64 System V ABI: ; - Callee-saved registers (like rbp, rbx) must be preserved. ; - Return value is placed in rax. sum_array_asm: ; 3. Set up the stack and establish the initial stack pointer. ; Standard function prologue: push rbp ; Save the caller's base pointer (rbp) onto the stack. mov rbp, rsp ; Set the current stack pointer (rsp) as the new base pointer (rbp). ; rbx is a callee-saved register, meaning this function must preserve its value. ; We'll use rbx to accumulate the sum, so we save its original value first. push rbx ; Save the original value of rbx. ; 2. Initialize a sum variable to zero. ; Clear rbx, which will act as our sum accumulator. xor rbx, rbx ; Set rbx to 0 (sum = 0). ; Set up rdi to point to the beginning of the array. ; rdi is a caller-saved register, so we don't need to save its original value. mov rdi, array_dw ; rdi = address of the first element of 'array_dw'. ; Set up rcx as the loop counter. ; rcx is a caller-saved register. mov rcx, ARRAY_SIZE ; rcx = 10 (number of elements). ; 4. Use a loop to iterate over the elements of the array: .loop_start: ; Loop termination condition: Check if the loop counter has reached zero. cmp rcx, 0 ; Compare rcx with 0. je .loop_end ; If rcx is 0, jump to '.loop_end' (loop has finished). ; 4.1. Load the current array element into a register (eax). ; 'eax' is the lower 32-bit part of 'rax'. Loading a 32-bit value into 'eax' ; automatically zero-extends it into the 64-bit 'rax' register. mov eax, dword ptr [rdi] ; Load the 4-byte integer from [rdi] into eax. ; 4.2. Store the register value on the stack. ; 4.3. Update the stack pointer. ; Push the current element's value (from rax) onto the stack. ; 'push rax' decrements rsp by 8 bytes (size of a qword). ; This demonstrates using the stack for temporary storage. push rax ; To explicitly show using the stack for a temporary value, ; we immediately pop the value back from the stack into another register (rdx). ; 'pop rdx' increments rsp by 8 bytes. pop rdx ; 4.4. Add the register value to the sum variable. ; Add the element (now in rdx) to our sum accumulator (in rbx). add rbx, rdx ; Move the array pointer to the next element. add rdi, ELEMENT_SIZE ; rdi = rdi + 4 (point to the next dword). ; Decrement the loop counter. dec rcx ; Decrement rcx by 1. ; Jump back to the start of the loop. jmp .loop_start .loop_end: ; 5. Restore the registers and the stack pointer to their original state. ; The function's return value must be placed in rax. mov rax, rbx ; Move the final sum from rbx to rax. ; Standard function epilogue: Restore callee-saved registers. ; Pop in reverse order of pushing. pop rbx ; Restore the original value of rbx. pop rbp ; Restore the caller's base pointer (rbp). ret ; Return from the function (pops the return address from the stack).main.c(C Wrapper Code)// Filename: main.c #include <stdio.h> // Required for printf function. // Declare the assembly function. // 'extern' tells the C compiler that 'sum_array_asm' is defined elsewhere (in assembly). // The return type 'long long' matches the 64-bit value returned in rax by the assembly. extern long long sum_array_asm(); int main() { // Call the assembly function to compute the sum of the array elements. long long total_sum = sum_array_asm(); // 6. Display the sum result on the screen. printf("The sum of the array elements is: %lld\n", total_sum); return 0; // Indicate successful program execution. }How to Compile and Run:
-
Save the files:
- Save the assembly code as
sum_array.asm. - Save the C code as
main.c.
- Save the assembly code as
-
Open a terminal or command prompt.
-
Assemble the assembly code using NASM:
- For Linux/WSL/Cygwin:
nasm -f elf64 sum_array.asm -o sum_array.o - For macOS:
nasm -f macho64 sum_array.asm -o sum_array.o
- For Linux/WSL/Cygwin:
-
Compile the C code using GCC:
gcc -c main.c -o main.o -
Link the assembled object file and the compiled C object file:
gcc sum_array.o main.o -o sum_program -
Run the executable:
./sum_program
Expected Output:
The sum of the array elements is: 550-
Other models, same or similar prompt
Responses were produced in different, uncontrolled contexts (system prompts, settings, prior turns). This is not a benchmark.