簡體   English   中英

如何比較裝配中兩個陣列的元素?

[英]How to compare elements of two arrays in assembly?

我需要解決以下問題:
我需要在內存中放置4個數組,每個數組有10個數字,一個字節的大小。
現在,我需要找到一種方法來檢查一個字符串中的任何數字在另一個字符串中是否有一對,如果確實如此,則需要將這些答案疊加。

這是我到目前為止所做的:

arr1 db 1, 2, 3, 4, 5, 6, 7, 8, 9, 10
arr2 db 2, 11, 12, 13, 14, 15, 16, 17, 18, 19
arr3 db 1, 20, 21, 22, 23, 24, 25, 26, 27, 28
arr4 db 1, 29, 30, 31, 32, 33, 34, 35, 36, 37

lea si, arr1
lea di, arr2
mov al, 0
mov bl, 0

mov cx, 10
loopOne:
    loopTwo: 
        cmp [si+al],[di+bl]
        je done
        inc al
        loop loopTwo
    inc bl
    mov al, 0    
loop loopOne 
done: 
mov dl, si+al
inc 21h
ret

我正在使用emu8086。

編輯:

這是在Java中的樣子:

public class Main {

    public static void main(String[] args) {

        int[] arr1 = { 1, 2, 3, 4, 5, 6, 7, 8, 9, 10};
        int[] arr2 = { 1, 11, 12, 13, 14, 15, 16, 17, 18, 19};
        int[] arr3 = { 1, 20, 21, 22, 23, 24, 25, 26, 27, 28};
        int[] arr4 =  { 1, 29, 30, 31, 32, 33, 34, 35, 36, 37 };
        int a = 0; //counter of matches in every pair of arrays
        for (int i = 0; i < arr1.length ; i++) {
            for( int j = 0; j < arr2.length ; j++){
                if( arr1[i] == arr2[j]){
                    a++;
                }
            }
        }
        //instead of printing, the number of matches ( a ) should be pushed on to stack
        System.out.println("Number of matches: " + a);
        a = 0;
        for (int i = 0; i < arr1.length ; i++) {
            for( int j = 0; j < arr3.length ; j++){
                if( arr1[i] == arr3[j]){
                    a++;
                }
            }
        }
        System.out.println("Number of matches: " + a);
        a = 0;
        for (int i = 0; i < arr1.length ; i++) {
            for( int j = 0; j < arr4.length ; j++){
                if( arr1[i] == arr4[j]){
                    a++;
                }
            }
        }
        System.out.println("Number of matches: " + a);
        a = 0;
        for (int i = 0; i < arr2.length ; i++) {
            for( int j = 0; j < arr3.length ; j++){
                if( arr2[i] == arr3[j]){
                    a++;
                }
            }
        }
        System.out.println("Number of matches: " + a);
        a = 0;
        for (int i = 0; i < arr2.length ; i++) {
            for( int j = 0; j < arr4.length ; j++){
                if( arr2[i] == arr4[j]){
                    a++;
                }
            }
        }
        System.out.println("Number of matches: " + a);
        a = 0;
        for (int i = 0; i < arr3.length ; i++) {
            for( int j = 0; j < arr4.length ; j++){
                if( arr3[i] == arr4[j]){
                    a++;
                }
            }
        }
        System.out.println("Number of matches: " + a);
        a = 0;


    }

}

LOOP設計用於小的和簡單的循環。 當用於更長的計算或嵌套循環時,事情變得復雜。 我建議在這種情況下避免使用LOOP

cmp [si+al],[di+bl]錯誤。 您不能以這種方式比較內存中的兩個值。 所謂的字符串操作( scasmovscmps )在16位環境(MS-DOS)中不適合處理,尤其是對於此任務。 此外,您不能添加WORD( si )和BYTE( bl )。

我為您找到了第一個比較( arr1 / arr2 ),希望您可以自己添加其余比較。

.MODEL small
.STACK

include "emu8086.inc"

.DATA
    arr1 db 1, 2, 3, 4, 5, 6, 7, 8, 9, 10
    arr2 db 2, 11, 12, 13, 14, 15, 16, 17, 18, 19
    arr3 db 1, 20, 21, 22, 23, 24, 25, 26, 27, 28
    arr4 db 1, 29, 30, 31, 32, 33, 34, 35, 36, 37


.CODE
define_print_num_uns            ; emu8086.inc


start:
    mov ax, @data               ; Initialize DS
    mov ds, ax

    ; Compare arr1 and arr2
    lea si, arr1                ; Reset pointer to arr1
    mov cl, 10                  ; Length of arr1 = loop counter for loopOne
    mov bx, 0                   ; Counter for matches
    loopOne:                    ; Loop through arr1
        mov al, [si]            ; Load one element of arr1
        lea di, arr2            ; Reset pointer to arr2
        mov ch, 10              ; Length of arr2 = loop counter for loopTwo
        loopTwo:                ; Loop through arr2
            mov ah, [di]        ; Load one element of arr2
            cmp al, ah          ; Compare it
            jne @1              ; Skip the next line if no match
            inc bx              ; Increment match counter
            @1:
            inc di              ; Next element in arr2
            dec ch              ; Decrement loop counter
            jne loopTwo         ; Loop - break if CH == 0
        inc si                  ; Next elemnt in arr1
        dec cl                  ; Decrement loop counter
        jne loopOne             ; Loop - break if CL == 0
    mov ax, bx                  ; match counter into AX for print_num_uns
    call print_num_uns          ; emu8086.inc

    mov ax, 4C00h               ; MS-DOS function 4C: Exit program
    int 21h                     ; Call MS-DOS

end start

您對x86匯編的要求太多。
在任何給定的指令中,只能有一個存儲操作數。

您的cmp [si+al],[di+bl]有兩個,因此將不會匯編。
另外,您將cx用作2個循環的循環計數器。 那不管用。 在第一個循環完成后, cx將為0,即65536,這意味着外部循環+內部循環將運行64k次(oops)。

由於您的意圖尚不清楚,因此我無法為您提供代碼詳細信息。

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM