I need this in assembly: #include <iostream> using namespace std; int main() { /
ID: 3593365 • Letter: I
Question
I need this in assembly:
#include <iostream>
using namespace std;
int main()
{
//Given an int data type array of 10 elements {12, 9, 18, 2, 7, 34, 52, 19, 66, 92}
int Array[] = {3,5,8,7,13,19,17,19};
//Then print out the elements of the original array.
for(int i = 0; i < 8; i++)
cout<<Array[i]<<" ";
cout<<endl;
//Then the program will swap 12 with 9, swap 18 with 2, swap 7 with 34, swap 52 with 19, swap 66 with 92.
//You should use a loop structure to finish the swap.
for(int i = 0; i < 8; i += 2)
{
int temp = Array[i];
Array[i] = Array[i+1];
Array[i+1] = temp;
}
for(int i = 0; i < 8; i++)
cout<<Array[i]<<" ";
cout<<endl;
}
What I have so far:
INCLUDE Irvine32.inc
.data
warray DWORD 3, 5, 8, 7, 13, 19, 17, 19
.code
main PROC
mov esi, OFFSET warray
mov ecx, LENGTHOF warray ;ecx register for our counter(8/2=4)
mov ebx, TYPE warray
L1:
mov eax, warray
mov eax, [warray + 4]
loop L1
call DumpMem ; See the results in Array
call DumpRegs ;display registers
call WaitDec ;display output in decimal form
call WaitMsg ;will keep window running
exit
main ENDP
end main
Explanation / Answer
Please find beow my working code for g++ (GCC) 4.9.3 x686 intel
.file "123.cpp"
.intel_syntax noprefix
.lcomm __ZStL8__ioinit,1,1
.def ___main; .scl 2; .type 32; .endef
.section .rdata,"dr"
LC0:
.ascii " "
.text
.globl _main
.def _main; .scl 2; .type 32; .endef
_main:
LFB1018:
.cfi_startproc
push ebp
.cfi_def_cfa_offset 8
.cfi_offset 5, -8
mov ebp, esp
.cfi_def_cfa_register 5
and esp, -16
sub esp, 64
call ___main
mov DWORD PTR [esp+16], 3
mov DWORD PTR [esp+20], 5
mov DWORD PTR [esp+24], 8
mov DWORD PTR [esp+28], 7
mov DWORD PTR [esp+32], 13
mov DWORD PTR [esp+36], 19
mov DWORD PTR [esp+40], 17
mov DWORD PTR [esp+44], 19
mov DWORD PTR [esp+60], 0
jmp L2
L3:
mov eax, DWORD PTR [esp+60]
mov eax, DWORD PTR [esp+16+eax*4]
mov DWORD PTR [esp+4], eax
mov DWORD PTR [esp], OFFSET FLAT:__ZSt4cout
call __ZNSolsEi
mov DWORD PTR [esp+4], OFFSET FLAT:LC0
mov DWORD PTR [esp], eax
call __ZStlsISt11char_traitsIcEERSt13basic_ostreamIcT_ES5_PKc
add DWORD PTR [esp+60], 1
L2:
cmp DWORD PTR [esp+60], 7
jle L3
mov DWORD PTR [esp+4], OFFSET FLAT:__ZSt4endlIcSt11char_traitsIcEERSt13basic_ostreamIT_T0_ES6_
mov DWORD PTR [esp], OFFSET FLAT:__ZSt4cout
call __ZNSolsEPFRSoS_E
mov DWORD PTR [esp+56], 0
jmp L4
L5:
mov eax, DWORD PTR [esp+56]
mov eax, DWORD PTR [esp+16+eax*4]
mov DWORD PTR [esp+48], eax
mov eax, DWORD PTR [esp+56]
add eax, 1
mov edx, DWORD PTR [esp+16+eax*4]
mov eax, DWORD PTR [esp+56]
mov DWORD PTR [esp+16+eax*4], edx
mov eax, DWORD PTR [esp+56]
lea edx, [eax+1]
mov eax, DWORD PTR [esp+48]
mov DWORD PTR [esp+16+edx*4], eax
add DWORD PTR [esp+56], 2
L4:
cmp DWORD PTR [esp+56], 7
jle L5
mov DWORD PTR [esp+52], 0
jmp L6
L7:
mov eax, DWORD PTR [esp+52]
mov eax, DWORD PTR [esp+16+eax*4]
mov DWORD PTR [esp+4], eax
mov DWORD PTR [esp], OFFSET FLAT:__ZSt4cout
call __ZNSolsEi
mov DWORD PTR [esp+4], OFFSET FLAT:LC0
mov DWORD PTR [esp], eax
call __ZStlsISt11char_traitsIcEERSt13basic_ostreamIcT_ES5_PKc
add DWORD PTR [esp+52], 1
L6:
cmp DWORD PTR [esp+52], 7
jle L7
mov DWORD PTR [esp+4], OFFSET FLAT:__ZSt4endlIcSt11char_traitsIcEERSt13basic_ostreamIT_T0_ES6_
mov DWORD PTR [esp], OFFSET FLAT:__ZSt4cout
call __ZNSolsEPFRSoS_E
mov eax, 0
leave
.cfi_restore 5
.cfi_def_cfa 4, 4
ret
.cfi_endproc
LFE1018:
.def __Z41__static_initialization_and_destruction_0ii; .scl 3; .type 32; .endef
__Z41__static_initialization_and_destruction_0ii:
LFB1027:
.cfi_startproc
push ebp
.cfi_def_cfa_offset 8
.cfi_offset 5, -8
mov ebp, esp
.cfi_def_cfa_register 5
sub esp, 24
cmp DWORD PTR [ebp+8], 1
jne L9
cmp DWORD PTR [ebp+12], 65535
jne L9
mov DWORD PTR [esp], OFFSET FLAT:__ZStL8__ioinit
call __ZNSt8ios_base4InitC1Ev
mov DWORD PTR [esp+8], OFFSET FLAT:___dso_handle
mov DWORD PTR [esp+4], OFFSET FLAT:__ZStL8__ioinit
mov DWORD PTR [esp], OFFSET FLAT:__ZNSt8ios_base4InitD1Ev
call ___cxa_atexit
L9:
leave
.cfi_restore 5
.cfi_def_cfa 4, 4
ret
.cfi_endproc
LFE1027:
.def __GLOBAL__sub_I_main; .scl 3; .type 32; .endef
__GLOBAL__sub_I_main:
LFB1028:
.cfi_startproc
push ebp
.cfi_def_cfa_offset 8
.cfi_offset 5, -8
mov ebp, esp
.cfi_def_cfa_register 5
sub esp, 24
mov DWORD PTR [esp+4], 65535
mov DWORD PTR [esp], 1
call __Z41__static_initialization_and_destruction_0ii
leave
.cfi_restore 5
.cfi_def_cfa 4, 4
ret
.cfi_endproc
LFE1028:
.section .ctors,"w"
.align 4
.long __GLOBAL__sub_I_main
.ident "GCC: (GNU) 4.9.3"
.def __ZNSolsEi; .scl 2; .type 32; .endef
.def __ZStlsISt11char_traitsIcEERSt13basic_ostreamIcT_ES5_PKc; .scl 2; .type 32; .endef
.def __ZSt4endlIcSt11char_traitsIcEERSt13basic_ostreamIT_T0_ES6_; .scl 2; .type 32; .endef
.def __ZNSolsEPFRSoS_E; .scl 2; .type 32; .endef
.def __ZNSt8ios_base4InitC1Ev; .scl 2; .type 32; .endef
.def __ZNSt8ios_base4InitD1Ev; .scl 2; .type 32; .endef
.def ___cxa_atexit; .scl 2; .type 32; .endef
Also specify if you want me this code on any specific compiler as it was not mentioned.
thanks
Related Questions
drjack9650@gmail.com
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.