1

So I am tasked with writing assembly code that will perform a prefix sum on a set of numbers.

The example given was 2 4 6 -1 and the return needs to be 12 10 6. -1 serves as a stopper.

        jmp main
        prefix:     addl %edx, %eax
        ret


        print:      irmovl $32, %ecx
                    wrint %eax
                    wrch %ecx
                    ret


        read:       pushl %ebp           # ON_ENTRY saving old frame ptr 
                    rrmovl %esp, %ebp    # ON_ENTRY set new frame ptr

                    mrmovl 8(%ebp), %edx # Retrieving parameter
                    irmovl $1, %ecx  # writing ecx with 1
                    addl %ecx, %esi
                    addl %edx, %ecx      # adding edx and ecx
                    je baseCase          # checking if they equal 0

        recStep:    rdint %ebx       # reading parameter from user
                    pushl %ebx
                    call read
                    popl %ebx

                    mrmovl 8(%ebp), %edx

                    pushl %edx
                    call prefix
                    popl %edx


                    call print
                    jmp end
        baseCase:   irmovl $0, %eax

        end:        rrmovl %ebp, %esp # ON_EXIT reset stack ptr 
                    popl %ebp   # ON_EXIT restore old base/frame ptr 
                    ret     # ON_EXIT 

        main:       irmovl $0x1000, %esp    # init stack ptr 
                    irmovl $-1, %esi
                    rdint %ebx      # reading parameter from user

                    pushl %ebx  # pushing parameter
                    call read   # function call 
                    popl %ebx       # removing parameter


                    call prtnl 
                    halt



        prtnl:     irmovl $10, %edx  # assuming edx is caller save 
                   wrch %edx 
                   ret  

So basically my code prints 6 10 12 and I need to find a way to reverse this output. Any ideas?

greatwolf
  • 20,287
  • 13
  • 71
  • 105
JKT
  • 19
  • 4

1 Answers1

0

So basically my code prints 6 10 12 and I need to find a way to reverse this output. Any ideas?

Yeah, don't use recursion -- there's no reason to abuse the stack more than you have to.

Instead save each input onto the stack as you get it. Afterwards treat it like an array -- iterate through each item and compute the prefix sum for it. What I would do is use one register to indicate where the first item starts on the stack and use the difference between esp and that to get the array length. I would also use another register to accumulate the sum as the input is received.

The prefix sum for each element can be computed by subtracting accumulator from current array[i] and storing result back into the accumulator for the next element.

greatwolf
  • 20,287
  • 13
  • 71
  • 105
  • Thank you for the answer! I actually had to scrap this design and start all over. The project required recursion, even though it was incredibly inefficient. You are correct tho, if I got rid of recursion and saved each thing like an array, than this would have worked! – JKT Nov 12 '13 at 18:19