2017-03-26 76 views
1

我正在使用MASM & Irvine 32位程序集,並且我有數組A,B,C和堆疊執行A + B = C,將數組A的每個[i]項與數組B和寫入陣列C正確地求和數組

例如,

arrA 1, 2, 4, 1 
+ 
arrB 2, 1, 1, 3 
= 
arrC 3, 3, 5, 4 

我試圖用指針來工作,但我有00,00,00,0F輸出。

不關注StrHex_MY程序,它在輸出數組上測試。

代碼:


.586 
.model flat, stdcall 
ExitProcess PROTO, dwExitCode:DWORD 

include \Irvine\Irvine32.inc 
includelib \Irvine\kernel32.lib 
includelib \Irvine\user32.lib 
include module.inc 

.data 
    CaptionGreet BYTE "Test me", 0 

    arrA DWORD 1, 2, 4, 1 
    arrB DWORD 2, 1, 1, 3 
    arrC DWORD 0, 0, 0, 0 

    toOut DB 64 dup(?) 

.code 

main PROC 

    mov edi, OFFSET arrA ; Address of arrA 
    mov esi, OFFSET arrB ; Address of arrB 

    mov eax, 0 ; Register with result 

    mov ecx, LENGTHOF arrA ; Lenght of arrays 

    L1: 
     add eax, [edi] ; Add current arrA element to eax 
     add eax, [esi] ; Add current arrB element to eax 

     add edi, TYPE arrA ; Move pointer to the next arrA element 
     add esi, TYPE arrB ; Move pointer to the next arrB element 

     mov arrC, eax ; Move current eax value to arrC 

     loop L1 

    ; Converting result to HEX toOut. Don't pay attention to this part 
    ; ---- 
    push OFFSET toOut 
    push OFFSET arrC 
    push 256 
    call StrHex_MY 
    ; --- 

    ; Output result 
    INVOKE MessageBoxA, 0, ADDR toOut, ADDR CaptionGreet, 0 
    INVOKE ExitProcess,0 

main ENDP 
END main 

+0

所以它輸出0F,現在我堆棧訪問每個arrC元素並用arrA + arrB當前元素的總和重寫它。 – NLis

回答

0

我想你想的arrA每個項目增加的arrB其對應的項目,將其存儲到arrC對應的項目。所以

add eax, [edi] ; Add current arrA element to eax 

是錯誤的。你必須「重新初始化」 EAX與項目:

mov eax, [edi] ; Copy current arrA element to eax 

您需要第三個指針ARRC。現在您將結果重複存儲到arrC的第一項。我選擇EBX作爲第三指針。

INCLUDE Irvine32.inc 

.data 
    CaptionGreet BYTE "Test me", 0 

    arrA DWORD 1, 2, 4, 1 
    arrB DWORD 2, 1, 1, 3 
    arrC DWORD 0, 0, 0, 0 

    toOut DB 64 dup(?) 

.code 

main PROC 

    mov edi, OFFSET arrA ; Address of arrA 
    mov esi, OFFSET arrB ; Address of arrB 
    mov ebx, Offset arrC ; Address of arrC 

    mov eax, 0    ; Register with result 

    mov ecx, LENGTHOF arrA ; Length of arrays 

    L1: 
     mov eax, [edi]  ; Copy current arrA element to eax 
     add eax, [esi]  ; Add current arrB element to eax 

     add edi, TYPE arrA ; Move pointer to the next arrA element 
     add esi, TYPE arrB ; Move pointer to the next arrB element 

     mov [ebx], eax  ; Move current eax value to current arrC element 
     add ebx, TYPE arrC ; Move pointer to the next arrC element 

     loop L1 

    mov esi, OFFSET arrC 
    mov ecx, 4 
    mov ebx, 4 
    call DumpMem 


    INVOKE ExitProcess,0 

main ENDP 

END main 
+0

謝謝! 我想知道爲什麼它完美的工作,即使沒有 'mov esi,OFFSET arrC,mov ecx,4,mov ebx,4'代碼。 你能解釋一下爲什麼我們需要它嗎? – NLis

+0

該塊準備'DumpMem'的參數,Irvine庫的函數用於轉儲數組。看看http://programming.msjc.edu/asm/help/index.html?page=source%2Firvinelib%2Fdumpmem.htm – rkhb

0

我也改進了@rkhb的回答,所以現在它不僅執行arrA添加到相應的arrB,但它也可以與進位標誌一起使用。因此,如果我們想要執行多精度加法操作,我們需要將數字作爲數組呈現並使用進位標誌。

沒有找到很多在互聯網上就可以了信息,因此如果需要的話,那就是:

,因爲它概括這兩個陣列中的所有元素

.586 
.model flat, stdcall 
ExitProcess PROTO, dwExitCode:DWORD 

include \Irvine\Irvine32.inc 
includelib \Irvine\kernel32.lib 
includelib \Irvine\user32.lib 
include module.inc 

.data 
    CaptionGreet BYTE "Last >> Last 
    arrA DWORD 80010001h, 80020001h, 80030001h 
    arrB DWORD 80000001h, 80000001h, 80000001h 
    arrC DWORD 0, 0, 0 

    toOut DB 64 dup(?) 

.code 

main PROC 

    mov edi, OFFSET arrA ; Address of arrA 
    mov esi, OFFSET arrB ; Address of arrB 
    mov ebx, Offset arrC ; Address of arrC 

    mov eax, 0    ; Register with result 
    pushad 
    clc ; Make Carry flag zero 

    mov ecx, LENGTHOF arrA ; Length of arrays 

    L1: 
     pushfd ; Restore Carry flag 
     mov eax, [edi]  ; Copy current arrA element to eax 
     popfd 
     adc eax, [esi]  ; Add current arrB element to eax, if Carry flag is non-zero, it is also added 
     pushfd ; Push Carry flag to FLAGS register 

     add edi, TYPE arrA ; Move pointer to the next arrA element 
     add esi, TYPE arrB ; Move pointer to the next arrB element 

     mov [ebx], eax  ; Move current eax value to current arrC element 
     add ebx, TYPE arrC ; Move pointer to the next arrC element 
     popfd 
     loop L1 

    ; Convert result to HEX toOut 
    push OFFSET toOut 
    push OFFSET arrC 
    push 480 
    call StrHex_MY 

    ; Output result 
    INVOKE MessageBoxA, 0, ADDR toOut, ADDR CaptionGreet, 0 
    INVOKE ExitProcess,0 

main ENDP 
END main