找到堆栈上传递的最大的两个数字并将它们相乘,返回DX:AX对



我有一个赋值,在这里我们在堆栈上传递4个值(v1、v2、v3、v4(,从四个值中找出最大的两个值,然后将它们相乘以返回DX:AX对。

这是我到目前为止编写的代码,将所有值相互比较,并将最高值存储在AX中,将第二高值存储在BX中。问题是代码在DOSbox中测试时挂起,我不确定是什么原因导致的

编辑:完成并工作!

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

您在函数开始时将bp推送到堆栈上。但在返回之前,您不会将其从堆栈中pop。因此,当ret指令执行并试图从堆栈中获取返回地址时,它会获得bp的旧值。

最新更新