Search code examples
assemblyx86-16tasm

Finding the minimum in an array (Intel8086)


I'd like to write a simple program to find the minimum value in an array. I'm using Intel 8086 architecture (if I'm right?). The problem is that I am totally new to Assembly language and well, I just cannot figure out what am I doing wrong in my code, I'm stuck.

At the end (before exiting) my ax register does not contain my result.
I'm afraid it doesn't even place it there (I check registers' values with Turbo Debugger tool).

.MODEL TINY

Data SEGMENT
                        arr_len     EQU 5
                        arr         DB 07h, 02h, 03h, 10h, 12h
                        minimum     DB 255
Data ENDS

Code SEGMENT
                        ORG 100h
                        ASSUME CS:Code, DS:Data, SS:Code
Start:
                        mov ax, SEG Data
                        mov ds, ax  ;load data to ds reg    
                        mov cx, arr_len ;arr_length to counter
                        mov bx, OFFSET arr ;load first elem.
Search: 
                        inc bx
                        cmp bl, [minimum] ;compare loaded elem with min
                        jb Swap ;if bl<minimum
Swap:
                        mov minimum, bl
                        loop Search         

                        mov al, minimum ;result here?
                        mov ax, 4C00h
                        int 21h

Code ENDS
END Start

Could anyone give me advices what is wrong here? Thanks in advance.


Solution

    • The tiny model doesn't need setting DS explicitly. (CS=DS=ES=SS)
    • Since BX points at your array the first time, you should not immediately increment it! Do inc before looping only.
    • You didn't really compare an array element but rather the low byte of the address by which you iterate the array.
    • There's no sense in putting any result in AL if directly afterwards you exit by putting 4C00h in AX. Viewing AL using a debugger could work though.

    Here's a version that can work:

    Search: 
        mov al, [bx]
        cmp al, [minimum] ;compare loaded elem with min
        jnb NoSwap        ;if al>=minimum
        mov [minimum], al ;New minimum
    NoSwap:
        inc bx
        loop Search