comparing an array in assembly language

Discussion in 'Programmer's Corner' started by cupcake, Feb 11, 2011.

  1. cupcake

    Thread Starter Member

    Sep 20, 2010
    Hi, I wanna ask.. suppose I have an array and a constant called threshold in my data segments

    array db 1,2,3,4,5,6
    threshold equ 5

    I want to compare my array elements with the constant, and only display/print the number which is less that the threshold, if there are none, display the none message

    this is what I have done so far...I got error messages regarding the bold one

    mov ax, threshold
    mov al, array[bx]
    mov bx,al
    cmp al,ax
    jl print
    dec al
    jnz start2

    add bx,30h
    mov al,array[bx] ;store the number at the array
    mov ah,2 ;display the number at the screen
    mov dl,al
    add dl,30h
    int 21h
    inc bl ;increment index register
    jnz print

    can someone enlighten me?
  2. nigelwright7557

    Senior Member

    May 10, 2008
    mov bx,al is an error al is 8 bit and bx 16 bit.
  3. cjdelphi

    New Member

    Mar 26, 2009

    That's the issue, AL,AH are 8 bit registers, so when you move the threshold into ax..

    ax = 5

    now when you move the value off the array into al... AX becomes null you just overwrote the register!

    mov al,threshold
    mov ah,array[bx]


    cmp al,ah
    jle to print.... etc.
  4. cjdelphi

    New Member

    Mar 26, 2009
    If the compiler does not allow it, push it onto the stack and pop it or use some other way around it by using 2 16 bit registers.
  5. cupcake

    Thread Starter Member

    Sep 20, 2010
    ok, seems the compiler doesn't allow it..

    so, how I'm gonna use the pop and stack? I'm not so familiar with it..
  6. cupcake

    Thread Starter Member

    Sep 20, 2010
    here is my revision code

    Code ( (Unknown Language)):
    1. count equ 6
    3. start:
    4.     mov ax,data ;initialise data segment
    5.     mov ds,ax
    7.     xor ch,ch       ;initialize counter
    8.     mov cl,count   
    9.     xor bx,bx       ;initialize index register 
    12. start2:
    13.     mov dl,10d ;display line
    14.     mov ah,2h
    15.     int 21h
    16.     mov al,array[bx]
    17.     mov ah,threshold
    18.     cmp al,ah
    19.     jl print
    20.     [COLOR="Red"]inc bl
    21.     dec cl[/COLOR]
    22.     jnz start2
    24. print:
    25.     mov ah,2
    26.     mov al,array[bx]    ;display data on screen
    27.     add al, 30h
    28.     int 21h
    29.     [COLOR="red"]dec    bl      ;next piece
    30.     inc cl[/COLOR]
    31.     jnz print
    I'm not sure what I'm doing with the red portion, I'm thinking to increase the index register, so that I could traverse along the array and check the element what by one, but I think I didn't do it correctly since it's not working... any advise?