汇编语言 给出一组数 (例如20个数), 按照从小到大的顺序排序

assume ds:data  
data segment  
  a dw 1,4,2,5,7,9,6,3  
data ends  
code segment  
start:  
     mov ax,data  
     mov ds,ax  
     mov cx,8   
     dec cx  
    lop1:  
         push cx  
         mov dx,0  
         mov si,0  
         lop2:    
         mov bp,a[si]  
         cmp bp,a[si+2]  
         jnb go_on  
         xchg bp,a[si+2]  
         mov a[si],bp  
         mov dx,1 ;标志位  
      go_on:  
         add si,2  
         loop lop2  
         pop cx  
         cmp dx,0   
         jz over  
    loop lop1  
      over:  
      mov ax,4c00h  
      int 21h  
code ends  
end start

  • 6
    点赞
  • 23
    收藏
    觉得还不错? 一键收藏
  • 3
    评论
以下是使用x86汇编语言实现从键盘输入10个无符号从小到大排序并显示的代码: ``` section .data prompt db "Enter a number: ", 0 newline db 0Ah nums db 10, 0, 0, 0, 0, 0, 0, 0, 0, 0 fmt db "%u ", 0 section .text global _start _start: ; display prompt and read input mov edx, prompt mov ecx, 0 mov ebx, 1 mov eax, 4 int 0x80 mov ecx, nums mov ebx, 0 mov eax, 3 int 0x80 ; convert input to unsigned integers mov ecx, nums mov edi, ecx mov ebp, 10 convert_loop: ; read next byte movzx eax, byte [ecx] ; check if end of input cmp eax, 0 je sort ; convert byte to number sub eax, '0' movzx edx, byte [edi] imul edx, ebp add edx, eax mov byte [edi], dl ; move to next byte inc ecx inc edi jmp convert_loop ; sort numbers sort: mov ecx, nums mov esi, ecx mov edi, ecx inc edi sort_outer: cmp byte [esi], 0 je print_nums mov ecx, esi inc ecx sort_inner: cmp byte [ecx], 0 je next_outer movzx eax, byte [esi] movzx edx, byte [ecx] cmp eax, edx jle next_inner mov byte [esi], dl mov byte [ecx], al next_inner: inc ecx jmp sort_inner next_outer: inc esi mov edi, esi inc edi jmp sort_outer ; display sorted numbers print_nums: mov ecx, nums mov ebx, 1 mov eax, 4 int 0x80 mov edx, newline mov ecx, edx mov ebx, 1 mov eax, 4 int 0x80 mov ecx, nums mov ebx, 0 mov eax, 3 int 0x80 mov eax, 1 xor ebx, ebx int 0x80 ``` 在Linux操作系统中,使用nasm汇编器将代码汇编成可执行文件: ``` nasm -f elf32 -o sort_numbers.o sort_numbers.asm ld -m elf_i386 -o sort_numbers sort_numbers.o ``` 然后运行可执行文件: ``` ./sort_numbers ``` 程序将提示用户输入10个数字,然后按照从小到大顺序排序并显示。

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论 3
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值