sorting - Find the two largest numbers passed on the stack and multiply them, return DX:AX pair -
i have assignment passed 4 values on stack (v1, v2, v3, v4), find 2 largest values out of four, , multiply them return dx:ax pair.
this code have come far, comparing values 1 , storing highest value in ax , second highest value in bx. problem code hangs when tested in dosbox, , i'm not sure causing it.
edit: finished , working!
;--------------------------------------- ; ; code segment ; ;--------------------------------------- _linkhll: push bp ; saves caller's bp mov bp,sp ; loads bp sp mov ax,[bp+4] ;load v1 ax mov bx,[bp+6] ;load v2 bx ;--------------------------------------- ; find first largest number ;--------------------------------------- cmp ax,bx ;compare value 1 , 2 je doubles cmp ax,bx ja l2 ;ax > bx, goto l2 mov ax,bx ;make v2 largest number l2:mov bx,[bp+8] ;load v3 bx cmp ax,bx ;compare value ax , v3 je doubles cmp ax,bx ja l3 ;ax > bx, goto l3 mov ax,bx ;make v3 largest number l3:mov bx,[bp+10] ;load v3 bx cmp ax,bx ;compare value ax , v4 je doubles ja s1 ;ax > bx, goto l3 mov ax,bx ;make v4 largest number jmp s1 doubles: mov cx,[bp+8] ;mov v3 cx cmp ax,cx ; bx > cx ja v4bigger ; yes, skip v4 test mov ax,cx ;if no, make cx new ax v4bigger: mov cx,[bp+10] ;v4 cx cmp bx,cx ;compare current highest ja mult mov bx,cx jmp mult ;--------------------------------------- ; find second largest number ;--------------------------------------- s1:mov bx,[bp+4] ;load v1 bx mov cx,[bp+6] ;load v2 cx cmp ax,bx ;compare value ax , v1 je v2mov ;ax = bx, multiply them cmp ax,cx ;compare value ax , v2 je s2 ;ax = cx, mov cx bx , multiply cmp bx,cx ;compare v1 , v2 ja s2 ;bx > cx goto s2 v2mov: mov bx,cx ; make v2 current second highest s2:mov cx,[bp+8] ;load v3 cx cmp ax,cx ;compare value ax , v3 je s3 ;ax = cx, goto s3 cmp bx,cx ;compare ax , v3 ja s3 ;bx > cx goto s3 v3mov: mov bx,cx ;mov v3 2nd highest number spot s3:mov cx,[bp+10] ;load v4 cx cmp ax,cx ;compare value ax , v4 je mult ;ax = cx, goto s3 // mult???? cmp bx,cx ;compare ax , v3 ja mult ;bx > cx goto s3 v4mov: mov bx,cx ;make v4 second highest number mult: mul bx ;multiply ax bx pop bp ; ; ; ret ; ; end ; ;---------------------------------------
you're pushing bp
on stack @ start of function. don't pop
off stack before returning. hence, when ret
instruction executes , tries return address off stack gets old value of bp
instead.
Comments
Post a Comment