3 Replies - 242 Views - Last Post: 17 January 2019 - 07:14 AM

#1 zavisnik   User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 3
  • Joined: 14-January 19

emu8086 comparing two arrays

Posted 14 January 2019 - 04:37 PM

Hello everyone, I hope you can help me with something..

I need to make two arrays, and a function to find the largest number that both arrays have.
So if array1 has 1,3,7,10 and array2 has 3,5,12,15, output should be: 3

I managed to find largest number from one array, but don't know how to compare it with another one..
I had an idea of making third array that would store only numbers that both arrays have, but I don't even know if that's possible in assembly, I'm still a beginner.
What would be an easiest solution to this problem?

data segment
    niz db 1,3,7,10,16,22,23,8,11,12  
    niz2 db 2,6,9,15,21,24,8,13,26,14
    ;output should be: 8
    len dw $-niz
    max db ?
data ends

code segment
assume ds:data cs:code

pocetak:
    mov ax,data
    mov ds,ax
    lea si,niz
    mov al,niz[si]
    mov max,al
    mov cx,len

ponovi:
    mov al,niz[si]
    cmp max,al
    jg promene_bez
    mov max,al

promene_bez:
    inc si
    loop ponovi
    mov ah,4ch
    int 21h
    
code ends
end pocetak



Thanks!

Is This A Good Question/Topic? 0
  • +

Replies To: emu8086 comparing two arrays

#2 zavisnik   User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 3
  • Joined: 14-January 19

Re: emu8086 comparing two arrays

Posted 14 January 2019 - 06:59 PM

I found soulution myself! :bananaman:
If someone needs the code:

.data
niz1 db 10 dup (15,3,7,10,16,22,23,8,11,12)
niz2 db 10 dup (2,6,9,15,21,24,8,13,26,14)   
max db 10 dup (0)
     
.code
pocetak:
    mov ax, @data     
    mov ds, ax

    lea si, niz1                
    mov cl, 10                              
        petlja:                 
        mov al, [si]          
        lea di, niz2           
        mov ch, 10             
            petlja2:            
            mov ah, [di]        
            cmp al, ah          
            jne @1       
            cmp max,al   
            ja @1       
            mov max,al  
                @1:
                inc di         
                dec ch          
            jne petlja2         
            inc si              
            dec cl             
        jne petlja             
              
    int 21h
end pocetak


Was This Post Helpful? 0
  • +
  • -

#3 turboscrew   User is offline

  • D.I.C Lover
  • member icon

Reputation: 171
  • View blog
  • Posts: 1,107
  • Joined: 03-April 12

Re: emu8086 comparing two arrays

Posted 16 January 2019 - 10:54 PM

There is no simpler way than to compare each number in one array to each number in the other array, and remembering the maximum.

in pseudo:

max = 0
for each element1 in array 1
  for each element2 in array 2
    if element1 equals element2
      if element1 is greater than max then
         set max to element1
      endif
    endif
  endfor
endfor


And if it can't be done in assembly, it can't be done (with that device) at all.

This post has been edited by turboscrew: 16 January 2019 - 10:56 PM

Was This Post Helpful? 1
  • +
  • -

#4 zavisnik   User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 3
  • Joined: 14-January 19

Re: emu8086 comparing two arrays

Posted 17 January 2019 - 07:14 AM

Yup, that is exactly how I did it.
Was This Post Helpful? 0
  • +
  • -

Page 1 of 1