Mouhib Boucherika
Mouhib Boucherika

Reputation: 3

infinite loop in assembly 8086

So I have a bit of a problem here with a loop.I want an array to be of size n inputed by the user which can be no smaller than 5 and which elements are given by the user then give me the smallest value in the array.The problem is that the loop works for far more than n and then for min value it just prints all the elements of the array.

data segment
    value_n    db  13,10,'Entrer n : $'    
    text        db  13,10,' Entrer un chiffre : $'
    mini        db  13,10,' Min : $'
    n           dw  ?              ;taille du tableau              
    tab1        db  n dup(0)
    k           dw  4
    j           dw  11
ends

stack segment
    db 128 dup(0)
ends

code segment
    lea dx,n
    mov ah,09h
    int 21h
    
    mov ah,01
    int 21h
    
    k equ 4
    j equ 11
    
    cmp dx,4
    jle instruction   

    
    instruction :
    mov ax,data
    mov ds,ax
    mov ax,stack
    mov ss,ax
    
    mov ax,00h
    mov si,00h
    mov cx,dx
    mov [200h],3Ah  ;min
    
    i:
    mov  ah,09h
    mov  dl,offset text
    int  21h
    mov  ah,01h
    int  21h
    mov  tab1[si],al 
    cmp  al,[200h]
    jl   min
    x:
    cmp  al,[201h]
    jmp  v
    min: 
    mov  [200h],al
    jmp  x
    v:
    inc  si
    loop i
    
    mov  ah,02h
    mov  dl,0Ah
    int  21h
    
    mov  ah,09h
    mov  dl,offset mini
    int  21h
    mov  ah,02h
    mov  dl,[200h]
    int  21h
    mov  ah,09h
    int  21h
    mov  ah,02h
    mov  dl,[201h]
    int  21h
    
    mov  ax,4C00h
    int  21h
ends

Upvotes: 0

Views: 391

Answers (1)

Nate Eldredge
Nate Eldredge

Reputation: 58132

The handling of dx at the beginning seems wrong.

lea dx, n loads dx with the address of n (by the way, mov dx, offset n is equivalent and saves 1 byte). There are no further writes to dx, and the int 21h calls would preserve it, so the subsequent cmp dx, 4 is comparing the address of n with the number 4, which doesn't make much sense. If it's less than or equal (signed compare, also seems odd) then you jump to instruction. If it isn't... then you fall through into instruction anyway, so the whole cmp/jle sequence was just pointless.

So you end up in instruction with dx still equal to the address of n. You move that into cx and use it as your loop count. That surely doesn't seem right.

So: what is it that you are trying to compare against 4? Whatever it is, it isn't in dx at that point. And when you do make the compare, if the jle is not taken, you need to write some code after that to say what should happen instead.

By the way, your assumption that the memory at ds:[200h] is free and safe to overwrite also seems really sketchy.

Upvotes: 1

Related Questions