来自输入的程序集最大值



简单的汇编程序,吐出两个用户输入数字中的较大者。我在正确输出时遇到问题。例如,如果我输入 45 和 55,最大值将是 55,但是当我尝试反向 55 和 45(答案仍然应该是 55)时,我得到 45。该程序似乎只输出在EAX输入和存储的第二个值。任何帮助将不胜感激。

.586
.MODEL FLAT
INCLUDE io.h
.STACK 4096
.DATA
value1 DWORD ?
value2 DWORD ?
prompt1 BYTE "Enter the first number", 0
prompt2 BYTE "Enter the second number", 0
string BYTE 40 DUP (?)
resultLbl BYTE  "The maximum value you entered was:", 0
.CODE
_MainProc PROC      
input prompt1, string, 40       ;get user input value1
atod string                     ;convert input (ASCII) to integer
mov ebx, eax
input prompt2, string, 40   ; repeat for value2
atod string
mov value2, eax
cmp eax, ebx                ;compare user inputs
jg greater_than     ;jump conditional if value 1 is greater then value 2
greater_than:   ;condition if greater than ouput
dtoa value1, eax                    ;decimal to ASC for output of 
integer value stored at ebx
output  resultLbl, value1           ;output value 1
jmp exit                    
less_than:  ;condition if less than ouput
dtoa value1, eax
output  resultLbl, value2           ;else output value 2    
jmp exit
exit:   ;quit jump              ;end if/else conditional
mov eax, 0          ;clear memory
mov ebx, 0
ret
_MainProc ENDP
END

问题在于指令的顺序。即使操作数 1 小于操作数 2,它唯一的区别是jg greater_than不会导致显式跳转到greater_than标签。但是,下一组指令是打印操作数 1,也就是说,执行控制将进入标签greater_than"内部"。为什么?那是因为下一个指令dtoa value1, eax正好在jg greater_than之后。那么,你现在看到问题了吗?如果操作数 1 小于操作数 2,则不会阻止打印操作数 1。您的代码所做的只是确保程序打印value1确定操作数 1 是否>操作数 2。

您的代码等效于以下 C 代码:

if (op1>op2)goto gt;
gt: 
printf("%d",op1);
exit(0);
lt:
printf("%d",op2);
exit(0);

虽然您打算执行以下操作:

if(op1>op2){
printf("%d",op1);
exit(0);
}
printf("%d",op2);
exit(0);

要解决此问题,您必须更改指令的顺序,如下所示:

jg greater_than     ;jump conditional if value 1 is greater then value 2
less_than:  ;condition if less than ouput
dtoa value1, ebx
output  resultLbl, value1           ;else output value 2    
jmp exit
greater_than:   ;condition if greater than ouput
dtoa value1, eax                    ;decimal to ASC for output of integer value stored at eax
output  resultLbl, value1           ;output value 1
jmp exit

上面的代码通过显式跳转到标签less_than确保在操作数 1>操作数 2 时阻止执行标签greater_than代码。当操作数 1 <= 操作数 2 时,不进行显式跳转,并执行序列中的下一条指令。在这种情况下,当操作数 1 <= 操作数 2 时,将执行less_than内部的指令。

简而言之,条件跳转只有在满足条件时才会导致跳转,否则将执行序列中的下一条指令。您有责任按正确的顺序排列您的说明。它们不同于高级条件构造,例如if...还。

更新:请注意,我的整个解决方案中只使用value1

正如Mayank Verma所说,如果你在此指令之后立即使用"jg greater_than"跳转,即使EAX <= EBX你执行的指令与EAX>EBX条件应该执行的指令相同,你也永远不会执行跳转到less_than。标签"less_than"可以删除。你应该写:

CMP EAX,EBX
JG greater_than
;less_than
dtoa value1, eax
output resultLbl, value2
jmp exit
greater_than:
integer value stored at ebx
output resultLbl, value1
exit:

但我有一个新的解决方案。这是一个优化的 MAX(A,B) 函数:

;  INPUT: EAX, EBX
; OUTPUT: EAX <- The maximum value between EAX, EBX
CMP EAX,EBX
CMOVL EAX,EBX

输入了3 个值。这看起来会起作用吗?

.586
.MODEL FLAT
INCLUDE io.h
.STACK 4096
.DATA
value1 DWORD ?
value2 DWORD ?
value3 DWORD ?
titleLbl BYTE   "MAXIMUM NUMBERS",0
formula BYTE "Taking any THREE random user input numbers, this program can 
determine which of those numbers is greater.",0
prompt1 BYTE "Enter the first number",0
prompt2 BYTE "Enter the second number",0
prompt3 BYTE "Enter the third number",0
string BYTE 40 DUP (?)
resultLbl BYTE  "The maximum value you entered was:", 0
max_value BYTE 11 DUP (?), 0
.CODE
_MainProc PROC  
output titleLbl, formula
input prompt1, string, 40       ;get user input value1
atod string                     ;convert input (ASCII) to integer
mov ebx, eax
input prompt2, string, 40   ; repeat for value2
atod string
mov ecx, eax
input prompt3, string, 40   ; repeat for value2
atod string
mov value3, eax
cmp ebx, ecx                ;compare user inputs
jg greater_than     ;jump conditional if value 2 is greater then value 3
less_than:  ;condition if less than ouput
cmp eax, ecx
jg greater_than_again   ;jump conditional if 2 is greater than 1
less_than_again: 
dtoa max_value, ecx
output  resultLbl, max_value          ;else output value 2    
jmp exit
greater_than_again:
dtoa max_value, eax                    ;decimal to ASC for output of 
integer value stored at eax
output  resultLbl, max_value           ;output value 1
jmp exit
greater_than:   ;condition if greater than ouput
cmp eax, ebx
jg greater_than_again   ;jump conditional if 2 is greater than 1
less_than_again2: 
dtoa max_value, ebx
output  resultLbl, max_value          ;else output value 2    
jmp exit
greater_than_again2:
dtoa max_value, eax                    ;decimal to ASC for output of 
integer value stored at eax
output  resultLbl, max_value           ;output value 1
jmp exit
exit:   ;quit jump              ;end if/else conditional
mov eax, 0          ;clear memory
mov ebx, 0
mov ecx, 0
ret
_MainProc ENDP
END

最新更新