找出栈上传递的最大的两个数相乘,returnDX:AX对
Find the two largest numbers passed on the stack and multiply them, return DX:AX pair
我有一个作业,我们在堆栈上传递了 4 个值(v1、v2、v3、v4),要从四个值中找到两个最大值,然后将它们相乘得到 return 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
在 returning 之前将其从堆栈中删除。因此,当 ret
指令执行并尝试从堆栈中获取 return 地址时,它会获取 bp
的旧值。
我有一个作业,我们在堆栈上传递了 4 个值(v1、v2、v3、v4),要从四个值中找到两个最大值,然后将它们相乘得到 return 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
在 returning 之前将其从堆栈中删除。因此,当 ret
指令执行并尝试从堆栈中获取 return 地址时,它会获取 bp
的旧值。