This is a part of my homework. I have completed part 1. Part 2 asks that I re-write my part 1:

"Write a modified version of part A, in which the function is to be called as though it had the following prototype:

void sumPowers(int n, int *pSum);

is this asking me to input n and a pointer to the sum or the address at which sum is stored?

An attachment of my code:

[SECTION .data]

n:      dd      0
fmt1:   db      "%d", 0
fmt2:   db      "%d", 10, 0

[SECTION .text]

        global  main
        extern scanf, printf

main:   push    EBP
        mov     EBP, ESP
        push    EBX
        push    ESI
        push    EDI

first:
        push    n
        push    fmt1
        call    scanf
        add     ESP, 8

cool:
        mov     EDI,[n]
        push    EDI
        call    sumPowers
        add     ESP, 4

        push    EAX
        push    fmt2
        call    printf
        add     ESP, 8

last:   pop     EDI
        pop     ESI
        pop     EBX
        mov     ESP, EBP
        pop     EBP
        ret

sumPowers:
        push    EBP
        mov     ESP,EBP

        mov     ECX, 2
        mov     EBX, 2
        mov     EDX,3
        mov     EAX, 0          ; Initialize the counter
        mov     ESI, EDI        ; Store n in ESI
        sub     ESI,1

loop1:  cmp     EAX, ESI        ; 
        jge     step1
        imul    ECX,EBX
        add     EDX,ECX
        add     EAX,1
        jmp     loop1
 
step1:  mov     EAX, ECX        ; Initialize the sum
        pop     EBP
        ret

Do you think you've implemented
int sumPowers(int n);

Because you're still missing a small step.
You don't pull the parameter off the stack.

The program works. It results in EDI having the value of 63 when I entered 5, which was the intended value. I need to figure out how to modify my code though so it passes the parameters (int n, int *pSum). Im not even too sure what my professor is asking though.

Is he asking for me to pass a parameter that points to the sum in the given location. For example if I pass the parameters (5, 2) 5 would normally return 63. However in this case it would only return 4. It would perform sumPowers(5) resulting in (1,2,4,8,16,32) and point to the second position....meaning 4?

I didn't say that it didn't work.

I said it wouldn't work if you called it from a C program which expects to put parameters on the stack.

It "works" because you know the parameter is still in EDI, and the push EDI you do is in fact a waste of time (at present).

So, I talked to my professor and apparently all I am doing is taking the value in EDI, adding w/e to EBP until I get to the pointer address (sum) in the stack. Then I move the value in EDI into the pointer address.

Ok, so I completely fixed my code taking your advice to the best of my ability. Removing alot of unecessary push commands. My program now works, however when exiting the next1 label and proceeding to step1 my values are lost. I believe this is because printf and scanf are known to mess with values in EAX, EBX, ECX, and EDX. If someone could please tell me if this is the case, I can just put the *pSum in EDI or ESI instead of ECX. If this isn't the case, could someone please point out what I am doing wrong. My main works, my function works, but transfering data from the function back into the main is a catastrophe.

Thank You,

Code:

[SECTION .data]

sum:    dd      0
n:      dd      0
fmt1:   db      "%d", 0

[SECTION .text]

        global  main
        extern scanf, printf

main:   push    EBP
        mov     EBP, ESP
        push    EBX
        push    ESI
        push    EDI

first:
        push    dword sum
        push    dword n
        push    fmt1
        call    scanf
        add     ESP, 12

cool:
        mov     EDI, [n]
        push    EDI
        call    sumPowers
        add     ESP, 4
        call    printf
        add     ESP, 4

last:   pop     EDI
        pop     ESI
        pop     EBX
        mov     ESP, EBP
        pop     EBP
        ret

sumPowers:
        push    EBP
        mov     ESP,EBP

        mov     ECX, 2
        mov     EBX, 2
        mov     EDX,3
        mov     EAX, 0          ; Initialize the counter
        mov     ESI, EDI        ; Store n in ESI
        sub     ESI,1

loop1:  cmp     EAX, ESI        
        jge     next
        imul    ECX,EBX
        add     EDX,ECX
        add     EAX,1
        jmp     loop1

next:
        mov     ECX, EDX
        mov     ECX, [EBP + 12]
 
step1:  mov     ESP, EBP      ; Initialize the sum
        pop     EBP
        ret

disregard the line push EDI, it has been removed and I forgot to remove it before posting. Also, do I even need to have fmt1? It seems like I am not implementing it at all. I literally just learned arrays last week and now my professor randomly decided to move onto implimentation of C functions (When I don't know C well, I took courses in java....therefore, I didn't even know what a pointer was until 2 days ago.).

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.