Lab Description:
To create a very basic calculator in C++. The functions includes a 2^n, product of number and sum of number. In product of a number function, the user inputs a number such as 5 and the results is every number before it is added; example, 5 = 5 + 4 + 3 + 2 + 1 = 15. Product of a number is the same idea but instead of addition it will be multiplication. The program will also exit from the menu only when the user chooses so, therefore after every execution of the function, the program will go back to the menu. After completion of this program, conversion into assembly language with the command gcc -S program.c. Comparison between the two should be distinguishable considering that the two languages, C++ and Assembly Language, is different in terms of syntax. C++ and any related higher level language, such as Java, C, Visual Basic and etc., are object oriented and are geared for easier entrance into the coding world. Assembly language requires a sort of background knowledge of how computer systems work and how manipulation of data to perform tasks are needed.
Code:
#include using namespace std; //function declarations: void menu() //menu function { cout<<"|-------- Menu --------|" <<endl; cout<<"| 1) Sum of Number |" <<endl; cout<<"| 2) Product of Number |" <<endl; cout<<"| 3) 2^n |" <<endl; cout<<"| 4) exit |" <<endl; cout<<"|----------------------|" <<endl; cout<<"Enter in selection: "; } void son()//Sum of Number function { int num, ans; cout<>num; for(int i = 1; i <= num; i++){ ans = ans + i; } cout<<"Answer: "<<ans<<endl; ans = 0; } void sop() //sum of products { int num; int ans; cout<>num; ans = 1; for(int i = 1; i <= num; i++){ ans*=i; } cout<<"Answer: "<<ans<<endl; } void po2() //Power of 2 { int num; int ans = 1; cout<>num; for (int i = 0; i < num; i ++){ ans += ans; } cout<<"Answer: "<<ans<>choice; if(choice == 1) son(); else if (choice == 2) sop(); else if (choice == 3) po2(); else if(choice == 4){ cout<<"Thanks for using my C++ program :)" << endl;} else cout<<"Invalid Selection..."<<endl; } return 0; }
Assembly Conversion result:
.file "lab5.cpp" .local _ZStL8__ioinit .comm _ZStL8__ioinit,1,1 .section .rodata .LC0: .string "|-------- Menu --------|" .LC1: .string "| 1) Sum of Number |" .LC2: .string "| 2) Product of Number |" .LC3: .string "| 3) 2^n |" .LC4: .string "| 4) exit |" .LC5: .string "|----------------------|" .LC6: .string "Enter in selection: " .text .globl _Z4menuv .type _Z4menuv, @function _Z4menuv: .LFB966: .cfi_startproc pushl %ebp .cfi_def_cfa_offset 8 .cfi_offset 5, -8 movl %esp, %ebp .cfi_def_cfa_register 5 subl $24, %esp movl $.LC0, 4(%esp) movl $_ZSt4cout, (%esp) call _ZStlsISt11char_traitsIcEERSt13basic_ostreamIcT_ES5_PKc movl $_ZSt4endlIcSt11char_traitsIcEERSt13basic_ostreamIT_T0_ES6_, 4(%esp) movl %eax, (%esp) call _ZNSolsEPFRSoS_E movl $.LC1, 4(%esp) movl $_ZSt4cout, (%esp) call _ZStlsISt11char_traitsIcEERSt13basic_ostreamIcT_ES5_PKc movl $_ZSt4endlIcSt11char_traitsIcEERSt13basic_ostreamIT_T0_ES6_, 4(%esp) movl %eax, (%esp) call _ZNSolsEPFRSoS_E movl $.LC2, 4(%esp) movl $_ZSt4cout, (%esp) call _ZStlsISt11char_traitsIcEERSt13basic_ostreamIcT_ES5_PKc movl $_ZSt4endlIcSt11char_traitsIcEERSt13basic_ostreamIT_T0_ES6_, 4(%esp) movl %eax, (%esp) call _ZNSolsEPFRSoS_E movl $.LC3, 4(%esp) movl $_ZSt4cout, (%esp) call _ZStlsISt11char_traitsIcEERSt13basic_ostreamIcT_ES5_PKc movl $_ZSt4endlIcSt11char_traitsIcEERSt13basic_ostreamIT_T0_ES6_, 4(%esp) movl %eax, (%esp) call _ZNSolsEPFRSoS_E movl $.LC4, 4(%esp) movl $_ZSt4cout, (%esp) call _ZStlsISt11char_traitsIcEERSt13basic_ostreamIcT_ES5_PKc movl $_ZSt4endlIcSt11char_traitsIcEERSt13basic_ostreamIT_T0_ES6_, 4(%esp) movl %eax, (%esp) call _ZNSolsEPFRSoS_E movl $.LC5, 4(%esp) movl $_ZSt4cout, (%esp) call _ZStlsISt11char_traitsIcEERSt13basic_ostreamIcT_ES5_PKc movl $_ZSt4endlIcSt11char_traitsIcEERSt13basic_ostreamIT_T0_ES6_, 4(%esp) movl %eax, (%esp) call _ZNSolsEPFRSoS_E movl $.LC6, 4(%esp) movl $_ZSt4cout, (%esp) call _ZStlsISt11char_traitsIcEERSt13basic_ostreamIcT_ES5_PKc leave .cfi_restore 5 .cfi_def_cfa 4, 4 ret .cfi_endproc .LFE966: .size _Z4menuv, .-_Z4menuv .section .rodata .LC7: .string "Enter in number: " .LC8: .string "Answer: " .text .globl _Z3sonv .type _Z3sonv, @function _Z3sonv: .LFB967: .cfi_startproc pushl %ebp .cfi_def_cfa_offset 8 .cfi_offset 5, -8 movl %esp, %ebp .cfi_def_cfa_register 5 subl $40, %esp movl $.LC7, 4(%esp) movl $_ZSt4cout, (%esp) call _ZStlsISt11char_traitsIcEERSt13basic_ostreamIcT_ES5_PKc leal -20(%ebp), %eax movl %eax, 4(%esp) movl $_ZSt3cin, (%esp) call _ZNSirsERi movl $1, -12(%ebp) jmp .L3 .L4: movl -12(%ebp), %eax addl %eax, -16(%ebp) addl $1, -12(%ebp) .L3: movl -20(%ebp), %eax cmpl %eax, -12(%ebp) setle %al testb %al, %al jne .L4 movl $.LC8, 4(%esp) movl $_ZSt4cout, (%esp) call _ZStlsISt11char_traitsIcEERSt13basic_ostreamIcT_ES5_PKc movl -16(%ebp), %edx movl %edx, 4(%esp) movl %eax, (%esp) call _ZNSolsEi movl $_ZSt4endlIcSt11char_traitsIcEERSt13basic_ostreamIT_T0_ES6_, 4(%esp) movl %eax, (%esp) call _ZNSolsEPFRSoS_E movl $0, -16(%ebp) leave .cfi_restore 5 .cfi_def_cfa 4, 4 ret .cfi_endproc .LFE967: .size _Z3sonv, .-_Z3sonv .globl _Z3sopv .type _Z3sopv, @function _Z3sopv: .LFB968: .cfi_startproc pushl %ebp .cfi_def_cfa_offset 8 .cfi_offset 5, -8 movl %esp, %ebp .cfi_def_cfa_register 5 subl $40, %esp movl $.LC7, 4(%esp) movl $_ZSt4cout, (%esp) call _ZStlsISt11char_traitsIcEERSt13basic_ostreamIcT_ES5_PKc leal -20(%ebp), %eax movl %eax, 4(%esp) movl $_ZSt3cin, (%esp) call _ZNSirsERi movl $1, -16(%ebp) movl $1, -12(%ebp) jmp .L6 .L7: movl -16(%ebp), %eax imull -12(%ebp), %eax movl %eax, -16(%ebp) addl $1, -12(%ebp) .L6: movl -20(%ebp), %eax cmpl %eax, -12(%ebp) setle %al testb %al, %al jne .L7 movl $.LC8, 4(%esp) movl $_ZSt4cout, (%esp) call _ZStlsISt11char_traitsIcEERSt13basic_ostreamIcT_ES5_PKc movl -16(%ebp), %edx movl %edx, 4(%esp) movl %eax, (%esp) call _ZNSolsEi movl $_ZSt4endlIcSt11char_traitsIcEERSt13basic_ostreamIT_T0_ES6_, 4(%esp) movl %eax, (%esp) call _ZNSolsEPFRSoS_E leave .cfi_restore 5 .cfi_def_cfa 4, 4 ret .cfi_endproc .LFE968: .size _Z3sopv, .-_Z3sopv .globl _Z3po2v .type _Z3po2v, @function _Z3po2v: .LFB969: .cfi_startproc pushl %ebp .cfi_def_cfa_offset 8 .cfi_offset 5, -8 movl %esp, %ebp .cfi_def_cfa_register 5 subl $40, %esp movl $1, -16(%ebp) movl $.LC7, 4(%esp) movl $_ZSt4cout, (%esp) call _ZStlsISt11char_traitsIcEERSt13basic_ostreamIcT_ES5_PKc leal -20(%ebp), %eax movl %eax, 4(%esp) movl $_ZSt3cin, (%esp) call _ZNSirsERi movl $0, -12(%ebp) jmp .L9 .L10: movl -16(%ebp), %eax addl %eax, %eax movl %eax, -16(%ebp) addl $1, -12(%ebp) .L9: movl -20(%ebp), %eax cmpl %eax, -12(%ebp) setl %al testb %al, %al jne .L10 movl $.LC8, 4(%esp) movl $_ZSt4cout, (%esp) call _ZStlsISt11char_traitsIcEERSt13basic_ostreamIcT_ES5_PKc movl -16(%ebp), %edx movl %edx, 4(%esp) movl %eax, (%esp) call _ZNSolsEi movl $_ZSt4endlIcSt11char_traitsIcEERSt13basic_ostreamIT_T0_ES6_, 4(%esp) movl %eax, (%esp) call _ZNSolsEPFRSoS_E leave .cfi_restore 5 .cfi_def_cfa 4, 4 ret .cfi_endproc .LFE969: .size _Z3po2v, .-_Z3po2v .section .rodata .align 4 .LC9: .string "Thanks for using my C++ program :)" .LC10: .string "Invalid Selection..." .text .globl main .type main, @function main: .LFB970: .cfi_startproc pushl %ebp .cfi_def_cfa_offset 8 .cfi_offset 5, -8 movl %esp, %ebp .cfi_def_cfa_register 5 andl $-16, %esp subl $32, %esp jmp .L12 .L17: call _Z4menuv leal 28(%esp), %eax movl %eax, 4(%esp) movl $_ZSt3cin, (%esp) call _ZNSirsERi movl 28(%esp), %eax cmpl $1, %eax jne .L13 call _Z3sonv jmp .L12 .L13: movl 28(%esp), %eax cmpl $2, %eax jne .L14 call _Z3sopv jmp .L12 .L14: movl 28(%esp), %eax cmpl $3, %eax jne .L15 call _Z3po2v jmp .L12 .L15: movl 28(%esp), %eax cmpl $4, %eax jne .L16 movl $.LC9, 4(%esp) movl $_ZSt4cout, (%esp) call _ZStlsISt11char_traitsIcEERSt13basic_ostreamIcT_ES5_PKc movl $_ZSt4endlIcSt11char_traitsIcEERSt13basic_ostreamIT_T0_ES6_, 4(%esp) movl %eax, (%esp) call _ZNSolsEPFRSoS_E jmp .L12 .L16: movl $.LC10, 4(%esp) movl $_ZSt4cout, (%esp) call _ZStlsISt11char_traitsIcEERSt13basic_ostreamIcT_ES5_PKc movl $_ZSt4endlIcSt11char_traitsIcEERSt13basic_ostreamIT_T0_ES6_, 4(%esp) movl %eax, (%esp) call _ZNSolsEPFRSoS_E .L12: movl 28(%esp), %eax cmpl $4, %eax setne %al testb %al, %al jne .L17 movl $0, %eax leave .cfi_restore 5 .cfi_def_cfa 4, 4 ret .cfi_endproc .LFE970: .size main, .-main .type _Z41__static_initialization_and_destruction_0ii, @function _Z41__static_initialization_and_destruction_0ii: .LFB979: .cfi_startproc pushl %ebp .cfi_def_cfa_offset 8 .cfi_offset 5, -8 movl %esp, %ebp .cfi_def_cfa_register 5 subl $24, %esp cmpl $1, 8(%ebp) jne .L18 cmpl $65535, 12(%ebp) jne .L18 movl $_ZStL8__ioinit, (%esp) call _ZNSt8ios_base4InitC1Ev movl $_ZNSt8ios_base4InitD1Ev, %eax movl $__dso_handle, 8(%esp) movl $_ZStL8__ioinit, 4(%esp) movl %eax, (%esp) call __cxa_atexit .L18: leave .cfi_restore 5 .cfi_def_cfa 4, 4 ret .cfi_endproc .LFE979: .size _Z41__static_initialization_and_destruction_0ii, .-_Z41__static_initialization_and_destruction_0ii .type _GLOBAL__sub_I__Z4menuv, @function _GLOBAL__sub_I__Z4menuv: .LFB980: .cfi_startproc pushl %ebp .cfi_def_cfa_offset 8 .cfi_offset 5, -8 movl %esp, %ebp .cfi_def_cfa_register 5 subl $24, %esp movl $65535, 4(%esp) movl $1, (%esp) call _Z41__static_initialization_and_destruction_0ii leave .cfi_restore 5 .cfi_def_cfa 4, 4 ret .cfi_endproc .LFE980: .size _GLOBAL__sub_I__Z4menuv, .-_GLOBAL__sub_I__Z4menuv .section .ctors,"aw",@progbits .align 4 .long _GLOBAL__sub_I__Z4menuv .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: (Ubuntu/Linaro 4.6.3-1ubuntu5) 4.6.3" .section .note.GNU-stack,"",@progbits
Screenshots: