-2

So I am writing a program and subroutine where this is basically the pseudocode.

int findmin(int* vals, int count){
    if(count == 1){
         return vals[0];
          }else{
          int minrest = findmin(vals+1,count-1);
              if (minrest < vals[0]){
              return minrest
              }else{
               return vals[0]
                   }
             }
        }

I basically have to put this into m68k assembly code. In the pictures is what i have so far. I think my logic is correct but for some reason all this does is printout my header and nothing else, I feel like for some reason I am not corectly storing my result into D0 where it should be. Is there a step I am missing or something that I am just completely off on? My prog4.s is my main that calls the subroutine

My prog4.s is my main that calls the subroutine subroutine recursive function

1 Answers1

0

The main code calls the subroutine like this:

    pea     vals
    move.w  D1, -(SP)
    jsr     findmin
    adda.l  #6, SP

The findmin subroutine makes next recursive call:

    move.l  A0, -(SP)
    move.w  D1, -(SP)
    jsr     findmin
    adda.l  #6, SP

The findmin subroutine retrieves its parameters like:

findmin:
    link    A6, #0
    ...
    move.w  8(A6), D1
    move.l  12(A6), A0      <<< Here is the error!

But wait, because of how you placed things on the stack, this is how the stack is laid out:

          A6  RTS  D1  A0
lower    ==== ==== == ====    higher
         ^
         SP == A6

The above shows that the word D1 is at 8(A6) and that the longword A0 is at 10(A6). That's 10 instead of 12.

Sep Roland
  • 33,889
  • 7
  • 43
  • 76
  • I'm surprised that the compiler doesn't just push everything as a long regardless of its actual size, that would make the pointer arithmetic easier. – puppydrum64 Aug 11 '22 at 16:05