Warning: file_get_contents(/data/phpspider/zhask/data//catemap/0/assembly/5.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Assembly 在程序集8086中的给定数组中查找最小值_Assembly_Emu8086_X86 16 - Fatal编程技术网

Assembly 在程序集8086中的给定数组中查找最小值

Assembly 在程序集8086中的给定数组中查找最小值,assembly,emu8086,x86-16,Assembly,Emu8086,X86 16,我编写了一个代码来计算给定数组中的最小值,其思想是取第一个元素(考虑到它是最小值),并将其与其余元素进行比较,然后交换值,以防发现较小的元素,下面是我的代码: array dw 7,4,12,5,1 mov si,00h mov ax,array[si] mov cx,5 minimum: inc si ;find the minimum value mov dx,array[si] cmp ax,dx jb nochange swap: xchg ax,dx nochang

我编写了一个代码来计算给定数组中的最小值,其思想是取第一个元素(考虑到它是最小值),并将其与其余元素进行比较,然后交换值,以防发现较小的元素,下面是我的代码:

array dw 7,4,12,5,1

mov si,00h
mov ax,array[si]


mov cx,5

minimum:

inc si ;find the minimum value 
mov dx,array[si]
cmp ax,dx 
jb nochange 

swap:
xchg ax,dx

nochange:
dec cx 
cmp cx,0
JNE minimum

lastcmp:  ; to compare the last item with the minimum value and swap if it's smaller  
mov dx,array[si]
cmp ax,dx
jb endi
xchg ax,dx 


end 
但是我在这里似乎遇到了一个问题,因为它比较了所有元素,但不是最后一个元素,所以它总是给我(4)和(1),任何帮助

处理的数组总共只有5个元素。您在单独的步骤中删除了第一个元素,因此您的代码只能与其余4个元素进行比较


由于数组包含字,您需要将
SI
寄存器增加2,以便前进到下一个数组元素。请记住,在类似于
mov-dx的指令中,数组[si]
[si]
部分实际上是数组中的偏移量(以字节数表示的位移)。它不像通常的高级语言那样是一个索引


这里的
cmp-cx,0
指令是非常无用的,因为
dec-cx
指令已经根据随后的条件跳转的需要定义了零标志。将代码缩短为:

dec cx
jnz minimum


你为什么认为你需要这最后一部分?结果已在
AX
寄存器中。此外,由于您没有更改
SI
寄存器,因此此额外比较只会复制您在循环中执行的上一次比较

该死的。。。永远不要使用
数组[si]
永远不要!你不知道你在做什么。您的数组是单词数组,而不是字节数组!程序集中没有索引数组访问!你是说最后一步“最后一步”,但我把它移到了dx寄存器以便比较!?不,我的意思是,@MargaretBloom试图告诉你的是,
array[si]
使用字节偏移量。尽管它看起来像数组索引,例如在C中,数组索引会自动按项目大小进行缩放。但在组装过程中并非如此,您必须亲自说明这一点。在这种情况下,这意味着每次都以
2
的方式递增
si
。@jester非常感谢您,现在一切正常;)
inc si
dec cx 
cmp cx,0
JNE minimum
dec cx
jnz minimum
lastcmp:  ; to compare the last item with the minimum value and swap if it's smaller
mov dx,array[si]
cmp ax,dx
jb endi
xchg ax,dx