Search code examples
assemblyx86dosx86-16masm

8086 assembly program to store unique values


I've been making this program that takes a list and finds unique elements in it and stores it in another list or register

the code below takes the main list into SI register and then BX acts as a pointer that walks through the whole array to find the similarities...if no similarities found the element in SI(which is stored in AL) gets stored in DI.. to avoid the first similarity that may occur when BX gets to the exact same location of it's value I set up a flag for that.


uniqueElement macro list1 list2
    local loop1,comparasionLabel,checkFlag,trigger,nextElement,addElement,endAddition
    lea si,list1
    lea di,list2 
loop1:
    mov ah,00H
    mov cx,00H   ;this is an intial flag gets triggered when an initial similarity is spotted
    lea bx,list1  ; this will be the search loop which will compare each of it's elements to SI
    mov al,BYTE PTR[SI] 
    cmp al,'$'  ; since I'm using a '$' terminated string
        je endAddition
comparasionLabel:
    mov dl,BYTE PTR[BX]
    cmp dl,'$'
        je addElement
    cmp al,dl
        je checkFlag
    inc bx
    jmp comparasionLabel    
            
checkFlag:           
    cmp cx,00H    ; this is when a first similarity is spotted, the flag gets triggered here
        je trigger
    cmp cx,01H       ; or if it was already triggered and another similarity spotted, SI moves to next element
        je nextElement     
trigger:
    mov cx,01H
    inc bx
    jmp comparasionLabel
nextElement:
    inc si
    jmp loop1
addElement:
    mov ah,00h
    mov BYTE PTR [di],al
    inc di
    inc si
    jmp loop1        
        
endAddition:
    inc di
    mov ah,00H
    mov al,'$'
    mov BYTE PTR[di],al               
endm


this is just the code that'll execute the macro

.model small
.data
list1 db 'H3llo$'    
list2 db 50 DUP [?]
.code  
mov ax,@data
mov ds,ax
uniqueElement list1 list2
mov ah,09H
mov dx,offset di
int 21h

.exit

and I have no Idea why it keeps printing the same list without removing the unique items


Solution

  • and I have no Idea why it keeps printing the same list without removing the unique items

    mov ah,09H
    mov dx,offset di
    int 21h
    

    The error is in the printing. The instruction mov dx,offset di is not correct! I would not be surprised if it would make MASM load the DX register will zero, and because the source text is at offset address zero, the outcome would be:

    h3llo
    

    The correct code is the following:

    mov  dx, offset list2
    mov  ah, 09h
    int  21h
    

    The rest of the code is correct except for an extraneous inc di that introduces one garbage character in the output string right before the terminating "$".


    Below is my rewrite of your algorithm, removing a lot of redundancies.
    I have taken an alternative approach because rather than flagging similarities, I count matches. Each unique character will produce a single match, and so a character is added to the output buffer only if the counter has CX=1.

        lea  si, list1
        lea  di, list2 
    loop1:
        mov  al, [si] 
        cmp  al, '$'
        je   endAddition
    
        xor  cx, cx       ; Counts matches, will become at least 1
        lea  bx, list1
    loop2:
        inc  bx
        mov  dl, [bx-1]
        cmp  dl, '$'
        je   maybeAddElement
        cmp  al, dl
        jne  loop2
        inc  cx
        jmp  loop2
    
    maybeAddElement:
        cmp  cx, 1
        jne  nextElement
        mov  [di], al
        inc  di
    nextElement:
        inc  si
        jmp  loop1        
            
    endAddition:
        mov  [di], al      ; AL='$'