+1 (315) 557-6473 

Calling x86 assembly functions from C homework help

The homework deals with implementing an assembly function to sum and display an array using x86 assembly. The function is used by the main function which is implemented in C. The function uses the x86 calling convention and uses the standard C library function printf to display the array. The function uses the stack to read the arguments and save the registers used. Below is how to call x86 assembly functions demonstrated by our C programming homework helpers.

Table Of Contents
  • Summing and Displaying an Array

Summing and Displaying an Array

Calling x86 assembly functions from C assignment help

section .data format: db "%d,",0 section .text global _sumAndPrintList extern printf ; Sum and print list, ; C Prototype: ; int _sumAndPrintList(int *list, int length); _sumAndPrintList: push ebp ; save ebp mov ebp,esp ; move stack pointer to ebp push ebx ; save ebx contents on the stack push esi ; save esi contents on the stack mov esi,[ebp+8] ; read the array pointer from the stack into esi mov ecx,[ebp+12] ; read the array length from the stack into ecx mov ebx,0 ; ebx will contain the sum, start in zero printLoop: mov eax,[esi] ; load a word from the array add esi,4 ; advance to next word in array add ebx,eax ; add number to the total sum push ecx ; save value of ecx push eax ; push value to print in the stack push format ; push printf format call printf ; print number on screen add esp,8 ; restore stack by popping the 2 arguments pop ecx ; restore ecx value loop printLoop ; repeat for all the numbers in array mov eax,ebx ; put sum in eax to return the value pop esi ; load old contents of esi from the stack pop ebx ; load old contents of ebx from the stack pop ebp ; load old contents of ebp from the stack ret ; return to calling function #include extern int _sumAndPrintList(int *list, int length); int main() { int list[1000]; // list of 1000 numbers int count,done,sum; count = 0; done=0; while(done==0) // read while the user is not done entering numbers { printf("Enter a number (0 to exit): "); scanf("%d",&list[count]); // read the number and save it in the list if(list[count]==0) // if the user entered a zero, terminate { count--; // discard the 0 done=1; } count ++; if(count>=1000) // if the user entered 1000 numbers, terminate { printf("You have entered 1000 numbers!\n"); done=1; } } printf("\nList of numbers:\n"); sum=_sumAndPrintList(list,count); printf("\nThe sum of the numbers in the list is: %d\n",sum); printf("\nSo long, and thanks for all the fish!\n\n"); return 0; }