
how to write assembly language for x86 and where do I put the code under as? please give me answer as soon as possible.
Problem:
Starting the below cpp program
#include<iostream>
using namespace std;
extern "C" long Average(long,long[]);
int main()
{
long Array1[10] = {1,2,3,4,5,6,7,8,9,10};
long Array2[11] = {1,2,3,4,5,6,7,8,9,10,11};
cout<<"Average of Array1 is
"<<Average(10,Array1)<<endl;
cout<<"Average of Array2 is
"<<Average(11,Array2)<<endl;
}
Write Assembly Language Code (in separate file named Lab8.asm) and modify above cpp program to calculate the average of array1 and array2, round the result to next highest number if the value is greater than or equal-to .5
Answer:
Above cpp code is modified as shown below to do problem statement. Highlighted In BOLD are modified code
$ cat Lab8.C
#include<iostream>
#include<cmath>
using namespace std;
extern "C" long Average(long,long[]);
//Below is the Function to calculate average of than
given long array
long Average(long arr_size,long arr[])
{
//Calculate Sum of array Element
double total_sum = 0;
for (int i =0; i < arr_size; i++)
{
total_sum += arr[i];
}
return round(total_sum/arr_size);
}
int main()
{
long Array1[10] = {1,2,3,4,5,6,7,8,9,10};
long Array2[11] = {1,2,3,4,5,6,7,8,9,10,11};
cout<<"Average of Array1 is
"<<Average(10,Array1)<<endl;
cout<<"Average of Array2 is
"<<Average(11,Array2)<<endl;
}
Compile the above Code in 'g++ compatible operating system' using below command
$ g++ -O Lab8.C
After compiling execute the generated ./a.out to find the output-result as shown below
$ ./a.out
Average of Array1 is 6
Average of Array2 is 6
Above Lab8.C code can be converted into Assembly Language Code
using Below Command
$ g++ -S Lab8.C
Above Command will generate Lab8.s file which will contain the exact assembly language code equal to above cpp code
This Lab8.s can be copied to format Lab8.asm as mentioned in problem statement using below command
$ cp Lab8.s Lab8.asm
View the assembly code as shown below
==========================
$ cat Lab8.asm
.file "Lab8.C"
.local _ZStL8__ioinit
.comm _ZStL8__ioinit,1,1
.text
.globl Average
.type Average, @function
Average:
.LFB1041:
.cfi_startproc
.cfi_personality 0x3,__gxx_personality_v0
pushq %rbp
.cfi_def_cfa_offset 16
.cfi_offset 6, -16
movq %rsp, %rbp
.cfi_def_cfa_register 6
subq $32, %rsp
movq %rdi, -24(%rbp)
movq %rsi, -32(%rbp)
movl $0, %eax
movq %rax, -16(%rbp)
movl $0, -4(%rbp)
jmp .L2
.L3:
movl -4(%rbp), %eax
cltq
salq $3, %rax
addq -32(%rbp), %rax
movq (%rax), %rax
cvtsi2sdq %rax, %xmm0
movsd -16(%rbp), %xmm1
addsd %xmm1, %xmm0
movsd %xmm0, -16(%rbp)
addl $1, -4(%rbp)
.L2:
movl -4(%rbp), %eax
cltq
cmpq -24(%rbp), %rax
setl %al
testb %al, %al
jne .L3
cvtsi2sdq -24(%rbp), %xmm0
movsd -16(%rbp), %xmm1
movapd %xmm1, %xmm2
divsd %xmm0, %xmm2
movapd %xmm2, %xmm0
call round
cvttsd2siq %xmm0, %rax
leave
.cfi_def_cfa 7, 8
ret
.cfi_endproc
.LFE1041:
.size Average, .-Average
.section .rodata
.LC1:
.string "Average of Array1 is "
.LC2:
.string "Average of Array2 is "
.text
.globl main
.type main, @function
main:
.LFB1042:
.cfi_startproc
.cfi_personality 0x3,__gxx_personality_v0
pushq %rbp
.cfi_def_cfa_offset 16
.cfi_offset 6, -16
movq %rsp, %rbp
.cfi_def_cfa_register 6
pushq %rbx
subq $184, %rsp
movq $1, -96(%rbp)
movq $2, -88(%rbp)
movq $3, -80(%rbp)
movq $4, -72(%rbp)
movq $5, -64(%rbp)
movq $6, -56(%rbp)
movq $7, -48(%rbp)
movq $8, -40(%rbp)
movq $9, -32(%rbp)
movq $10, -24(%rbp)
movq $1, -192(%rbp)
movq $2, -184(%rbp)
movq $3, -176(%rbp)
movq $4, -168(%rbp)
movq $5, -160(%rbp)
movq $6, -152(%rbp)
movq $7, -144(%rbp)
movq $8, -136(%rbp)
movq $9, -128(%rbp)
movq $10, -120(%rbp)
movq $11, -112(%rbp)
leaq -96(%rbp), %rax
movq %rax, %rsi
movl $10, %edi
.cfi_offset 3, -24
call Average
movq %rax, %rbx
movl $.LC1, %esi
movl $_ZSt4cout, %edi
call _ZStlsISt11char_traitsIcEERSt13basic_ostreamIcT_ES5_PKc
movq %rbx, %rsi
movq %rax, %rdi
call _ZNSolsEl
movl $_ZSt4endlIcSt11char_traitsIcEERSt13basic_ostreamIT_T0_ES6_,
%esi
movq %rax, %rdi
call _ZNSolsEPFRSoS_E
leaq -192(%rbp), %rax
movq %rax, %rsi
movl $11, %edi
call Average
movq %rax, %rbx
movl $.LC2, %esi
movl $_ZSt4cout, %edi
call _ZStlsISt11char_traitsIcEERSt13basic_ostreamIcT_ES5_PKc
movq %rbx, %rsi
movq %rax, %rdi
call _ZNSolsEl
movl $_ZSt4endlIcSt11char_traitsIcEERSt13basic_ostreamIT_T0_ES6_,
%esi
movq %rax, %rdi
call _ZNSolsEPFRSoS_E
movl $0, %eax
addq $184, %rsp
popq %rbx
leave
.cfi_def_cfa 7, 8
ret
.cfi_endproc
.LFE1042:
.size main, .-main
.type _Z41__static_initialization_and_destruction_0ii,
@function
_Z41__static_initialization_and_destruction_0ii:
.LFB1052:
.cfi_startproc
.cfi_personality 0x3,__gxx_personality_v0
pushq %rbp
.cfi_def_cfa_offset 16
.cfi_offset 6, -16
movq %rsp, %rbp
.cfi_def_cfa_register 6
subq $16, %rsp
movl %edi, -4(%rbp)
movl %esi, -8(%rbp)
cmpl $1, -4(%rbp)
jne .L9
cmpl $65535, -8(%rbp)
jne .L9
movl $_ZStL8__ioinit, %edi
call _ZNSt8ios_base4InitC1Ev
movl $_ZNSt8ios_base4InitD1Ev, %eax
movl $__dso_handle, %edx
movl $_ZStL8__ioinit, %esi
movq %rax, %rdi
call __cxa_atexit
.L9:
leave
.cfi_def_cfa 7, 8
ret
.cfi_endproc
.LFE1052:
.size _Z41__static_initialization_and_destruction_0ii,
.-_Z41__static_initialization_and_destruction_0ii
.type _GLOBAL__I_Average, @function
_GLOBAL__I_Average:
.LFB1053:
.cfi_startproc
.cfi_personality 0x3,__gxx_personality_v0
pushq %rbp
.cfi_def_cfa_offset 16
.cfi_offset 6, -16
movq %rsp, %rbp
.cfi_def_cfa_register 6
movl $65535, %esi
movl $1, %edi
call _Z41__static_initialization_and_destruction_0ii
leave
.cfi_def_cfa 7, 8
ret
.cfi_endproc
.LFE1053:
.size _GLOBAL__I_Average, .-_GLOBAL__I_Average
.section .ctors,"aw",@progbits
.align 8
.quad _GLOBAL__I_Average
.weakref _ZL20__gthrw_pthread_oncePiPFvvE,pthread_once
.weakref
_ZL27__gthrw_pthread_getspecificj,pthread_getspecific
.weakref
_ZL27__gthrw_pthread_setspecificjPKv,pthread_setspecific
.weakref
_ZL22__gthrw_pthread_createPmPK14pthread_attr_tPFPvS3_ES3_,pthread_create
.weakref _ZL20__gthrw_pthread_joinmPPv,pthread_join
.weakref _ZL21__gthrw_pthread_equalmm,pthread_equal
.weakref _ZL20__gthrw_pthread_selfv,pthread_self
.weakref _ZL22__gthrw_pthread_detachm,pthread_detach
.weakref _ZL22__gthrw_pthread_cancelm,pthread_cancel
.weakref _ZL19__gthrw_sched_yieldv,sched_yield
.weakref
_ZL26__gthrw_pthread_mutex_lockP15pthread_mutex_t,pthread_mutex_lock
.weakref
_ZL29__gthrw_pthread_mutex_trylockP15pthread_mutex_t,pthread_mutex_trylock
.weakref
_ZL31__gthrw_pthread_mutex_timedlockP15pthread_mutex_tPK8timespec,pthread_mutex_timedlock
.weakref
_ZL28__gthrw_pthread_mutex_unlockP15pthread_mutex_t,pthread_mutex_unlock
.weakref
_ZL26__gthrw_pthread_mutex_initP15pthread_mutex_tPK19pthread_mutexattr_t,pthread_mutex_init
.weakref
_ZL29__gthrw_pthread_mutex_destroyP15pthread_mutex_t,pthread_mutex_destroy
.weakref
_ZL30__gthrw_pthread_cond_broadcastP14pthread_cond_t,pthread_cond_broadcast
.weakref
_ZL27__gthrw_pthread_cond_signalP14pthread_cond_t,pthread_cond_signal
.weakref
_ZL25__gthrw_pthread_cond_waitP14pthread_cond_tP15pthread_mutex_t,pthread_cond_wait
.weakref
_ZL30__gthrw_pthread_cond_timedwaitP14pthread_cond_tP15pthread_mutex_tPK8timespec,pthread_cond_timedwait
.weakref
_ZL28__gthrw_pthread_cond_destroyP14pthread_cond_t,pthread_cond_destroy
.weakref
_ZL26__gthrw_pthread_key_createPjPFvPvE,pthread_key_create
.weakref _ZL26__gthrw_pthread_key_deletej,pthread_key_delete
.weakref
_ZL30__gthrw_pthread_mutexattr_initP19pthread_mutexattr_t,pthread_mutexattr_init
.weakref
_ZL33__gthrw_pthread_mutexattr_settypeP19pthread_mutexattr_ti,pthread_mutexattr_settype
.weakref
_ZL33__gthrw_pthread_mutexattr_destroyP19pthread_mutexattr_t,pthread_mutexattr_destroy
.ident "GCC: (GNU) 4.4.7 20120313 (Red Hat 4.4.7-3)"
.section .note.GNU-stack,"",@progbits
========================
Ran the above asm code in x85/x86 compatible operating systems and verify the outputs are proper and working fine.
How to write assembly language for x86 and where do I put the code under as? please give me answe...
Write a C++ program for the instructions below. Please
read the instructions carefully and make sure they are followed
correctly.
please put comment with code! and please do not just
copy other solutions.
Instructions 1. Read instructions carefully! 2. Use C++ syntax only, C syntax will not be accepted. 3. Always use braces to define blocks. 4. Indent all lines within a block. Each block requires one more tab. 5. Organize your code well with proper formatting and a single...