汇编语言 三个数利用分支结构从大到小排序
时间: 2023-08-12 15:09:58 浏览: 17
下面是一个使用汇编语言的程序,可以将三个数从大到小排序:
```
; 定义三个变量
num1 dw 5
num2 dw 9
num3 dw 2
; 定义跳转标签
start:
mov ax, num1
cmp ax, num2 ; 比较num1和num2
jg swap1 ; 如果num1大于num2,则跳转到swap1
jmp compare2 ; 否则跳转到compare2
swap1:
xchg ax, num2 ; 交换num1和num2
mov num1, ax
jmp compare2
compare2:
mov ax, num2
cmp ax, num3 ; 比较num2和num3
jg swap2 ; 如果num2大于num3,则跳转到swap2
jmp done ; 否则跳转到done
swap2:
xchg ax, num3 ; 交换num2和num3
mov num2, ax
jmp done
done:
; 排序完成,这里可以输出结果或进行其他操作
ret
```
这个程序使用了三个变量 `num1`、`num2` 和 `num3` 来存储要排序的三个数。它使用分支结构来比较和交换这些数,以便将它们从大到小排序。
相关问题
汇编语言 三个数利用分支结构从大到小排序并输出
下面是一个基于x86汇编语言的程序,可以将三个数从大到小排序并输出:
```
section .data
msg db 'The sorted numbers are: ',0
num1 db 'First number: ',0
num2 db 'Second number: ',0
num3 db 'Third number: ',0
nl db 10,0
section .bss
input resb 2
temp resb 2
section .text
global _start
_start:
; Get the first number
mov eax, 4
mov ebx, 1
mov ecx, num1
mov edx, 13
int 80h
mov eax, 3
mov ebx, 0
mov ecx, input
mov edx, 2
int 80h
mov bl, byte [input]
sub bl, 48
mov bh, 0
mov byte [temp], bx
; Get the second number
mov eax, 4
mov ebx, 1
mov ecx, num2
mov edx, 14
int 80h
mov eax, 3
mov ebx, 0
mov ecx, input
mov edx, 2
int 80h
mov bl, byte [input]
sub bl, 48
mov bh, 0
mov byte [temp+1], bx
; Get the third number
mov eax, 4
mov ebx, 1
mov ecx, num3
mov edx, 13
int 80h
mov eax, 3
mov ebx, 0
mov ecx, input
mov edx, 2
int 80h
mov bl, byte [input]
sub bl, 48
mov bh, 0
mov byte [temp+2], bx
; Sort the numbers
mov bl, byte [temp]
mov cl, byte [temp+1]
cmp bl, cl
jge swap1
mov bl, byte [temp+1]
mov byte [temp+1], byte [temp]
mov byte [temp], bl
swap1:
mov bl, byte [temp+1]
mov cl, byte [temp+2]
cmp bl, cl
jge swap2
mov bl, byte [temp+2]
mov byte [temp+2], byte [temp+1]
mov byte [temp+1], bl
mov bl, byte [temp]
mov cl, byte [temp+1]
cmp bl, cl
jge swap1
mov bl, byte [temp+1]
mov byte [temp+1], byte [temp]
mov byte [temp], bl
; Print the sorted numbers
mov eax, 4
mov ebx, 1
mov ecx, msg
mov edx, 22
int 80h
mov eax, 4
mov ebx, 1
mov ecx, temp
mov edx, 3
int 80h
mov eax, 4
mov ebx, 1
mov ecx, nl
mov edx, 1
int 80h
mov eax, 4
mov ebx, 1
mov ecx, temp+1
mov edx, 3
int 80h
mov eax, 4
mov ebx, 1
mov ecx, nl
mov edx, 1
int 80h
mov eax, 4
mov ebx, 1
mov ecx, temp+2
mov edx, 3
int 80h
mov eax, 4
mov ebx, 1
mov ecx, nl
mov edx, 1
int 80h
; Exit the program
mov eax, 1
xor ebx, ebx
int 80h
```
这个程序使用Linux系统调用来获取用户输入和输出结果。它先要求用户输入三个数字,然后使用分支结构来对它们进行排序。最后,它将排序后的数字打印出来并退出程序。
汇编语言 三个数利用分支结构从大到小排序并能够在masm5环境下直接看到
下面是一个在 MASM5 环境下能够直接运行的三个数从大到小排序的汇编语言程序:
```
.model small
.stack 100h
.data
a db 3
b db 2
c db 1
.code
main proc
mov ax, @data
mov ds, ax
; 比较 a 和 b
mov al, a
cmp al, b
jge ab
xchg al, b
; 比较 b 和 c
cmp al, c
jge bc
xchg al, c
cmp b, c
jge ab
; 输出结果
mov dl, a
add dl, 30h
mov ah, 2
int 21h
mov dl, ','
mov ah, 2
int 21h
mov dl, b
add dl, 30h
mov ah, 2
int 21h
mov dl, ','
mov ah, 2
int 21h
mov dl, c
add dl, 30h
mov ah, 2
int 21h
mov ah, 4ch
int 21h
ab:
; 比较 a 和 c
cmp al, c
jge ac
xchg al, c
; 输出结果
mov dl, a
add dl, 30h
mov ah, 2
int 21h
mov dl, ','
mov ah, 2
int 21h
mov dl, c
add dl, 30h
mov ah, 2
int 21h
mov dl, ','
mov ah, 2
int 21h
mov dl, b
add dl, 30h
mov ah, 2
int 21h
mov ah, 4ch
int 21h
ac:
; 输出结果
mov dl, c
add dl, 30h
mov ah, 2
int 21h
mov dl, ','
mov ah, 2
int 21h
mov dl, a
add dl, 30h
mov ah, 2
int 21h
mov dl, ','
mov ah, 2
int 21h
mov dl, b
add dl, 30h
mov ah, 2
int 21h
mov ah, 4ch
int 21h
main endp
end main
```
上述程序中,我们将三个数分别存储在变量 `a`、`b` 和 `c` 中,然后依次比较这三个数的大小,并使用 `xchg` 指令交换变量的值,以达到从大到小排序的效果。最后,我们使用 `int 21h` 中断调用来输出排序后的结果。
相关推荐
![asm](https://img-home.csdnimg.cn/images/20210720083646.png)
![docx](https://img-home.csdnimg.cn/images/20210720083331.png)
![doc](https://img-home.csdnimg.cn/images/20210720083327.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)