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

时间:2015-07-27 12:26:21

标签: sorting memory assembly x86 x86-16

我有一个赋值,我们在堆栈上传递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                           ;
;---------------------------------------

1 个答案:

答案 0 :(得分:4)

您在功能开始时在堆栈上推送pop。但是在返回之前你没有ret它离开堆栈。因此,当bp指令执行并尝试从堆栈中获取返回地址时,它将获得旧值#!/bin/bash date > currentdate