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 ;
;---------------------------------------