gpt4 book ai didi

linux - nasm x86 查找最大和最小值并存储在寄存器中

转载 作者:塔克拉玛干 更新时间:2023-11-03 00:46:32 24 4
gpt4 key购买 nike

到目前为止,我了解如何获得最大的数字并将其存储在寄存器中,但我不确定如何继续获得最小的数字?任何帮助将不胜感激,甚至是一种更有效的方式来做我目前正在做的事情。我试图在 ax 中存储最大数字,在 dx 中存储最小数字

    section .data

A dw 1
B dw 3
C dw 8
D dw 5

section .bss

section .text
global _start

_start: mov eax,0 ;clear registers
mov ecx,3
mov esi,A
mov ax, [esi]
add esi,2

again: cmp ax,[esi]
jg cont
mov ax,[esi]

cont: add esi,2
dec ecx
cmp ecx,0
jnz again

exit: mov eax,1
mov ebx,0
int 80h

最佳答案

我会这样做的方式是在测试后添加另一个比较如果当前值大于当前最大值(存储在 ax 中),则将测试当前元素是否小于当前最小值(存储在 dx 中)。您还需要最初将 dx 设置为与第一个元素相同。

我添加了 dx 寄存器的初始设置和一个额外的部分“isSmaller”,它执行检查并在当前检查的值小于存储在 dx 中的值时设置 dx 寄存器。

section .data

A dw 1
B dw 3
C dw 8
D dw 5

section .text
global _start

_start: mov eax,0 ;clear registers
mov ecx,3
mov esi,A
mov ax, [esi] ; Initially set largest
mov dx, [esi] ; and initial smallest
add esi,2

again: cmp ax,[esi]
jg isSmaller
mov ax,[esi]

isSmaller: cmp dx, [esi]
jl cont
mov dx, [esi]

cont: add esi,2
dec ecx
cmp ecx,0
jnz again

exit: mov eax,1
mov ebx,0
int 80h

关于linux - nasm x86 查找最大和最小值并存储在寄存器中,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/36580652/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com