程序需求:从键盘上输入3个有符号的双字整数,编写一完整的程序按照由大到小的顺序输出这三个数。
编程思路:三个数每两个数进行比较,如果后一个数大于前一个数则交换两个变量的值。
开发环境
Win10 + VS2017
C语言代码实现如下:
#include <stdio.h>
int value1 = 0;
int value2 = 0;
int value3 = 0;
int main()
{
printf("Please input three number.\n");
scanf("%d%d%d", &value1, &value2, &value3);
if (value1 < value2)
{
int tmp = value1;
value1 = value2;
value2 = tmp;
}
if (value1 < value3)
{
int tmp = value1;
value1 = value3;
value3 = tmp;
}
if (value2 < value3)
{
int tmp = value2;
value2 = value3;
value3 = tmp;
}
printf("%d > %d > %d\n",value1,value2,value3);
return 0;
}
汇编语言代码实现如下:
INCLUDELIB kernel32.lib
INCLUDELIB ucrt.lib
INCLUDELIB legacy_stdio_definitions.lib
.386
.model flat,stdcall
ExitProcess PROTO,
dwExitCode:DWORD
printf PROTO C : dword,:vararg
scanf PROTO C : dword,:vararg
.data
msg byte 'Please input three number.',10,0
format1 byte '%d%d%d',0
format2 byte '%d > %d > %d',10,0
value1 dword 0
value2 dword 0
value3 dword 0
.code
main Proc
invoke printf,offset msg
invoke scanf,offset format1,offset value1,offset value2,offset value3
mov eax,dword ptr value1
mov ebx,dword ptr value2
cmp eax,ebx
jge next
mov dword ptr value1,ebx
mov dword ptr value2,eax
next:
mov eax,dword ptr value1
mov ebx,dword ptr value3
cmp eax,ebx
jge next2
mov dword ptr value1,ebx
mov dword ptr value3,eax
next2:
mov eax,dword ptr value2
mov ebx,dword ptr value3
jge next3
mov dword ptr value2,ebx
mov dword ptr value3,eax
next3:
invoke printf,offset format2,dword ptr value1,dword ptr value2,dword ptr value3
push 0h
call ExitProcess
main endp
end main