0

I am trying to create a linear search in assembly, and I want it to print the lowest value of the array. What I have prints the answer as 1 which is correct but if I change the arrays lowest value from 1 to 3, it wont show 3 as the lowest value.

The program declares an array of 20 elements initialized with different integer values. Then calls procedure findmin which computes the smallest element in the array in stores it in variable min. This is the code...

.model small

.stack 32

 

.data

 

arr DB 1,2,3,4,5,6,7,8,9,11,12,13,14,15,16,17,18,19,20,21

 

arr_len EQU $-arr

 

minimum DB ?

 

.code

MAIN PROC FAR

 

mov AX, @data

mov DS, AX

 

call Findmin

 

mov AX, 4C00H

int 21H

 

MAIN endp

 

Findmin proc NEAR

 

mov cx, arr_len

mov bx, OFFSET arr

 

Search:

mov al, [bx]

cmp al, [minimum]

jnb NoSwap

mov [minimum], al

 

NoSwap:

inc bx

loop Search

 

mov al, minimum

mov ax, 4C00h

int 21h

 

Findmin endp

 

end MAIN

I'd appreciate any help. Thank you

Peter Cordes
  • 328,167
  • 45
  • 605
  • 847
Zaywillaa
  • 27
  • 4
  • Set `minimum` to `-1`. – xiver77 Mar 29 '22 at 04:48
  • So you are saying minimum DB ? should be minimum DB EQU -1? – Zaywillaa Mar 29 '22 at 05:02
  • That's what @xiver77 is saying; you're doing unsigned compares, so you need to start with a min that's either the first element of the array, or the highest possible unsigned 8-bit number, i.e. `-1 = 0xff`. Also, keep your current `min` in another register, not reloading / storing to memory all the time inside the loop! – Peter Cordes Mar 29 '22 at 05:04
  • If you single-step with a debugger and look at `minimum`, you should see either a `0` (the lowest possible unsigned value) or some random garbage. You didn't say what output you do get so it's not a proper [mcve], but any debugging should make the problem obvious. – Peter Cordes Mar 29 '22 at 05:06

0 Answers0