1

I have an assignment where we are passed 4 values on the stack (v1, v2, v3, v4), are to find the two largest values out of the four, and then multiply them together to return the DX:AX pair.

This is the code that I have come up with so far, comparing all the values to one another and storing the highest value in AX and the second highest value in BX. The problem is that the code hangs when tested in DOSbox, and I'm not sure what is causing it.

EDIT: Finished and working!

;---------------------------------------
;
; Code Segment
;
;---------------------------------------
_linkhll: 

    push bp     ; saves the caller's bp
    mov bp,sp   ; loads bp from sp                  

      MOV AX,[bp+4] ;Load v1 to AX
      MOV BX,[bp+6] ;Load v2 to BX
;---------------------------------------
; Find the first largest number
;---------------------------------------

   CMP AX,BX        ;compare value 1 and 2
      JE doubles
      CMP AX,BX
      JA  L2        ;AX > BX, goto L2
      MOV AX,BX     ;make v2 the largest number

   L2:MOV BX,[bp+8] ;Load v3 to BX
      CMP AX,BX     ;compare value AX and v3
      JE doubles
      CMP AX,BX
      JA  L3        ;AX > BX, goto L3
      MOV AX,BX     ;make v3 the largest number

   L3:MOV BX,[bp+10]    ;Load v3 to BX
      CMP AX,BX     ;compare value AX and v4
      JE doubles
      JA  S1        ;AX > BX, goto L3
      MOV AX,BX     ;make v4 the largest number
      JMP s1

doubles:
      MOV CX,[bp+8] ;mov v3 to cx
      CMP AX,CX     ; BX > CX
      JA  v4bigger  ; yes, skip to v4 test
      MOV AX,CX     ;if no, make CX the new AX
v4bigger:
      MOV CX,[bp+10]    ;v4 to CX 
      CMP BX,CX     ;Compare to current highest
      JA mult    
      MOV BX,CX
      JMP mult




;---------------------------------------
; Find the second largest number
;---------------------------------------

   S1:MOV BX,[bp+4] ;Load v1 to BX
      MOV CX,[bp+6] ;load v2 to CX
      CMP AX,BX     ;compare value AX and v1
      JE  v2mov     ;AX = BX, multiply them
      CMP AX,CX     ;compare value AX and v2
      JE  s2        ;AX = CX, mov cx to bx and multiply
      CMP BX,CX     ;Compare v1 and v2
      JA  S2        ;BX > CX goto S2
v2mov:
      MOV BX,CX     ; make v2 the current second highest

   S2:MOV CX,[bp+8] ;load v3 to CX
      CMP AX,CX     ;compare value AX and v3
      JE  s3        ;AX = CX, goto S3
      CMP BX,CX     ;Compare AX and v3
      JA  S3        ;BX > CX goto S3
v3mov:
      MOV BX,CX     ;mov v3  to 2nd highest number spot

   S3:MOV CX,[bp+10]    ;load v4 to CX
      CMP AX,CX     ;compare value AX and v4
      JE  mult      ;AX = CX, goto S3 // mult????
      CMP BX,CX     ;Compare AX and v3
      JA  mult      ;BX > CX goto S3
v4mov:
      MOV BX,CX     ;Make v4 second highest number

mult:
      MUL BX        ;multiply ax by bx

      POP BP


                               ;
                                       ;
                                       ;
         ret                           ;
                                       ;
         end                           ;
;---------------------------------------
CadetFayed
  • 67
  • 3
  • 10
  • You only need to do five comparisons. Compare the first two arguments to see which highest. Then compare the third to see whether its highest or second highest, and then again for the fourth. – Ross Ridge Jul 27 '15 at 16:43

1 Answers1

4

You're pushing bp on the stack at the start of your function. But you don't pop it off the stack before returning. Hence, when the ret instruction executes and tries to get the return address off the stack it gets the old value of bp instead.

Michael
  • 57,169
  • 9
  • 80
  • 125
  • Added POP BP (mistake as i was programming this really late last night) and it did get the programming running. Only problem is that i gets the correct answer only some of the time. Maybe a 65/35 split. Would you happen to know why? – CadetFayed Jul 27 '15 at 14:47
  • If you have an example input that always gives incorrect output, then you can trace the execution for that input, either with a debugger or with pen and paper to see what's happening as each instruction executes. – Michael Jul 27 '15 at 15:09
  • Thats exactly how i did it @michael. It took a little while, but that;s the route I endd up going. Thanks for the help – CadetFayed Jul 27 '15 at 22:22