Use a loop with indirect or indexed addressing to reverse the elements of an int
ID: 3640202 • Letter: U
Question
Use a loop with indirect or indexed addressing to reverse the elements of an integer array in place. Do
not copy the elements to any other array. Use the SIZEOF, TYPE, and LENGTHOF operators to make
the program as flexible as possible if the array size and type should be changed in the future. Optionally,
you may display the modified array by calling the DumpMem method from the Irvine32 library.
My current code:
.data
array BYTE 10h, 20h, 30h, 40h
.code
main PROC
mov esi, 0
mov edi, 0
mov esi, OFFSET array + SIZEOF array - 1
mov edi, OFFSET array + SIZEOF array - 1
mov ecx, SIZEOF array/2
l1: mov al, [esi]
mov bl, [edi]
mov [edi], al
mov [esi], bl
inc esi
dec edi
LOOP l1
call DumpRegs
call DumpMem
exit
main ENDP
END main
Explanation / Answer
TITLE Integer Summation Program (Addnums.asm) ;This program prompts the user for three Integers, ; stores them in an Array, Calculates the sum of the ; array, and displays the sum INCLUDE Irvine32.inc TAB = 9 ; ASCII code for Tab INTEGER_VALUE_COUNTS = 5 .data str1 BYTE "Enter an integer: ",0 str2 BYTE "The sum of the integers is: ",0 array DWORD INTEGER_VALUE_COUNTS DUP(?) .code ;main Program control procedure ;calls: Clrsr, PromptForIntegersValues, ; ArraySum, DisplaySum main PROC call Clrscr mov esi, OFFSET array mov ecx, INTEGER_VALUE_COUNTS call PromptForIntegerValues call ArraySum call DisplaySum exit main ENDP ;------------------------------------------------------ PromptForIntegerValues PROC USES ecx edx esi ; ;Prompts the user for the ten integers, inserts ;them in an array. ;Receives: ESI points to an array of ; doubleword integers, ECX = array size. ;Returns: nothing ;Calls: ReaInt, WriteString ;------------------------------------------------------ mov edx, OFFSET str1 ; "Eter a signed integer" L1: call WriteString ; display string call ReadInt ; read integer into EAX call Crlf ; go to next output line mov [esi],eax ; store in array add esi, TYPE DWORD ; next integer loop L1 ret PromptForIntegerValues ENDP ;------------------------------------------------------ ArraySum PROC USES esi ecx ; ; Calculate the sum of an array of 32-bit integers. ; Receives: ESI points to the array, ECX = number of array elements ; Returns: EAX = sum of the array elements ;------------------------------------------------------ mov eax, 0 ; Set the sum to zero L1: add eax, [esi] ; add each integer to sum add esi, TYPE DWORD ; point to next integer loop L1 ; repeat for array size ret ArraySum ENDP ;------------------------------------------------------ DisplaySum PROC USES edx ; ; Display the sum on the screen. ; Receives: EAX = the sum ; Returns: nothing ; Calls: WriteString, writeInt ;------------------------------------------------------ mov edx, OFFSET str2 ; "The sum of the ..." call WriteString ; display the string of values call WriteInt ; display EAX contents in signed decimal mov al, TAB ; horizontal tab call Crlf ;Display the digits in Hexadecimal mov edx, OFFSET str2 ; "The sum of the digits in HEX ..." call WriteHex ; display EAX contents in Hexadecimal mov al, TAB ; horizontal tab call Crlf ;Display the digits in Hexadecimal mov edx, OFFSET str2 ; "The sum of the digits in BIN..." call WriteBin ; display EAX contents in Binary mov al, TAB ; horizontal tab call Crlf ;Display the digits in Hexadecimal mov edx, OFFSET str2 ; "The sum of the digits in Dec..." call WriteDec ; display EAX contents in unsigned decimal mov al, TAB ; horizontal tab call Crlf ret DisplaySum ENDP END main
Related Questions
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.