Reputation: 21
I am trying to find the smallest value in a given array, and the code I used is this
MOV SI,500
MOV CL,[SI]
MOV CH,00
INC SI
MOV AL,[SI]
DEC CL
INC SI
L1:CMP AL,[SI]
JNC SKIP
MOV AL,[SI]
SKIP:INC SI
LOOP L1
MOV [600],AL
HLT
It compiles fine and runs fine, then I go to "aux" and then to "memory" to type in my input values. I set the address as 0100:0500 and then I give input "01" "02" "03" "04" "05" then against the first row, like this -- https://i.sstatic.net/EMyiU.png and i click update, and then "RUN" and then go to aux-->memory and check the address 0600, which is where i guess the output would be, and i get just zeroes, like this --- https://i.sstatic.net/fBcPd.png what is wrong with my code? why am i not getting the minimum value and just zeroes in 0600? I am a total beginner to the 8086 programming, please help.
Upvotes: 0
Views: 60
Reputation: 9899
Don't you need setting the DS
segment register to 0100h on top of your program?
You clearly expect to address memory at 0100h:0500h.
Don't you have to use the hexadecimal suffix?
MOV SI,500
uses a decimal 500; you need MOV SI,0500h
for hexadecimal!
Note: If you're using a debugger then the hexadecimal notation could well be the default. If you're using a normal assembler then using the correct prefix or suffix is essential.
The loop runs for much too long!
MOV SI,500
MOV CL,[SI] <<<< If THIS reads 01 as is expected...
MOV CH,00
INC SI
MOV AL,[SI]
DEC CL <<<< then THIS will produce 0
INC SI
L1:CMP AL,[SI]
JNC SKIP
MOV AL,[SI]
SKIP:INC SI
LOOP L1 <<<< So THIS runs 65536 times.
MOV [600],AL
HLT
In order to find the minimum you will have to change the jnc skip
instruction to jb skip
. Currently you're looking for the maximum.
This is a version that you could try. As always: don't just copy but try to understand how it works.
mov ax, 0100h
mov ds, ax
mov si, 0500h
mov cx, 5 ;The number of values in the array
mov al, 255 ;This will become the mininum
L1:
cmp al, [si]
jb SKIP
mov al, [si]
SKIP:
inc si
loop L1 ;This now runs 5 times.
mov [0600h], al
hlt
It would be a good idea to try the code with data that is a bit more random. Perhaps use 3, 2, 5, 1, 4.
Upvotes: 1