汇编语言代码,用于在数组中查找正数

问题描述 投票:0回答:1

我们只能编辑粗体部分以从给定数组中找到正数。这是我在visual basic中尝试过的,我只是将结果变为零,有人可以说出错吗?

int solution(const int arr[], size_t arr_size)
{
    int result = 0;
    __asm
    {
        **MOV eax, arr
        MOV edx, eax
        MOV ebx, 10
        XOR ecx, ecx
        LEA esi, size arr
        NEXT2 : 
        MOV edi, esi
                SHR edi, 10
                JNC NEXT1
                JMP NEXT3
                NEXT1 : INC ecx
                        NEXT3 : INC SI
                                DEC ebx
                                JNZ NEXT2
        MOV[result], ecx;**
    }
        return result;
}
    int main()
    {
        int result;
        int arr[] = { 0, -1, 2, -3, 4, -5, 6, -7, 8, -9 };
        result = solution(arr, sizeof(arr) / sizeof(arr[0]));
        printf("Grade 6 result = %d\n", result);
        getchar();
        return 0;
    }
arrays inline-assembly
1个回答
0
投票

1)这是我用来获取数组大小的一段代码:

    mov     ebx, 0              ; set ebx to zero to start checking at index 0
countloop:
    inc     ebx                 ;Increase ebx for next loop

    cmp     arr[ebx], '\0'      ;compare arr at index [ebx] to end char '\0'
    jne     countloop           ;if not equal, jump back and try for next index 

    mov     arrlength, ebx      ;if equal to '\0', load the value of ebx (actual length of the array) into the empty length variable

你查找'\ 0'的原因是字符串像char数组一样存储,寄存器只存储第一个char,循环来获取另一个,直到它得到'end'字符。我相信其他角色会让循环停止,不确定哪一个,但是\ 0确实有效

2)然后使用存储在arrlength中的值作为检查数组并找到正数所需的循环数。

    MOV ecx, arrlength  ; This sets the loop counter register (ECX) to the size of your array
    MOV ebx, 0          ; Set this to 0 as we will use it again as index
    MOV esi, 0          ; Same 

compareLoop:     
    MOV eax, arr[ebx]   ; load value of arr at index ebx

    CMP eax, 0          ; sets flag, comparing eax to 0

    JL  lessThan        ;JL --> jump if first operand lower than second operand

    MOV newArr[esi], eax  ;Put the value (which is >0, in a new array) 
    add esi, 4           ; To point to the next position   

lessThan:
    add   ebx, 4        ; adding for to ebx so that now it has the index of next value in array

    loop  compareLoop   ; until ecx = 0

我基本上向你展示了我将如何做到这一点,我远非一个专业人士,只是不明白你的方式。

© www.soinside.com 2019 - 2024. All rights reserved.