如何在不使用Assembler循环的情况下对3个变量进行排序?

问题描述 投票:0回答:1

我想在汇编程序中编写一个程序,向用户询问3个变量,然后用户将数字写入寄存器(我知道如何编写),但现在我遇到了一个问题:我必须使用条件/对这3个变量进行排序无条件跳转(我不能使用循环)。所以,在我看来这个程序会很长,因为我必须为每一组编写9个比较。你有什么想法如何写这个程序更短?

assembly nasm
1个回答
1
投票

基本的“3值排序”:

if(a > b) swap(a, b)
if(a > c) swap(a, c)   // a must be the smallest value now
if(b > c) swap(b, c)   // b must be the second smallest value, c must be the biggest value

在32位80x86汇编(NASM语法)中:

    cmp eax,ebx
    jna .l1
    xchg eax,ebx
.l1:
    cmp eax,ecx
    jna .l2
    xchg eax,ecx
.l2:
    cmp ebx,ecx
    jna .l3
    xchg ebx,ecx
.l3:
; eax must contain smallest value, ebx the second smallest, ecx the biggest
© www.soinside.com 2019 - 2024. All rights reserved.