2015-11-04 67 views
0

我只是試圖編寫一個簡單的程序,它需要用戶輸入x個整數,然後讓用戶輸入它們,然後使用子例程來計算數組並打印出總和。MASM將數組中元素的總和加起來

我想將EAX中的數組地址和數組元素的數量傳遞給EBX。

;Lab 4 

title Template Lab4 Adder 

INCLUDE Irvine32.inc ;32bit assembler 

.data 
theansweris DWORD ? 
welcometoadder BYTE "Welcome to adder",0dh,0ah,0 
howmanynumbers BYTE "How many numbers would you like to add?:",0dh,0ah,0 
enteranumber BYTE "Enter a Number:",0dh,0ah,0 
youentered BYTE "You entered:",0dh,0ah,0 
thesumis BYTE "The sum of the numbers is:",0dh,0ah,0 
space BYTE " ",0dh,0ah,0 
readStr DB 50 DUP(?) ;allocates 50 BYTES WITH ? 
var1 DWORD ? 
testz DWORD 0 
sum   BYTE 11 DUP(?), " is the sum.", 0dh, 0ah 
numArray DWORD ? 
numElts  DWORD 100 
num   DWORD ? 
resultLbl BYTE "Results", 0 

.code 
main PROC 
    call Clrscr 
    mov edx, OFFSET welcometoadder 
    call WriteString 
    ;mov  ecx, 100     ; loop count (size of array) 
    ;lea  ebx, numElts    ; get address of the array 
    mov edx, OFFSET howmanynumbers 
    call WriteString 

    mov edx,OFFSET enteranumber 
    call WriteString 
    call ReadInt 
    mov edx,OFFSET youentered 
    call WriteString 
    mov ebx,eax 
    mov edx,ebx 
    call WriteInt 
    mov edx, OFFSET space 
    call WriteString 
    ;mov ebx,edx 
    mov ecx,ebx 
    lea ebx,NumElts 

LOOP1: 
     mov edx, OFFSET enteranumber 
     call WriteString 
     mov edx, OFFSET space 
     call WriteString 
     call ReadInt 
     mov  [ebx], eax     ; store in the array    
     add  ebx, 4      ; get address of next array elt 
     cmp ecx,0 
     je LOOP2 
     loop LOOP1      ; repeat nbrElts times 
LOOP2: 
call DumpRegs 
;mov eax,[ebx]     ;get the num 
;add edx,eax     ;increase the sum 
;add ebx,4      ;next array element 
;loop LOOP2 

; CALL ADDER 

;ADDER PROC 
; add bx, cx 
; add bx, cx 
; add ax, bx 
    ;mov edx, OFFSET thesumis 
    ;call WriteString 
; RET 
;ADDER ENDP 

    exit 
    main ENDP 

END main 

回答

-5
ADDER PROC 
add eax,[ebx] 

類似的東西,我想

0

看起來你可能需要使用某種堆棧操作和循環的加法器子過程。 ECX是循環計數器,所以第一部分應該工作。你需要在subproc被調用之前重置循環計數器。

+1

我編輯了你的答案,刪除了一些不相關的元評論。關於評論的50個聲望要求,您可以[在meta上閱讀更多內容](https://meta.stackoverflow.com/questions/252133/50-reputation-points-to-make-comments) – ryanyuyu

1

您的程序不會保留任何內存來存儲陣列!
所有你寫的是:

numElts DWORD 100 

mov  ecx, ebx 
lea  ebx, NumElts 
LOOP1: 

更改定義:

numElts DWORD 100 dup(?) 

額外費用。在LOOP1 ECX的額外測試是無用的!

cmp  ecx, 0  <<<< remove 
je  LOOP2  <<<< remove 
loop LOOP1 
LOOP2: