title: Assembly (68k) stack operations
body: <p>I am having trouble making this code work and was hoping to get some insight here.</p>
  
  <p>The program is supposed to read two numbers from input (>0 and &lt;300) and output their sum and product. We are supposed to use the stack to pass values to subroutines.</p>
  
  <p>My program runs and reads the input and displays output, but it is not the correct output. I'm assuming my stack operations are off, but I can't figure out what's missing.</p>
  
  <p>Here is my code so far.</p>
  
  <p>EDIT: I have a functioning code except for the multiplying part. I know for certain that the next items on the stack are the input numbers but the Multiplier subroutine does not calculate the correct product value. Can anyone take a look at the subroutine and check what is wrong?
              ORG    $1000
      START:                  ; first instruction of program</p>
  
  <pre><code>* Put program code here
  
  First:
      move    #14,D0      Display string
      lea     prompt1,A1  
      trap    #15      
      move    #4,D0       Input num1 to D1
      trap    #15
      move.l  D1,-(SP)    push num1 to stack
      move.l  D1,D4       move num1 to keep
  
      move.l  #300,D2     Upper limit  
      cmp     D1, D2      Compare user input to upper lim
      bhi     Second
      blo     Error1
  
  Second:    
      move    #14,D0      Display string
      lea     prompt2,A1 
      trap    #15    
      move    #4,D0       Input num2
      trap    #15
      move.l  D1,-(SP)    push num2
      move.l  #0,-(SP)    make room for sum on the stack
      move.l  D1,D5       move num2 to keep
  
      move.l  #300,D2     Upper limit  
      cmp     D1, D2      Compare user input to upper lim
      bhi     MoveOn
      blo     Error2
  
  MoveOn:
      *add
      bsr     Adder
      move.l  (SP)+,D1    pull sum, D1 = sum
      lea     (8,SP),SP   clean up stack
      move    #14,D0      Display string
      lea     result1,A1
      trap    #15
      move    #3,D0
      trap    #15
  
      bsr     newLine
  
      move.l  D4,-(SP)    push num1 to stack
      move.l  D5,-(SP)    push num2 to stack
  
      *multiply
      bsr     Multiplier
      move.l  (SP)+,D1    pull product, D1 = prod
      lea     (8,SP),SP   clean up stack
      move    #14,D0      Display string
      lea     result2,A1
      trap    #15
      move    #3,D0
      trap    #15
  
      SIMHALT             ; halt simulator
  
  Error1:
      move    #14,D0 
      lea     error,A1
      trap    #15
      move    (SP)+,D3    Pull incorrect num1 from stack
      bsr     newLine
      bra     First
  
  Error2:
      move    #14,D0  
      lea     error,A1
      trap    #15
      move    (SP)+,D3    Pull incorrect num2 from stack
      bsr     newLine
      bra     Second
  
  
  
  *----------------------------
          offset  4+4
  sum     ds.l    1
  num2   ds.l    1
  num1    ds.l    1
          org     *
  
  Adder
      link    A0,#0           create stack frame
      move.l  (num1,A0),D0  
      add.l   (num2,A0),D0   
      move.l  D0,(sum,A0)    
      move.l  (SP)+,D0        
      unlk    A0
      rts
  
  *----------------------------
      offset  4+4
  prod    ds.l    1
  num4    ds.l    1
  num3    ds.l    1
          org     *
  
  Multiplier
      link    A0,#0           create stack frame
      move.l  (num3,A0),D0    
      mulu    (num4,A0),D0    
      move.l  D0,(prod,A0)    
      move.l  (SP)+,D0        
      unlk    A0
      rts
  
  newLine 
      movem.l d0/a1,-(SP)         push d0 &amp; a1
      move    #14,d0              task number into D0
      lea     crlf,a1             address of string
      trap    #15                 display return, linefeed
      movem.l (SP)+,d0/a1         restore d0 &amp; a1
      rts                         return
  
  * Put variables and constants here
  prompt1 dc.b    'Sláðu inn fyrri tölu: ',0
  prompt2 dc.b    'Sláðu inn seinni tölu: ',0
  error   dc.b    '** Tala er ekki á réttu bili, reyndu aftur **',0
  result1 dc.b    'Summa talnanna er: ',0
  result2 dc.b    'Margfeldi talnanna er: ',0
  crlf    dc.b    $d,$a,0
      END    START        ; last line of source
  </code></pre>