[英]Assembler task - min and max values of the array
I've encountered problems with assembler code. 我遇到了汇编代码问题。 I'm a newbie to assembler, so it seems difficult for me to solve it myself.
我是汇编程序的新手,所以我自己很难解决。
The task is: "To find minimal and maximal elements of the array." 任务是:“查找数组的最小和最大元素。”
All I've already done is searching for maximal element. 我已经做的就是搜索最大元素。 I can't find out, how to make check for the minimal element and where I should put such verification.
我不知道如何检查最小元素以及将验证放在何处。 Or, probably, I should loop through elements second time after finding maximal element?
或者,也许我应该在找到最大元素之后第二次遍历元素?
Code: 码:
#include <conio.h>
#include <stdio.h>
#include <iostream.h>
void main() {
int N = 10, i;
clrscr();
// on this platform, int is 16-bit
int a[] = { 1, 4, -6, 12, -25, 10, 3, -4, 15, 7}, MAX, MIN, RESULT;
__asm{
mov cx, N
lea si, a
lodsw
mov bx, ax
mov dx, ax
dec cx }
m:
__asm{
lodsw
cmp dx, ax
jge m1
mov dx, ax
}
m1:
__asm{
loop m
mov MAX, dx
}
cout << "Max = " << MAX;
//cout << "Min = " << MIN;
getch();
}
What happens if you replace "jge" with "jle"? 如果将“ jge”替换为“ jle”会怎样? Try it and see.
试试看。
If it's interesting to someone, here is the solution for my question(I found out it today with help of my tutor): 如果某人感兴趣,这是我的问题的解决方案(我今天在老师的帮助下找到了):
#include <conio.h>
#include <stdio.h>
#include <iostream.h>
void main() {
int N = 10, i;
clrscr();
int a[] = { 1, 4, -6, 12, -25, 10, 3, -4, 15, 7}, MAX, MIN, RESULT;
__asm{
mov cx, N
lea si, a
lodsw
mov MIN, ax
mov MAX, ax
dec cx
}
m:
__asm{
lodsw
cmp MIN, ax
jle m1
mov MIN, ax
jmp m2
}
m1:
__asm{
cmp MAX, ax
jge m2
mov MAX, ax
}
m2:
__asm{
loop m;
}
cout << "Max = " << MAX << "\n";
cout << "Min = " << MIN;
getch();
}
Algorithm : if cmp MIN, ax
has negative result, it means ax
is greater, than MIN
. 算法 :如果
cmp MIN, ax
结果为负,则意味着ax
比MIN
。 So script jumps to m1
label to compare ax
value with MAX
. 因此脚本跳到
m1
标签以将ax
值与MAX
进行比较。 When cmp MIN, ax
returns positive value, scripts assigns value of ax
register to MIN
variable and after that jumps to the m2
label to decrement loop counter. 当
cmp MIN, ax
返回正值,脚本将ax
寄存器的值分配给MIN
变量,然后跳转到m2
标签以递减循环计数器。 Algorithm of finding maximal value works similarly(label m1
). 寻找最大值的算法的工作原理相似(标签
m1
)。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.