Calculating the Sum of an Array Row...for assembly language Write a procedure na
ID: 3622337 • Letter: C
Question
Calculating the Sum of an Array Row...for assembly languageWrite a procedure named calc_row_sum that calculates the sum of a single row in a two-dimensional array of bytes, words, or doublewords. The procedure should have the following stack parameters: array offset, row size, array type, row index. It must return the sum in EAX. Use explicit stack parameters not INVOKE or extended PROC. Write a program that tests your procedure with arrays of byte, word, doubleword. Prompt the user of the row index, and display the sum of the selected row.
Explanation / Answer
Dear, INCLUDE Irvine32.inc .data tableB DWORD 10h, 20h, 30h, 40h, 50h RowSizeB = ($ - tableB) DWORD 60h, 70h, 80h, 90h, 0A0h DWORD 0B0h, 0C0h, 0D0h, 0E0h, 0F0h tableW DWORD 10h, 20h, 30h, 40h, 50h RowSizeW = ($ - tableW) DWORD 60h, 70h, 80h, 90h, 0A0h DWORD 0B0h, 0C0h, 0D0h, 0E0h, 0F0h tableD DWORD 10h, 20h, 30h, 40h, 50h RowSizeD = ($ - tableD) DWORD 60h, 70h, 80h, 90h, 0A0h DWORD 0B0h, 0C0h, 0D0h, 0E0h, 0F0h index DWORD ? msg1 BYTE "Enter row number: ",0 msg2 BYTE "The sum is: ",0 .code main PROC mov edx,OFFSET msg1 ; "Enter row number:" call WriteString call Readint ; EAX = row number mov index,eax ; byte array: push OFFSET tableB ; array offset push RowSizeB ; row size push TYPE BYTE ; array type push index ; row index call calc_row_sum ; EAX = sum mov edx,OFFSET msg2 ; "The sum is:" call WriteString call WriteHex ; write sum in EAX call Crlf ; word array: push OFFSET tableW ; array offset push RowSizeW ; row size push TYPE WORD ; array type push index ; row index call calc_row_sum ; EAX = sum mov edx,OFFSET msg2 ; "The sum is:" call WriteString call WriteHex ; write sum in EAX call Crlf ; doubleword array: push OFFSET tableD ; array offset push RowSizeD ; row size push TYPE DWORD ; array type push index ; row index call calc_row_sum ; EAX = sum mov edx,OFFSET msg2 ; "The sum is:" call WriteString call WriteHex ; write sum in EAX call Crlf exit main ENDP ;------------------------------------------------------------ calc_row_sum PROC ; ; Calculates the sum of a row in a two-dimensional table ; of any type. ; Receives: array offset, row size in bytes, array type, row index. ; Returns: EAX = sum. ;------------------------------------------------------------ _arrayOffset EQU DWORD PTR [ebp+20] _rowSize EQU DWORD PTR [ebp+16] _arrayType EQU DWORD PTR [ebp+12] _rowindex EQU DWORD PTR [ebp+8] push ebp mov ebp,esp mov eax,_rowIndex mul DWORD PTR _rowSize mov ebx,_arrayOffset add ebx,eax ; row offset ; Convert row size to a loop counter. mov ecx,_rowSize .IF _arrayType == 2 shr ecx, 1 .ELSEIF _arrayType == 4 shr ecx, 2 .ENDIF mov eax,0 ; accumulator mov esi,0 ; column index ; Loop through the columns. L1: .IF _arrayType == 1 movzx edx,BYTE PTR[ebx + esi] ; get a byte .ELSEIF _arrayType == 2 movzx edx,WORD PTR[ebx + esi*2] ; get a word .ELSEIF _arrayType == 4 mov edx,DWORD PTR[ebx + esi*4] ; get a doubleword .ENDIF add eax,edx ; add to accumulator inc esi ; next value in row loop L1 pop ebp ret 16 calc_row_sum ENDP END main
Related Questions
drjack9650@gmail.com
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.