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();
}
If it’s interesting to someone, here is the solution for my question(I found out it today with help of my tutor):
Algorithm: if
cmp MIN, axhas negative result, it meansaxis greater, thanMIN. So script jumps tom1label to compareaxvalue withMAX. Whencmp MIN, axreturns positive value, scripts assigns value ofaxregister toMINvariable and after that jumps to them2label to decrement loop counter. Algorithm of finding maximal value works similarly(labelm1).